
software.amazon.awssdk.services.ec2instanceconnect.DefaultEc2InstanceConnectClient Maven / Gradle / Ivy
/*
* 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.ec2instanceconnect;
import java.util.Collections;
import java.util.List;
import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.RequestOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.core.client.handler.ClientExecutionParams;
import software.amazon.awssdk.core.client.handler.SyncClientHandler;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.http.HttpResponseHandler;
import software.amazon.awssdk.core.metrics.CoreMetric;
import software.amazon.awssdk.metrics.MetricCollector;
import software.amazon.awssdk.metrics.MetricPublisher;
import software.amazon.awssdk.metrics.NoOpMetricCollector;
import software.amazon.awssdk.protocols.core.ExceptionMetadata;
import software.amazon.awssdk.protocols.json.AwsJsonProtocol;
import software.amazon.awssdk.protocols.json.AwsJsonProtocolFactory;
import software.amazon.awssdk.protocols.json.BaseAwsJsonProtocolFactory;
import software.amazon.awssdk.protocols.json.JsonOperationMetadata;
import software.amazon.awssdk.services.ec2instanceconnect.model.AuthException;
import software.amazon.awssdk.services.ec2instanceconnect.model.Ec2InstanceConnectException;
import software.amazon.awssdk.services.ec2instanceconnect.model.Ec2InstanceNotFoundException;
import software.amazon.awssdk.services.ec2instanceconnect.model.Ec2InstanceStateInvalidException;
import software.amazon.awssdk.services.ec2instanceconnect.model.Ec2InstanceTypeInvalidException;
import software.amazon.awssdk.services.ec2instanceconnect.model.InvalidArgsException;
import software.amazon.awssdk.services.ec2instanceconnect.model.SendSerialConsoleSshPublicKeyRequest;
import software.amazon.awssdk.services.ec2instanceconnect.model.SendSerialConsoleSshPublicKeyResponse;
import software.amazon.awssdk.services.ec2instanceconnect.model.SendSshPublicKeyRequest;
import software.amazon.awssdk.services.ec2instanceconnect.model.SendSshPublicKeyResponse;
import software.amazon.awssdk.services.ec2instanceconnect.model.SerialConsoleAccessDisabledException;
import software.amazon.awssdk.services.ec2instanceconnect.model.SerialConsoleSessionLimitExceededException;
import software.amazon.awssdk.services.ec2instanceconnect.model.SerialConsoleSessionUnavailableException;
import software.amazon.awssdk.services.ec2instanceconnect.model.ServiceException;
import software.amazon.awssdk.services.ec2instanceconnect.model.ThrottlingException;
import software.amazon.awssdk.services.ec2instanceconnect.transform.SendSerialConsoleSshPublicKeyRequestMarshaller;
import software.amazon.awssdk.services.ec2instanceconnect.transform.SendSshPublicKeyRequestMarshaller;
import software.amazon.awssdk.utils.Logger;
/**
* Internal implementation of {@link Ec2InstanceConnectClient}.
*
* @see Ec2InstanceConnectClient#builder()
*/
@Generated("software.amazon.awssdk:codegen")
@SdkInternalApi
final class DefaultEc2InstanceConnectClient implements Ec2InstanceConnectClient {
private static final Logger log = Logger.loggerFor(DefaultEc2InstanceConnectClient.class);
private final SyncClientHandler clientHandler;
private final AwsJsonProtocolFactory protocolFactory;
private final SdkClientConfiguration clientConfiguration;
protected DefaultEc2InstanceConnectClient(SdkClientConfiguration clientConfiguration) {
this.clientHandler = new AwsSyncClientHandler(clientConfiguration);
this.clientConfiguration = clientConfiguration;
this.protocolFactory = init(AwsJsonProtocolFactory.builder()).build();
}
@Override
public final String serviceName() {
return SERVICE_NAME;
}
/**
*
* Pushes an SSH public key to the specified EC2 instance for use by the specified user. The key remains for 60
* seconds. For more information, see Connect to
* your Linux instance using EC2 Instance Connect in the Amazon EC2 User Guide.
*
*
* @param sendSshPublicKeyRequest
* @return Result of the SendSSHPublicKey operation returned by the service.
* @throws AuthException
* Either your AWS credentials are not valid or you do not have access to the EC2 instance.
* @throws InvalidArgsException
* One of the parameters is not valid.
* @throws ServiceException
* The service encountered an error. Follow the instructions in the error message and try again.
* @throws ThrottlingException
* The requests were made too frequently and have been throttled. Wait a while and try again. To increase
* the limit on your request frequency, contact AWS Support.
* @throws Ec2InstanceNotFoundException
* The specified instance was not found.
* @throws Ec2InstanceStateInvalidException
* Unable to connect because the instance is not in a valid state. Connecting to a stopped or terminated
* instance is not supported. If the instance is stopped, start your instance, and try to connect again.
* @throws SdkException
* Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
* catch all scenarios.
* @throws SdkClientException
* If any client side error occurs such as an IO related failure, failure to get credentials, etc.
* @throws Ec2InstanceConnectException
* Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
* @sample Ec2InstanceConnectClient.SendSSHPublicKey
* @see AWS API Documentation
*/
@Override
public SendSshPublicKeyResponse sendSSHPublicKey(SendSshPublicKeyRequest sendSshPublicKeyRequest) throws AuthException,
InvalidArgsException, ServiceException, ThrottlingException, Ec2InstanceNotFoundException,
Ec2InstanceStateInvalidException, AwsServiceException, SdkClientException, Ec2InstanceConnectException {
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
.isPayloadJson(true).build();
HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata,
SendSshPublicKeyResponse::builder);
HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory,
operationMetadata);
List metricPublishers = resolveMetricPublishers(clientConfiguration, sendSshPublicKeyRequest
.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector
.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "EC2 Instance Connect");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "SendSSHPublicKey");
return clientHandler.execute(new ClientExecutionParams()
.withOperationName("SendSSHPublicKey").withResponseHandler(responseHandler)
.withErrorResponseHandler(errorResponseHandler).withInput(sendSshPublicKeyRequest)
.withMetricCollector(apiCallMetricCollector)
.withMarshaller(new SendSshPublicKeyRequestMarshaller(protocolFactory)));
} finally {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
}
}
/**
*
* Pushes an SSH public key to the specified EC2 instance. The key remains for 60 seconds, which gives you 60
* seconds to establish a serial console connection to the instance using SSH. For more information, see EC2 Serial Console in the
* Amazon EC2 User Guide.
*
*
* @param sendSerialConsoleSshPublicKeyRequest
* @return Result of the SendSerialConsoleSSHPublicKey operation returned by the service.
* @throws AuthException
* Either your AWS credentials are not valid or you do not have access to the EC2 instance.
* @throws SerialConsoleAccessDisabledException
* Your account is not authorized to use the EC2 Serial Console. To authorize your account, run the
* EnableSerialConsoleAccess API. For more information, see EnableSerialConsoleAccess in the Amazon EC2 API Reference.
* @throws InvalidArgsException
* One of the parameters is not valid.
* @throws ServiceException
* The service encountered an error. Follow the instructions in the error message and try again.
* @throws ThrottlingException
* The requests were made too frequently and have been throttled. Wait a while and try again. To increase
* the limit on your request frequency, contact AWS Support.
* @throws Ec2InstanceNotFoundException
* The specified instance was not found.
* @throws Ec2InstanceTypeInvalidException
* The instance type is not supported for connecting via the serial console. Only Nitro instance types are
* currently supported.
* @throws SerialConsoleSessionLimitExceededException
* The instance currently has 1 active serial console session. Only 1 session is supported at a time.
* @throws SerialConsoleSessionUnavailableException
* Unable to start a serial console session. Please try again.
* @throws Ec2InstanceStateInvalidException
* Unable to connect because the instance is not in a valid state. Connecting to a stopped or terminated
* instance is not supported. If the instance is stopped, start your instance, and try to connect again.
* @throws SdkException
* Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
* catch all scenarios.
* @throws SdkClientException
* If any client side error occurs such as an IO related failure, failure to get credentials, etc.
* @throws Ec2InstanceConnectException
* Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
* @sample Ec2InstanceConnectClient.SendSerialConsoleSSHPublicKey
* @see AWS API Documentation
*/
@Override
public SendSerialConsoleSshPublicKeyResponse sendSerialConsoleSSHPublicKey(
SendSerialConsoleSshPublicKeyRequest sendSerialConsoleSshPublicKeyRequest) throws AuthException,
SerialConsoleAccessDisabledException, InvalidArgsException, ServiceException, ThrottlingException,
Ec2InstanceNotFoundException, Ec2InstanceTypeInvalidException, SerialConsoleSessionLimitExceededException,
SerialConsoleSessionUnavailableException, Ec2InstanceStateInvalidException, AwsServiceException, SdkClientException,
Ec2InstanceConnectException {
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
.isPayloadJson(true).build();
HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(
operationMetadata, SendSerialConsoleSshPublicKeyResponse::builder);
HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory,
operationMetadata);
List metricPublishers = resolveMetricPublishers(clientConfiguration,
sendSerialConsoleSshPublicKeyRequest.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector
.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "EC2 Instance Connect");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "SendSerialConsoleSSHPublicKey");
return clientHandler
.execute(new ClientExecutionParams()
.withOperationName("SendSerialConsoleSSHPublicKey").withResponseHandler(responseHandler)
.withErrorResponseHandler(errorResponseHandler).withInput(sendSerialConsoleSshPublicKeyRequest)
.withMetricCollector(apiCallMetricCollector)
.withMarshaller(new SendSerialConsoleSshPublicKeyRequestMarshaller(protocolFactory)));
} finally {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
}
}
private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration,
RequestOverrideConfiguration requestOverrideConfiguration) {
List publishers = null;
if (requestOverrideConfiguration != null) {
publishers = requestOverrideConfiguration.metricPublishers();
}
if (publishers == null || publishers.isEmpty()) {
publishers = clientConfiguration.option(SdkClientOption.METRIC_PUBLISHERS);
}
if (publishers == null) {
publishers = Collections.emptyList();
}
return publishers;
}
private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory,
JsonOperationMetadata operationMetadata) {
return protocolFactory.createErrorResponseHandler(operationMetadata);
}
private > T init(T builder) {
return builder
.clientConfiguration(clientConfiguration)
.defaultServiceExceptionSupplier(Ec2InstanceConnectException::builder)
.protocol(AwsJsonProtocol.AWS_JSON)
.protocolVersion("1.1")
.registerModeledException(
ExceptionMetadata.builder().errorCode("AuthException").exceptionBuilderSupplier(AuthException::builder)
.build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("SerialConsoleSessionLimitExceededException")
.exceptionBuilderSupplier(SerialConsoleSessionLimitExceededException::builder).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("SerialConsoleAccessDisabledException")
.exceptionBuilderSupplier(SerialConsoleAccessDisabledException::builder).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("SerialConsoleSessionUnavailableException")
.exceptionBuilderSupplier(SerialConsoleSessionUnavailableException::builder).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("EC2InstanceTypeInvalidException")
.exceptionBuilderSupplier(Ec2InstanceTypeInvalidException::builder).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("EC2InstanceStateInvalidException")
.exceptionBuilderSupplier(Ec2InstanceStateInvalidException::builder).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("ServiceException")
.exceptionBuilderSupplier(ServiceException::builder).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("EC2InstanceNotFoundException")
.exceptionBuilderSupplier(Ec2InstanceNotFoundException::builder).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("ThrottlingException")
.exceptionBuilderSupplier(ThrottlingException::builder).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("InvalidArgsException")
.exceptionBuilderSupplier(InvalidArgsException::builder).build());
}
@Override
public void close() {
clientHandler.close();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy