From 92076d6b798524c253e15d3ba9c3f47c0a0229d8 Mon Sep 17 00:00:00 2001 From: Dani Estevez Date: Wed, 11 Jul 2018 18:10:42 -0400 Subject: [PATCH] Adds method checkIpAvailability to VirtualNetworkApi Addresses @nacx comments Fixed formatter maximum line width. Removed redundantTest --- .../domain/IpAddressAvailabilityResult.java | 37 ++++++++++ .../arm/features/VirtualNetworkApi.java | 15 ++-- .../features/VirtualNetworkApiLiveTest.java | 68 +++++++++++++++++-- .../features/VirtualNetworkApiMockTest.java | 18 +++++ .../internal/BaseAzureComputeApiLiveTest.java | 4 ++ .../src/test/resources/ipnotavailable.json | 10 +++ 6 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/IpAddressAvailabilityResult.java create mode 100644 providers/azurecompute-arm/src/test/resources/ipnotavailable.json diff --git a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/IpAddressAvailabilityResult.java b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/IpAddressAvailabilityResult.java new file mode 100644 index 0000000000..efc08db381 --- /dev/null +++ b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/IpAddressAvailabilityResult.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.azurecompute.arm.domain; + +import java.util.List; + +import org.jclouds.json.SerializedNames; + +import com.google.auto.value.AutoValue; +import com.google.common.collect.ImmutableList; + +@AutoValue +public abstract class IpAddressAvailabilityResult { + public abstract boolean available(); + + public abstract List availableIPAddresses(); + + @SerializedNames({ "available", "availableIPAddresses" }) + public static IpAddressAvailabilityResult create(final boolean available, final List availableIPAddresses) { + return new AutoValue_IpAddressAvailabilityResult(available, + availableIPAddresses != null ? ImmutableList.copyOf(availableIPAddresses) : ImmutableList. of()); + } +} diff --git a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/features/VirtualNetworkApi.java b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/features/VirtualNetworkApi.java index a4237ff7ad..5190d425d4 100644 --- a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/features/VirtualNetworkApi.java +++ b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/features/VirtualNetworkApi.java @@ -15,9 +15,9 @@ * limitations under the License. */ package org.jclouds.azurecompute.arm.features; + import java.util.List; import java.util.Map; - import javax.inject.Named; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -25,10 +25,12 @@ import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import org.jclouds.Fallbacks.EmptyListOnNotFoundOr404; import org.jclouds.Fallbacks.NullOnNotFoundOr404; +import org.jclouds.azurecompute.arm.domain.IpAddressAvailabilityResult; import org.jclouds.azurecompute.arm.domain.VirtualNetwork; import org.jclouds.azurecompute.arm.filters.ApiVersionFilter; import org.jclouds.azurecompute.arm.functions.FalseOn204; @@ -58,9 +60,8 @@ public interface VirtualNetworkApi { @MapBinder(BindToJsonPayload.class) @PUT VirtualNetwork createOrUpdate(@PathParam("virtualnetworkname") String virtualnetworkname, - @PayloadParam("location") String location, - @Nullable @PayloadParam("tags") Map tags, - @PayloadParam("properties")VirtualNetwork.VirtualNetworkProperties properties); + @PayloadParam("location") String location, @Nullable @PayloadParam("tags") Map tags, + @PayloadParam("properties") VirtualNetwork.VirtualNetworkProperties properties); @Named("virtualnetwork:get") @Path("/{virtualnetworkname}") @@ -73,4 +74,10 @@ public interface VirtualNetworkApi { @DELETE @ResponseParser(FalseOn204.class) boolean delete(@PathParam("virtualnetworkname") String virtualnetworkname); + + @Named("virtualnetwork:check_ip_address_availability") + @Path("/{virtualnetworkname}/CheckIPAddressAvailability") + @GET + IpAddressAvailabilityResult checkIPAddressAvailability(@PathParam("virtualnetworkname") String virtualnetworkname, + @QueryParam("ipAddress") String ipAddress); } diff --git a/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualNetworkApiLiveTest.java b/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualNetworkApiLiveTest.java index 5a89b07bed..27ea3542ec 100644 --- a/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualNetworkApiLiveTest.java +++ b/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualNetworkApiLiveTest.java @@ -21,20 +21,36 @@ import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; +import java.net.URI; import java.util.Arrays; import java.util.List; import org.jclouds.azurecompute.arm.domain.AddressSpace; +import org.jclouds.azurecompute.arm.domain.FrontendIPConfigurations; +import org.jclouds.azurecompute.arm.domain.FrontendIPConfigurationsProperties; +import org.jclouds.azurecompute.arm.domain.IdReference; +import org.jclouds.azurecompute.arm.domain.IpAddressAvailabilityResult; +import org.jclouds.azurecompute.arm.domain.LoadBalancer; +import org.jclouds.azurecompute.arm.domain.LoadBalancerProperties; +import org.jclouds.azurecompute.arm.domain.Subnet; +import org.jclouds.azurecompute.arm.domain.Subnet.SubnetProperties; import org.jclouds.azurecompute.arm.domain.VirtualNetwork; import org.jclouds.azurecompute.arm.internal.BaseAzureComputeApiLiveTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import com.google.common.collect.ImmutableList; + @Test(groups = "live", singleThreaded = true) public class VirtualNetworkApiLiveTest extends BaseAzureComputeApiLiveTest { + private static final String TEST_VIRTUALNETWORK_ADDRESS_PREFIX = "10.20.0.0/16"; + private static final String TEST_IP_ADDRESS_AVAILABLE = "10.20.0.15"; + private static final String TEST_IP_ADDRESS_USED_IN_PROVIDER = "10.20.0.7"; + private String virtualNetworkName; + @BeforeClass @Override public void setup() { @@ -52,12 +68,17 @@ public class VirtualNetworkApiLiveTest extends BaseAzureComputeApiLiveTest { @Test(dependsOnMethods = "deleteVirtualNetworkResourceDoesNotExist") public void createVirtualNetwork() { - final VirtualNetwork.VirtualNetworkProperties virtualNetworkProperties = - VirtualNetwork.VirtualNetworkProperties.builder().addressSpace( - AddressSpace.create(Arrays.asList(DEFAULT_VIRTUALNETWORK_ADDRESS_PREFIX))).build(); + Subnet subnet = Subnet.builder().name("subnetName") + .properties(SubnetProperties.builder().addressPrefix(TEST_VIRTUALNETWORK_ADDRESS_PREFIX).build()).build(); + + final VirtualNetwork.VirtualNetworkProperties virtualNetworkProperties = VirtualNetwork.VirtualNetworkProperties + .builder().subnets(ImmutableList. of(subnet)) + .addressSpace(AddressSpace.create(Arrays.asList(TEST_VIRTUALNETWORK_ADDRESS_PREFIX))).build(); VirtualNetwork vn = api().createOrUpdate(virtualNetworkName, LOCATION, null, virtualNetworkProperties); + networkAvailablePredicate.create(resourceGroupName).apply(virtualNetworkName); + assertEquals(vn.name(), virtualNetworkName); assertEquals(vn.location(), LOCATION); } @@ -77,7 +98,24 @@ public class VirtualNetworkApiLiveTest extends BaseAzureComputeApiLiveTest { assertTrue(vnList.size() > 0); } - @Test(dependsOnMethods = {"listVirtualNetworks", "getVirtualNetwork"}, alwaysRun = true) + @Test(dependsOnMethods = "getVirtualNetwork") + public void checkIpAvailability() { + final IpAddressAvailabilityResult checkResultAvailable = api() + .checkIPAddressAvailability(virtualNetworkName, TEST_IP_ADDRESS_AVAILABLE); + assertTrue(checkResultAvailable.available()); + assertTrue(checkResultAvailable.availableIPAddresses().isEmpty()); + + LoadBalancer lbCreated = createLoadBalancerWithPrivateIP(TEST_IP_ADDRESS_USED_IN_PROVIDER); + + final IpAddressAvailabilityResult checkResultUnavailable = api() + .checkIPAddressAvailability(virtualNetworkName, TEST_IP_ADDRESS_USED_IN_PROVIDER); + assertFalse(checkResultUnavailable.available()); + assertFalse(checkResultUnavailable.availableIPAddresses().isEmpty()); + + deleteLoadBalancer(lbCreated); + } + + @Test(dependsOnMethods = { "listVirtualNetworks", "getVirtualNetwork", "checkIpAvailability" }, alwaysRun = true) public void deleteVirtualNetwork() { boolean status = api().delete(virtualNetworkName); assertTrue(status); @@ -87,4 +125,26 @@ public class VirtualNetworkApiLiveTest extends BaseAzureComputeApiLiveTest { return api.getVirtualNetworkApi(resourceGroupName); } + private LoadBalancerApi lbApi() { + return api.getLoadBalancerApi(resourceGroupName); + } + + private LoadBalancer createLoadBalancerWithPrivateIP(final String ipAddress) { + + FrontendIPConfigurationsProperties frontendProps = FrontendIPConfigurationsProperties.builder() + .privateIPAddress(ipAddress).privateIPAllocationMethod("Static") + .subnet(IdReference.create(api().get(virtualNetworkName).properties().subnets().get(0).id())).build(); + FrontendIPConfigurations frontendIps = FrontendIPConfigurations.create("ipConfigs", null, frontendProps, null); + LoadBalancerProperties props = LoadBalancerProperties.builder() + .frontendIPConfigurations(ImmutableList.of(frontendIps)).build(); + + LoadBalancer lbCreated = lbApi().createOrUpdate("lbName", LOCATION, null, props); + assertNotNull(lbCreated); + return lbCreated; + } + + private void deleteLoadBalancer(LoadBalancer lbCreated) { + URI lbDeletedURI = lbApi().delete(lbCreated.name()); + assertResourceDeleted(lbDeletedURI); + } } diff --git a/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualNetworkApiMockTest.java b/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualNetworkApiMockTest.java index 2cf3a41023..11f7fe369d 100644 --- a/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualNetworkApiMockTest.java +++ b/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualNetworkApiMockTest.java @@ -26,6 +26,7 @@ import java.util.Arrays; import java.util.List; import org.jclouds.azurecompute.arm.domain.AddressSpace; +import org.jclouds.azurecompute.arm.domain.IpAddressAvailabilityResult; import org.jclouds.azurecompute.arm.domain.VirtualNetwork; import org.jclouds.azurecompute.arm.internal.BaseAzureComputeApiMockTest; import org.testng.annotations.Test; @@ -38,6 +39,8 @@ public class VirtualNetworkApiMockTest extends BaseAzureComputeApiMockTest { private final String virtualNetwork = "mockvirtualnetwork"; private final String apiVersion = "api-version=2015-06-15"; private final String location = "westeurope"; + private final String ipAvailable = "10.20.0.7"; + private final String ipNotAvailable = "10.20.0.3"; public void getVirtualNetwork() throws InterruptedException { server.enqueue(jsonResponse("/virtualnetwork.json")); @@ -53,6 +56,21 @@ public class VirtualNetworkApiMockTest extends BaseAzureComputeApiMockTest { assertEquals(vn.tags().get("tagkey"), "tagvalue"); } + public void checkIpNotAvailable() throws InterruptedException { + server.enqueue(jsonResponse("/ipnotavailable.json")); + + final VirtualNetworkApi vnApi = api.getVirtualNetworkApi(resourcegroup); + IpAddressAvailabilityResult checkResult = vnApi.checkIPAddressAvailability(virtualNetwork, ipNotAvailable); + + String path = String.format("/subscriptions/%s/resourcegroups/%s/providers/Microsoft" + + ".Network/virtualNetworks/%s/CheckIPAddressAvailability?ipAddress=%s&%s", subscriptionid, + resourcegroup, + virtualNetwork, ipNotAvailable, apiVersion); + assertSent(server, "GET", path); + assertFalse(checkResult.available()); + assertFalse(checkResult.availableIPAddresses().isEmpty()); + } + public void getVirtualNetworkReturns404() throws InterruptedException { server.enqueue(response404()); diff --git a/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/BaseAzureComputeApiLiveTest.java b/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/BaseAzureComputeApiLiveTest.java index 58338345be..11bed4d482 100644 --- a/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/BaseAzureComputeApiLiveTest.java +++ b/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/BaseAzureComputeApiLiveTest.java @@ -17,6 +17,7 @@ package org.jclouds.azurecompute.arm.internal; import static com.google.common.base.Preconditions.checkNotNull; + import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.TIMEOUT_RESOURCE_DELETED; import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.VAULT_CERTIFICATE_DELETE_STATUS; import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.VAULT_CERTIFICATE_OPERATION_STATUS; @@ -40,6 +41,7 @@ import java.util.Random; import org.jclouds.apis.BaseApiLiveTest; import org.jclouds.azurecompute.arm.AzureComputeApi; +import org.jclouds.azurecompute.arm.compute.config.AzurePredicatesModule.NetworkAvailablePredicateFactory; import org.jclouds.azurecompute.arm.compute.config.AzurePredicatesModule.PublicIpAvailablePredicateFactory; import org.jclouds.azurecompute.arm.compute.config.AzurePredicatesModule.VaultCertificatePredicates.CertificateOperationStatusPredicateFactory; import org.jclouds.azurecompute.arm.compute.config.AzurePredicatesModule.VaultCertificatePredicates.DeletedCertificateStatusPredicateFactory; @@ -96,6 +98,7 @@ public class BaseAzureComputeApiLiveTest extends BaseApiLiveTest