|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +// Code generated by Microsoft (R) AutoRest Code Generator. |
| 4 | + |
| 5 | +package com.azure.resourcemanager.authorization.generated; |
| 6 | + |
| 7 | +import com.azure.core.credential.TokenCredential; |
| 8 | +import com.azure.core.http.HttpClient; |
| 9 | +import com.azure.core.http.HttpPipeline; |
| 10 | +import com.azure.core.http.HttpPipelineBuilder; |
| 11 | +import com.azure.core.http.policy.AddDatePolicy; |
| 12 | +import com.azure.core.http.policy.BearerTokenAuthenticationPolicy; |
| 13 | +import com.azure.core.http.policy.HttpLogOptions; |
| 14 | +import com.azure.core.http.policy.HttpLoggingPolicy; |
| 15 | +import com.azure.core.http.policy.HttpPipelinePolicy; |
| 16 | +import com.azure.core.http.policy.HttpPolicyProviders; |
| 17 | +import com.azure.core.http.policy.RequestIdPolicy; |
| 18 | +import com.azure.core.http.policy.RetryPolicy; |
| 19 | +import com.azure.core.http.policy.UserAgentPolicy; |
| 20 | +import com.azure.core.management.profile.AzureProfile; |
| 21 | +import com.azure.core.util.Configuration; |
| 22 | +import com.azure.core.util.logging.ClientLogger; |
| 23 | +import com.azure.resourcemanager.authorization.generated.fluent.AuthorizationManagementClient; |
| 24 | +import com.azure.resourcemanager.authorization.generated.implementation.AuthorizationManagementClientBuilder; |
| 25 | +import com.azure.resourcemanager.authorization.generated.implementation.RoleAssignmentsImpl; |
| 26 | +import com.azure.resourcemanager.authorization.generated.models.RoleAssignments; |
| 27 | +import java.time.Duration; |
| 28 | +import java.time.temporal.ChronoUnit; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +/** |
| 34 | + * Entry point to AuthorizationManager. Role based access control provides you a way to apply granular level policy |
| 35 | + * administration down to individual resources or resource groups. These operations enable you to manage role |
| 36 | + * assignments. A role assignment grants access to Azure Active Directory users. |
| 37 | + */ |
| 38 | +public final class AuthorizationManager { |
| 39 | + private RoleAssignments roleAssignments; |
| 40 | + |
| 41 | + private final AuthorizationManagementClient clientObject; |
| 42 | + |
| 43 | + private AuthorizationManager(HttpPipeline httpPipeline, AzureProfile profile, Duration defaultPollInterval) { |
| 44 | + Objects.requireNonNull(httpPipeline, "'httpPipeline' cannot be null."); |
| 45 | + Objects.requireNonNull(profile, "'profile' cannot be null."); |
| 46 | + this.clientObject = |
| 47 | + new AuthorizationManagementClientBuilder() |
| 48 | + .pipeline(httpPipeline) |
| 49 | + .endpoint(profile.getEnvironment().getResourceManagerEndpoint()) |
| 50 | + .subscriptionId(profile.getSubscriptionId()) |
| 51 | + .defaultPollInterval(defaultPollInterval) |
| 52 | + .buildClient(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates an instance of Authorization service API entry point. |
| 57 | + * |
| 58 | + * @param credential the credential to use. |
| 59 | + * @param profile the Azure profile for client. |
| 60 | + * @return the Authorization service API instance. |
| 61 | + */ |
| 62 | + public static AuthorizationManager authenticate(TokenCredential credential, AzureProfile profile) { |
| 63 | + Objects.requireNonNull(credential, "'credential' cannot be null."); |
| 64 | + Objects.requireNonNull(profile, "'profile' cannot be null."); |
| 65 | + return configure().authenticate(credential, profile); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Gets a Configurable instance that can be used to create AuthorizationManager with optional configuration. |
| 70 | + * |
| 71 | + * @return the Configurable instance allowing configurations. |
| 72 | + */ |
| 73 | + public static Configurable configure() { |
| 74 | + return new AuthorizationManager.Configurable(); |
| 75 | + } |
| 76 | + |
| 77 | + /** The Configurable allowing configurations to be set. */ |
| 78 | + public static final class Configurable { |
| 79 | + private final ClientLogger logger = new ClientLogger(Configurable.class); |
| 80 | + |
| 81 | + private HttpClient httpClient; |
| 82 | + private HttpLogOptions httpLogOptions; |
| 83 | + private final List<HttpPipelinePolicy> policies = new ArrayList<>(); |
| 84 | + private RetryPolicy retryPolicy; |
| 85 | + private Duration defaultPollInterval; |
| 86 | + |
| 87 | + private Configurable() { |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Sets the http client. |
| 92 | + * |
| 93 | + * @param httpClient the HTTP client. |
| 94 | + * @return the configurable object itself. |
| 95 | + */ |
| 96 | + public Configurable withHttpClient(HttpClient httpClient) { |
| 97 | + this.httpClient = Objects.requireNonNull(httpClient, "'httpClient' cannot be null."); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Sets the logging options to the HTTP pipeline. |
| 103 | + * |
| 104 | + * @param httpLogOptions the HTTP log options. |
| 105 | + * @return the configurable object itself. |
| 106 | + */ |
| 107 | + public Configurable withLogOptions(HttpLogOptions httpLogOptions) { |
| 108 | + this.httpLogOptions = Objects.requireNonNull(httpLogOptions, "'httpLogOptions' cannot be null."); |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Adds the pipeline policy to the HTTP pipeline. |
| 114 | + * |
| 115 | + * @param policy the HTTP pipeline policy. |
| 116 | + * @return the configurable object itself. |
| 117 | + */ |
| 118 | + public Configurable withPolicy(HttpPipelinePolicy policy) { |
| 119 | + this.policies.add(Objects.requireNonNull(policy, "'policy' cannot be null.")); |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Sets the retry policy to the HTTP pipeline. |
| 125 | + * |
| 126 | + * @param retryPolicy the HTTP pipeline retry policy. |
| 127 | + * @return the configurable object itself. |
| 128 | + */ |
| 129 | + public Configurable withRetryPolicy(RetryPolicy retryPolicy) { |
| 130 | + this.retryPolicy = Objects.requireNonNull(retryPolicy, "'retryPolicy' cannot be null."); |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Sets the default poll interval, used when service does not provide "Retry-After" header. |
| 136 | + * |
| 137 | + * @param defaultPollInterval the default poll interval. |
| 138 | + * @return the configurable object itself. |
| 139 | + */ |
| 140 | + public Configurable withDefaultPollInterval(Duration defaultPollInterval) { |
| 141 | + this.defaultPollInterval = Objects.requireNonNull(defaultPollInterval, "'retryPolicy' cannot be null."); |
| 142 | + if (this.defaultPollInterval.isNegative()) { |
| 143 | + throw logger.logExceptionAsError(new IllegalArgumentException("'httpPipeline' cannot be negative")); |
| 144 | + } |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Creates an instance of Authorization service API entry point. |
| 150 | + * |
| 151 | + * @param credential the credential to use. |
| 152 | + * @param profile the Azure profile for client. |
| 153 | + * @return the Authorization service API instance. |
| 154 | + */ |
| 155 | + public AuthorizationManager authenticate(TokenCredential credential, AzureProfile profile) { |
| 156 | + Objects.requireNonNull(credential, "'credential' cannot be null."); |
| 157 | + Objects.requireNonNull(profile, "'profile' cannot be null."); |
| 158 | + |
| 159 | + if (retryPolicy == null) { |
| 160 | + retryPolicy = new RetryPolicy("Retry-After", ChronoUnit.SECONDS); |
| 161 | + } |
| 162 | + List<HttpPipelinePolicy> policies = new ArrayList<>(); |
| 163 | + policies |
| 164 | + .add( |
| 165 | + new UserAgentPolicy( |
| 166 | + null, |
| 167 | + "com.azure.resourcemanager.authorization.generated", |
| 168 | + "1.0.0-beta.1", |
| 169 | + Configuration.getGlobalConfiguration())); |
| 170 | + policies.add(new RequestIdPolicy()); |
| 171 | + HttpPolicyProviders.addBeforeRetryPolicies(policies); |
| 172 | + policies.add(retryPolicy); |
| 173 | + policies.add(new AddDatePolicy()); |
| 174 | + policies |
| 175 | + .add( |
| 176 | + new BearerTokenAuthenticationPolicy( |
| 177 | + credential, profile.getEnvironment().getManagementEndpoint() + "/.default")); |
| 178 | + HttpPolicyProviders.addAfterRetryPolicies(policies); |
| 179 | + policies.add(new HttpLoggingPolicy(httpLogOptions)); |
| 180 | + HttpPipeline httpPipeline = |
| 181 | + new HttpPipelineBuilder() |
| 182 | + .httpClient(httpClient) |
| 183 | + .policies(policies.toArray(new HttpPipelinePolicy[0])) |
| 184 | + .build(); |
| 185 | + return new AuthorizationManager(httpPipeline, profile, defaultPollInterval); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + /** @return Resource collection API of RoleAssignments. */ |
| 190 | + public RoleAssignments roleAssignments() { |
| 191 | + if (this.roleAssignments == null) { |
| 192 | + this.roleAssignments = new RoleAssignmentsImpl(clientObject.getRoleAssignments(), this); |
| 193 | + } |
| 194 | + return roleAssignments; |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * @return Wrapped service client AuthorizationManagementClient providing direct access to the underlying |
| 199 | + * auto-generated API implementation, based on Azure REST API. |
| 200 | + */ |
| 201 | + public AuthorizationManagementClient serviceClient() { |
| 202 | + return this.clientObject; |
| 203 | + } |
| 204 | +} |
0 commit comments