Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified infrastructures/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ private void deployNodesOnInstance(final String instanceId, final boolean existP

private String createOrUseKeyPair(String infrastructureId, int nbInstances, AWSEC2CustomizableParameter params) {
SimpleImmutableEntry<String, String> keyPairInfo;
if (params.getVmPrivateKey().isEmpty() || params.getVmKeyPairName().isEmpty()) {
if (params.getVmKeyPairName().isEmpty()) {
// create a key pair in AWS
try {
logger.info("Creating an AWS key pair");
Expand All @@ -422,12 +422,25 @@ private String createOrUseKeyPair(String infrastructureId, int nbInstances, AWSE
logger.warn("Key pair creation in AWS failed. Trying to use persisted key pair.");
keyPairInfo = handleKeyPairCreationFailure();
}
} else {
// or use the private key provided by the user
logger.info("Using AWS key pair provided by the user");
keyPairInfo = new SimpleImmutableEntry<>(params.getVmKeyPairName(), params.getVmPrivateKey());
isUsingAutoGeneratedKeyPair = false;
} else if (params.getVmPrivateKey().isEmpty()) {
// or use the key pair provided by the user and find private key
try {
logger.info("Getting private key related to the AWS key pair");
keyPairInfo = connectorIaasController.getAwsEc2KeyPairInfo(infrastructureId,
params.getVmKeyPairName(),
getRegionFromImage());
isUsingAutoGeneratedKeyPair = false;
} catch (Exception e) {
logger.warn("Key pair information could not be retrieved from AWS. Trying to use persisted key pair.");
keyPairInfo = handleKeyPairCreationFailure();
}
}
else {
// or use the key pair and private key provided by the user
logger.info("Using AWS key pair provided by the user");
keyPairInfo = new SimpleImmutableEntry<>(params.getVmKeyPairName(), params.getVmPrivateKey());
isUsingAutoGeneratedKeyPair = false;
}
persistKeyPairInfo(keyPairInfo);

// we return the name of the key pair
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ public SimpleImmutableEntry<String, String> createAwsEc2KeyPair(String infrastru
}
}

public SimpleImmutableEntry<String, String> getAwsEc2KeyPairInfo(String infrastructureId, String keyPairName, String region) {
String response = restClient.getKeyPairs(infrastructureId, keyPairName, region);

JSONObject keyPairInfoJson = new JSONObject(response);

if (keyPairInfoJson.length() > 0) {
String keyPairPrivateKey = (String) keyPairInfoJson.get(keyPairName);
return new SimpleImmutableEntry<>(keyPairName, keyPairPrivateKey);
} else {
throw new IllegalStateException("The received key pair information is empty");
}
}

public void deleteKeyPair(String infrastructureId, String keyPairName, String region) {
restClient.deleteKeyPair(infrastructureId, keyPairName, region);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ public SimpleImmutableEntry<String, String> createAwsEc2KeyPair(String infrastru
return connectorIaasClient.createAwsEc2KeyPair(infrastructureId, instanceJson);
}

public SimpleImmutableEntry<String, String> getAwsEc2KeyPairInfo(String infrastructureId, String keyPairName, String region) {
return connectorIaasClient.getAwsEc2KeyPairInfo(infrastructureId, keyPairName, region);
}

public void deleteKeyPair(String infrastructureId, String keyPairName, String region) {
connectorIaasClient.deleteKeyPair(infrastructureId, keyPairName, region);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ public String postKeyPairs(String infrastructureId, String instanceJson) {
return checkAndGetResponse(response);
}

public String getKeyPairs(String infrastructureId, String keyPairName, String region) {
ResteasyWebTarget target = initWebTarget(connectorIaasURL + "/infrastructures/" + infrastructureId +
"/keypairs/" + keyPairName);
final MultivaluedMap<String, Object> queryParams = new MultivaluedHashMap<>();
queryParams.add("region", region);
Response response = target.queryParams(queryParams).request(MediaType.APPLICATION_JSON_TYPE).get();
return checkAndGetResponse(response);
}

public String deleteKeyPair(String infrastructureId, String keyPairName, String region) {
ResteasyWebTarget target = initWebTarget(connectorIaasURL + "/infrastructures/" + infrastructureId +
"/keypairs");
Expand Down