Search in sources :

Example 11 with Compute

use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.

the class GcpInstanceResourceBuilder method stopStart.

private CloudVmInstanceStatus stopStart(GcpContext context, AuthenticatedContext auth, CloudInstance instance, boolean stopRequest) {
    String projectId = GcpStackUtil.getProjectId(auth.getCloudCredential());
    String availabilityZone = context.getLocation().getAvailabilityZone().value();
    Compute compute = context.getCompute();
    String instanceId = instance.getInstanceId();
    try {
        Get get = compute.instances().get(projectId, availabilityZone, instanceId);
        String state = stopRequest ? "RUNNING" : "TERMINATED";
        if (state.equals(get.execute().getStatus())) {
            Operation operation = stopRequest ? compute.instances().stop(projectId, availabilityZone, instanceId).setPrettyPrint(true).execute() : compute.instances().start(projectId, availabilityZone, instanceId).setPrettyPrint(true).execute();
            CloudInstance operationAwareCloudInstance = createOperationAwareCloudInstance(instance, operation);
            return checkInstances(context, auth, Collections.singletonList(operationAwareCloudInstance)).get(0);
        } else {
            LOGGER.info("Instance {} is not in {} state - won't start it.", state, instanceId);
            return null;
        }
    } catch (IOException e) {
        throw new GcpResourceException(String.format("An error occurred while stopping the vm '%s'", instanceId), e);
    }
}
Also used : Compute(com.google.api.services.compute.Compute) Get(com.google.api.services.compute.Compute.Instances.Get) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) Operation(com.google.api.services.compute.model.Operation) IOException(java.io.IOException)

Example 12 with Compute

use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.

the class GcpNetworkResourceBuilder method delete.

@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource, Network network) throws Exception {
    if (!isExistingNetwork(network)) {
        Compute compute = context.getCompute();
        String projectId = context.getProjectId();
        try {
            Operation operation = compute.networks().delete(projectId, resource.getName()).execute();
            return createOperationAwareCloudResource(resource, operation);
        } catch (GoogleJsonResponseException e) {
            exceptionHandler(e, resource.getName(), resourceType());
            return null;
        }
    }
    return null;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Compute(com.google.api.services.compute.Compute) Operation(com.google.api.services.compute.model.Operation)

Example 13 with Compute

use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.

the class GcpSubnetResourceBuilder method build.

@Override
public CloudResource build(GcpContext context, AuthenticatedContext auth, Network network, Security security, CloudResource resource) throws Exception {
    if (isNewNetworkAndSubnet(network) || isNewSubnetInExistingNetwork(network)) {
        Compute compute = context.getCompute();
        String projectId = context.getProjectId();
        Subnetwork gcpSubnet = new Subnetwork();
        gcpSubnet.setName(resource.getName());
        gcpSubnet.setIpCidrRange(network.getSubnet().getCidr());
        String networkName = context.getStringParameter(GcpNetworkResourceBuilder.NETWORK_NAME);
        gcpSubnet.setNetwork(String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", projectId, networkName));
        Insert snInsert = compute.subnetworks().insert(projectId, auth.getCloudContext().getLocation().getRegion().value(), gcpSubnet);
        try {
            Operation operation = snInsert.execute();
            if (operation.getHttpErrorStatusCode() != null) {
                throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), resource.getName());
            }
            context.putParameter(SUBNET_NAME, resource.getName());
            return createOperationAwareCloudResource(resource, operation);
        } catch (GoogleJsonResponseException e) {
            throw new GcpResourceException(checkException(e), resourceType(), resource.getName());
        }
    }
    context.putParameter(SUBNET_NAME, resource.getName());
    return new Builder().cloudResource(resource).persistent(false).build();
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Subnetwork(com.google.api.services.compute.model.Subnetwork) Compute(com.google.api.services.compute.Compute) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Subnetworks.Insert)

Example 14 with Compute

use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.

the class GcpStackUtil method buildCompute.

public static Compute buildCompute(CloudCredential gcpCredential) {
    try {
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        GoogleCredential credential = buildCredential(gcpCredential, httpTransport);
        return new Compute.Builder(httpTransport, JSON_FACTORY, null).setApplicationName(gcpCredential.getName()).setHttpRequestInitializer(credential).build();
    } catch (Exception e) {
        LOGGER.error("Error occurred while building Google Compute access.", e);
    }
    return null;
}
Also used : HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) Compute(com.google.api.services.compute.Compute) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)

Example 15 with Compute

use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.

the class GcpMetadataCollector method getCloudVmMetaDataStatus.

private CloudVmMetaDataStatus getCloudVmMetaDataStatus(AuthenticatedContext authenticatedContext, CloudResource cloudResource, CloudInstance matchedInstance) {
    CloudVmMetaDataStatus cloudVmMetaDataStatus;
    if (cloudResource != null) {
        CloudInstance cloudInstance = new CloudInstance(cloudResource.getName(), matchedInstance.getTemplate(), matchedInstance.getAuthentication());
        try {
            CloudCredential credential = authenticatedContext.getCloudCredential();
            CloudContext cloudContext = authenticatedContext.getCloudContext();
            Compute compute = GcpStackUtil.buildCompute(credential);
            Instance executeInstance = getInstance(cloudContext, credential, compute, cloudResource.getName());
            String privateIp = executeInstance.getNetworkInterfaces().get(0).getNetworkIP();
            String publicIp = null;
            List<AccessConfig> acl = executeInstance.getNetworkInterfaces().get(0).getAccessConfigs();
            if (acl != null && acl.get(0) != null) {
                publicIp = executeInstance.getNetworkInterfaces().get(0).getAccessConfigs().get(0).getNatIP();
            }
            CloudInstanceMetaData metaData = new CloudInstanceMetaData(privateIp, publicIp);
            CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
            cloudVmMetaDataStatus = new CloudVmMetaDataStatus(status, metaData);
        } catch (IOException e) {
            LOGGER.warn(String.format("Instance %s is not reachable", cloudResource.getName()), e);
            CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.UNKNOWN);
            cloudVmMetaDataStatus = new CloudVmMetaDataStatus(status, CloudInstanceMetaData.EMPTY_METADATA);
        }
    } else {
        CloudVmInstanceStatus status = new CloudVmInstanceStatus(matchedInstance, InstanceStatus.TERMINATED);
        cloudVmMetaDataStatus = new CloudVmMetaDataStatus(status, CloudInstanceMetaData.EMPTY_METADATA);
    }
    return cloudVmMetaDataStatus;
}
Also used : CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Instance(com.google.api.services.compute.model.Instance) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Compute(com.google.api.services.compute.Compute) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) IOException(java.io.IOException) AccessConfig(com.google.api.services.compute.model.AccessConfig)

Aggregations

Compute (com.google.api.services.compute.Compute)43 IOException (java.io.IOException)26 Operation (com.google.api.services.compute.model.Operation)16 ArrayList (java.util.ArrayList)13 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)12 HashMap (java.util.HashMap)9 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)8 GeneralSecurityException (java.security.GeneralSecurityException)8 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)7 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)7 Instance (com.google.api.services.compute.model.Instance)6 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)6 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)6 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)4 HttpRequest (com.google.api.client.http.HttpRequest)4 HttpTransport (com.google.api.client.http.HttpTransport)4 AttachedDisk (com.google.api.services.compute.model.AttachedDisk)4 ManagedInstance (com.google.api.services.compute.model.ManagedInstance)4 NetworkInterface (com.google.api.services.compute.model.NetworkInterface)4 Subnetwork (com.google.api.services.compute.model.Subnetwork)4