use of com.google.api.services.compute.Compute.InstanceGroups.AddInstances in project cloudbreak by hortonworks.
the class GcpInstanceResourceBuilderTest method addToInstanceGroupFailsAuth.
@Test
public void addToInstanceGroupFailsAuth() throws Exception {
// GIVEN
Group group = newGroupWithParams(ImmutableMap.of());
List<CloudResource> buildableResources = builder.create(context, group.getInstances().get(0), privateId, authenticatedContext, group, image);
List<CloudResource> resourcesWithGroup = buildableResources.stream().map(b -> CloudResource.builder().cloudResource(b).group(group.getName()).build()).collect(Collectors.toList());
context.addComputeResources(0L, buildableResources);
// WHEN
when(compute.instances()).thenReturn(instances);
when(instances.insert(anyString(), anyString(), any(Instance.class))).thenReturn(insert);
when(insert.setPrettyPrint(anyBoolean())).thenReturn(insert);
when(insert.execute()).thenReturn(operation);
Operation addOperation = new Operation();
addOperation.setName("operation");
addOperation.setHttpErrorStatusCode(401);
addOperation.setHttpErrorMessage("Not Authorized");
addOperation.setError(new Operation.Error());
CloudResource instanceGroup = CloudResource.builder().type(ResourceType.GCP_INSTANCE_GROUP).status(CommonStatus.CREATED).name(group.getName()).group(group.getName()).build();
context.addGroupResources(group.getName(), Collections.singletonList(instanceGroup));
when(compute.instanceGroups()).thenReturn(instanceGroups);
when(instanceGroups.addInstances(anyString(), anyString(), anyString(), any())).thenReturn(addInstances);
InstanceGroups.List list = mock(InstanceGroups.List.class);
when(instanceGroups.list(anyString(), anyString())).thenReturn(list);
InstanceGroupList instanceGroupList = new InstanceGroupList();
instanceGroupList.setItems(singletonList(new InstanceGroup().setName(group.getName())));
when(list.execute()).thenReturn(instanceGroupList);
when(addInstances.execute()).thenReturn(addOperation);
Assert.assertThrows("Not Authorized", GcpResourceException.class, () -> builder.build(context, group.getInstances().get(0), privateId, authenticatedContext, group, resourcesWithGroup, cloudStack));
// THEN
verify(compute).instances();
verify(instances).insert(anyString(), anyString(), instanceArg.capture());
assertNull(instanceArg.getValue().getHostname());
}
use of com.google.api.services.compute.Compute.InstanceGroups.AddInstances 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