software.amazon.awssdk.services.iam.waiters.DefaultIamAsyncWaiter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of iam Show documentation
Show all versions of iam Show documentation
The AWS Java SDK for AWS IAM module holds the client classes that are used for communicating with AWS
Identity and Access Management Service
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package software.amazon.awssdk.services.iam.waiters;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Consumer;
import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.annotations.ThreadSafe;
import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.ApiName;
import software.amazon.awssdk.core.internal.waiters.WaiterAttribute;
import software.amazon.awssdk.core.waiters.AsyncWaiter;
import software.amazon.awssdk.core.waiters.WaiterAcceptor;
import software.amazon.awssdk.core.waiters.WaiterOverrideConfiguration;
import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.core.waiters.WaiterState;
import software.amazon.awssdk.retries.api.BackoffStrategy;
import software.amazon.awssdk.services.iam.IamAsyncClient;
import software.amazon.awssdk.services.iam.model.GetInstanceProfileRequest;
import software.amazon.awssdk.services.iam.model.GetInstanceProfileResponse;
import software.amazon.awssdk.services.iam.model.GetPolicyRequest;
import software.amazon.awssdk.services.iam.model.GetPolicyResponse;
import software.amazon.awssdk.services.iam.model.GetRoleRequest;
import software.amazon.awssdk.services.iam.model.GetRoleResponse;
import software.amazon.awssdk.services.iam.model.GetUserRequest;
import software.amazon.awssdk.services.iam.model.GetUserResponse;
import software.amazon.awssdk.services.iam.model.IamRequest;
import software.amazon.awssdk.services.iam.waiters.internal.WaitersRuntime;
import software.amazon.awssdk.utils.AttributeMap;
import software.amazon.awssdk.utils.SdkAutoCloseable;
import software.amazon.awssdk.utils.ThreadFactoryBuilder;
@Generated("software.amazon.awssdk:codegen")
@SdkInternalApi
@ThreadSafe
final class DefaultIamAsyncWaiter implements IamAsyncWaiter {
private static final WaiterAttribute CLIENT_ATTRIBUTE = new WaiterAttribute<>(SdkAutoCloseable.class);
private static final WaiterAttribute SCHEDULED_EXECUTOR_SERVICE_ATTRIBUTE = new WaiterAttribute<>(
ScheduledExecutorService.class);
private final IamAsyncClient client;
private final AttributeMap managedResources;
private final AsyncWaiter instanceProfileExistsWaiter;
private final AsyncWaiter userExistsWaiter;
private final AsyncWaiter roleExistsWaiter;
private final AsyncWaiter policyExistsWaiter;
private final ScheduledExecutorService executorService;
private DefaultIamAsyncWaiter(DefaultBuilder builder) {
AttributeMap.Builder attributeMapBuilder = AttributeMap.builder();
if (builder.client == null) {
this.client = IamAsyncClient.builder().build();
attributeMapBuilder.put(CLIENT_ATTRIBUTE, this.client);
} else {
this.client = builder.client;
}
if (builder.executorService == null) {
this.executorService = Executors.newScheduledThreadPool(1,
new ThreadFactoryBuilder().threadNamePrefix("waiters-ScheduledExecutor").build());
attributeMapBuilder.put(SCHEDULED_EXECUTOR_SERVICE_ATTRIBUTE, this.executorService);
} else {
this.executorService = builder.executorService;
}
managedResources = attributeMapBuilder.build();
this.instanceProfileExistsWaiter = AsyncWaiter.builder(GetInstanceProfileResponse.class)
.acceptors(instanceProfileExistsWaiterAcceptors())
.overrideConfiguration(instanceProfileExistsWaiterConfig(builder.overrideConfiguration))
.scheduledExecutorService(executorService).build();
this.userExistsWaiter = AsyncWaiter.builder(GetUserResponse.class).acceptors(userExistsWaiterAcceptors())
.overrideConfiguration(userExistsWaiterConfig(builder.overrideConfiguration))
.scheduledExecutorService(executorService).build();
this.roleExistsWaiter = AsyncWaiter.builder(GetRoleResponse.class).acceptors(roleExistsWaiterAcceptors())
.overrideConfiguration(roleExistsWaiterConfig(builder.overrideConfiguration))
.scheduledExecutorService(executorService).build();
this.policyExistsWaiter = AsyncWaiter.builder(GetPolicyResponse.class).acceptors(policyExistsWaiterAcceptors())
.overrideConfiguration(policyExistsWaiterConfig(builder.overrideConfiguration))
.scheduledExecutorService(executorService).build();
}
private static String errorCode(Throwable error) {
if (error instanceof AwsServiceException) {
return ((AwsServiceException) error).awsErrorDetails().errorCode();
}
return null;
}
@Override
public CompletableFuture> waitUntilInstanceProfileExists(
GetInstanceProfileRequest getInstanceProfileRequest) {
return instanceProfileExistsWaiter.runAsync(() -> client
.getInstanceProfile(applyWaitersUserAgent(getInstanceProfileRequest)));
}
@Override
public CompletableFuture> waitUntilInstanceProfileExists(
GetInstanceProfileRequest getInstanceProfileRequest, WaiterOverrideConfiguration overrideConfig) {
return instanceProfileExistsWaiter.runAsync(
() -> client.getInstanceProfile(applyWaitersUserAgent(getInstanceProfileRequest)),
instanceProfileExistsWaiterConfig(overrideConfig));
}
@Override
public CompletableFuture> waitUntilPolicyExists(GetPolicyRequest getPolicyRequest) {
return policyExistsWaiter.runAsync(() -> client.getPolicy(applyWaitersUserAgent(getPolicyRequest)));
}
@Override
public CompletableFuture> waitUntilPolicyExists(GetPolicyRequest getPolicyRequest,
WaiterOverrideConfiguration overrideConfig) {
return policyExistsWaiter.runAsync(() -> client.getPolicy(applyWaitersUserAgent(getPolicyRequest)),
policyExistsWaiterConfig(overrideConfig));
}
@Override
public CompletableFuture> waitUntilRoleExists(GetRoleRequest getRoleRequest) {
return roleExistsWaiter.runAsync(() -> client.getRole(applyWaitersUserAgent(getRoleRequest)));
}
@Override
public CompletableFuture> waitUntilRoleExists(GetRoleRequest getRoleRequest,
WaiterOverrideConfiguration overrideConfig) {
return roleExistsWaiter.runAsync(() -> client.getRole(applyWaitersUserAgent(getRoleRequest)),
roleExistsWaiterConfig(overrideConfig));
}
@Override
public CompletableFuture> waitUntilUserExists(GetUserRequest getUserRequest) {
return userExistsWaiter.runAsync(() -> client.getUser(applyWaitersUserAgent(getUserRequest)));
}
@Override
public CompletableFuture> waitUntilUserExists(GetUserRequest getUserRequest,
WaiterOverrideConfiguration overrideConfig) {
return userExistsWaiter.runAsync(() -> client.getUser(applyWaitersUserAgent(getUserRequest)),
userExistsWaiterConfig(overrideConfig));
}
private static List> instanceProfileExistsWaiterAcceptors() {
List> result = new ArrayList<>();
result.add(new WaitersRuntime.ResponseStatusAcceptor(200, WaiterState.SUCCESS));
result.add(new WaitersRuntime.ResponseStatusAcceptor(404, WaiterState.RETRY));
result.addAll(WaitersRuntime.DEFAULT_ACCEPTORS);
return result;
}
private static List> userExistsWaiterAcceptors() {
List> result = new ArrayList<>();
result.add(new WaitersRuntime.ResponseStatusAcceptor(200, WaiterState.SUCCESS));
result.add(WaiterAcceptor.retryOnExceptionAcceptor(error -> Objects.equals(errorCode(error), "NoSuchEntity")));
result.addAll(WaitersRuntime.DEFAULT_ACCEPTORS);
return result;
}
private static List> roleExistsWaiterAcceptors() {
List> result = new ArrayList<>();
result.add(new WaitersRuntime.ResponseStatusAcceptor(200, WaiterState.SUCCESS));
result.add(WaiterAcceptor.retryOnExceptionAcceptor(error -> Objects.equals(errorCode(error), "NoSuchEntity")));
result.addAll(WaitersRuntime.DEFAULT_ACCEPTORS);
return result;
}
private static List> policyExistsWaiterAcceptors() {
List> result = new ArrayList<>();
result.add(new WaitersRuntime.ResponseStatusAcceptor(200, WaiterState.SUCCESS));
result.add(WaiterAcceptor.retryOnExceptionAcceptor(error -> Objects.equals(errorCode(error), "NoSuchEntity")));
result.addAll(WaitersRuntime.DEFAULT_ACCEPTORS);
return result;
}
private static WaiterOverrideConfiguration instanceProfileExistsWaiterConfig(WaiterOverrideConfiguration overrideConfig) {
Optional optionalOverrideConfig = Optional.ofNullable(overrideConfig);
int maxAttempts = optionalOverrideConfig.flatMap(WaiterOverrideConfiguration::maxAttempts).orElse(40);
BackoffStrategy backoffStrategy = optionalOverrideConfig.flatMap(WaiterOverrideConfiguration::backoffStrategyV2).orElse(
BackoffStrategy.fixedDelayWithoutJitter(Duration.ofSeconds(1)));
Duration waitTimeout = optionalOverrideConfig.flatMap(WaiterOverrideConfiguration::waitTimeout).orElse(null);
return WaiterOverrideConfiguration.builder().maxAttempts(maxAttempts).backoffStrategyV2(backoffStrategy)
.waitTimeout(waitTimeout).build();
}
private static WaiterOverrideConfiguration userExistsWaiterConfig(WaiterOverrideConfiguration overrideConfig) {
Optional optionalOverrideConfig = Optional.ofNullable(overrideConfig);
int maxAttempts = optionalOverrideConfig.flatMap(WaiterOverrideConfiguration::maxAttempts).orElse(20);
BackoffStrategy backoffStrategy = optionalOverrideConfig.flatMap(WaiterOverrideConfiguration::backoffStrategyV2).orElse(
BackoffStrategy.fixedDelayWithoutJitter(Duration.ofSeconds(1)));
Duration waitTimeout = optionalOverrideConfig.flatMap(WaiterOverrideConfiguration::waitTimeout).orElse(null);
return WaiterOverrideConfiguration.builder().maxAttempts(maxAttempts).backoffStrategyV2(backoffStrategy)
.waitTimeout(waitTimeout).build();
}
private static WaiterOverrideConfiguration roleExistsWaiterConfig(WaiterOverrideConfiguration overrideConfig) {
Optional optionalOverrideConfig = Optional.ofNullable(overrideConfig);
int maxAttempts = optionalOverrideConfig.flatMap(WaiterOverrideConfiguration::maxAttempts).orElse(20);
BackoffStrategy backoffStrategy = optionalOverrideConfig.flatMap(WaiterOverrideConfiguration::backoffStrategyV2).orElse(
BackoffStrategy.fixedDelayWithoutJitter(Duration.ofSeconds(1)));
Duration waitTimeout = optionalOverrideConfig.flatMap(WaiterOverrideConfiguration::waitTimeout).orElse(null);
return WaiterOverrideConfiguration.builder().maxAttempts(maxAttempts).backoffStrategyV2(backoffStrategy)
.waitTimeout(waitTimeout).build();
}
private static WaiterOverrideConfiguration policyExistsWaiterConfig(WaiterOverrideConfiguration overrideConfig) {
Optional optionalOverrideConfig = Optional.ofNullable(overrideConfig);
int maxAttempts = optionalOverrideConfig.flatMap(WaiterOverrideConfiguration::maxAttempts).orElse(20);
BackoffStrategy backoffStrategy = optionalOverrideConfig.flatMap(WaiterOverrideConfiguration::backoffStrategyV2).orElse(
BackoffStrategy.fixedDelayWithoutJitter(Duration.ofSeconds(1)));
Duration waitTimeout = optionalOverrideConfig.flatMap(WaiterOverrideConfiguration::waitTimeout).orElse(null);
return WaiterOverrideConfiguration.builder().maxAttempts(maxAttempts).backoffStrategyV2(backoffStrategy)
.waitTimeout(waitTimeout).build();
}
@Override
public void close() {
managedResources.close();
}
public static IamAsyncWaiter.Builder builder() {
return new DefaultBuilder();
}
private T applyWaitersUserAgent(T request) {
Consumer userAgentApplier = b -> b.addApiName(ApiName.builder()
.version("waiter").name("hll").build());
AwsRequestOverrideConfiguration overrideConfiguration = request.overrideConfiguration()
.map(c -> c.toBuilder().applyMutation(userAgentApplier).build())
.orElse((AwsRequestOverrideConfiguration.builder().applyMutation(userAgentApplier).build()));
return (T) request.toBuilder().overrideConfiguration(overrideConfiguration).build();
}
public static final class DefaultBuilder implements IamAsyncWaiter.Builder {
private IamAsyncClient client;
private WaiterOverrideConfiguration overrideConfiguration;
private ScheduledExecutorService executorService;
private DefaultBuilder() {
}
@Override
public IamAsyncWaiter.Builder scheduledExecutorService(ScheduledExecutorService executorService) {
this.executorService = executorService;
return this;
}
@Override
public IamAsyncWaiter.Builder overrideConfiguration(WaiterOverrideConfiguration overrideConfiguration) {
this.overrideConfiguration = overrideConfiguration;
return this;
}
@Override
public IamAsyncWaiter.Builder client(IamAsyncClient client) {
this.client = client;
return this;
}
public IamAsyncWaiter build() {
return new DefaultIamAsyncWaiter(this);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy