Search in sources :

Example 1 with InstanceGroupsAddInstancesRequest

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

the class GcpInstanceResourceBuilder method assignToExistingInstanceGroup.

/**
 * if a InstanceGroup was created in GCP for this Instance's group, then after creating this compute instance assign it to that group.
 * the group in general can be used to manage all instances in the same group, specifiaclly one way is used to assign to a load balancer.
 * also provides aggrigated monitoring
 */
private void assignToExistingInstanceGroup(GcpContext context, Group group, Instance instance, List<CloudResource> buildableResource) throws IOException {
    Compute compute = context.getCompute();
    String projectId = context.getProjectId();
    String zone = context.getLocation().getAvailabilityZone().value();
    List<CloudResource> instanceGroupResources = filterGroupFromName(filterResourcesByType(context.getGroupResources(group.getName()), ResourceType.GCP_INSTANCE_GROUP), group.getName());
    if (!instanceGroupResources.isEmpty() && doesGcpInstanceGroupExist(compute, projectId, zone, instanceGroupResources.get(0))) {
        LOGGER.info("adding instance {} to group {} in project {}", instance.getName(), group.getName(), projectId);
        InstanceGroupsAddInstancesRequest request = createAddInstancesRequest(instance, projectId, zone);
        AddInstances addInstances = compute.instanceGroups().addInstances(projectId, zone, instanceGroupResources.get(0).getName(), request);
        try {
            Operation execute = addInstances.execute();
            verifyOperation(execute, buildableResource);
        } catch (GoogleJsonResponseException e) {
            LOGGER.error("Error in Google response, unable to add instance {} to group {} : {} for {}", instance.getName(), group.getName(), e.getMessage(), e.getDetails().getMessage());
            throw new GcpResourceException(checkException(e), resourceType(), buildableResource.get(0).getName());
        }
    } else {
        LOGGER.info("skipping group assignment {} doesn't exist in project {}", group.getName(), projectId);
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) AddInstances(com.google.api.services.compute.Compute.InstanceGroups.AddInstances) Compute(com.google.api.services.compute.Compute) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Operation(com.google.api.services.compute.model.Operation) InstanceGroupsAddInstancesRequest(com.google.api.services.compute.model.InstanceGroupsAddInstancesRequest)

Aggregations

GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 Compute (com.google.api.services.compute.Compute)1 AddInstances (com.google.api.services.compute.Compute.InstanceGroups.AddInstances)1 InstanceGroupsAddInstancesRequest (com.google.api.services.compute.model.InstanceGroupsAddInstancesRequest)1 Operation (com.google.api.services.compute.model.Operation)1 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)1 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)1