diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/Backend.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/Backend.java index e78c82fb2c74a..f97d203631fc6 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/Backend.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/Backend.java @@ -6,6 +6,7 @@ package com.microsoft.azure.management.network; import java.util.Map; +import java.util.Set; import com.microsoft.azure.management.network.implementation.BackendAddressPoolInner; import com.microsoft.azure.management.network.model.HasLoadBalancingRules; @@ -28,6 +29,11 @@ public interface Backend extends */ Map backendNicIpConfigurationNames(); + /** + * @return a list of the resource IDs of the virtual machines associated with this backend + */ + Set getVirtualMachineIds(); + /** * Grouping of backend definition stages. */ diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/LoadBalancer.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/LoadBalancer.java index 69501a4a9ac7f..18a939d4e319d 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/LoadBalancer.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/LoadBalancer.java @@ -84,7 +84,10 @@ interface Definition extends DefinitionStages.WithProbe, DefinitionStages.WithProbeOrLoadBalancingRule, DefinitionStages.WithLoadBalancingRule, - DefinitionStages.WithLoadBalancingRuleOrCreate { + DefinitionStages.WithLoadBalancingRuleOrCreate, + DefinitionStages.WithCreateAndInboundNatPool, + DefinitionStages.WithCreateAndInboundNatRule, + DefinitionStages.WithCreateAndNatChoice { } /** @@ -240,8 +243,7 @@ interface WithVirtualMachine { } /** - * The stage of a load balancer definition allowing to add a public ip address as one of the load - * balancer's public frontends. + * The stage of a load balancer definition allowing to add a public IP address as the default public frontend. * @param the next stage of the definition */ interface WithPublicIpAddress { @@ -342,7 +344,7 @@ interface WithLoadBalancingRule { /** * The stage of a load balancer definition allowing to create a load balancing rule or create the load balancer. */ - interface WithLoadBalancingRuleOrCreate extends WithLoadBalancingRule, WithCreate { + interface WithLoadBalancingRuleOrCreate extends WithLoadBalancingRule, WithCreateAndNatChoice { } /** @@ -352,9 +354,32 @@ interface WithLoadBalancingRuleOrCreate extends WithLoadBalancingRule, WithCreat */ interface WithCreate extends Creatable, - Resource.DefinitionWithTags, - DefinitionStages.WithInboundNatRule, - DefinitionStages.WithInboundNatPool { + Resource.DefinitionWithTags { + } + + /** + * The stage of a load balancer definition allowing to create the load balancer or start configuring optional inbound NAT rules or pools. + */ + interface WithCreateAndNatChoice extends + WithCreate, + WithInboundNatRule, + WithInboundNatPool { + } + + /** + * The stage of a load balancer definition allowing to create the load balancer or add an inbound NAT pool. + */ + interface WithCreateAndInboundNatPool extends + WithCreate, + WithInboundNatPool { + } + + /** + * The stage of a load balancer definition allowing to create the load balancer or add an inbound NAT rule. + */ + interface WithCreateAndInboundNatRule extends + WithCreate, + WithInboundNatRule { } /** @@ -368,7 +393,7 @@ interface WithInboundNatRule { * @param name the name of the inbound NAT rule * @return the first stage of the new inbound NAT rule definition */ - InboundNatRule.DefinitionStages.Blank defineInboundNatRule(String name); + InboundNatRule.DefinitionStages.Blank defineInboundNatRule(String name); } /** @@ -382,7 +407,7 @@ interface WithInboundNatPool { * @param name the name of the inbound NAT pool * @return the first stage of the new inbound NAT pool definition */ - InboundNatPool.DefinitionStages.Blank defineInboundNatPool(String name); + InboundNatPool.DefinitionStages.Blank defineInboundNatPool(String name); } } @@ -531,7 +556,7 @@ interface WithLoadBalancingRule { /** * The stage of a load balancer update allowing to define, remove or edit Internet-facing frontends. */ - interface WithInternetFrontend { + interface WithInternetFrontend extends WithPublicIpAddress { /** * Begins the update of a load balancer frontend. *

@@ -556,6 +581,46 @@ interface WithInternetFrontend { PublicFrontend.Update updateInternetFrontend(String name); } + /** + * The stage of a load balancer update allowing to add a public IP address as the default public frontend. + */ + interface WithPublicIpAddress { + /** + * Assigns the provided public IP address to the default public frontend to the load balancer. + *

+ * This will create a new default frontend for the load balancer under the name "default", if one does not already exist. + * @param publicIpAddress an existing public IP address + * @return the next stage of the update + */ + Update withExistingPublicIpAddress(PublicIpAddress publicIpAddress); + + /** + * Creates a new public IP address as the default public frontend of the load balancer, + * using an automatically generated name and leaf DNS label + * derived from the load balancer's name, in the same resource group and region. + *

+ * This will create a new default frontend for the load balancer under the name "default", if one does not already exist. + * @return the next stage of the update + */ + Update withNewPublicIpAddress(); + + /** + * Adds a new public IP address as the default public frontend of the load balancer, + * using the specified DNS leaf label, an automatically generated frontend name derived from the DNS label, + * in the same resource group and region as the load balancer. + * @param dnsLeafLabel a DNS leaf label + * @return the next stage of the update + */ + Update withNewPublicIpAddress(String dnsLeafLabel); + + /** + * Adds a new public IP address to the default front end of the load balancer. + * @param creatablePublicIpAddress the creatable stage of a public IP address definition + * @return the next stage of the update + */ + Update withNewPublicIpAddress(Creatable creatablePublicIpAddress); + } + /** * The stage of a load balancer update allowing to define one or more private frontends. */ diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkInterface.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkInterface.java index 6e52465237c46..6351e8518f7c6 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkInterface.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/NetworkInterface.java @@ -130,7 +130,12 @@ public interface NetworkInterface extends * * @return the network security group associated with this network interface. */ - NetworkSecurityGroup networkSecurityGroup(); + NetworkSecurityGroup getNetworkSecurityGroup(); + + /** + * @return the resource ID of the associated virtual machine, or null if none. + */ + String virtualMachineId(); // Setters (fluent) diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddress.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddress.java index 68ec03db56b62..b955f6057996f 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddress.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddress.java @@ -25,9 +25,12 @@ public interface PublicIpAddress extends Wrapper, Updatable { - /*********************************************************** - * Getters - ***********************************************************/ + // Getters + + /** + * @return the IP version of the public IP address + */ + IPVersion version(); /** * @return the assigned IP address @@ -59,6 +62,16 @@ public interface PublicIpAddress extends */ int idleTimeoutInMinutes(); + /** + * @return the load balancer frontend that this public IP address is assigned to + */ + Frontend getAssignedLoadBalancerFrontend(); + + /** + * @return true if this public IP address is assigned to a load balancer frontend + */ + boolean hasAssignedLoadBalancerFrontend(); + /************************************************************** * Fluent interfaces for builder pattern **************************************************************/ diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/BackendImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/BackendImpl.java index bc4c5148e763f..da2ad93ba31b9 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/BackendImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/BackendImpl.java @@ -6,13 +6,17 @@ package com.microsoft.azure.management.network.implementation; import java.util.Collections; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import java.util.TreeMap; +import com.microsoft.azure.CloudException; import com.microsoft.azure.SubResource; import com.microsoft.azure.management.network.Backend; import com.microsoft.azure.management.network.LoadBalancer; import com.microsoft.azure.management.network.LoadBalancingRule; +import com.microsoft.azure.management.network.NetworkInterface; import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.ChildResourceImpl; @@ -70,6 +74,30 @@ public String name() { return this.inner().name(); } + @Override + public Set getVirtualMachineIds() { + Set vmIds = new HashSet<>(); + Map nicConfigs = this.backendNicIpConfigurationNames(); + if (nicConfigs != null) { + for (String nicId : nicConfigs.keySet()) { + try { + NetworkInterface nic = this.parent().manager().networkInterfaces().getById(nicId); + if (nic == null || nic.virtualMachineId() == null) { + continue; + } else { + vmIds.add(nic.virtualMachineId()); + } + } catch (CloudException | IllegalArgumentException e) { + continue; + } + } + } + + return vmIds; + } + + // Verbs + @Override public LoadBalancerImpl attach() { this.parent().withBackend(this); diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/InboundNatPoolImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/InboundNatPoolImpl.java index 585e0b03ceb3b..7a4a66dca6c7d 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/InboundNatPoolImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/InboundNatPoolImpl.java @@ -20,7 +20,7 @@ class InboundNatPoolImpl extends ChildResourceImpl implements InboundNatPool, - InboundNatPool.Definition, + InboundNatPool.Definition, InboundNatPool.UpdateDefinition, InboundNatPool.Update { diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/InboundNatRuleImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/InboundNatRuleImpl.java index 6050bc9f1138f..862d3915e4d2e 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/InboundNatRuleImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/InboundNatRuleImpl.java @@ -20,7 +20,7 @@ class InboundNatRuleImpl extends ChildResourceImpl implements InboundNatRule, - InboundNatRule.Definition, + InboundNatRule.Definition, InboundNatRule.UpdateDefinition, InboundNatRule.Update { diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java index 29b1b2bda90d1..00b63b273f1b6 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java @@ -240,6 +240,15 @@ public NetworkInterfaceImpl withInternalDnsNameLabel(String dnsNameLabel) { // Getters + @Override + public String virtualMachineId() { + if (this.inner().virtualMachine() != null) { + return this.inner().virtualMachine().id(); + } else { + return null; + } + } + @Override public boolean isIpForwardingEnabled() { return Utils.toPrimitiveBoolean(this.inner().enableIPForwarding()); @@ -320,7 +329,7 @@ public String networkSecurityGroupId() { } @Override - public NetworkSecurityGroup networkSecurityGroup() { + public NetworkSecurityGroup getNetworkSecurityGroup() { if (this.networkSecurityGroup == null && this.networkSecurityGroupId() != null) { String id = this.networkSecurityGroupId(); this.networkSecurityGroup = super.myManager diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java index 5e6d6b60f236f..67346a8e21708 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java @@ -7,8 +7,12 @@ import com.microsoft.azure.management.apigeneration.LangDefinition; import com.microsoft.azure.management.network.IPAllocationMethod; +import com.microsoft.azure.management.network.IPVersion; +import com.microsoft.azure.management.network.LoadBalancer; +import com.microsoft.azure.management.network.PublicFrontend; import com.microsoft.azure.management.network.PublicIPAddressDnsSettings; import com.microsoft.azure.management.network.PublicIpAddress; +import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; import rx.Observable; @@ -106,14 +110,27 @@ public IPAllocationMethod ipAllocationMethod() { return this.inner().publicIPAllocationMethod(); } + @Override + public IPVersion version() { + return this.inner().publicIPAddressVersion(); + } + @Override public String fqdn() { - return this.inner().dnsSettings().fqdn(); + if (this.inner().dnsSettings() != null) { + return this.inner().dnsSettings().fqdn(); + } else { + return null; + } } @Override public String reverseFqdn() { - return this.inner().dnsSettings().reverseFqdn(); + if (this.inner().dnsSettings() != null) { + return this.inner().dnsSettings().reverseFqdn(); + } else { + return null; + } } @Override @@ -146,4 +163,28 @@ public Observable createResourceAsync() { return this.client.createOrUpdateAsync(this.resourceGroupName(), this.name(), this.inner()) .map(innerToFluentMap(this)); } -} \ No newline at end of file + + @Override + public PublicFrontend getAssignedLoadBalancerFrontend() { + if (this.hasAssignedLoadBalancerFrontend()) { + final String refId = this.inner().ipConfiguration().id(); + final String loadBalancerId = ResourceUtils.parentResourcePathFromResourceId(refId); + final LoadBalancer lb = this.myManager.loadBalancers().getById(loadBalancerId); + final String frontendName = ResourceUtils.nameFromResourceId(refId); + return (PublicFrontend) lb.frontends().get(frontendName); + } else { + return null; + } + } + + @Override + public boolean hasAssignedLoadBalancerFrontend() { + if (this.inner().ipConfiguration() == null) { + return false; + } else { + final String refId = this.inner().ipConfiguration().id(); + final String resourceType = ResourceUtils.resourceTypeFromResourceId(refId); + return resourceType.equalsIgnoreCase("frontendIPConfigurations"); + } + } +} diff --git a/azure/src/test/java/com/microsoft/azure/TestLoadBalancer.java b/azure/src/test/java/com/microsoft/azure/TestLoadBalancer.java index 6c8f9c9c1aec6..0ebb8e1cc75e3 100644 --- a/azure/src/test/java/com/microsoft/azure/TestLoadBalancer.java +++ b/azure/src/test/java/com/microsoft/azure/TestLoadBalancer.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map.Entry; +import java.util.Set; import org.junit.Assert; @@ -48,7 +49,7 @@ public class TestLoadBalancer { static final String[] PIP_NAMES = {"pipa" + TEST_ID, "pipb" + TEST_ID}; static final String[] VM_IDS = { "/subscriptions/9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef/resourceGroups/marcinslbtest/providers/Microsoft.Compute/virtualMachines/marcinslbtest1", - "/subscriptions/9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef/resourceGroups/marcinslbtest/providers/Microsoft.Compute/virtualMachines/marcinslbtest2" + "/subscriptions/9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef/resourceGroups/marcinslbtest/providers/Microsoft.Compute/virtualMachines/marcinslbtest3" }; /** @@ -168,7 +169,6 @@ public LoadBalancer createResource(LoadBalancers resources) throws Exception { @Override public LoadBalancer updateResource(LoadBalancer resource) throws Exception { resource = resource.update() - //TODO .withExistingPublicIpAddress(pip) .withoutFrontend("default") .withoutBackend("default") .withoutLoadBalancingRule("rule1") @@ -215,13 +215,11 @@ public LoadBalancer createResource(LoadBalancers resources) throws Exception { .withExistingResourceGroup(TestLoadBalancer.GROUP_NAME) // Frontends - .withExistingPublicIpAddress(existingPips.get(0)) .definePublicFrontend("frontend1") - .withExistingPublicIpAddress(existingPips.get(1)) + .withExistingPublicIpAddress(existingPips.get(0)) .attach() // Backends - .withExistingVirtualMachines(existingVMs) .defineBackend("backend1") .attach() @@ -259,13 +257,11 @@ public LoadBalancer createResource(LoadBalancers resources) throws Exception { // Verify frontends Assert.assertTrue(lb.frontends().containsKey("frontend1")); - Assert.assertTrue(lb.frontends().containsKey("default")); - Assert.assertTrue(lb.frontends().size() == 2); + Assert.assertTrue(lb.frontends().size() == 1); // Verify backends - Assert.assertTrue(lb.backends().containsKey("default")); Assert.assertTrue(lb.backends().containsKey("backend1")); - Assert.assertTrue(lb.backends().size() == 2); + Assert.assertTrue(lb.backends().size() == 1); // Verify probes Assert.assertTrue(lb.httpProbes().containsKey("httpProbe1")); @@ -295,8 +291,11 @@ public LoadBalancer createResource(LoadBalancers resources) throws Exception { @Override public LoadBalancer updateResource(LoadBalancer resource) throws Exception { + List existingPips = ensurePIPs(pips); resource = resource.update() - //TODO .withExistingPublicIpAddress(pip) + .updateInternetFrontend("frontend1") + .withExistingPublicIpAddress(existingPips.get(1)) + .parent() .withoutFrontend("default") .withoutBackend("default") .withoutLoadBalancingRule("rule1") @@ -369,9 +368,7 @@ public LoadBalancer updateResource(LoadBalancer resource) throws Exception { List existingPips = ensurePIPs(pips); PublicIpAddress pip = existingPips.get(1); resource = resource.update() - .updateInternetFrontend("default") - .withExistingPublicIpAddress(pip) - .parent() + .withExistingPublicIpAddress(pip) .updateTcpProbe("default") .withPort(22) .parent() @@ -460,7 +457,7 @@ public LoadBalancer createResource(LoadBalancers resources) throws Exception { // Frontend (default) .withExistingSubnet(network, "subnet1") // Backend (default) - //TODO .withExistingVirtualMachines(existingVMs) + .withExistingVirtualMachines(existingVMs) .defineBackend("foo") .attach() // Probe (default) @@ -780,9 +777,17 @@ static void printLB(LoadBalancer resource) { .append(" - IP Config: ").append(entry.getValue()); } + // Show assigned virtual machines + Set vmIds = backend.getVirtualMachineIds(); + info.append("\n\t\t\tReferenced virtual machine ids: ") + .append(vmIds.size()); + for (String vmId : vmIds) { + info.append("\n\t\t\t\tVM ID: ").append(vmId); + } + // Show assigned load balancing rules info.append("\n\t\t\tReferenced load balancing rules: ") - .append(backend.loadBalancingRules().keySet().toArray(new String[0])); + .append(new ArrayList(backend.loadBalancingRules().keySet())); } System.out.println(info.toString()); diff --git a/azure/src/test/java/com/microsoft/azure/TestNetworkInterface.java b/azure/src/test/java/com/microsoft/azure/TestNetworkInterface.java index a5e022f643246..f289d149cb716 100644 --- a/azure/src/test/java/com/microsoft/azure/TestNetworkInterface.java +++ b/azure/src/test/java/com/microsoft/azure/TestNetworkInterface.java @@ -53,6 +53,7 @@ public void print(NetworkInterface resource) { .append("\n\tInternal DNS name label: ").append(resource.internalDnsNameLabel()) .append("\n\tInternal FQDN: ").append(resource.internalFqdn()) .append("\n\tInternal domain name suffix: ").append(resource.internalDomainNameSuffix()) + .append("\n\tVirtual machine ID: ").append(resource.virtualMachineId()) .append("\n\tApplied DNS servers: ").append(resource.appliedDnsServers().toString()) .append("\n\tDNS server IPs: "); @@ -61,7 +62,7 @@ public void print(NetworkInterface resource) { info.append("\n\t\t").append(dnsServerIp); } - info.append("\n\t IP forwarding enabled: ").append(resource.isIpForwardingEnabled()) + info.append("\n\tIP forwarding enabled: ").append(resource.isIpForwardingEnabled()) .append("\n\tMAC Address:").append(resource.macAddress()) .append("\n\tPrivate IP:").append(resource.primaryPrivateIp()) .append("\n\tPrivate allocation method:").append(resource.primaryPrivateIpAllocationMethod()) diff --git a/azure/src/test/java/com/microsoft/azure/TestPublicIpAddress.java b/azure/src/test/java/com/microsoft/azure/TestPublicIpAddress.java index d4e3f0818b6f9..10343ee2c03b9 100644 --- a/azure/src/test/java/com/microsoft/azure/TestPublicIpAddress.java +++ b/azure/src/test/java/com/microsoft/azure/TestPublicIpAddress.java @@ -47,9 +47,13 @@ public PublicIpAddress updateResource(PublicIpAddress resource) throws Exception } @Override - public void print(PublicIpAddress resource) { - System.out.println(new StringBuilder().append("Public IP Address: ").append(resource.id()) - .append("Name: ").append(resource.name()) + public void print(PublicIpAddress pip) { + TestPublicIpAddress.printPIP(pip); + } + + public static void printPIP(PublicIpAddress resource) { + StringBuilder info = new StringBuilder().append("Public IP Address: ").append(resource.id()) + .append("\n\tName: ").append(resource.name()) .append("\n\tResource group: ").append(resource.resourceGroupName()) .append("\n\tRegion: ").append(resource.region()) .append("\n\tTags: ").append(resource.tags()) @@ -59,6 +63,16 @@ public void print(PublicIpAddress resource) { .append("\n\tReverse FQDN: ").append(resource.reverseFqdn()) .append("\n\tIdle timeout (minutes): ").append(resource.idleTimeoutInMinutes()) .append("\n\tIP allocation method: ").append(resource.ipAllocationMethod().toString()) - .toString()); + .append("\n\tIP version: ").append(resource.version().toString()); + + // Show the associated load balancer frontend if any + info.append("\n\tAssociated load balancer frontend name: "); + if (resource.hasAssignedLoadBalancerFrontend()) { + info.append(resource.getAssignedLoadBalancerFrontend().name()); + } else { + info.append("(None)"); + } + + System.out.println(info.toString()); } }