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);
}
}
Aggregations