|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +// Code generated by Microsoft (R) TypeSpec Code Generator. |
| 4 | + |
| 5 | +package com.azure.resourcemanager.chaos; |
| 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.HttpPipelinePosition; |
| 12 | +import com.azure.core.http.policy.AddDatePolicy; |
| 13 | +import com.azure.core.http.policy.AddHeadersFromContextPolicy; |
| 14 | +import com.azure.core.http.policy.BearerTokenAuthenticationPolicy; |
| 15 | +import com.azure.core.http.policy.HttpLogOptions; |
| 16 | +import com.azure.core.http.policy.HttpLoggingPolicy; |
| 17 | +import com.azure.core.http.policy.HttpPipelinePolicy; |
| 18 | +import com.azure.core.http.policy.HttpPolicyProviders; |
| 19 | +import com.azure.core.http.policy.RequestIdPolicy; |
| 20 | +import com.azure.core.http.policy.RetryOptions; |
| 21 | +import com.azure.core.http.policy.RetryPolicy; |
| 22 | +import com.azure.core.http.policy.UserAgentPolicy; |
| 23 | +import com.azure.core.management.profile.AzureProfile; |
| 24 | +import com.azure.core.util.Configuration; |
| 25 | +import com.azure.core.util.CoreUtils; |
| 26 | +import com.azure.core.util.logging.ClientLogger; |
| 27 | +import com.azure.resourcemanager.chaos.fluent.ChaosManagementClient; |
| 28 | +import com.azure.resourcemanager.chaos.implementation.CapabilitiesImpl; |
| 29 | +import com.azure.resourcemanager.chaos.implementation.CapabilityTypesImpl; |
| 30 | +import com.azure.resourcemanager.chaos.implementation.ChaosManagementClientBuilder; |
| 31 | +import com.azure.resourcemanager.chaos.implementation.ExperimentsImpl; |
| 32 | +import com.azure.resourcemanager.chaos.implementation.OperationStatusesImpl; |
| 33 | +import com.azure.resourcemanager.chaos.implementation.OperationsImpl; |
| 34 | +import com.azure.resourcemanager.chaos.implementation.TargetTypesImpl; |
| 35 | +import com.azure.resourcemanager.chaos.implementation.TargetsImpl; |
| 36 | +import com.azure.resourcemanager.chaos.models.Capabilities; |
| 37 | +import com.azure.resourcemanager.chaos.models.CapabilityTypes; |
| 38 | +import com.azure.resourcemanager.chaos.models.Experiments; |
| 39 | +import com.azure.resourcemanager.chaos.models.OperationStatuses; |
| 40 | +import com.azure.resourcemanager.chaos.models.Operations; |
| 41 | +import com.azure.resourcemanager.chaos.models.TargetTypes; |
| 42 | +import com.azure.resourcemanager.chaos.models.Targets; |
| 43 | +import java.time.Duration; |
| 44 | +import java.time.temporal.ChronoUnit; |
| 45 | +import java.util.ArrayList; |
| 46 | +import java.util.List; |
| 47 | +import java.util.Map; |
| 48 | +import java.util.Objects; |
| 49 | +import java.util.stream.Collectors; |
| 50 | + |
| 51 | +/** |
| 52 | + * Entry point to ChaosManager. |
| 53 | + * Chaos Management Client. |
| 54 | + */ |
| 55 | +public final class ChaosManager { |
| 56 | + private Capabilities capabilities; |
| 57 | + |
| 58 | + private Operations operations; |
| 59 | + |
| 60 | + private Targets targets; |
| 61 | + |
| 62 | + private CapabilityTypes capabilityTypes; |
| 63 | + |
| 64 | + private Experiments experiments; |
| 65 | + |
| 66 | + private TargetTypes targetTypes; |
| 67 | + |
| 68 | + private OperationStatuses operationStatuses; |
| 69 | + |
| 70 | + private final ChaosManagementClient clientObject; |
| 71 | + |
| 72 | + private ChaosManager(HttpPipeline httpPipeline, AzureProfile profile, Duration defaultPollInterval) { |
| 73 | + Objects.requireNonNull(httpPipeline, "'httpPipeline' cannot be null."); |
| 74 | + Objects.requireNonNull(profile, "'profile' cannot be null."); |
| 75 | + this.clientObject = new ChaosManagementClientBuilder().pipeline(httpPipeline) |
| 76 | + .endpoint(profile.getEnvironment().getResourceManagerEndpoint()) |
| 77 | + .subscriptionId(profile.getSubscriptionId()) |
| 78 | + .defaultPollInterval(defaultPollInterval) |
| 79 | + .buildClient(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Creates an instance of Chaos service API entry point. |
| 84 | + * |
| 85 | + * @param credential the credential to use. |
| 86 | + * @param profile the Azure profile for client. |
| 87 | + * @return the Chaos service API instance. |
| 88 | + */ |
| 89 | + public static ChaosManager authenticate(TokenCredential credential, AzureProfile profile) { |
| 90 | + Objects.requireNonNull(credential, "'credential' cannot be null."); |
| 91 | + Objects.requireNonNull(profile, "'profile' cannot be null."); |
| 92 | + return configure().authenticate(credential, profile); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Creates an instance of Chaos service API entry point. |
| 97 | + * |
| 98 | + * @param httpPipeline the {@link HttpPipeline} configured with Azure authentication credential. |
| 99 | + * @param profile the Azure profile for client. |
| 100 | + * @return the Chaos service API instance. |
| 101 | + */ |
| 102 | + public static ChaosManager authenticate(HttpPipeline httpPipeline, AzureProfile profile) { |
| 103 | + Objects.requireNonNull(httpPipeline, "'httpPipeline' cannot be null."); |
| 104 | + Objects.requireNonNull(profile, "'profile' cannot be null."); |
| 105 | + return new ChaosManager(httpPipeline, profile, null); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Gets a Configurable instance that can be used to create ChaosManager with optional configuration. |
| 110 | + * |
| 111 | + * @return the Configurable instance allowing configurations. |
| 112 | + */ |
| 113 | + public static Configurable configure() { |
| 114 | + return new ChaosManager.Configurable(); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * The Configurable allowing configurations to be set. |
| 119 | + */ |
| 120 | + public static final class Configurable { |
| 121 | + private static final ClientLogger LOGGER = new ClientLogger(Configurable.class); |
| 122 | + private static final String SDK_VERSION = "version"; |
| 123 | + private static final Map<String, String> PROPERTIES |
| 124 | + = CoreUtils.getProperties("azure-resourcemanager-chaos.properties"); |
| 125 | + |
| 126 | + private HttpClient httpClient; |
| 127 | + private HttpLogOptions httpLogOptions; |
| 128 | + private final List<HttpPipelinePolicy> policies = new ArrayList<>(); |
| 129 | + private final List<String> scopes = new ArrayList<>(); |
| 130 | + private RetryPolicy retryPolicy; |
| 131 | + private RetryOptions retryOptions; |
| 132 | + private Duration defaultPollInterval; |
| 133 | + |
| 134 | + private Configurable() { |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Sets the http client. |
| 139 | + * |
| 140 | + * @param httpClient the HTTP client. |
| 141 | + * @return the configurable object itself. |
| 142 | + */ |
| 143 | + public Configurable withHttpClient(HttpClient httpClient) { |
| 144 | + this.httpClient = Objects.requireNonNull(httpClient, "'httpClient' cannot be null."); |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Sets the logging options to the HTTP pipeline. |
| 150 | + * |
| 151 | + * @param httpLogOptions the HTTP log options. |
| 152 | + * @return the configurable object itself. |
| 153 | + */ |
| 154 | + public Configurable withLogOptions(HttpLogOptions httpLogOptions) { |
| 155 | + this.httpLogOptions = Objects.requireNonNull(httpLogOptions, "'httpLogOptions' cannot be null."); |
| 156 | + return this; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Adds the pipeline policy to the HTTP pipeline. |
| 161 | + * |
| 162 | + * @param policy the HTTP pipeline policy. |
| 163 | + * @return the configurable object itself. |
| 164 | + */ |
| 165 | + public Configurable withPolicy(HttpPipelinePolicy policy) { |
| 166 | + this.policies.add(Objects.requireNonNull(policy, "'policy' cannot be null.")); |
| 167 | + return this; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Adds the scope to permission sets. |
| 172 | + * |
| 173 | + * @param scope the scope. |
| 174 | + * @return the configurable object itself. |
| 175 | + */ |
| 176 | + public Configurable withScope(String scope) { |
| 177 | + this.scopes.add(Objects.requireNonNull(scope, "'scope' cannot be null.")); |
| 178 | + return this; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Sets the retry policy to the HTTP pipeline. |
| 183 | + * |
| 184 | + * @param retryPolicy the HTTP pipeline retry policy. |
| 185 | + * @return the configurable object itself. |
| 186 | + */ |
| 187 | + public Configurable withRetryPolicy(RetryPolicy retryPolicy) { |
| 188 | + this.retryPolicy = Objects.requireNonNull(retryPolicy, "'retryPolicy' cannot be null."); |
| 189 | + return this; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Sets the retry options for the HTTP pipeline retry policy. |
| 194 | + * <p> |
| 195 | + * This setting has no effect, if retry policy is set via {@link #withRetryPolicy(RetryPolicy)}. |
| 196 | + * |
| 197 | + * @param retryOptions the retry options for the HTTP pipeline retry policy. |
| 198 | + * @return the configurable object itself. |
| 199 | + */ |
| 200 | + public Configurable withRetryOptions(RetryOptions retryOptions) { |
| 201 | + this.retryOptions = Objects.requireNonNull(retryOptions, "'retryOptions' cannot be null."); |
| 202 | + return this; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Sets the default poll interval, used when service does not provide "Retry-After" header. |
| 207 | + * |
| 208 | + * @param defaultPollInterval the default poll interval. |
| 209 | + * @return the configurable object itself. |
| 210 | + */ |
| 211 | + public Configurable withDefaultPollInterval(Duration defaultPollInterval) { |
| 212 | + this.defaultPollInterval |
| 213 | + = Objects.requireNonNull(defaultPollInterval, "'defaultPollInterval' cannot be null."); |
| 214 | + if (this.defaultPollInterval.isNegative()) { |
| 215 | + throw LOGGER |
| 216 | + .logExceptionAsError(new IllegalArgumentException("'defaultPollInterval' cannot be negative")); |
| 217 | + } |
| 218 | + return this; |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Creates an instance of Chaos service API entry point. |
| 223 | + * |
| 224 | + * @param credential the credential to use. |
| 225 | + * @param profile the Azure profile for client. |
| 226 | + * @return the Chaos service API instance. |
| 227 | + */ |
| 228 | + public ChaosManager authenticate(TokenCredential credential, AzureProfile profile) { |
| 229 | + Objects.requireNonNull(credential, "'credential' cannot be null."); |
| 230 | + Objects.requireNonNull(profile, "'profile' cannot be null."); |
| 231 | + |
| 232 | + String clientVersion = PROPERTIES.getOrDefault(SDK_VERSION, "UnknownVersion"); |
| 233 | + |
| 234 | + StringBuilder userAgentBuilder = new StringBuilder(); |
| 235 | + userAgentBuilder.append("azsdk-java") |
| 236 | + .append("-") |
| 237 | + .append("com.azure.resourcemanager.chaos") |
| 238 | + .append("/") |
| 239 | + .append(clientVersion); |
| 240 | + if (!Configuration.getGlobalConfiguration().get("AZURE_TELEMETRY_DISABLED", false)) { |
| 241 | + userAgentBuilder.append(" (") |
| 242 | + .append(Configuration.getGlobalConfiguration().get("java.version")) |
| 243 | + .append("; ") |
| 244 | + .append(Configuration.getGlobalConfiguration().get("os.name")) |
| 245 | + .append("; ") |
| 246 | + .append(Configuration.getGlobalConfiguration().get("os.version")) |
| 247 | + .append("; auto-generated)"); |
| 248 | + } else { |
| 249 | + userAgentBuilder.append(" (auto-generated)"); |
| 250 | + } |
| 251 | + |
| 252 | + if (scopes.isEmpty()) { |
| 253 | + scopes.add(profile.getEnvironment().getManagementEndpoint() + "/.default"); |
| 254 | + } |
| 255 | + if (retryPolicy == null) { |
| 256 | + if (retryOptions != null) { |
| 257 | + retryPolicy = new RetryPolicy(retryOptions); |
| 258 | + } else { |
| 259 | + retryPolicy = new RetryPolicy("Retry-After", ChronoUnit.SECONDS); |
| 260 | + } |
| 261 | + } |
| 262 | + List<HttpPipelinePolicy> policies = new ArrayList<>(); |
| 263 | + policies.add(new UserAgentPolicy(userAgentBuilder.toString())); |
| 264 | + policies.add(new AddHeadersFromContextPolicy()); |
| 265 | + policies.add(new RequestIdPolicy()); |
| 266 | + policies.addAll(this.policies.stream() |
| 267 | + .filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_CALL) |
| 268 | + .collect(Collectors.toList())); |
| 269 | + HttpPolicyProviders.addBeforeRetryPolicies(policies); |
| 270 | + policies.add(retryPolicy); |
| 271 | + policies.add(new AddDatePolicy()); |
| 272 | + policies.add(new BearerTokenAuthenticationPolicy(credential, scopes.toArray(new String[0]))); |
| 273 | + policies.addAll(this.policies.stream() |
| 274 | + .filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_RETRY) |
| 275 | + .collect(Collectors.toList())); |
| 276 | + HttpPolicyProviders.addAfterRetryPolicies(policies); |
| 277 | + policies.add(new HttpLoggingPolicy(httpLogOptions)); |
| 278 | + HttpPipeline httpPipeline = new HttpPipelineBuilder().httpClient(httpClient) |
| 279 | + .policies(policies.toArray(new HttpPipelinePolicy[0])) |
| 280 | + .build(); |
| 281 | + return new ChaosManager(httpPipeline, profile, defaultPollInterval); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + /** |
| 286 | + * Gets the resource collection API of Capabilities. |
| 287 | + * |
| 288 | + * @return Resource collection API of Capabilities. |
| 289 | + */ |
| 290 | + public Capabilities capabilities() { |
| 291 | + if (this.capabilities == null) { |
| 292 | + this.capabilities = new CapabilitiesImpl(clientObject.getCapabilities(), this); |
| 293 | + } |
| 294 | + return capabilities; |
| 295 | + } |
| 296 | + |
| 297 | + /** |
| 298 | + * Gets the resource collection API of Operations. |
| 299 | + * |
| 300 | + * @return Resource collection API of Operations. |
| 301 | + */ |
| 302 | + public Operations operations() { |
| 303 | + if (this.operations == null) { |
| 304 | + this.operations = new OperationsImpl(clientObject.getOperations(), this); |
| 305 | + } |
| 306 | + return operations; |
| 307 | + } |
| 308 | + |
| 309 | + /** |
| 310 | + * Gets the resource collection API of Targets. |
| 311 | + * |
| 312 | + * @return Resource collection API of Targets. |
| 313 | + */ |
| 314 | + public Targets targets() { |
| 315 | + if (this.targets == null) { |
| 316 | + this.targets = new TargetsImpl(clientObject.getTargets(), this); |
| 317 | + } |
| 318 | + return targets; |
| 319 | + } |
| 320 | + |
| 321 | + /** |
| 322 | + * Gets the resource collection API of CapabilityTypes. |
| 323 | + * |
| 324 | + * @return Resource collection API of CapabilityTypes. |
| 325 | + */ |
| 326 | + public CapabilityTypes capabilityTypes() { |
| 327 | + if (this.capabilityTypes == null) { |
| 328 | + this.capabilityTypes = new CapabilityTypesImpl(clientObject.getCapabilityTypes(), this); |
| 329 | + } |
| 330 | + return capabilityTypes; |
| 331 | + } |
| 332 | + |
| 333 | + /** |
| 334 | + * Gets the resource collection API of Experiments. It manages Experiment. |
| 335 | + * |
| 336 | + * @return Resource collection API of Experiments. |
| 337 | + */ |
| 338 | + public Experiments experiments() { |
| 339 | + if (this.experiments == null) { |
| 340 | + this.experiments = new ExperimentsImpl(clientObject.getExperiments(), this); |
| 341 | + } |
| 342 | + return experiments; |
| 343 | + } |
| 344 | + |
| 345 | + /** |
| 346 | + * Gets the resource collection API of TargetTypes. |
| 347 | + * |
| 348 | + * @return Resource collection API of TargetTypes. |
| 349 | + */ |
| 350 | + public TargetTypes targetTypes() { |
| 351 | + if (this.targetTypes == null) { |
| 352 | + this.targetTypes = new TargetTypesImpl(clientObject.getTargetTypes(), this); |
| 353 | + } |
| 354 | + return targetTypes; |
| 355 | + } |
| 356 | + |
| 357 | + /** |
| 358 | + * Gets the resource collection API of OperationStatuses. |
| 359 | + * |
| 360 | + * @return Resource collection API of OperationStatuses. |
| 361 | + */ |
| 362 | + public OperationStatuses operationStatuses() { |
| 363 | + if (this.operationStatuses == null) { |
| 364 | + this.operationStatuses = new OperationStatusesImpl(clientObject.getOperationStatuses(), this); |
| 365 | + } |
| 366 | + return operationStatuses; |
| 367 | + } |
| 368 | + |
| 369 | + /** |
| 370 | + * Gets wrapped service client ChaosManagementClient providing direct access to the underlying auto-generated API |
| 371 | + * implementation, based on Azure REST API. |
| 372 | + * |
| 373 | + * @return Wrapped service client ChaosManagementClient. |
| 374 | + */ |
| 375 | + public ChaosManagementClient serviceClient() { |
| 376 | + return this.clientObject; |
| 377 | + } |
| 378 | +} |
0 commit comments