diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..afedf79 --- /dev/null +++ b/.gitignore @@ -0,0 +1,50 @@ +*.class + +# Auth filed +*.auth +*.azureauth + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# Azure Tooling # +node_modules +packages + +# Eclipse # +*.pydevproject +.project +.metadata +bin/** +tmp/** +tmp/**/* +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# Other Tooling # +.classpath +.project +target +.idea +*.iml + +# Mac OS # +.DS_Store +.DS_Store? + +# Windows # +Thumbs.db + +# reduced pom files should not be included +dependency-reduced-pom.xml \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..8494123 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,11 @@ +# Contributing to Azure samples + +Thank you for your interest in contributing to Azure samples! + +## Ways to contribute + +You can contribute to [Azure samples](https://azure.microsoft.com/documentation/samples/) in a few different ways: + +- Submit feedback on [this sample page](https://azure.microsoft.com/documentation/samples/key-vault-java-manage-key-vaults/) whether it was helpful or not. +- Submit issues through [issue tracker](https://github.com/Azure-Samples/key-vault-java-manage-key-vaults/issues) on GitHub. We are actively monitoring the issues and improving our samples. +- If you wish to make code changes to samples, or contribute something new, please follow the [GitHub Forks / Pull requests model](https://help.github.com/articles/fork-a-repo/): Fork the sample repo, make the change and propose it back by submitting a pull request. \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d8d98a8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Microsoft Corporation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 0d03954..b1dc338 100644 --- a/README.md +++ b/README.md @@ -1 +1,41 @@ -# key-vault-java-manage-key-vaults +--- +services: Keyvault +platforms: java +author: jianghaolu +--- + +#Getting Started with Keyvault - Manage Key Vault - in Java # + + + Azure Key Vault sample for managing key vaults - + - Create a key vault + - Authorize an application + - Update a key vault + - alter configurations + - change permissions + - Create another key vault + - List key vaults + - Delete a key vault. + + +## Running this Sample ## + +To run this sample: + +Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an auth file. See [how to create an auth file](https://github.com/Azure/azure-sdk-for-java/blob/master/AUTH.md). + + git clone https://github.com/Azure-Samples/key-vault-java-manage-key-vaults.git + + cd key-vault-java-manage-key-vaults + + mvn clean compile exec:java + +## More information ## + +[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) + +--- + +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..c5e837d --- /dev/null +++ b/pom.xml @@ -0,0 +1,66 @@ + + 4.0.0 + com.microsoft.azure + key-vault-java-manage-key-vaults + 0.0.1-SNAPSHOT + ManageKeyVault + + https://github.com/Azure/key-vault-java-manage-key-vaults + + + com.microsoft.azure + azure + 1.0.0-beta3 + + + + src + + + resources + + + + + org.codehaus.mojo + exec-maven-plugin + 1.4.0 + + com.microsoft.azure.management.keyvault.samples.ManageKeyVault + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.0 + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + attached + + package + + + jar-with-dependencies + + + + com.microsoft.azure.management.keyvault.samples.ManageKeyVault + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/microsoft/azure/management/keyvault/samples/ManageKeyVault.java b/src/main/java/com/microsoft/azure/management/keyvault/samples/ManageKeyVault.java new file mode 100644 index 0000000..c5c6a0d --- /dev/null +++ b/src/main/java/com/microsoft/azure/management/keyvault/samples/ManageKeyVault.java @@ -0,0 +1,170 @@ +/** + * + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + * + */ + +package com.microsoft.azure.management.keyvault.samples; + +import com.microsoft.azure.management.Azure; +import com.microsoft.azure.credentials.ApplicationTokenCredentials; +import com.microsoft.azure.management.keyvault.KeyPermissions; +import com.microsoft.azure.management.keyvault.SecretPermissions; +import com.microsoft.azure.management.keyvault.Vault; +import com.microsoft.azure.management.resources.fluentcore.arm.Region; +import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer; +import com.microsoft.azure.management.samples.Utils; +import okhttp3.logging.HttpLoggingInterceptor; + +import java.io.File; + +/** + * Azure Key Vault sample for managing key vaults - + * - Create a key vault + * - Authorize an application + * - Update a key vault + * - alter configurations + * - change permissions + * - Create another key vault + * - List key vaults + * - Delete a key vault. + */ +public final class ManageKeyVault { + + /** + * Main entry point. + * @param args the parameters + */ + public static void main(String[] args) { + final String vaultName1 = ResourceNamer.randomResourceName("vault1", 20); + final String vaultName2 = ResourceNamer.randomResourceName("vault2", 20); + final String rgName = ResourceNamer.randomResourceName("rgNEMV", 24); + + try { + + //============================================================= + // Authenticate + + final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION")); + + Azure azure = Azure + .configure() + .withLogLevel(HttpLoggingInterceptor.Level.BASIC) + .authenticate(credFile) + .withDefaultSubscription(); + + // Print selected subscription + System.out.println("Selected subscription: " + azure.subscriptionId()); + try { + + + //============================================================ + // Create a key vault with empty access policy + + System.out.println("Creating a key vault..."); + + Vault vault1 = azure.vaults() + .define(vaultName1) + .withRegion(Region.US_WEST) + .withNewResourceGroup(rgName) + .withEmptyAccessPolicy() + .create(); + + System.out.println("Created key vault"); + Utils.print(vault1); + + //============================================================ + // Authorize an application + + System.out.println("Authorizing the application associated with the current service principal..."); + + vault1 = vault1.update() + .defineAccessPolicy() + .forServicePrincipal(ApplicationTokenCredentials.fromFile(credFile).getClientId()) + .allowKeyAllPermissions() + .allowSecretPermissions(SecretPermissions.GET) + .allowSecretPermissions(SecretPermissions.LIST) + .attach() + .apply(); + + System.out.println("Updated key vault"); + Utils.print(vault1); + + //============================================================ + // Update a key vault + + System.out.println("Update a key vault to enable deployments and add permissions to the application..."); + + vault1 = vault1.update() + .withDeploymentEnabled() + .withTemplateDeploymentEnabled() + .updateAccessPolicy(vault1.accessPolicies().get(0).objectId()) + .allowSecretAllPermissions() + .parent() + .apply(); + + System.out.println("Updated key vault"); + // Print the network security group + Utils.print(vault1); + + + //============================================================ + // Create another key vault + + Vault vault2 = azure.vaults() + .define(vaultName2) + .withRegion(Region.US_EAST) + .withExistingResourceGroup(rgName) + .defineAccessPolicy() + .forServicePrincipal(ApplicationTokenCredentials.fromFile(credFile).getClientId()) + .allowKeyPermissions(KeyPermissions.LIST) + .allowKeyPermissions(KeyPermissions.GET) + .allowKeyPermissions(KeyPermissions.DECRYPT) + .allowSecretPermissions(SecretPermissions.GET) + .attach() + .create(); + + System.out.println("Created key vault"); + // Print the network security group + Utils.print(vault2); + + + //============================================================ + // List key vaults + + System.out.println("Listing key vaults..."); + + for (Vault vault : azure.vaults().listByGroup(rgName)) { + Utils.print(vault); + } + + //============================================================ + // Delete key vaults + System.out.println("Deleting the key vaults"); + azure.vaults().delete(vault1.id()); + azure.vaults().delete(vault2.id()); + System.out.println("Deleted the key vaults"); + } catch (Exception e) { + System.err.println(e.getMessage()); + } finally { + try { + System.out.println("Deleting Resource Group: " + rgName); + azure.resourceGroups().delete(rgName); + System.out.println("Deleted Resource Group: " + rgName); + } catch (NullPointerException npe) { + System.out.println("Did not create any resources in Azure. No clean up is necessary"); + } catch (Exception g) { + g.printStackTrace(); + } + } + + } catch (Exception e) { + System.out.println(e.getMessage()); + e.printStackTrace(); + } + } + + private ManageKeyVault() { + } +} diff --git a/src/main/java/com/microsoft/azure/management/samples/Utils.java b/src/main/java/com/microsoft/azure/management/samples/Utils.java new file mode 100644 index 0000000..224fb3f --- /dev/null +++ b/src/main/java/com/microsoft/azure/management/samples/Utils.java @@ -0,0 +1,596 @@ +/** + * + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + * + */ + +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.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.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; + +/** + * Common utils for Azure management samples. + */ + +public final class Utils { + + /** + * Print virtual machine info. + * @param resource a virtual machine + */ + public static void print(VirtualMachine resource) { + + StringBuilder storageProfile = new StringBuilder().append("\n\tStorageProfile: "); + if (resource.storageProfile().imageReference() != null) { + storageProfile.append("\n\t\tImageReference:"); + storageProfile.append("\n\t\t\tPublisher: ").append(resource.storageProfile().imageReference().publisher()); + storageProfile.append("\n\t\t\tOffer: ").append(resource.storageProfile().imageReference().offer()); + storageProfile.append("\n\t\t\tSKU: ").append(resource.storageProfile().imageReference().sku()); + storageProfile.append("\n\t\t\tVersion: ").append(resource.storageProfile().imageReference().version()); + } + + if (resource.storageProfile().osDisk() != null) { + storageProfile.append("\n\t\tOSDisk:"); + storageProfile.append("\n\t\t\tOSType: ").append(resource.storageProfile().osDisk().osType()); + storageProfile.append("\n\t\t\tName: ").append(resource.storageProfile().osDisk().name()); + storageProfile.append("\n\t\t\tCaching: ").append(resource.storageProfile().osDisk().caching()); + storageProfile.append("\n\t\t\tCreateOption: ").append(resource.storageProfile().osDisk().createOption()); + storageProfile.append("\n\t\t\tDiskSizeGB: ").append(resource.storageProfile().osDisk().diskSizeGB()); + if (resource.storageProfile().osDisk().image() != null) { + storageProfile.append("\n\t\t\tImage Uri: ").append(resource.storageProfile().osDisk().image().uri()); + } + if (resource.storageProfile().osDisk().vhd() != null) { + storageProfile.append("\n\t\t\tVhd Uri: ").append(resource.storageProfile().osDisk().vhd().uri()); + } + if (resource.storageProfile().osDisk().encryptionSettings() != null) { + storageProfile.append("\n\t\t\tEncryptionSettings: "); + storageProfile.append("\n\t\t\t\tEnabled: ").append(resource.storageProfile().osDisk().encryptionSettings().enabled()); + storageProfile.append("\n\t\t\t\tDiskEncryptionKey Uri: ").append(resource + .storageProfile() + .osDisk() + .encryptionSettings() + .diskEncryptionKey().secretUrl()); + storageProfile.append("\n\t\t\t\tKeyEncryptionKey Uri: ").append(resource + .storageProfile() + .osDisk() + .encryptionSettings() + .keyEncryptionKey().keyUrl()); + } + } + + if (resource.storageProfile().dataDisks() != null) { + int i = 0; + for (DataDisk disk : resource.storageProfile().dataDisks()) { + storageProfile.append("\n\t\tDataDisk: #").append(i++); + storageProfile.append("\n\t\t\tName: ").append(disk.name()); + storageProfile.append("\n\t\t\tCaching: ").append(disk.caching()); + storageProfile.append("\n\t\t\tCreateOption: ").append(disk.createOption()); + storageProfile.append("\n\t\t\tDiskSizeGB: ").append(disk.diskSizeGB()); + storageProfile.append("\n\t\t\tLun: ").append(disk.lun()); + if (disk.vhd().uri() != null) { + storageProfile.append("\n\t\t\tVhd Uri: ").append(disk.vhd().uri()); + } + if (disk.image() != null) { + storageProfile.append("\n\t\t\tImage Uri: ").append(disk.image().uri()); + } + } + } + + StringBuilder osProfile = new StringBuilder().append("\n\tOSProfile: "); + osProfile.append("\n\t\tComputerName:").append(resource.osProfile().computerName()); + if (resource.osProfile().windowsConfiguration() != null) { + osProfile.append("\n\t\t\tWindowsConfiguration: "); + osProfile.append("\n\t\t\t\tProvisionVMAgent: ") + .append(resource.osProfile().windowsConfiguration().provisionVMAgent()); + osProfile.append("\n\t\t\t\tEnableAutomaticUpdates: ") + .append(resource.osProfile().windowsConfiguration().enableAutomaticUpdates()); + osProfile.append("\n\t\t\t\tTimeZone: ") + .append(resource.osProfile().windowsConfiguration().timeZone()); + } + + if (resource.osProfile().linuxConfiguration() != null) { + osProfile.append("\n\t\t\tLinuxConfiguration: "); + osProfile.append("\n\t\t\t\tDisablePasswordAuthentication: ") + .append(resource.osProfile().linuxConfiguration().disablePasswordAuthentication()); + } + + StringBuilder networkProfile = new StringBuilder().append("\n\tNetworkProfile: "); + for (String networkInterfaceId : resource.networkInterfaceIds()) { + 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()) + .append("\n\tRegion: ").append(resource.region()) + .append("\n\tTags: ").append(resource.tags()) + .append("\n\tHardwareProfile: ") + .append("\n\t\tSize: ").append(resource.size()) + .append(storageProfile) + .append(osProfile) + .append(networkProfile) + .append(extensions) + .toString()); + } + + + /** + * Print availability set info. + * @param resource an availability set + */ + public static void print(AvailabilitySet resource) { + + System.out.println(new StringBuilder().append("Availability Set: ").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\tFault domain count: ").append(resource.faultDomainCount()) + .append("\n\tUpdate domain count: ").append(resource.updateDomainCount()) + .toString()); + } + + /** + * Print network info. + * @param resource a network + * @throws IOException IO errors + * @throws CloudException Cloud errors + */ + public static void print(Network resource) throws CloudException, IOException { + StringBuilder info = new StringBuilder(); + info.append("Network: ").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\tAddress spaces: ").append(resource.addressSpaces()) + .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.getNetworkSecurityGroup(); + if (subnetNsg != null) { + info.append("\n\t\tNetwork security group: ").append(subnetNsg.id()); + } + } + + System.out.println(info.toString()); + } + + /** + * Print network interface. + * @param resource a network interface + */ + public static void print(NetworkInterface resource) { + StringBuilder info = new StringBuilder(); + info.append("NetworkInterface: ").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\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 + for (String dnsServerIp : resource.dnsServers()) { + info.append("\n\t\t").append(dnsServerIp); + } + info.append("\n\t IP 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()) + .append("\n\tPrimary virtual network ID: ").append(resource.primaryIpConfiguration().networkId()) + .append("\n\tPrimary subnet name:").append(resource.primaryIpConfiguration().subnetName()); + + System.out.println(info.toString()); + } + + /** + * Print network security group. + * @param resource a network security group + */ + public static void print(NetworkSecurityGroup resource) { + StringBuilder info = new StringBuilder(); + info.append("NSG: ").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()); + + // Output security rules + 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()) + .append("\n\t\tFrom address: ").append(rule.sourceAddressPrefix()) + .append("\n\t\tFrom port range: ").append(rule.sourcePortRange()) + .append("\n\t\tTo address: ").append(rule.destinationAddressPrefix()) + .append("\n\t\tTo port: ").append(rule.destinationPortRange()) + .append("\n\t\tProtocol: ").append(rule.protocol()) + .append("\n\t\tPriority: ").append(rule.priority()); + } + + System.out.println(info.toString()); + } + + /** + * Print public IP address. + * @param resource a public IP address + */ + public static void print(PublicIpAddress resource) { + System.out.println(new StringBuilder().append("Public IP Address: ").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\tIP Address: ").append(resource.ipAddress()) + .append("\n\tLeaf domain label: ").append(resource.leafDomainLabel()) + .append("\n\tFQDN: ").append(resource.fqdn()) + .append("\n\tReverse FQDN: ").append(resource.reverseFqdn()) + .append("\n\tIdle timeout (minutes): ").append(resource.idleTimeoutInMinutes()) + .append("\n\tIP allocation method: ").append(resource.ipAllocationMethod()) + .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. + * @param storageAccount a storage account + */ + public static void print(StorageAccount storageAccount) { + System.out.println(storageAccount.name() + + " created @ " + storageAccount.creationTime()); + } + + /** + * Print storage account keys. + * @param storageAccountKeys a list of storage account keys + */ + public static void print(List storageAccountKeys) { + for (int i = 0; i < storageAccountKeys.size(); 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. + * @return a random name + * */ + public static String createRandomName(String namePrefix) { + String root = UUID.randomUUID().toString().replace("-", ""); + long millis = Calendar.getInstance().getTimeInMillis(); + long datePart = millis % 10000000L; + return namePrefix + root.toLowerCase().substring(0, 3) + datePart; + } + + private Utils() { + + } +}