Search in sources :

Example 6 with OpenstackComputeClient

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);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Server(org.openstack.model.compute.Server) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient) OpenstackNotFoundException(org.openstack.client.OpenstackNotFoundException) OpenstackException(org.openstack.client.OpenstackException)

Example 7 with OpenstackComputeClient

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;
}
Also used : OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient)

Example 8 with OpenstackComputeClient

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);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient) OpenstackNotFoundException(org.openstack.client.OpenstackNotFoundException) AsyncServerOperation(org.openstack.client.compute.AsyncServerOperation) OpenstackException(org.openstack.client.OpenstackException)

Example 9 with OpenstackComputeClient

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;
}
Also used : OpsException(org.platformlayer.ops.OpsException) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient) AsyncServerOperation(org.openstack.client.compute.AsyncServerOperation) OpenstackException(org.openstack.client.OpenstackException)

Aggregations

OpenstackComputeClient (org.openstack.client.common.OpenstackComputeClient)9 OpsException (org.platformlayer.ops.OpsException)6 Server (org.openstack.model.compute.Server)5 OpenstackException (org.openstack.client.OpenstackException)4 AsyncServerOperation (org.openstack.client.compute.AsyncServerOperation)4 OpenstackNotFoundException (org.openstack.client.OpenstackNotFoundException)3 SecurityGroup (org.openstack.model.compute.SecurityGroup)3 CreateSecurityGroupRuleRequest (org.openstack.model.compute.CreateSecurityGroupRuleRequest)2 SecurityGroupRule (org.openstack.model.compute.SecurityGroupRule)2 Tag (org.platformlayer.core.model.Tag)2 Handler (org.platformlayer.ops.Handler)2 CloudBehaviours (org.platformlayer.service.cloud.openstack.ops.openstack.CloudBehaviours)2 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Extension (org.openstack.model.common.Extension)1 Ip (org.openstack.model.compute.Addresses.Network.Ip)1 Flavor (org.openstack.model.compute.Flavor)1 FloatingIp (org.openstack.model.compute.FloatingIp)1 Image (org.openstack.model.compute.Image)1 KeyPair (org.openstack.model.compute.KeyPair)1