Skip to content
This repository has been archived by the owner on Jul 25, 2020. It is now read-only.

Commit

Permalink
Adds method checkIpAvailability to VirtualNetworkApi
Browse files Browse the repository at this point in the history
Addresses @nacx comments

Fixed formatter maximum line width. Removed redundantTest
  • Loading branch information
danielestevez authored and nacx committed Jul 13, 2018
1 parent cc3ad73 commit 4d1f64d
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@
* 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;
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;
Expand Down Expand Up @@ -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}")
Expand All @@ -73,4 +74,10 @@ VirtualNetwork createOrUpdate(@PathParam("virtualnetworkname") String virtualnet
@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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,36 @@
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() {
Expand All @@ -52,12 +68,17 @@ public void deleteVirtualNetworkResourceDoesNotExist() {
@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);
}
Expand All @@ -77,7 +98,24 @@ public void listVirtualNetworks() {
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);
Expand All @@ -87,4 +125,26 @@ private VirtualNetworkApi api() {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand All @@ -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"));
Expand All @@ -53,6 +56,21 @@ public void getVirtualNetwork() throws InterruptedException {
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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,6 +41,7 @@

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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -152,6 +155,7 @@ public void setup() {
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);
Expand Down
10 changes: 10 additions & 0 deletions providers/azurecompute-arm/src/test/resources/ipnotavailable.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit 4d1f64d

Please sign in to comment.