From d530ec6750083af861a35e7fd297528a25aee339 Mon Sep 17 00:00:00 2001 From: Anudeep Sharma Date: Thu, 29 Sep 2016 15:39:02 -0700 Subject: [PATCH] Updated sample --- README.md | 19 +- pom.xml | 11 +- .../samples/ManageNetworkInterface.java | 5 +- .../azure/management/samples/Utils.java | 323 +++++++++++++++++- 4 files changed, 327 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 1e60434..19355d1 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,17 @@ --- -services: networking +services: Network platforms: java author: selvasingh --- -#Getting Started with Networking - Manage Network Interface - in Java # +#Getting Started with Network - Manage Network Interface - in Java # -Networking Manage Network Interface Sample (for 1.0.0-beta2) - demonstrates how to perform common management tasks using the Microsoft Azure Networking service. - -- Create a virtual machine with multiple network interfaces -- Configure a network interface -- List network interfaces -- Delete a network interface. + Azure Network sample for managing network interfaces - + - Create a virtual machine with multiple network interfaces + - Configure a network interface + - List network interfaces + - Delete a network interface. ## Running this Sample ## @@ -31,10 +30,6 @@ Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an aut [http://azure.com/java] (http://azure.com/java) -[Azure Networking](https://azure.microsoft.com/en-us/services/virtual-network/) - -[Create a virtual machine with multiple network interfaces](https://azure.microsoft.com/en-us/documentation/articles/virtual-networks-multiple-nics/) - If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212) --- diff --git a/pom.xml b/pom.xml index 3d07ca1..d8c6400 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.microsoft.azure network-java-manage-network-interface - 1.0.0-beta2 + 0.0.1-SNAPSHOT ManageNetworkInterface https://github.com/Azure/network-java-manage-network-interface @@ -10,7 +10,7 @@ com.microsoft.azure azure - 1.0.0-beta2 + 1.0.0-beta3 @@ -27,7 +27,6 @@ 1.4.0 com.microsoft.azure.management.network.samples.ManageNetworkInterface - false @@ -35,8 +34,8 @@ maven-compiler-plugin 3.0 - 1.8 - 1.8 + 1.7 + 1.7 @@ -64,4 +63,4 @@ - + \ No newline at end of file diff --git a/src/main/java/com/microsoft/azure/management/network/samples/ManageNetworkInterface.java b/src/main/java/com/microsoft/azure/management/network/samples/ManageNetworkInterface.java index 5195f79..03359f1 100644 --- a/src/main/java/com/microsoft/azure/management/network/samples/ManageNetworkInterface.java +++ b/src/main/java/com/microsoft/azure/management/network/samples/ManageNetworkInterface.java @@ -7,7 +7,7 @@ package com.microsoft.azure.management.network.samples; -import com.microsoft.azure.Azure; +import com.microsoft.azure.management.Azure; import com.microsoft.azure.PagedList; import com.microsoft.azure.management.compute.KnownWindowsVirtualMachineImage; import com.microsoft.azure.management.compute.VirtualMachine; @@ -45,7 +45,8 @@ public static void main(String[] args) { final String publicIpAddressLeafDNS1 = ResourceNamer.randomResourceName("pip1", 24); final String publicIpAddressLeafDNS2 = ResourceNamer.randomResourceName("pip2", 24); - final String vmName = ResourceNamer.randomResourceName("vm", 24); + // TODO adjust the length of vm name from 8 to 24 + final String vmName = ResourceNamer.randomResourceName("vm", 8); final String rgName = ResourceNamer.randomResourceName("rgNEMI", 24); final String userName = "tirekicker"; final String password = "12NewPA$$w0rd!"; diff --git a/src/main/java/com/microsoft/azure/management/samples/Utils.java b/src/main/java/com/microsoft/azure/management/samples/Utils.java index f8da904..224fb3f 100644 --- a/src/main/java/com/microsoft/azure/management/samples/Utils.java +++ b/src/main/java/com/microsoft/azure/management/samples/Utils.java @@ -7,22 +7,44 @@ package com.microsoft.azure.management.samples; +import com.google.common.base.Joiner; import com.microsoft.azure.CloudException; +import com.microsoft.azure.management.batch.Application; +import com.microsoft.azure.management.batch.ApplicationPackage; +import com.microsoft.azure.management.batch.BatchAccount; +import com.microsoft.azure.management.batch.BatchAccountKeys; import com.microsoft.azure.management.compute.AvailabilitySet; -import com.microsoft.azure.management.compute.VirtualMachine; import com.microsoft.azure.management.compute.DataDisk; +import com.microsoft.azure.management.compute.VirtualMachine; +import com.microsoft.azure.management.compute.VirtualMachineExtension; +import com.microsoft.azure.management.keyvault.AccessPolicy; +import com.microsoft.azure.management.keyvault.Vault; import com.microsoft.azure.management.network.Network; import com.microsoft.azure.management.network.NetworkInterface; import com.microsoft.azure.management.network.NetworkSecurityGroup; import com.microsoft.azure.management.network.NetworkSecurityRule; -import com.microsoft.azure.management.network.Subnet; import com.microsoft.azure.management.network.PublicIpAddress; +import com.microsoft.azure.management.network.Subnet; +import com.microsoft.azure.management.network.LoadBalancer; +import com.microsoft.azure.management.network.TcpProbe; +import com.microsoft.azure.management.network.LoadBalancingRule; +import com.microsoft.azure.management.network.InboundNatPool; +import com.microsoft.azure.management.network.InboundNatRule; +import com.microsoft.azure.management.network.Frontend; +import com.microsoft.azure.management.network.Backend; +import com.microsoft.azure.management.network.Probe; +import com.microsoft.azure.management.network.HttpProbe; +import com.microsoft.azure.management.network.PublicFrontend; +import com.microsoft.azure.management.network.PrivateFrontend; import com.microsoft.azure.management.storage.StorageAccount; import com.microsoft.azure.management.storage.StorageAccountKey; import java.io.IOException; import java.util.Calendar; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.ArrayList; import java.util.UUID; /** @@ -116,6 +138,21 @@ public static void print(VirtualMachine resource) { networkProfile.append("\n\t\tId:").append(networkInterfaceId); } + StringBuilder extensions = new StringBuilder().append("\n\tExtensions: "); + for (Map.Entry extensionEntry : resource.extensions().entrySet()) { + VirtualMachineExtension extension = extensionEntry.getValue(); + extensions.append("\n\t\tExtension: ").append(extension.id()) + .append("\n\t\t\tName: ").append(extension.name()) + .append("\n\t\t\tTags: ").append(extension.tags()) + .append("\n\t\t\tProvisioningState: ").append(extension.provisioningState()) + .append("\n\t\t\tAuto upgrade minor version enabled: ").append(extension.autoUpgradeMinorVersionEnabled()) + .append("\n\t\t\tPublisher: ").append(extension.publisherName()) + .append("\n\t\t\tType: ").append(extension.typeName()) + .append("\n\t\t\tVersion: ").append(extension.versionName()) + .append("\n\t\t\tPublic Settings: ").append(extension.publicSettingsAsJsonString()); + } + + System.out.println(new StringBuilder().append("Virtual Machine: ").append(resource.id()) .append("Name: ").append(resource.name()) .append("\n\tResource group: ").append(resource.resourceGroupName()) @@ -126,6 +163,7 @@ public static void print(VirtualMachine resource) { .append(storageProfile) .append(osProfile) .append(networkProfile) + .append(extensions) .toString()); } @@ -160,13 +198,13 @@ public static void print(Network resource) throws CloudException, IOException { .append("\n\tRegion: ").append(resource.region()) .append("\n\tTags: ").append(resource.tags()) .append("\n\tAddress spaces: ").append(resource.addressSpaces()) - .append("\n\tDNS server IPs: ").append(resource.dnsServerIPs()); + .append("\n\tDNS server IPs: ").append(resource.dnsServerIps()); // Output subnets for (Subnet subnet : resource.subnets().values()) { info.append("\n\tSubnet: ").append(subnet.name()) .append("\n\t\tAddress prefix: ").append(subnet.addressPrefix()); - NetworkSecurityGroup subnetNsg = subnet.networkSecurityGroup(); + NetworkSecurityGroup subnetNsg = subnet.getNetworkSecurityGroup(); if (subnetNsg != null) { info.append("\n\t\tNetwork security group: ").append(subnetNsg.id()); } @@ -188,7 +226,9 @@ public static void print(NetworkInterface resource) { .append("\n\tTags: ").append(resource.tags()) .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\tNetwork security group: ").append(resource.networkSecurityGroupId()) + .append("\n\tApplied DNS servers: ").append(resource.appliedDnsServers().toString()) .append("\n\tDNS server IPs: "); // Output dns servers @@ -196,11 +236,11 @@ public static void print(NetworkInterface resource) { info.append("\n\t\t").append(dnsServerIp); } info.append("\n\t IP forwarding enabled: ").append(resource.isIpForwardingEnabled()) - .append("\n\tIs Primary:").append(resource.isPrimary()) .append("\n\tMAC Address:").append(resource.macAddress()) .append("\n\tPrivate IP:").append(resource.primaryPrivateIp()) .append("\n\tPrivate allocation method:").append(resource.primaryPrivateIpAllocationMethod()) - .append("\n\tSubnet Id:").append(resource.primarySubnetId()); + .append("\n\tPrimary virtual network ID: ").append(resource.primaryIpConfiguration().networkId()) + .append("\n\tPrimary subnet name:").append(resource.primaryIpConfiguration().subnetName()); System.out.println(info.toString()); } @@ -218,7 +258,7 @@ public static void print(NetworkSecurityGroup resource) { .append("\n\tTags: ").append(resource.tags()); // Output security rules - for (NetworkSecurityRule rule : resource.securityRules()) { + for (NetworkSecurityRule rule : resource.securityRules().values()) { info.append("\n\tRule: ").append(rule.name()) .append("\n\t\tAccess: ").append(rule.access()) .append("\n\t\tDirection: ").append(rule.direction()) @@ -252,6 +292,26 @@ public static void print(PublicIpAddress resource) { .toString()); } + /** + * Print a key vault. + * @param vault the key vault resource + */ + public static void print(Vault vault) { + StringBuilder info = new StringBuilder().append("Key Vault: ").append(vault.id()) + .append("Name: ").append(vault.name()) + .append("\n\tResource group: ").append(vault.resourceGroupName()) + .append("\n\tRegion: ").append(vault.region()) + .append("\n\tSku: ").append(vault.sku().name()).append(" - ").append(vault.sku().family()) + .append("\n\tVault URI: ").append(vault.vaultUri()) + .append("\n\tAccess policies: "); + for (AccessPolicy accessPolicy: vault.accessPolicies()) { + info.append("\n\t\tIdentity:").append(accessPolicy.objectId()) + .append("\n\t\tKey permissions: ").append(Joiner.on(", ").join(accessPolicy.permissions().keys())) + .append("\n\t\tSecret permissions: ").append(Joiner.on(", ").join(accessPolicy.permissions().secrets())); + } + System.out.println(info.toString()); + } + /** * Print storage account. @@ -267,16 +327,257 @@ public static void print(StorageAccount storageAccount) { * @param storageAccountKeys a list of storage account keys */ public static void print(List storageAccountKeys) { - StorageAccountKey storageAccountKey; - for (int i = 0; i < storageAccountKeys.size(); i++) { - storageAccountKey = (StorageAccountKey) storageAccountKeys.get(i); + StorageAccountKey storageAccountKey = storageAccountKeys.get(i); System.out.println("Key (" + i + ") " + storageAccountKey.keyName() + "=" + storageAccountKey.value()); } + } + + + /** + * Print load balancer. + * @param resource a load balancer + */ + public static void print(LoadBalancer resource) { + StringBuilder info = new StringBuilder(); + info.append("Load balancer: ").append(resource.id()) + .append("Name: ").append(resource.name()) + .append("\n\tResource group: ").append(resource.resourceGroupName()) + .append("\n\tRegion: ").append(resource.region()) + .append("\n\tTags: ").append(resource.tags()) + .append("\n\tBackends: ").append(resource.backends().keySet().toString()); + + // Show public IP addresses + info.append("\n\tPublic IP address IDs: ") + .append(resource.publicIpAddressIds().size()); + for (String pipId : resource.publicIpAddressIds()) { + info.append("\n\t\tPIP id: ").append(pipId); + } + + // Show TCP probes + info.append("\n\tTCP probes: ") + .append(resource.tcpProbes().size()); + for (TcpProbe probe : resource.tcpProbes().values()) { + info.append("\n\t\tProbe name: ").append(probe.name()) + .append("\n\t\t\tPort: ").append(probe.port()) + .append("\n\t\t\tInterval in seconds: ").append(probe.intervalInSeconds()) + .append("\n\t\t\tRetries before unhealthy: ").append(probe.numberOfProbes()); + + // Show associated load balancing rules + info.append("\n\t\t\tReferenced from load balancing rules: ") + .append(probe.loadBalancingRules().size()); + for (LoadBalancingRule rule : probe.loadBalancingRules().values()) { + info.append("\n\t\t\t\tName: ").append(rule.name()); + } + } + + // Show HTTP probes + info.append("\n\tHTTP probes: ") + .append(resource.httpProbes().size()); + for (HttpProbe probe : resource.httpProbes().values()) { + info.append("\n\t\tProbe name: ").append(probe.name()) + .append("\n\t\t\tPort: ").append(probe.port()) + .append("\n\t\t\tInterval in seconds: ").append(probe.intervalInSeconds()) + .append("\n\t\t\tRetries before unhealthy: ").append(probe.numberOfProbes()) + .append("\n\t\t\tHTTP request path: ").append(probe.requestPath()); + + // Show associated load balancing rules + info.append("\n\t\t\tReferenced from load balancing rules: ") + .append(probe.loadBalancingRules().size()); + for (LoadBalancingRule rule : probe.loadBalancingRules().values()) { + info.append("\n\t\t\t\tName: ").append(rule.name()); + } + } + + // Show load balancing rules + info.append("\n\tLoad balancing rules: ") + .append(resource.loadBalancingRules().size()); + for (LoadBalancingRule rule : resource.loadBalancingRules().values()) { + info.append("\n\t\tLB rule name: ").append(rule.name()) + .append("\n\t\t\tProtocol: ").append(rule.protocol()) + .append("\n\t\t\tFloating IP enabled? ").append(rule.floatingIpEnabled()) + .append("\n\t\t\tIdle timeout in minutes: ").append(rule.idleTimeoutInMinutes()) + .append("\n\t\t\tLoad distribution method: ").append(rule.loadDistribution().toString()); + + Frontend frontend = rule.frontend(); + info.append("\n\t\t\tFrontend: "); + if (frontend != null) { + info.append(frontend.name()); + } else { + info.append("(None)"); + } + + info.append("\n\t\t\tFrontend port: ").append(rule.frontendPort()); + Backend backend = rule.backend(); + info.append("\n\t\t\tBackend: "); + if (backend != null) { + info.append(backend.name()); + } else { + info.append("(None)"); + } + + info.append("\n\t\t\tBackend port: ").append(rule.backendPort()); + + Probe probe = rule.probe(); + info.append("\n\t\t\tProbe: "); + if (probe == null) { + info.append("(None)"); + } else { + info.append(probe.name()).append(" [").append(probe.protocol().toString()).append("]"); + } + } + + // Show frontends + info.append("\n\tFrontends: ") + .append(resource.frontends().size()); + for (Frontend frontend : resource.frontends().values()) { + info.append("\n\t\tFrontend name: ").append(frontend.name()) + .append("\n\t\t\tInternet facing: ").append(frontend.isPublic()); + if (frontend.isPublic()) { + info.append("\n\t\t\tPublic IP Address ID: ").append(((PublicFrontend) frontend).publicIpAddressId()); + } else { + info.append("\n\t\t\tVirtual network ID: ").append(((PrivateFrontend) frontend).networkId()) + .append("\n\t\t\tSubnet name: ").append(((PrivateFrontend) frontend).subnetName()) + .append("\n\t\t\tPrivate IP address: ").append(((PrivateFrontend) frontend).privateIpAddress()) + .append("\n\t\t\tPrivate IP allocation method: ").append(((PrivateFrontend) frontend).privateIpAllocationMethod()); + } + + // Inbound NAT pool references + info.append("\n\t\t\tReferenced inbound NAT pools: ") + .append(frontend.inboundNatPools().size()); + for (InboundNatPool pool : frontend.inboundNatPools().values()) { + info.append("\n\t\t\t\tName: ").append(pool.name()); + } + + // Inbound NAT rule references + info.append("\n\t\t\tReferenced inbound NAT rules: ") + .append(frontend.inboundNatRules().size()); + for (InboundNatRule rule : frontend.inboundNatRules().values()) { + info.append("\n\t\t\t\tName: ").append(rule.name()); + } + + // Load balancing rule references + info.append("\n\t\t\tReferenced load balancing rules: ") + .append(frontend.loadBalancingRules().size()); + for (LoadBalancingRule rule : frontend.loadBalancingRules().values()) { + info.append("\n\t\t\t\tName: ").append(rule.name()); + } + } + + // Show inbound NAT rules + info.append("\n\tInbound NAT rules: ") + .append(resource.inboundNatRules().size()); + for (InboundNatRule natRule : resource.inboundNatRules().values()) { + info.append("\n\t\tInbound NAT rule name: ").append(natRule.name()) + .append("\n\t\t\tProtocol: ").append(natRule.protocol().toString()) + .append("\n\t\t\tFrontend: ").append(natRule.frontend().name()) + .append("\n\t\t\tFrontend port: ").append(natRule.frontendPort()) + .append("\n\t\t\tBackend port: ").append(natRule.backendPort()) + .append("\n\t\t\tBackend NIC ID: ").append(natRule.backendNetworkInterfaceId()) + .append("\n\t\t\tBackend NIC IP config name: ").append(natRule.backendNicIpConfigurationName()) + .append("\n\t\t\tFloating IP? ").append(natRule.floatingIpEnabled()) + .append("\n\t\t\tIdle timeout in minutes: ").append(natRule.idleTimeoutInMinutes()); + } + + // Show inbound NAT pools + info.append("\n\tInbound NAT pools: ") + .append(resource.inboundNatPools().size()); + for (InboundNatPool natPool: resource.inboundNatPools().values()) { + info.append("\n\t\tInbound NAT pool name: ").append(natPool.name()) + .append("\n\t\t\tProtocol: ").append(natPool.protocol().toString()) + .append("\n\t\t\tFrontend: ").append(natPool.frontend().name()) + .append("\n\t\t\tFrontend port range: ") + .append(natPool.frontendPortRangeStart()) + .append("-") + .append(natPool.frontendPortRangeEnd()) + .append("\n\t\t\tBackend port: ").append(natPool.backendPort()); + } + + // Show backends + info.append("\n\tBackends: ") + .append(resource.backends().size()); + for (Backend backend : resource.backends().values()) { + info.append("\n\t\tBackend name: ").append(backend.name()); + + // Show assigned backend NICs + info.append("\n\t\t\tReferenced NICs: ") + .append(backend.backendNicIpConfigurationNames().entrySet().size()); + for (Map.Entry entry : backend.backendNicIpConfigurationNames().entrySet()) { + info.append("\n\t\t\t\tNIC ID: ").append(entry.getKey()) + .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(new ArrayList(backend.loadBalancingRules().keySet())); + } + + System.out.println(info.toString()); + } + + /** + * Prints batch account keys. + * @param batchAccountKeys a list of batch account keys + */ + public static void print(BatchAccountKeys batchAccountKeys) { + System.out.println("Primary Key (" + batchAccountKeys.primary() + ") Secondary key = (" + + batchAccountKeys.secondary() + ")"); } + /** + * Prints batch account. + * @param batchAccount a Batch Account + */ + public static void print(BatchAccount batchAccount) { + StringBuilder applicationsOutput = new StringBuilder().append("\n\tapplications: "); + + if (batchAccount.applications().size() > 0) { + for (Map.Entry applicationEntry : batchAccount.applications().entrySet()) { + Application application = applicationEntry.getValue(); + StringBuilder applicationPackages = new StringBuilder().append("\n\t\t\tapplicationPackages : "); + + for (Map.Entry applicationPackageEntry: application.applicationPackages().entrySet()) { + ApplicationPackage applicationPackage = applicationPackageEntry.getValue(); + StringBuilder singleApplicationPackage = new StringBuilder().append("\n\t\t\t\tapplicationPackage : " + applicationPackage.name()); + singleApplicationPackage.append("\n\t\t\t\tapplicationPackageState : " + applicationPackage.state()); + + applicationPackages.append(singleApplicationPackage); + singleApplicationPackage.append("\n"); + } + + StringBuilder singleApplication = new StringBuilder().append("\n\t\tapplication: " + application.name()); + singleApplication.append("\n\t\tdisplayName: " + application.displayName()); + singleApplication.append("\n\t\tdefaultVersion: " + application.defaultVersion()); + singleApplication.append(applicationPackages); + applicationsOutput.append(singleApplication); + applicationsOutput.append("\n"); + } + } + + System.out.println(new StringBuilder().append("BatchAccount: ").append(batchAccount.id()) + .append("Name: ").append(batchAccount.name()) + .append("\n\tResource group: ").append(batchAccount.resourceGroupName()) + .append("\n\tRegion: ").append(batchAccount.region()) + .append("\n\tTags: ").append(batchAccount.tags()) + .append("\n\tAccountEndpoint: ").append(batchAccount.accountEndpoint()) + .append("\n\tPoolQuota: ").append(batchAccount.poolQuota()) + .append("\n\tActiveJobAndJobScheduleQuota: ").append(batchAccount.activeJobAndJobScheduleQuota()) + .append("\n\tStorageAccount: ").append(batchAccount.autoStorage() == null ? "No storage account attached" : batchAccount.autoStorage().storageAccountId()) + .append(applicationsOutput) + .toString()); + } + + /** * Creates and returns a randomized name based on the prefix file for use by the sample. * @param namePrefix The prefix string to be used in generating the name. @@ -292,4 +593,4 @@ public static String createRandomName(String namePrefix) { private Utils() { } -} \ No newline at end of file +}