Skip to content

Commit

Permalink
expose subscriptions and tenants (Azure#14174)
Browse files Browse the repository at this point in the history
* expose subscriptions and tenants

* refresh authorization manager when parameter update
  • Loading branch information
ChenTanyi authored Aug 17, 2020
1 parent fb7b067 commit 3483c20
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class ResourceManager extends ManagerBase implements HasInner<Resou
// The sdk clients
private final ResourceManagementClient resourceManagementClient;
private final FeatureClient featureClient;
// private final SubscriptionClientImpl subscriptionClientClient;
private final SubscriptionClient subscriptionClient;
private final PolicyClient policyClient;
// The collections
private ResourceGroups resourceGroups;
Expand All @@ -51,6 +51,8 @@ public final class ResourceManager extends ManagerBase implements HasInner<Resou
private Providers providers;
private PolicyDefinitions policyDefinitions;
private PolicyAssignments policyAssignments;
private Subscriptions subscriptions;
private Tenants tenants;

/**
* Creates an instance of ResourceManager that exposes resource management API entry points.
Expand Down Expand Up @@ -220,11 +222,10 @@ private ResourceManager(HttpPipeline httpPipeline, AzureProfile profile, SdkCont
.subscriptionId(profile.subscriptionId())
.buildClient();

// Unread in spot bugs
// this.subscriptionClientClient = new SubscriptionClientBuilder()
// .pipeline(restClient.getHttpPipeline())
// .host(restClient.getBaseUrl().toString())
// .buildClient();
this.subscriptionClient = new SubscriptionClientBuilder()
.pipeline(httpPipeline)
.endpoint(profile.environment().getResourceManagerEndpoint())
.buildClient();

this.policyClient = new PolicyClientBuilder()
.pipeline(httpPipeline)
Expand Down Expand Up @@ -303,6 +304,26 @@ public PolicyAssignments policyAssignments() {
return policyAssignments;
}

/**
* @return the subscription management API entry point
*/
public Subscriptions subscriptions() {
if (subscriptions == null) {
subscriptions = new SubscriptionsImpl(subscriptionClient.getSubscriptions());
}
return subscriptions;
}

/**
* @return the tenant management API entry point
*/
public Tenants tenants() {
if (tenants == null) {
tenants = new TenantsImpl(subscriptionClient.getTenants());
}
return tenants;
}

@Override
public ResourceManagementClient inner() {
return this.resourceManagementClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import com.azure.resourcemanager.resources.fluentcore.utils.SdkContext;
import com.azure.resourcemanager.resources.fluentcore.utils.Utils;
import com.azure.resourcemanager.resources.models.Deployments;
import com.azure.resourcemanager.resources.models.Features;
import com.azure.resourcemanager.resources.models.GenericResources;
import com.azure.resourcemanager.resources.models.PolicyAssignments;
import com.azure.resourcemanager.resources.models.PolicyDefinitions;
Expand Down Expand Up @@ -134,8 +135,9 @@ public final class Azure {
// private final EventHubManager eventHubManager;
private final AppPlatformManager appPlatformManager;
private final PrivateDnsZoneManager privateDnsZoneManager;
private final String subscriptionId;
private final Authenticated authenticated;
private final String subscriptionId;
private final String tenantId;
private final SdkContext sdkContext;

/**
Expand Down Expand Up @@ -258,7 +260,7 @@ public interface Authenticated extends AccessManagement {
private static final class AuthenticatedImpl implements Authenticated {
private final HttpPipeline httpPipeline;
private final ResourceManager.Authenticated resourceManagerAuthenticated;
private final AuthorizationManager authorizationManager;
private AuthorizationManager authorizationManager;
private SdkContext sdkContext;
private String tenantId;
private String subscriptionId;
Expand Down Expand Up @@ -322,6 +324,8 @@ public RoleAssignments roleAssignments() {
@Override
public Authenticated withSdkContext(SdkContext sdkContext) {
this.sdkContext = sdkContext;
this.authorizationManager = AuthorizationManager.authenticate(
httpPipeline, new AzureProfile(tenantId, subscriptionId, environment), sdkContext);
return this;
}

Expand All @@ -334,6 +338,8 @@ public SdkContext sdkContext() {
public Authenticated withTenantId(String tenantId) {
Objects.requireNonNull(tenantId);
this.tenantId = tenantId;
this.authorizationManager = AuthorizationManager.authenticate(
httpPipeline, new AzureProfile(tenantId, subscriptionId, environment), sdkContext);
return this;
}

Expand Down Expand Up @@ -378,8 +384,9 @@ private Azure(HttpPipeline httpPipeline, AzureProfile profile, Authenticated aut
// this.eventHubManager = EventHubManager.authenticate(restClient, subscriptionId, sdkContext);
this.appPlatformManager = AppPlatformManager.authenticate(httpPipeline, profile, sdkContext);
this.privateDnsZoneManager = PrivateDnsZoneManager.authenticate(httpPipeline, profile, sdkContext);
this.subscriptionId = profile.subscriptionId();
this.authenticated = authenticated;
this.subscriptionId = profile.subscriptionId();
this.tenantId = profile.tenantId();
}

/** @return the currently selected subscription ID this client is authenticated to work with */
Expand All @@ -392,14 +399,24 @@ public String subscriptionId() {
return this.subscriptionId;
}

/** @return the currently selected tenant ID this client is authenticated to work with */
public String tenantId() {
return this.tenantId;
}

/** @return the currently selected subscription this client is authenticated to work with */
public Subscription getCurrentSubscription() {
return this.subscriptions().getById(this.subscriptionId());
}

/** @return subscriptions that this authenticated client has access to */
/** @return entry point to managing subscriptions */
public Subscriptions subscriptions() {
return this.authenticated.subscriptions();
return this.resourceManager.subscriptions();
}

/** @return entry point to managing tenants */
public Tenants tenants() {
return this.resourceManager.tenants();
}

/** @return entry point to managing resource groups */
Expand All @@ -423,13 +440,11 @@ public GenericResources genericResources() {
// public ManagementLocks managementLocks() {
// return this.authorizationManager.managementLocks();
// }
//
// /**
// * @return entry point to managing features
// */
// public Features features() {
// return resourceManager.features();
// }

/** @return entry point to managing features */
public Features features() {
return resourceManager.features();
}

/** @return entry point to managing resource providers */
public Providers providers() {
Expand Down

0 comments on commit 3483c20

Please sign in to comment.