diff --git a/README.md b/README.md index d4eef58..aef7d45 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,18 @@ --- -services: storage +services: Storage platforms: java author: selvasingh --- -#Getting Started with Storage - Manage Storage Accounts - in Java # +#Getting Started with Storage - Manage Storage Account - in Java # -Storage Manage Storage Accounts Sample (for 1.0.0-beta2) - demonstrates how to perform common management tasks using the Microsoft Azure Storage service. - -- Create a storage account -- Get or regenerate storage account access keys -- Create another storage account -- List storage accounts -- Delete a storage account. + Azure Storage sample for managing storage accounts - + - Create a storage account + - Get | regenerate storage account access keys + - Create another storage account + - List storage accounts + - Delete a storage account. ## Running this Sample ## @@ -30,9 +29,7 @@ Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an aut ## More information ## -[http://azure.com/java](http://azure.com/java) - -[Azure Storage](https://azure.microsoft.com/en-us/services/storage/) +[http://azure.com/java] (http://azure.com/java) 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 8a69d5a..ddc7b20 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.microsoft.azure storage-java-manage-storage-accounts - 1.0.0-beta2 + 0.0.1-SNAPSHOT ManageStorageAccount https://github.com/Azure/storage-java-manage-storage-accounts @@ -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.storage.samples.ManageStorageAccount - 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/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 +} diff --git a/src/main/java/com/microsoft/azure/management/storage/samples/ManageStorageAccount.java b/src/main/java/com/microsoft/azure/management/storage/samples/ManageStorageAccount.java index 2407103..563ba67 100644 --- a/src/main/java/com/microsoft/azure/management/storage/samples/ManageStorageAccount.java +++ b/src/main/java/com/microsoft/azure/management/storage/samples/ManageStorageAccount.java @@ -7,7 +7,7 @@ package com.microsoft.azure.management.storage.samples; -import com.microsoft.azure.Azure; +import com.microsoft.azure.management.Azure; import com.microsoft.azure.management.resources.fluentcore.arm.Region; import com.microsoft.azure.management.samples.Utils; import com.microsoft.azure.management.storage.StorageAccount; @@ -73,7 +73,7 @@ public static void main(String[] args) { System.out.println("Getting storage account access keys"); - List storageAccountKeys = storageAccount.keys(); + List storageAccountKeys = storageAccount.getKeys(); Utils.print(storageAccountKeys); @@ -105,7 +105,7 @@ public static void main(String[] args) { StorageAccounts storageAccounts = azure.storageAccounts(); - List accounts = storageAccounts.listByGroup(rgName); + List accounts = storageAccounts.listByGroup(rgName); StorageAccount sa; for (int i = 0; i < accounts.size(); i++) { sa = (StorageAccount) accounts.get(i); @@ -127,11 +127,12 @@ public static void main(String[] args) { System.out.println(f.getMessage()); f.printStackTrace(); } finally { - if (azure.resourceGroups().getByName(rgName) != null) { + try { System.out.println("Deleting Resource Group: " + rgName); azure.resourceGroups().delete(rgName); System.out.println("Deleted Resource Group: " + rgName); - } else { + } + catch (Exception e) { System.out.println("Did not create any resources in Azure. No clean up is necessary"); } }