Search in sources :

Example 1 with GcpResourceException

use of com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException in project cloudbreak by hortonworks.

the class GcpAttachedDiskResourceBuilder method build.

@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> tags) throws Exception {
    CloudInstance instance = group.getReferenceInstanceConfiguration();
    InstanceTemplate template = instance.getTemplate();
    Volume volume = template.getVolumes().get(0);
    List<CloudResource> resources = new ArrayList<>();
    List<CloudResource> syncedResources = Collections.synchronizedList(resources);
    String projectId = context.getProjectId();
    Location location = context.getLocation();
    Compute compute = context.getCompute();
    Collection<Future<Void>> futures = new ArrayList<>();
    for (CloudResource cloudResource : buildableResource) {
        Disk disk = createDisk(volume, projectId, location.getAvailabilityZone(), cloudResource.getName(), tags);
        Future<Void> submit = intermediateBuilderExecutor.submit(() -> {
            Insert insDisk = compute.disks().insert(projectId, location.getAvailabilityZone().value(), disk);
            try {
                Operation operation = insDisk.execute();
                syncedResources.add(createOperationAwareCloudResource(cloudResource, operation));
                if (operation.getHttpErrorStatusCode() != null) {
                    throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), cloudResource.getName());
                }
            } catch (GoogleJsonResponseException e) {
                throw new GcpResourceException(checkException(e), resourceType(), cloudResource.getName());
            }
            return null;
        });
        futures.add(submit);
    }
    for (Future<Void> future : futures) {
        future.get();
    }
    return resources;
}
Also used : ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Disks.Insert) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) Compute(com.google.api.services.compute.Compute) Future(java.util.concurrent.Future) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Disk(com.google.api.services.compute.model.Disk) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

Example 2 with GcpResourceException

use of com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException in project cloudbreak by hortonworks.

the class GcpDiskResourceBuilder method build.

@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResources, Map<String, String> tags) throws Exception {
    String projectId = context.getProjectId();
    Location location = context.getLocation();
    Disk disk = new Disk();
    disk.setSizeGb(DEFAULT_ROOT_DISK_SIZE);
    disk.setName(buildableResources.get(0).getName());
    disk.setKind(GcpDiskType.HDD.getUrl(projectId, location.getAvailabilityZone()));
    Map<String, String> customTags = new HashMap<>();
    customTags.putAll(tags);
    customTags.putAll(defaultCostTaggingService.prepareDiskTagging());
    disk.setLabels(customTags);
    Insert insDisk = context.getCompute().disks().insert(projectId, location.getAvailabilityZone().value(), disk);
    insDisk.setSourceImage(GcpStackUtil.getAmbariImage(projectId, image.getImageName()));
    try {
        Operation operation = insDisk.execute();
        if (operation.getHttpErrorStatusCode() != null) {
            throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), buildableResources.get(0).getName());
        }
        return Collections.singletonList(createOperationAwareCloudResource(buildableResources.get(0), operation));
    } catch (GoogleJsonResponseException e) {
        throw new GcpResourceException(checkException(e), resourceType(), buildableResources.get(0).getName());
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) HashMap(java.util.HashMap) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Disks.Insert) Disk(com.google.api.services.compute.model.Disk) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

Example 3 with GcpResourceException

use of com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException 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 4 with GcpResourceException

use of com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException 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 5 with GcpResourceException

use of com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException in project cloudbreak by hortonworks.

the class GcpReservedIpResourceBuilder method build.

@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> tags) throws Exception {
    List<CloudResource> result = buildableResource;
    if (!buildableResource.isEmpty()) {
        CloudResource resource = buildableResource.get(0);
        String projectId = context.getProjectId();
        String region = context.getLocation().getRegion().value();
        Address address = new Address();
        address.setName(resource.getName());
        Map<String, String> customTags = new HashMap<>();
        customTags.putAll(tags);
        customTags.putAll(defaultCostTaggingService.prepareIpTagging());
        address.setLabels(customTags);
        Insert networkInsert = context.getCompute().addresses().insert(projectId, region, address);
        try {
            Operation operation = networkInsert.execute();
            if (operation.getHttpErrorStatusCode() != null) {
                throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), resource.getName());
            }
            result = Collections.singletonList(createOperationAwareCloudResource(resource, operation));
        } catch (GoogleJsonResponseException e) {
            throw new GcpResourceException(checkException(e), resourceType(), resource.getName());
        }
    }
    return result;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Address(com.google.api.services.compute.model.Address) HashMap(java.util.HashMap) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Addresses.Insert)

Aggregations

Operation (com.google.api.services.compute.model.Operation)10 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)10 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)8 Compute (com.google.api.services.compute.Compute)6 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)4 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)3 Location (com.sequenceiq.cloudbreak.cloud.model.Location)3 HashMap (java.util.HashMap)3 Insert (com.google.api.services.compute.Compute.Disks.Insert)2 Disk (com.google.api.services.compute.model.Disk)2 Firewall (com.google.api.services.compute.model.Firewall)2 Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)2 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Insert (com.google.api.services.compute.Compute.Addresses.Insert)1 Insert (com.google.api.services.compute.Compute.Firewalls.Insert)1 Get (com.google.api.services.compute.Compute.Instances.Get)1 Insert (com.google.api.services.compute.Compute.Instances.Insert)1 Networks (com.google.api.services.compute.Compute.Networks)1