From 90b00de2a3bb39c175ad5ab4604fa90db71b09a9 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 11 May 2016 15:50:28 -0700 Subject: [PATCH 1/5] Support for Azure.configure().authenticate(File credentialsFile) (equivalent to Azure.authenticate(File credentialsFile)) --- .../microsoft/azure/implementation/Azure.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/azure/src/main/java/com/microsoft/azure/implementation/Azure.java b/azure/src/main/java/com/microsoft/azure/implementation/Azure.java index f0650d884cd1..82f338eb4f36 100644 --- a/azure/src/main/java/com/microsoft/azure/implementation/Azure.java +++ b/azure/src/main/java/com/microsoft/azure/implementation/Azure.java @@ -77,16 +77,44 @@ public static Configurable configure() { } public interface Configurable extends AzureConfigurable { + /** + * Authenticates API access based on the provided credentials + * @param credentials The credentials to authenticate API access with + * @return + */ Authenticated authenticate(ServiceClientCredentials credentials); + + /** + * Authenticates API access using a properties file containing the required credentials + * @param credentialsFile The file containing the credentials in the standard Java properties file format, + * with the following keys: + * subscription= + * tenant= + * client= + * key= + * managementURI= + * baseURL= + * authURL= + * @return Authenticated Azure client + * @throws IOException + */ + Authenticated authenticate(File credentialsFile) throws IOException; } + private static final class ConfigurableImpl extends AzureConfigurableImpl implements Configurable { @Override public Authenticated authenticate(ServiceClientCredentials credentials) { return Azure.authenticate(buildRestClient(credentials)); } + + @Override + public Authenticated authenticate(File credentialsFile) throws IOException { + return Azure.authenticate(credentialsFile); + } } + public interface Authenticated { Subscriptions subscriptions(); Tenants tenants(); From 6c95a50c2b52aaf61e0e6a82293c045cf465cfa3 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 11 May 2016 23:17:26 -0700 Subject: [PATCH 2/5] Skeletal support for PublicIpAddresses (basic creation and listing) --- .../azure/management/network/PublicIP.java | 5 - .../management/network/PublicIpAddress.java | 37 ++++++ .../management/network/PublicIpAddresses.java | 30 +++++ .../implementation/NetworkManager.java | 64 +++++++++++ .../implementation/PublicIpAddressImpl.java | 66 +++++++++++ .../implementation/PublicIpAddressesImpl.java | 105 ++++++++++++++++++ .../microsoft/azure/implementation/Azure.java | 8 ++ .../java/com/microsoft/azure/AzureTests.java | 49 +++++++- 8 files changed, 356 insertions(+), 8 deletions(-) delete mode 100644 azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIP.java create mode 100644 azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddress.java create mode 100644 azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddresses.java create mode 100644 azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkManager.java create mode 100644 azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java create mode 100644 azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressesImpl.java diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIP.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIP.java deleted file mode 100644 index 669f22fdfb86..000000000000 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIP.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.microsoft.azure.management.network; - - -public interface PublicIP { -} 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 new file mode 100644 index 000000000000..a66811c9fafa --- /dev/null +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddress.java @@ -0,0 +1,37 @@ +/** + * 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.network; + +import com.microsoft.azure.management.network.implementation.api.PublicIPAddressInner; +import com.microsoft.azure.management.resources.fluentcore.arm.models.GroupableResource; +import com.microsoft.azure.management.resources.fluentcore.model.Provisionable; +import com.microsoft.azure.management.resources.fluentcore.model.Refreshable; +import com.microsoft.azure.management.resources.fluentcore.model.Wrapper; + +public interface PublicIpAddress extends + GroupableResource, + Refreshable, + Wrapper { + + /*********************************************************** + * Getters + ***********************************************************/ + + + /************************************************************** + * Fluent interfaces for provisioning + **************************************************************/ + + interface DefinitionBlank extends GroupableResource.DefinitionWithRegion { + } + + interface DefinitionWithGroup extends GroupableResource.DefinitionWithGroup { + } + + interface DefinitionProvisionable extends Provisionable { + } +} + diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddresses.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddresses.java new file mode 100644 index 000000000000..facc59b0f940 --- /dev/null +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/PublicIpAddresses.java @@ -0,0 +1,30 @@ +/** + * 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.network; + +import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsDeletingByGroup; +import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingByGroup; +import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsListingByGroup; +import com.microsoft.azure.management.resources.fluentcore.collection.SupportsCreating; +import com.microsoft.azure.management.resources.fluentcore.collection.SupportsDeleting; +import com.microsoft.azure.management.resources.fluentcore.collection.SupportsGetting; +import com.microsoft.azure.management.resources.fluentcore.collection.SupportsListing; + +public interface PublicIpAddresses extends + SupportsCreating, + SupportsListing, + SupportsListingByGroup, + SupportsGetting, + SupportsGettingByGroup, + SupportsDeleting, + SupportsDeletingByGroup { + + interface InGroup extends + SupportsListing, + SupportsCreating, + SupportsDeleting { + } +} diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkManager.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkManager.java new file mode 100644 index 000000000000..a2c9e2429430 --- /dev/null +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkManager.java @@ -0,0 +1,64 @@ +/** + * 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.network.implementation; + +import com.microsoft.azure.management.network.PublicIpAddresses; +import com.microsoft.azure.management.network.implementation.api.NetworkManagementClientImpl; +import com.microsoft.azure.management.resources.fluentcore.arm.AzureConfigurable; +import com.microsoft.azure.management.resources.fluentcore.arm.implementation.AzureConfigurableImpl; +import com.microsoft.azure.management.resources.implementation.ResourceManager; +import com.microsoft.rest.RestClient; +import com.microsoft.rest.credentials.ServiceClientCredentials; + +public final class NetworkManager { + private final NetworkManagementClientImpl networkManagementClient; + + // Dependent managers + private final ResourceManager resourceManager; + + // Collections + private PublicIpAddresses publicIpAddresses; + + public static Configurable configure() { + return new NetworkManager.ConfigurableImpl(); + } + + public static NetworkManager authenticate(ServiceClientCredentials credentials, String subscriptionId) { + return new NetworkManager(new RestClient + .Builder("https://management.azure.com") + .withCredentials(credentials) + .build(), subscriptionId); + } + + public static NetworkManager authenticate(RestClient restClient, String subscriptionId) { + return new NetworkManager(restClient, subscriptionId); + } + + public interface Configurable extends AzureConfigurable { + NetworkManager authenticate(ServiceClientCredentials credentials, String subscriptionId); + } + + private static class ConfigurableImpl extends AzureConfigurableImpl implements Configurable { + public NetworkManager authenticate(ServiceClientCredentials credentials, String subscriptionId) { + return NetworkManager.authenticate(buildRestClient(credentials), subscriptionId); + } + } + + private NetworkManager(RestClient restClient, String subscriptionId) { + networkManagementClient = new NetworkManagementClientImpl(restClient); + networkManagementClient.setSubscriptionId(subscriptionId); + this.resourceManager = ResourceManager.authenticate(restClient).withSubscription(subscriptionId); + } + + public PublicIpAddresses publicIpAddresses() { + if(this.publicIpAddresses == null) { + this.publicIpAddresses = new PublicIpAddressesImpl( + this.networkManagementClient.publicIPAddresses(), + this.resourceManager.resourceGroups()); + } + return this.publicIpAddresses; + } + } 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 new file mode 100644 index 000000000000..7d5020fe4612 --- /dev/null +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java @@ -0,0 +1,66 @@ +/** + * 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.network.implementation; + +import com.microsoft.azure.management.network.PublicIpAddress; +import com.microsoft.azure.management.network.implementation.api.PublicIPAddressInner; +import com.microsoft.azure.management.network.implementation.api.PublicIPAddressesInner; +import com.microsoft.azure.management.resources.ResourceGroups; +import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +import com.microsoft.rest.ServiceResponse; + +class PublicIpAddressImpl + extends GroupableResourceImpl + implements + PublicIpAddress, + PublicIpAddress.DefinitionBlank, + PublicIpAddress.DefinitionWithGroup, + PublicIpAddress.DefinitionProvisionable + { + + private String name; + + private final PublicIPAddressesInner client; + + PublicIpAddressImpl(String name, + PublicIPAddressInner innerModel, + final PublicIPAddressesInner client, + final ResourceGroups resourceGroups) { + super(innerModel.id(), innerModel, resourceGroups); + this.name = name; + this.client = client; + } + + @Override + public String name() { + return this.name; + } + + @Override + public PublicIpAddress refresh() throws Exception { + ServiceResponse response = + this.client.get(this.group(), this.name()); + PublicIPAddressInner inner = response.getBody(); + this.setInner(inner); + clearWrapperProperties(); + return this; + } + + @Override + public PublicIpAddressImpl provision() throws Exception { + ensureGroup(); + + ServiceResponse response = + this.client.createOrUpdate(this.group(), this.name(), this.inner()); + this.setInner(response.getBody()); + clearWrapperProperties(); + return this; + } + + private void clearWrapperProperties() { + + } +} diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressesImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressesImpl.java new file mode 100644 index 000000000000..83f94224affc --- /dev/null +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressesImpl.java @@ -0,0 +1,105 @@ +/** + * 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.network.implementation; + +import com.microsoft.azure.CloudException; +import com.microsoft.azure.Page; +import com.microsoft.azure.PagedList; +import com.microsoft.azure.management.network.PublicIpAddress; +import com.microsoft.azure.management.network.PublicIpAddresses; +import com.microsoft.azure.management.network.implementation.api.PublicIPAddressInner; +import com.microsoft.azure.management.network.implementation.api.PublicIPAddressesInner; +import com.microsoft.azure.management.resources.ResourceGroups; +import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils; +import com.microsoft.azure.management.resources.fluentcore.utils.PagedListConverter; +import com.microsoft.azure.management.resources.implementation.api.PageImpl; +import com.microsoft.rest.RestException; +import com.microsoft.rest.ServiceResponse; + +import java.io.IOException; +import java.util.List; + +public class PublicIpAddressesImpl + implements PublicIpAddresses { + private final PublicIPAddressesInner client; + private final ResourceGroups resourceGroups; + private final PagedListConverter converter; + + public PublicIpAddressesImpl(final PublicIPAddressesInner client, final ResourceGroups resourceGroups) { + this.client = client; + this.resourceGroups = resourceGroups; + this.converter = new PagedListConverter() { + @Override + public PublicIpAddress typeConvert(PublicIPAddressInner publicIpAddressInner) { + return createFluentModel(publicIpAddressInner); + } + }; + } + + @Override + public PagedList list() throws CloudException, IOException { + ServiceResponse> response = client.listAll(); + return converter.convert(toPagedList(response.getBody())); + } + + @Override + public PagedList list(String groupName) throws CloudException, IOException { + ServiceResponse> response = client.list(groupName); + return converter.convert(toPagedList(response.getBody())); + } + + @Override + public PublicIpAddress get(String id) throws CloudException, IOException { + PublicIPAddressInner inner = client.get( + ResourceUtils.groupFromResourceId(id), + ResourceUtils.nameFromResourceId(id)).getBody(); + return createFluentModel(inner); + } + + @Override + public PublicIpAddress get(String groupName, String name) throws CloudException, IOException { + ServiceResponse serviceResponse = this.client.get(groupName, name); + return createFluentModel(serviceResponse.getBody()); + } + + @Override + public void delete(String id) throws Exception { + this.delete(ResourceUtils.groupFromResourceId(id), ResourceUtils.nameFromResourceId(id)); + } + + @Override + public void delete(String groupName, String name) throws Exception { + this.client.delete(groupName, name); + } + + @Override + public PublicIpAddress.DefinitionBlank define(String name) throws Exception { + return createFluentModel(name); + } + + private PagedList toPagedList(List list) { + PageImpl page = new PageImpl<>(); + page.setItems(list); + page.setNextPageLink(null); + return new PagedList(page) { + @Override + public Page nextPage(String nextPageLink) throws RestException, IOException { + return null; + } + }; + } + + /** Fluent model create helpers **/ + + private PublicIpAddressImpl createFluentModel(String name) { + PublicIPAddressInner inner = new PublicIPAddressInner(); + return new PublicIpAddressImpl(name, inner, this.client, this.resourceGroups); + } + + private PublicIpAddressImpl createFluentModel(PublicIPAddressInner inner) { + return new PublicIpAddressImpl(inner.name(), inner, this.client, this.resourceGroups); + } +} diff --git a/azure/src/main/java/com/microsoft/azure/implementation/Azure.java b/azure/src/main/java/com/microsoft/azure/implementation/Azure.java index 82f338eb4f36..a78cf2390f8c 100644 --- a/azure/src/main/java/com/microsoft/azure/implementation/Azure.java +++ b/azure/src/main/java/com/microsoft/azure/implementation/Azure.java @@ -14,6 +14,8 @@ import com.microsoft.azure.management.compute.AvailabilitySets; import com.microsoft.azure.management.compute.VirtualMachines; import com.microsoft.azure.management.compute.implementation.ComputeManager; +import com.microsoft.azure.management.network.PublicIpAddresses; +import com.microsoft.azure.management.network.implementation.NetworkManager; import com.microsoft.azure.management.resources.Deployments; import com.microsoft.azure.management.resources.GenericResources; import com.microsoft.azure.management.resources.Subscriptions; @@ -35,6 +37,7 @@ public final class Azure { private final ResourceManager resourceManager; private final StorageManager storageManager; private final ComputeManager computeManager; + private final NetworkManager networkManager; public static Authenticated authenticate(ServiceClientCredentials credentials) { return new AuthenticatedImpl(new RestClient @@ -168,6 +171,7 @@ private Azure(RestClient restClient, String subscriptionId) { this.resourceManager = ResourceManager.authenticate(restClient).withSubscription(subscriptionId); this.storageManager = StorageManager.authenticate(restClient, subscriptionId); this.computeManager = ComputeManager.authenticate(restClient, subscriptionId); + this.networkManager = NetworkManager.authenticate(restClient, subscriptionId); } public ResourceGroups resourceGroups() { @@ -193,4 +197,8 @@ public AvailabilitySets availabilitySets() { public VirtualMachines virtualMachines() { return computeManager.virtualMachines(); } + + public PublicIpAddresses publicIpAddresses() { + return this.networkManager.publicIpAddresses(); + } } diff --git a/azure/src/test/java/com/microsoft/azure/AzureTests.java b/azure/src/test/java/com/microsoft/azure/AzureTests.java index dfb596898271..87360b64403e 100644 --- a/azure/src/test/java/com/microsoft/azure/AzureTests.java +++ b/azure/src/test/java/com/microsoft/azure/AzureTests.java @@ -3,12 +3,18 @@ import com.microsoft.azure.credentials.ApplicationTokenCredentials; import com.microsoft.azure.credentials.AzureEnvironment; import com.microsoft.azure.implementation.Azure; +import com.microsoft.azure.management.network.PublicIpAddress; import com.microsoft.azure.management.resources.Subscriptions; import com.microsoft.azure.management.resources.fluentcore.arm.Region; import com.microsoft.azure.management.storage.StorageAccount; import com.microsoft.azure.management.storage.implementation.api.AccountType; import com.microsoft.rest.credentials.ServiceClientCredentials; import okhttp3.logging.HttpLoggingInterceptor; +import okhttp3.logging.HttpLoggingInterceptor.Level; + +import java.io.File; +import java.io.IOException; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -21,19 +27,56 @@ public class AzureTests { AzureEnvironment.AZURE); private static final String subscriptionId = System.getenv("resourceClient-id"); private Subscriptions subscriptions; - private Azure azure; + private Azure azure, azure2; + public static void main(String[] args) throws IOException, CloudException { + Azure azure = Azure.authenticate(new File("my.auth")) + .withSubscription("9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef"); + + System.out.println(String.valueOf(azure.resourceGroups().list().size())); + + Azure.configure().withLogLevel(Level.BASIC).authenticate(new File("my.auth")); + System.out.println(String.valueOf(azure.resourceGroups().list().size())); + } + @Before public void setup() throws Exception { - Azure.Authenticated azureAuthed = Azure.configure() + // Authenticate based on credentials instance + Azure.Authenticated azureAuthed = Azure.configure() .withLogLevel(HttpLoggingInterceptor.Level.BASIC) .withUserAgent("AzureTests") .authenticate(credentials); subscriptions = azureAuthed.subscriptions(); azure = azureAuthed.withSubscription(subscriptionId); + + // Authenticate based on file + this.azure2 = Azure.authenticate(new File("my.auth")) + .withSubscription("9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef"); + } + @Test public void listPublicIpAddresses() throws Exception { + // Create a new public IP address the minimal way + String suffix = String.valueOf(System.currentTimeMillis()); + String newPipName = "pip" + suffix; + PublicIpAddress pip = azure2.publicIpAddresses().define(newPipName) + .withRegion(Region.US_WEST) + .withNewGroup() + .provision(); + + // Verify list + int publicIpAddressCount = azure2.publicIpAddresses().list().size(); + System.out.println(publicIpAddressCount); + Assert.assertTrue(0 < publicIpAddressCount); + String resourceGroupName = pip.group(); + pip = azure2.publicIpAddresses().get(resourceGroupName, newPipName); + Assert.assertTrue(pip.name().equalsIgnoreCase(newPipName)); + + // Verify delete + azure.publicIpAddresses().delete(pip.id()); + } + @Test public void listSubscriptions() throws Exception { Assert.assertTrue(0 < subscriptions.list().size()); @@ -48,7 +91,7 @@ public void listResourceGroups() throws Exception { public void listStorageAccounts() throws Exception { Assert.assertTrue(0 < azure.storageAccounts().list().size()); } - + @Test public void createStorageAccount() throws Exception { StorageAccount storageAccount = azure.storageAccounts().define("my-stg1") From 5d6341900fbef821937282554ed6fdbc4617eb66 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 12 May 2016 01:14:48 -0700 Subject: [PATCH 3/5] PublicIPAddress now has IP allocation method and DNS leaf label (R/W). Also fixing some bugs in general delete() logic. --- .../management/network/PublicIpAddress.java | 52 ++++++++++++++++-- .../implementation/PublicIpAddressImpl.java | 54 +++++++++++++++++-- .../fluentcore/arm/ResourceUtils.java | 4 +- .../java/com/microsoft/azure/AzureTests.java | 11 +++- 4 files changed, 110 insertions(+), 11 deletions(-) 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 a66811c9fafa..3c797146ca41 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 @@ -19,19 +19,65 @@ public interface PublicIpAddress extends /*********************************************************** * Getters ***********************************************************/ - + String ipAddress(); + String leafDomainLabel(); /************************************************************** * Fluent interfaces for provisioning **************************************************************/ - + interface Definitions extends + DefinitionBlank, + DefinitionWithGroup, + DefinitionWithIpAddress, + DefinitionWithLeafDomainLabel, + DefinitionProvisionable {} + + interface DefinitionBlank extends GroupableResource.DefinitionWithRegion { } interface DefinitionWithGroup extends GroupableResource.DefinitionWithGroup { } - interface DefinitionProvisionable extends Provisionable { + public interface DefinitionWithIpAddress { + /** + * Enables static IP address allocation. The actual IP address allocated for this resource by Azure can be obtained + * after the provisioning process is complete from ipAddress(). + * @return The next stage of the public IP address definition + */ + DefinitionProvisionable withStaticIp(); + + /** + * Enables dynamic IP address allocation. + * @return The next stage of the public IP address definition + */ + DefinitionProvisionable withDynamicIp(); + } + + /** + * A public IP address definition allowing to specify the leaf domain label, if any + */ + public interface DefinitionWithLeafDomainLabel { + /** + * Specifies the leaf domain label to associate with this public IP address. The fully qualified domain name (FQDN) + * will be constructed automatically by appending the rest of the domain to this label. + * @param dnsName The leaf domain label to use. This must follow the required naming convention for leaf domain names. + * @return The next stage of the public IP address definition + */ + DefinitionProvisionable withLeafDomainLabel(String dnsName); + + /** + * Ensures that no leaf domain label will be used. This means that this public IP address will not be associated with a domain name. + * @return The next stage of the public IP address definition + */ + DefinitionProvisionable withoutLeafDomainLabel(); + } + + + interface DefinitionProvisionable extends + Provisionable, + DefinitionWithLeafDomainLabel, + DefinitionWithIpAddress { } } 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 7d5020fe4612..f696c7b1e2cf 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 @@ -6,6 +6,7 @@ package com.microsoft.azure.management.network.implementation; import com.microsoft.azure.management.network.PublicIpAddress; +import com.microsoft.azure.management.network.implementation.api.PublicIPAddressDnsSettings; import com.microsoft.azure.management.network.implementation.api.PublicIPAddressInner; import com.microsoft.azure.management.network.implementation.api.PublicIPAddressesInner; import com.microsoft.azure.management.resources.ResourceGroups; @@ -16,9 +17,7 @@ class PublicIpAddressImpl extends GroupableResourceImpl implements PublicIpAddress, - PublicIpAddress.DefinitionBlank, - PublicIpAddress.DefinitionWithGroup, - PublicIpAddress.DefinitionProvisionable + PublicIpAddress.Definitions { private String name; @@ -63,4 +62,51 @@ public PublicIpAddressImpl provision() throws Exception { private void clearWrapperProperties() { } -} + + + @Override + public PublicIpAddressImpl withStaticIp() { + this.inner().setPublicIPAllocationMethod("Static"); // TODO: Replace with IpAllocationMethod.STATIC + return this; + } + + + @Override + public PublicIpAddressImpl withDynamicIp() { + this.inner().setPublicIPAllocationMethod("Dynamic"); // TODO: Replace with IpAllocationMethod.DYNAMIC + return this; + } + + @Override + public PublicIpAddressImpl withLeafDomainLabel(String dnsName) { + PublicIPAddressDnsSettings dnsSettings; + if(dnsName == null) { + this.inner().setDnsSettings(null); + return this; + } else if(null == (dnsSettings = this.inner().dnsSettings())) { + dnsSettings = new PublicIPAddressDnsSettings(); + this.inner().setDnsSettings(dnsSettings); + } + dnsSettings.setDomainNameLabel(dnsName); + return this; + } + + @Override + public PublicIpAddressImpl withoutLeafDomainLabel() { + return this.withLeafDomainLabel(null); + } + + @Override + public String ipAddress() { + return this.inner().ipAddress(); + } + + @Override + public String leafDomainLabel() { + if(this.inner().dnsSettings() == null) { + return null; + } else { + return this.inner().dnsSettings().domainNameLabel(); + } + } +} \ No newline at end of file diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/ResourceUtils.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/ResourceUtils.java index 9b1af80bd671..96c60b70dff3 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/ResourceUtils.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/ResourceUtils.java @@ -5,7 +5,7 @@ public class ResourceUtils { public static String groupFromResourceId(String id) { - return extractFromResourceId(id, "resourcegroups"); + return extractFromResourceId(id, "resourceGroups"); } public static String extractFromResourceId(String id, String identifier) { @@ -22,6 +22,6 @@ public static String extractFromResourceId(String id, String identifier) { } public static String nameFromResourceId(String id) { - return null; + return id.split("/")[8]; } } diff --git a/azure/src/test/java/com/microsoft/azure/AzureTests.java b/azure/src/test/java/com/microsoft/azure/AzureTests.java index 87360b64403e..c665c31eff3b 100644 --- a/azure/src/test/java/com/microsoft/azure/AzureTests.java +++ b/azure/src/test/java/com/microsoft/azure/AzureTests.java @@ -56,13 +56,15 @@ public void setup() throws Exception { } - @Test public void listPublicIpAddresses() throws Exception { + @Test public void testPublicIpAddresses() throws Exception { // Create a new public IP address the minimal way String suffix = String.valueOf(System.currentTimeMillis()); String newPipName = "pip" + suffix; PublicIpAddress pip = azure2.publicIpAddresses().define(newPipName) .withRegion(Region.US_WEST) .withNewGroup() + .withDynamicIp() + .withLeafDomainLabel(newPipName) .provision(); // Verify list @@ -72,9 +74,14 @@ public void setup() throws Exception { String resourceGroupName = pip.group(); pip = azure2.publicIpAddresses().get(resourceGroupName, newPipName); Assert.assertTrue(pip.name().equalsIgnoreCase(newPipName)); + System.out.println(new StringBuilder().append("Public IP Address: ").append(pip.id()) + .append("\n\tIP Address: ").append(pip.ipAddress()) + .append("\n\tLeaf domain label: ").append(pip.leafDomainLabel()) + .toString()); // Verify delete - azure.publicIpAddresses().delete(pip.id()); + azure2.publicIpAddresses().delete(pip.id()); + azure2.resourceGroups().delete(resourceGroupName); } @Test From b4bc9c7f6450059f12e5d14b9328b32b2253bdc6 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 12 May 2016 11:44:02 -0700 Subject: [PATCH 4/5] fixing the network POM --- azure-mgmt-network/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/azure-mgmt-network/pom.xml b/azure-mgmt-network/pom.xml index 0bcabdee9ac8..0c243d2da07d 100644 --- a/azure-mgmt-network/pom.xml +++ b/azure-mgmt-network/pom.xml @@ -55,7 +55,6 @@ com.microsoft.azure azure-mgmt-resources 1.0.0-SNAPSHOT - test junit From b94b38ca0d0c5f9a4294dc626955758945f4a88a Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 12 May 2016 14:05:48 -0700 Subject: [PATCH 5/5] enabling Azure.authenticate(File).withDefaultSubscription() based on the subscription provided in the file --- .../microsoft/azure/implementation/Azure.java | 24 +++++++++++++++---- .../java/com/microsoft/azure/AzureTests.java | 8 +++---- .../ApplicationTokenCredentials.java | 24 +++++++++++++++---- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/azure/src/main/java/com/microsoft/azure/implementation/Azure.java b/azure/src/main/java/com/microsoft/azure/implementation/Azure.java index a78cf2390f8c..b77233dad927 100644 --- a/azure/src/main/java/com/microsoft/azure/implementation/Azure.java +++ b/azure/src/main/java/com/microsoft/azure/implementation/Azure.java @@ -63,12 +63,14 @@ public static Authenticated authenticate(ServiceClientCredentials credentials) { */ public static Authenticated authenticate(File credentialsFile) throws IOException { ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credentialsFile); - return new AuthenticatedImpl(new RestClient - .Builder(AzureEnvironment.AZURE.getBaseUrl()) - .withMapperAdapter(new AzureJacksonMapperAdapter()) + + return new AuthenticatedImpl( + new RestClient.Builder(AzureEnvironment.AZURE.getBaseUrl()) + .withMapperAdapter(new AzureJacksonMapperAdapter()) .withCredentials(credentials) .withBaseUrl(credentials.getEnvironment().getBaseUrl()) - .build()); + .build()) + .withDefaultSubscription(credentials.defaultSubscriptionId()); } public static Authenticated authenticate(RestClient restClient) { @@ -122,17 +124,24 @@ public interface Authenticated { Subscriptions subscriptions(); Tenants tenants(); Azure withSubscription(String subscriptionId); + Azure withDefaultSubscription(); } private static final class AuthenticatedImpl implements Authenticated { final private RestClient restClient; final private ResourceManager.Authenticated resourceManagerAuthenticated; - + private String defaultSubscription; + private AuthenticatedImpl(RestClient restClient) { this.resourceManagerAuthenticated = ResourceManager.authenticate(restClient); this.restClient = restClient; } + private AuthenticatedImpl withDefaultSubscription(String subscriptionId) { + this.defaultSubscription = subscriptionId; + return this; + } + @Override public Subscriptions subscriptions() { return resourceManagerAuthenticated.subscriptions(); @@ -147,6 +156,11 @@ public Tenants tenants() { public Azure withSubscription(String subscriptionId) { return new Azure(restClient, subscriptionId); } + + @Override + public Azure withDefaultSubscription() { + return withSubscription(this.defaultSubscription); + } } public interface ResourceGroups extends SupportsListing, diff --git a/azure/src/test/java/com/microsoft/azure/AzureTests.java b/azure/src/test/java/com/microsoft/azure/AzureTests.java index c665c31eff3b..ae678fcb1a79 100644 --- a/azure/src/test/java/com/microsoft/azure/AzureTests.java +++ b/azure/src/test/java/com/microsoft/azure/AzureTests.java @@ -31,8 +31,7 @@ public class AzureTests { public static void main(String[] args) throws IOException, CloudException { Azure azure = Azure.authenticate(new File("my.auth")) - .withSubscription("9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef"); - + .withDefaultSubscription(); System.out.println(String.valueOf(azure.resourceGroups().list().size())); Azure.configure().withLogLevel(Level.BASIC).authenticate(new File("my.auth")); @@ -52,12 +51,11 @@ public void setup() throws Exception { // Authenticate based on file this.azure2 = Azure.authenticate(new File("my.auth")) - .withSubscription("9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef"); - + .withDefaultSubscription(); } @Test public void testPublicIpAddresses() throws Exception { - // Create a new public IP address the minimal way + // Verify creation of a new public IP address String suffix = String.valueOf(System.currentTimeMillis()); String newPipName = "pip" + suffix; PublicIpAddress pip = azure2.publicIpAddresses().define(newPipName) diff --git a/runtimes/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java b/runtimes/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java index 1305f2c2c12d..c9eb75f58209 100644 --- a/runtimes/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java +++ b/runtimes/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java @@ -33,7 +33,9 @@ public class ApplicationTokenCredentials extends TokenCredentials { private AzureEnvironment environment; /** The current authentication result. */ private AuthenticationResult authenticationResult; - + /** The default subscription to use, if any */ + private String defaultSubscription; + /** * Initializes a new instance of the UserTokenCredentials. * @@ -80,7 +82,18 @@ public String toString() { } } - + /** + * @return The default subscription ID, if any + */ + public String defaultSubscriptionId() { + return defaultSubscription; + } + + ApplicationTokenCredentials withDefaultSubscriptionId(String subscriptionId) { + this.defaultSubscription = subscriptionId; + return this; + } + /** * Initializes the credentials based on the provided credentials file * @param credentialsFile A file with credentials, using the standard Java properties format @@ -113,7 +126,8 @@ public static ApplicationTokenCredentials fromFile(File credentialsFile) throws final String mgmtUri = authSettings.getProperty(CredentialSettings.MANAGEMENT_URI.toString()); final String authUrl = authSettings.getProperty(CredentialSettings.AUTH_URL.toString()); final String baseUrl = authSettings.getProperty(CredentialSettings.BASE_URL.toString()); - + final String defaultSubscriptionId = authSettings.getProperty(CredentialSettings.SUBSCRIPTION_ID.toString()); + return new ApplicationTokenCredentials( clientId, tenantId, @@ -123,7 +137,7 @@ public static ApplicationTokenCredentials fromFile(File credentialsFile) throws mgmtUri, true, baseUrl) - ); + ).withDefaultSubscriptionId(defaultSubscriptionId); } @@ -153,7 +167,7 @@ public String getDomain() { public String getSecret() { return secret; } - + /** * Gets the Azure environment to authenticate with. *