Search in sources :

Example 1 with RawDisk

use of com.google.api.services.compute.model.Image.RawDisk in project cloudbreak by hortonworks.

the class GcpProvisionSetup method prepareImage.

@Override
public void prepareImage(AuthenticatedContext authenticatedContext, CloudStack stack, com.sequenceiq.cloudbreak.cloud.model.Image image) {
    CloudCredential credential = authenticatedContext.getCloudCredential();
    CloudContext cloudContext = authenticatedContext.getCloudContext();
    try {
        String projectId = getProjectId(credential);
        String imageName = image.getImageName();
        Compute compute = buildCompute(credential);
        ImageList list = compute.images().list(projectId).execute();
        if (!containsSpecificImage(list, imageName)) {
            Storage storage = buildStorage(credential, cloudContext.getName());
            Bucket bucket = new Bucket();
            bucket.setName(String.format("%s-%s-%d", projectId, cloudContext.getName(), cloudContext.getId()));
            bucket.setStorageClass("STANDARD");
            try {
                Buckets.Insert ins = storage.buckets().insert(projectId, bucket);
                ins.execute();
            } catch (GoogleJsonResponseException ex) {
                if (ex.getStatusCode() != HttpStatus.SC_CONFLICT) {
                    throw ex;
                }
            }
            String tarName = getTarName(imageName);
            Copy copy = storage.objects().copy(getBucket(imageName), tarName, bucket.getName(), tarName, new StorageObject());
            copy.execute();
            Image gcpApiImage = new Image();
            gcpApiImage.setName(getImageName(imageName));
            RawDisk rawDisk = new RawDisk();
            rawDisk.setSource(String.format("http://storage.googleapis.com/%s/%s", bucket.getName(), tarName));
            gcpApiImage.setRawDisk(rawDisk);
            Insert ins = compute.images().insert(projectId, gcpApiImage);
            ins.execute();
        }
    } catch (Exception e) {
        Long stackId = cloudContext.getId();
        String msg = String.format("Error occurred on %s stack during the setup: %s", stackId, e.getMessage());
        LOGGER.error(msg, e);
        throw new CloudConnectorException(msg, e);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Image(com.google.api.services.compute.model.Image) Insert(com.google.api.services.compute.Compute.Images.Insert) Buckets(com.google.api.services.storage.Storage.Buckets) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) IOException(java.io.IOException) ImageList(com.google.api.services.compute.model.ImageList) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Storage(com.google.api.services.storage.Storage) GcpStackUtil.buildStorage(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil.buildStorage) GcpStackUtil.getBucket(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil.getBucket) Bucket(com.google.api.services.storage.model.Bucket) Copy(com.google.api.services.storage.Storage.Objects.Copy) GcpStackUtil.buildCompute(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil.buildCompute) Compute(com.google.api.services.compute.Compute) RawDisk(com.google.api.services.compute.model.Image.RawDisk)

Aggregations

GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 Compute (com.google.api.services.compute.Compute)1 Insert (com.google.api.services.compute.Compute.Images.Insert)1 Image (com.google.api.services.compute.model.Image)1 RawDisk (com.google.api.services.compute.model.Image.RawDisk)1 ImageList (com.google.api.services.compute.model.ImageList)1 Storage (com.google.api.services.storage.Storage)1 Buckets (com.google.api.services.storage.Storage.Buckets)1 Copy (com.google.api.services.storage.Storage.Objects.Copy)1 Bucket (com.google.api.services.storage.model.Bucket)1 StorageObject (com.google.api.services.storage.model.StorageObject)1 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)1 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 GcpStackUtil.buildCompute (com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil.buildCompute)1 GcpStackUtil.buildStorage (com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil.buildStorage)1 GcpStackUtil.getBucket (com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil.getBucket)1 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)1 IOException (java.io.IOException)1