Adds method checkIpAvailability to VirtualNetworkApi

Addresses @nacx comments

Fixed formatter maximum line width. Removed redundantTest
This commit is contained in:
Dani Estevez 2018-07-11 18:10:42 -04:00 committed by Ignasi Barrera
parent e45c262ff2
commit 92076d6b79
6 changed files with 144 additions and 8 deletions

View File

@ -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<String> availableIPAddresses();
@SerializedNames({ "available", "availableIPAddresses" })
public static IpAddressAvailabilityResult create(final boolean available, final List<String> availableIPAddresses) {
return new AutoValue_IpAddressAvailabilityResult(available,
availableIPAddresses != null ? ImmutableList.copyOf(availableIPAddresses) : ImmutableList.<String> of());
}
}

View File

@ -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<String, String> tags,
@PayloadParam("properties")VirtualNetwork.VirtualNetworkProperties properties);
@PayloadParam("location") String location, @Nullable @PayloadParam("tags") Map<String, String> 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);
}

View File

@ -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.<Subnet> 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);
}
}

View File

@ -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());

View File

@ -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<AzureComputeApi
protected DeletedCertificateStatusPredicateFactory deletedCertificateStatus;
protected RecoverableCertificateStatusPredicateFactory recoverableCertificateStatus;
protected CertificateOperationStatusPredicateFactory certificateOperationStatus;
protected NetworkAvailablePredicateFactory networkAvailablePredicate;
protected String resourceGroupName;
@ -152,6 +155,7 @@ public class BaseAzureComputeApiLiveTest extends BaseApiLiveTest<AzureComputeApi
deletedCertificateStatus = injector.getInstance(Key.get(DeletedCertificateStatusPredicateFactory.class, Names.named(VAULT_CERTIFICATE_DELETE_STATUS)));
recoverableCertificateStatus = injector.getInstance(Key.get(RecoverableCertificateStatusPredicateFactory.class, Names.named(VAULT_CERTIFICATE_RECOVERABLE_STATUS)));
certificateOperationStatus = injector.getInstance(Key.get(CertificateOperationStatusPredicateFactory.class, Names.named(VAULT_CERTIFICATE_OPERATION_STATUS)));
networkAvailablePredicate = injector.getInstance(NetworkAvailablePredicateFactory.class);
tenantId = injector.getInstance(Key.get(String.class, Tenant.class));
return injector.getInstance(AzureComputeApi.class);

View File

@ -0,0 +1,10 @@
{
"available": false,
"availableIPAddresses": [
"20.20.0.4",
"20.20.0.5",
"20.20.0.6",
"20.20.0.8",
"20.20.0.9"
]
}