use of org.openstack.client.common.OpenstackComputeClient in project platformlayer by platformlayer.
the class OpenstackCloudContext method findServerById.
// private AmazonEC2Client buildEc2Client() throws OpsException {
// OpsContext opsContext = OpsContext.get();
//
// AmazonEC2Client amazonEC2Client = opsContext.getUserInfo().buildEc2Client();
//
// return amazonEC2Client;
// }
//
// // // sshKeyPair = cloud.generateSshKeyPair(sshKeyName);
// // public KeyPair generateSshKeyPair(String sshKeyName) throws OpsException {
// // KeyPair sshKeyPair;
// //
// // try {
// // // OpenStack EC2 api does not support import
// // // // We create a new keypair each time... we don't want the user
// // // logging in
// // // KeyPair sshKeyPair = generateKeyPair("RSA", null);
// //
// // // ImportKeyPairRequest importKeyPairRequest = new
// // // ImportKeyPairRequest();
// // // importKeyPairRequest.setPublicKeyMaterial(publicKeyMaterial);
// // // ImportKeyPairResult importKeyPairResult =
// // // computeClient.importKeyPair(importKeyPairRequest);
// // // sshKeyName = importKeyPairResult.getKeyName();
// // AmazonEC2Client ec2Api = buildEc2Client();
// //
// // CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest(sshKeyName);
// // CreateKeyPairResult createKeyPairResult = ec2Api.createKeyPair(createKeyPairRequest);
// //
// // sshKeyPair = extractKeyPair(createKeyPairResult);
// //
// // return sshKeyPair;
// // } catch (AmazonClientException e) {
// // throw new OpsException("Error building server", e);
// // }
// // }
//
public Server findServerById(OpenstackCloud cloud, String serverId) throws OpsException {
OpenstackComputeClient computeClient = getComputeClient(cloud);
try {
log.info("Getting server info for: " + serverId);
Server server = computeClient.root().servers().server(serverId).show();
return server;
} catch (OpenstackNotFoundException e) {
return null;
} catch (OpenstackException e) {
throw new OpsException("Error getting server", e);
}
}
use of org.openstack.client.common.OpenstackComputeClient in project platformlayer by platformlayer.
the class OpenstackCloudContext method getComputeClient.
// OpenstackComputeClient openstackComputeClient;
// public void validate() throws OpsException {
// OpenstackComputeClient openstackComputeClient = buildOpenstackClient();
// // Dummy call to validate information
// try {
// openstackComputeClient.listLimits();
// } catch (OpenstackException e) {
// throw new OpsException("Unable to connect to OpenStack Compute API", e);
// }
//
// OpsContext opsContext = OpsContext.get();
// OpenstackImageClient openstackImageClient = opsContext.getUserInfo().getOpenstackImageClient();
// // Dummy call to validate information
// try {
// // TODO: Make this call more lightweight
// openstackImageClient.listImages(false);
// } catch (OpenstackException e) {
// throw new OpsException("Unable to connect to OpenStack Image API", e);
// }
//
// }
// private OpenstackComputeConfiguration getOpenstackComputeConfiguration(UserInfo userInfo) throws
// OpsConfigException {
// // ComputeConfiguration computeConfig = new ComputeConfiguration();
// // computeConfig.endpoint = config.getRequiredString("compute.url");
// //
// // String username = config.getRequiredString("compute.username");
// // String secret = config.getRequiredString("compute.secret");
// //
// // computeConfig.awsCredentials = new BasicAWSCredentials(username, secret);
// // return computeConfig;
//
// OpsConfig config = userInfo.getConfig();
//
// String url = config.getRequiredString("compute.os.auth");
//
// String username = config.getRequiredString("compute.os.username");
// String secret = config.getRequiredString("compute.os.secret");
// return new OpenstackComputeConfiguration(url, username, secret);
// }
// private OpenstackImageClient openstackImageClient;
// public OpenstackAuthenticationClient getOpenstackAuthenticationClient(OpenstackCloud cloud) throws OpsException {
// try {
// OpenstackClient openstackClient = getOpenstackClient(cloud);
// return OpenstackAuthenticationClient.loginUsingConfiguration(openstackClient);
// } catch (OpenstackException e) {
// throw new OpsException("Error connecting to OpenStack authentication API", e);
// }
// }
public OpenstackComputeClient getComputeClient(OpenstackCloud cloud) throws OpsException {
// OpsContext opsContext = OpsContext.get();
//
// OpenstackComputeClient computeClient = getOpenstackComputeClient(opsContext.getUserInfo());
OpenstackCloudHelpers helpers = new OpenstackCloudHelpers();
OpenstackComputeClient computeClient = helpers.buildOpenstackComputeClient(cloud);
return computeClient;
}
use of org.openstack.client.common.OpenstackComputeClient in project platformlayer by platformlayer.
the class OpenstackCloudContext method terminateInstance.
public AsyncServerOperation terminateInstance(OpenstackCloud cloud, String instanceId) throws OpsException {
try {
OpenstackComputeClient computeClient = getComputeClient(cloud);
log.info("Terminating server: " + instanceId);
AsyncServerOperation deleteOperation = computeClient.deleteServer(instanceId);
return deleteOperation;
} catch (OpenstackNotFoundException e) {
log.info("Could not find instance to be terminated, assuming already terminated: " + instanceId);
return null;
} catch (OpenstackException e) {
throw new OpsException("Error terminating server", e);
}
}
use of org.openstack.client.common.OpenstackComputeClient in project platformlayer by platformlayer.
the class OpenstackCloudContext method ensurePoweredOn.
public AsyncServerOperation ensurePoweredOn(OpenstackCloud cloud, Server server) throws OpsException {
String status = server.getStatus();
if (Objects.equal(status, "SHUTOFF")) {
try {
OpenstackComputeClient computeClient = getComputeClient(cloud);
String serverId = server.getId();
log.info("Starting SHUTOFF server: " + serverId);
AsyncServerOperation powerOnOperation = computeClient.powerServerOn(serverId);
return powerOnOperation;
} catch (OpenstackException e) {
throw new OpsException("Error powering server on", e);
}
}
return null;
}
Aggregations