Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mgmt update default subscription logic #11302

Merged
merged 6 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions sdk/management/azure/src/main/java/com/azure/management/Azure.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -229,17 +231,17 @@ public interface Authenticated extends AccessManagement {
* Selects the default subscription as the subscription for the APIs to work with.
*
* <p>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.</p>
* AzureProfile}. If no default subscription provided, we will try to set the only
* subscription if applicable returned by {@link Authenticated#subscriptions()}</p>
*
* @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();
}

/** 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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -179,6 +186,32 @@ private interface FileService {
Mono<HttpResponse> 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<Subscription> subscriptions) {
List<Subscription> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down