com.amazonaws.services.ec2.model.ImportKeyPairRequest Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.ImportKeyPairRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AwsIaasGatewayScriptService.java    From primecloud-controller with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void importKeyPair(String keyName, String publicKey) throws AutoException {
    // キーペアがすでに登録されていたら何もしない
    DescribeKeyPairsRequest request = new DescribeKeyPairsRequest();
    DescribeKeyPairsResult result = ec2Client.describeKeyPairs(request);
    List<KeyPairInfo> keyPairs = result.getKeyPairs();

    for (KeyPairInfo keyPair : keyPairs) {
        if (keyPair.getKeyName().equals(keyName)) {
            log.info(platform.getPlatformName() + " の " + keyName + " はすでに登録されている為、キーのインポートをスキップします");
            System.out.println("IMPORT_SKIPPED");
            return;
        }
    }

    // インポート
    ImportKeyPairRequest request2 = new ImportKeyPairRequest();
    request2.withKeyName(keyName);
    request2.withPublicKeyMaterial(publicKey);
    ec2Client.importKeyPair(request2);

    log.info(keyName + "のキーをインポートしました。");
}
 
Example #2
Source File: AwsPublicKeyConnector.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Override
public void register(PublicKeyRegisterRequest request) {
    LOGGER.debug("Importing public key {} to {} region on AWS", request.getPublicKeyId(), request.getRegion());
    AwsCredentialView awsCredential = new AwsCredentialView(request.getCredential());
    try {
        AmazonEC2Client client = awsClient.createAccess(awsCredential, request.getRegion());
        if (!exists(client, request.getPublicKeyId())) {
            ImportKeyPairRequest importKeyPairRequest = new ImportKeyPairRequest(request.getPublicKeyId(), request.getPublicKey());
            client.importKeyPair(importKeyPairRequest);
        }
    } catch (Exception e) {
        String errorMessage = String.format("Failed to import public key [%s:'%s', region: '%s'], detailed message: %s",
                getType(awsCredential), getAwsId(awsCredential), request.getRegion(), e.getMessage());
        LOGGER.error(errorMessage, e);
        throw new CloudConnectorException(e.getMessage(), e);
    }
}
 
Example #3
Source File: EC2Impl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public KeyPair importKeyPair(ImportKeyPairRequest request,
        ResultCapture<ImportKeyPairResult> extractor) {

    ActionResult result = service.performAction("ImportKeyPair", request,
            extractor);

    if (result == null) return null;
    return new KeyPairImpl(result.getResource());
}
 
Example #4
Source File: EC2Impl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public KeyPair importKeyPair(String publicKeyMaterial, String keyName,
        ResultCapture<ImportKeyPairResult> extractor) {

    ImportKeyPairRequest request = new ImportKeyPairRequest()
        .withPublicKeyMaterial(publicKeyMaterial)
        .withKeyName(keyName);
    return importKeyPair(request, extractor);
}
 
Example #5
Source File: AwsPublicKeyConnectorTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
void registerNew() {
    PublicKeyRegisterRequest request = generateRegisterRequest();
    when(ec2client.describeKeyPairs(any())).thenThrow(new AmazonServiceException("no such key"));
    underTest.register(request);
    ArgumentCaptor<ImportKeyPairRequest> captor = ArgumentCaptor.forClass(ImportKeyPairRequest.class);
    verify(ec2client).importKeyPair(captor.capture());
    ImportKeyPairRequest result = captor.getValue();
    assertThat(result.getKeyName()).isEqualTo(PUBLIC_KEY_ID);
    assertThat(result.getPublicKeyMaterial()).isEqualTo(PUBLIC_KEY);
    verifyNoMoreInteractions(ec2client);
}
 
Example #6
Source File: EC2Impl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public KeyPair importKeyPair(ImportKeyPairRequest request) {
    return importKeyPair(request, null);
}
 
Example #7
Source File: EC2.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Performs the <code>ImportKeyPair</code> action.
 *
 * <p>
 *
 * @return The <code>KeyPair</code> resource object associated with the
 *         result of this action.
 * @see ImportKeyPairRequest
 */
com.amazonaws.resources.ec2.KeyPair importKeyPair(ImportKeyPairRequest
        request);
 
Example #8
Source File: EC2.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Performs the <code>ImportKeyPair</code> action and use a ResultCapture to
 * retrieve the low-level client response.
 *
 * <p>
 *
 * @return The <code>KeyPair</code> resource object associated with the
 *         result of this action.
 * @see ImportKeyPairRequest
 */
com.amazonaws.resources.ec2.KeyPair importKeyPair(ImportKeyPairRequest
        request, ResultCapture<ImportKeyPairResult> extractor);