diff --git a/sdk/management/azure/src/main/java/com/azure/management/Azure.java b/sdk/management/azure/src/main/java/com/azure/management/Azure.java index 206f70ac26c1..764b125b95c3 100644 --- a/sdk/management/azure/src/main/java/com/azure/management/Azure.java +++ b/sdk/management/azure/src/main/java/com/azure/management/Azure.java @@ -5,7 +5,6 @@ import com.azure.core.credential.TokenCredential; import com.azure.core.http.HttpPipeline; -import com.azure.core.util.logging.ClientLogger; import com.azure.management.appservice.AppServiceCertificateOrders; import com.azure.management.appservice.AppServiceCertificates; import com.azure.management.appservice.AppServiceDomains; @@ -73,6 +72,8 @@ import com.azure.management.network.implementation.NetworkManager; import com.azure.management.resources.Deployments; import com.azure.management.resources.GenericResources; +import com.azure.management.resources.PolicyAssignments; +import com.azure.management.resources.PolicyDefinitions; import com.azure.management.resources.Providers; import com.azure.management.resources.ResourceGroups; import com.azure.management.resources.Subscription; @@ -83,6 +84,7 @@ import com.azure.management.resources.fluentcore.profile.AzureProfile; import com.azure.management.resources.fluentcore.utils.HttpPipelineProvider; import com.azure.management.resources.fluentcore.utils.SdkContext; +import com.azure.management.resources.fluentcore.utils.Utils; import com.azure.management.resources.implementation.ResourceManager; import com.azure.management.sql.SqlServers; import com.azure.management.sql.implementation.SqlServerManager; @@ -229,9 +231,10 @@ public interface Authenticated extends AccessManagement { * Selects the default subscription as the subscription for the APIs to work with. * *

The default subscription can be specified inside the Azure profile using {@link - * AzureProfile}. If no default subscription has been previously provided, the first subscription as - * returned by {@link Authenticated#subscriptions()} will be selected.

+ * AzureProfile}. If no default subscription provided, we will try to set the only + * subscription if applicable returned by {@link Authenticated#subscriptions()}

* + * @throws IllegalStateException when no subscription or more than one subscription found in the tenant. * @return an authenticated Azure client configured to work with the default subscription */ Azure withDefaultSubscription(); @@ -239,7 +242,6 @@ public interface Authenticated extends AccessManagement { /** The implementation for the Authenticated interface. */ private static final class AuthenticatedImpl implements Authenticated { - private final ClientLogger logger = new ClientLogger(AuthenticatedImpl.class); private final HttpPipeline httpPipeline; private final AzureProfile profile; private final ResourceManager.Authenticated resourceManagerAuthenticated; @@ -325,8 +327,7 @@ public Azure withSubscription(String subscriptionId) { @Override public Azure withDefaultSubscription() { if (profile.subscriptionId() == null) { - throw logger.logExceptionAsError( - new IllegalArgumentException("Please specify the subscription ID for resource management.")); + profile.withSubscriptionId(Utils.defaultSubscription(this.subscriptions().list())); } return new Azure(httpPipeline, profile, this); } @@ -416,19 +417,19 @@ public Providers providers() { return resourceManager.providers(); } - // /** - // * @return entry point to managing policy definitions. - // */ - // public PolicyDefinitions policyDefinitions() { - // return resourceManager.policyDefinitions(); - // } - // - // /** - // * @return entry point to managing policy assignments. - // */ - // public PolicyAssignments policyAssignments() { - // return resourceManager.policyAssignments(); - // } + /** + * @return entry point to managing policy definitions. + */ + public PolicyDefinitions policyDefinitions() { + return resourceManager.policyDefinitions(); + } + + /** + * @return entry point to managing policy assignments. + */ + public PolicyAssignments policyAssignments() { + return resourceManager.policyAssignments(); + } /** @return entry point to managing storage accounts */ public StorageAccounts storageAccounts() { diff --git a/sdk/resources/mgmt/src/main/java/com/azure/management/resources/fluentcore/utils/Utils.java b/sdk/resources/mgmt/src/main/java/com/azure/management/resources/fluentcore/utils/Utils.java index fdcbae74048f..f10641592306 100644 --- a/sdk/resources/mgmt/src/main/java/com/azure/management/resources/fluentcore/utils/Utils.java +++ b/sdk/resources/mgmt/src/main/java/com/azure/management/resources/fluentcore/utils/Utils.java @@ -7,17 +7,24 @@ import com.azure.core.annotation.PathParam; import com.azure.core.http.HttpPipeline; import com.azure.core.http.HttpResponse; +import com.azure.core.http.rest.PagedIterable; import com.azure.core.http.rest.RestProxy; +import com.azure.core.util.logging.ClientLogger; +import com.azure.management.resources.Subscription; import com.azure.management.resources.fluentcore.arm.ResourceId; import com.azure.management.resources.fluentcore.model.Indexable; import reactor.core.publisher.Mono; +import java.util.ArrayList; import java.util.List; /** * Defines a few utilities. */ public final class Utils { + private Utils() { + } + /** * Converts an object Boolean to a primitive boolean. * @@ -179,6 +186,32 @@ private interface FileService { Mono download(@PathParam("url") String url); } - private Utils() { + /** + * Gets the only subscription as the default one in the tenant if applicable. + * + * @param subscriptions the list of subscriptions + * @throws IllegalStateException when no subscription or more than one subscription found + * @return the only subscription existing in the tenant + */ + public static String defaultSubscription(PagedIterable subscriptions) { + List subscriptionList = new ArrayList<>(); + subscriptions.forEach(subscription -> { + subscriptionList.add(subscription); + }); + if (subscriptionList.size() == 0) { + throw new ClientLogger(Utils.class).logExceptionAsError( + new IllegalStateException("Please create a subscription before you start resource management. " + + "To learn more, see: https://azure.microsoft.com/en-us/free/.")); + } else if (subscriptionList.size() > 1) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("More than one subscription found in your tenant. " + + "Please specify which one below is desired for resource management."); + subscriptionList.forEach(subscription -> { + stringBuilder.append("\n" + subscription.displayName() + " : " + subscription.subscriptionId()); + }); + throw new ClientLogger(Utils.class).logExceptionAsError( + new IllegalStateException(stringBuilder.toString())); + } + return subscriptionList.get(0).subscriptionId(); } } diff --git a/sdk/resources/mgmt/src/main/java/com/azure/management/resources/implementation/ResourceManager.java b/sdk/resources/mgmt/src/main/java/com/azure/management/resources/implementation/ResourceManager.java index 5db24712e81b..33d5a7ad5a61 100644 --- a/sdk/resources/mgmt/src/main/java/com/azure/management/resources/implementation/ResourceManager.java +++ b/sdk/resources/mgmt/src/main/java/com/azure/management/resources/implementation/ResourceManager.java @@ -5,7 +5,6 @@ import com.azure.core.credential.TokenCredential; import com.azure.core.http.HttpPipeline; -import com.azure.core.util.logging.ClientLogger; import com.azure.management.resources.Deployments; import com.azure.management.resources.Features; import com.azure.management.resources.GenericResources; @@ -22,6 +21,7 @@ import com.azure.management.resources.fluentcore.profile.AzureProfile; import com.azure.management.resources.fluentcore.utils.HttpPipelineProvider; import com.azure.management.resources.fluentcore.utils.SdkContext; +import com.azure.management.resources.fluentcore.utils.Utils; import com.azure.management.resources.models.FeatureClientBuilder; import com.azure.management.resources.models.FeatureClientImpl; import com.azure.management.resources.models.PolicyClientBuilder; @@ -133,8 +133,10 @@ public interface Authenticated { ResourceManager withSubscription(String subscriptionId); /** - * Specifies to use subscription from Azure profile. + * Specifies to use subscription from {@link AzureProfile}. If no subscription provided, we will + * try to set the only subscription if applicable returned by {@link Authenticated#subscriptions()}. * + * @throws IllegalStateException when no subscription or more than one subscription found in the tenant. * @return the ResourceManager instance with entry points that work in a subscription */ ResourceManager withDefaultSubscription(); @@ -144,7 +146,6 @@ public interface Authenticated { * The implementation for Authenticated interface. */ private static final class AuthenticatedImpl implements Authenticated { - private final ClientLogger logger = new ClientLogger(AuthenticatedImpl.class); private HttpPipeline httpPipeline; private AzureProfile profile; private SdkContext sdkContext; @@ -192,8 +193,7 @@ public ResourceManager withSubscription(String subscriptionId) { @Override public ResourceManager withDefaultSubscription() { if (profile.subscriptionId() == null) { - throw logger.logExceptionAsError( - new IllegalArgumentException("Please specify the subscription ID for resource management.")); + profile.withSubscriptionId(Utils.defaultSubscription(this.subscriptions().list())); } return new ResourceManager(httpPipeline, profile, sdkContext); }