Skip to content

Commit

Permalink
Local Network Gateways
Browse files Browse the repository at this point in the history
  • Loading branch information
lenala committed Sep 11, 2017
1 parent 6f94148 commit 085c076
Show file tree
Hide file tree
Showing 5 changed files with 1,089 additions and 2,292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import com.microsoft.azure.management.resources.fluentcore.model.Refreshable;
import com.microsoft.azure.management.resources.fluentcore.model.Updatable;

import java.util.List;

/**
* Entry point for Local Network Gateway management API in Azure.
*/
Expand All @@ -28,11 +30,26 @@ public interface LocalNetworkGateway extends

// Getters

/**
* @return IP address of local network gateway
*/
String ipAddress();

/**
* @return local network gateway's BGP speaker settings
*/
BgpSettings bgpSettings();

/**
* @return local network site address spaces
*/
List<String> addressSpaces();

/**
* @return the provisioning state of the LocalNetworkGateway resource
*/
String provisioningState();

/**
* The entirety of the local network gateway definition.
*/
Expand Down Expand Up @@ -60,8 +77,23 @@ interface WithGroup
extends GroupableResource.DefinitionStages.WithGroup<DefinitionStages.WithIPAddress> {
}

/**
* The stage of the local network gateway definition allowing to specify IP address of local network gateway.
*/
interface WithIPAddress {
WithCreate withIPAddress(String ipAddress);
WithAddressSpace withIPAddress(String ipAddress);
}

/**
* The stage of the local network gateway definition allowing to specify the address space.
*/
interface WithAddressSpace {
/**
* Adds address space.
* Note: this method's effect is additive, i.e. each time it is used, a new address space is added to the network.
* @param cidr the CIDR representation of the local network site address space
*/
WithCreate withAddressSpace(String cidr);
}

/**
Expand All @@ -78,14 +110,40 @@ interface WithBgpSettingsAndCreate extends WithCreate {
*/
interface WithCreate extends
Creatable<LocalNetworkGateway>,
Resource.DefinitionWithTags<WithCreate> {
Resource.DefinitionWithTags<WithCreate>,
DefinitionStages.WithAddressSpace {
}
}

/**
* Grouping of local network gateway update stages.
*/
interface UpdateStages {
/**
* The stage of the local network gateway update allowing to change IP address of local network gateway.
*/
interface WithIPAddress {
Update withIPAddress(String ipAddress);
}

/**
* The stage of the local network gateway update allowing to specify the address spaces.
*/
interface WithAddressSpace {
/**
* Adds address space.
* Note: this method's effect is additive, i.e. each time it is used, a new address space is added to the network.
* @param cidr the CIDR representation of the local network site address space.
*/
Update withAddressSpace(String cidr);

/**
* Remove address space. Note: address space will be removed only in case of exact cidr string match.
* @param cidr the CIDR representation of the local network site address space.
*/
Update withoutAddressSpace(String cidr);
}

interface WithBgpSettings {

}
Expand All @@ -100,6 +158,8 @@ interface WithBgpSettings {
interface Update extends
Appliable<LocalNetworkGateway>,
Resource.UpdateWithTags<Update>,
UpdateStages.WithIPAddress,
UpdateStages.WithAddressSpace,
UpdateStages.WithBgpSettings {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
package com.microsoft.azure.management.network.implementation;

import com.microsoft.azure.management.apigeneration.LangDefinition;
import com.microsoft.azure.management.network.AddressSpace;
import com.microsoft.azure.management.network.BgpSettings;
import com.microsoft.azure.management.network.LocalNetworkGateway;
import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl;
import rx.Observable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Implementation for LocalNetworkGateway and its create and update interfaces.
*/
Expand All @@ -32,17 +37,61 @@ class LocalNetworkGatewayImpl
super(name, innerModel, networkManager);
}

@Override
public String ipAddress() {
return inner().gatewayIpAddress();
}

@Override
public BgpSettings bgpSettings() {
return inner().bgpSettings();
}

@Override
public LocalNetworkGateway.DefinitionStages.WithCreate withIPAddress(String ipAddress) {
public List<String> addressSpaces() {
List<String> addressSpaces = new ArrayList<String>();
if (this.inner().localNetworkAddressSpace() == null) {
return Collections.unmodifiableList(addressSpaces);
} else if (this.inner().localNetworkAddressSpace().addressPrefixes() == null) {
return Collections.unmodifiableList(addressSpaces);
} else {
return Collections.unmodifiableList(this.inner().localNetworkAddressSpace().addressPrefixes());
}
}

@Override
public String provisioningState() {
return inner().provisioningState();
}

@Override
public LocalNetworkGatewayImpl withIPAddress(String ipAddress) {
this.inner().withGatewayIpAddress(ipAddress);
return this;
}

@Override
public LocalNetworkGatewayImpl withAddressSpace(String cidr) {
if (this.inner().localNetworkAddressSpace() == null) {
this.inner().withLocalNetworkAddressSpace(new AddressSpace());
}
if (this.inner().localNetworkAddressSpace().addressPrefixes() == null) {
this.inner().localNetworkAddressSpace().withAddressPrefixes(new ArrayList<String>());
}

this.inner().localNetworkAddressSpace().addressPrefixes().add(cidr);
return this;
}

@Override
public Update withoutAddressSpace(String cidr) {
if (this.inner().localNetworkAddressSpace() == null || this.inner().localNetworkAddressSpace().addressPrefixes() == null) {
return this;
}
this.inner().localNetworkAddressSpace().addressPrefixes().remove(cidr);
return this;
}

@Override
protected Observable<LocalNetworkGatewayInner> getInnerAsync() {
return this.manager().inner().localNetworkGateways().getByResourceGroupAsync(this.resourceGroupName(), this.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,28 @@ public LocalNetworkGateway createResource(LocalNetworkGateways localNetworkGatew
.withRegion(REGION)
.withNewResourceGroup(groupName)
.withIPAddress("40.71.184.214")
.withAddressSpace("192.168.3.0/24")
.withAddressSpace("192.168.4.0/27")
.create();
Assert.assertEquals("40.71.184.214", gateway.ipAddress());
Assert.assertEquals(2, gateway.addressSpaces().size());
Assert.assertTrue(gateway.addressSpaces().contains("192.168.4.0/27"));
return gateway;
}

@Override
public LocalNetworkGateway updateResource(LocalNetworkGateway resource) throws Exception {
resource.update()
public LocalNetworkGateway updateResource(LocalNetworkGateway gateway) throws Exception {
gateway.update()
.withoutAddressSpace("192.168.3.0/24")
.withIPAddress("40.71.184.216")
.withTag("tag2", "value2")
.withoutTag("tag1")
.apply();
resource.refresh();
Assert.assertTrue(resource.tags().containsKey("tag2"));
Assert.assertTrue(!resource.tags().containsKey("tag1"));
return resource;
Assert.assertFalse(gateway.addressSpaces().contains("192.168.3.0/24"));
Assert.assertEquals("40.71.184.216", gateway.ipAddress());
Assert.assertTrue(gateway.tags().containsKey("tag2"));
Assert.assertTrue(!gateway.tags().containsKey("tag1"));
return gateway;
}

@Override
Expand All @@ -56,7 +64,14 @@ public void print(LocalNetworkGateway gateway) {
.append("\n\tName: ").append(gateway.name())
.append("\n\tResource group: ").append(gateway.resourceGroupName())
.append("\n\tRegion: ").append(gateway.regionName())
.append("\n\tTags: ").append(gateway.tags());
.append("\n\tIP address: ").append(gateway.ipAddress());
if (!gateway.addressSpaces().isEmpty()) {
info.append("\n\tAddress spaces:");
for (String addressSpace : gateway.addressSpaces()) {
info.append("\n\t\t" + addressSpace);
}
}
info.append("\n\tTags: ").append(gateway.tags());
System.out.println(info.toString());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
import org.junit.Assert;
import rx.Observable;
import rx.functions.Func1;

import java.util.ArrayList;
import java.util.List;


/**
* Tests Virtual Network Gateway.
Expand Down Expand Up @@ -110,7 +116,7 @@ public VirtualNetworkGateway createResource(VirtualNetworkGateways gateways) thr
.withRegion(vngw.region())
.withExistingResourceGroup(vngw.resourceGroupName())
.withIPAddress("40.71.184.214")

.withAddressSpace("192.168.3.0/24")
.create();
vngw.connections()
.define("myNewConnection")
Expand Down Expand Up @@ -147,38 +153,43 @@ public VirtualNetworkGateway createResource(final VirtualNetworkGateways gateway

// Create virtual network gateway
initializeResourceNames();
Thread creationThread1 = new Thread(new Runnable() {
@Override
public void run() {
VirtualNetworkGateway vngw = gateways.define(GATEWAY_NAME1)
.withRegion(REGION)
.withNewResourceGroup()
.withNewNetwork("10.0.0.0/25", "10.0.0.0/27")
.withRouteBasedVpn()
.withSku(VirtualNetworkGatewaySkuName.VPN_GW1)
.create();
}
});
Thread creationThread2 = new Thread(new Runnable() {
final List<VirtualNetworkGateway> gws = new ArrayList<>();
Observable<?> vngwObservable = gateways.define(GATEWAY_NAME1)
.withRegion(REGION)
.withNewResourceGroup(GROUP_NAME)
.withNewNetwork("10.11.0.0/16", "10.11.255.0/27")
.withRouteBasedVpn()
.withSku(VirtualNetworkGatewaySkuName.VPN_GW1)
.createAsync();

Observable<?> vngw2Observable = gateways.define(GATEWAY_NAME2)
.withRegion(REGION)
.withNewResourceGroup(GROUP_NAME)
.withNewNetwork("10.41.0.0/16", "10.41.255.0/27")
.withRouteBasedVpn()
.withSku(VirtualNetworkGatewaySkuName.VPN_GW1)
.createAsync();

Observable.merge(vngwObservable, vngw2Observable).map(new Func1<Object, Void>() {
@Override
public void run() {
VirtualNetworkGateway vngw2 = gateways.define(GATEWAY_NAME2)
.withRegion(REGION)
.withNewResourceGroup()
.withNewNetwork("10.0.0.0/25", "10.0.0.0/27")
.withRouteBasedVpn()
.withSku(VirtualNetworkGatewaySkuName.VPN_GW1)
.create();
public Void call(Object object) {
if (object instanceof VirtualNetworkGateway) {
gws.add((VirtualNetworkGateway) object);
}
return null;
}
});
creationThread1.start();
creationThread2.start();
//...But bail out after 30 sec, as it is enough to test the results
SdkContext.sleep(60 * 1000);

// Get the resources as created so far
VirtualNetworkGateway vngw1 = gateways.manager().virtualNetworkGateways().getById(createResourceId(gateways.manager().subscriptionId(), GATEWAY_NAME1));
VirtualNetworkGateway vngw2 = gateways.manager().virtualNetworkGateways().getById(createResourceId(gateways.manager().subscriptionId(), GATEWAY_NAME2));
}).toCompletable().await();
VirtualNetworkGateway vngw1 = gws.get(0);
VirtualNetworkGateway vngw2 = gws.get(1);
// {
// @Override
// public VirtualNetworkGateway call(VirtualNetworkGatewayInner inner) {
// VirtualNetworkGateway vngw1 = new VNGI
// self.setInner(inner);
// return self;
// }
// });
// observable.first();
// VirtualNetworkGateway vngw = gateways.getByResourceGroup("vngw115313group", "vngw115313");
vngw1.connections()
.define("myNewConnection")
Expand Down
Loading

0 comments on commit 085c076

Please sign in to comment.