use of com.google.api.services.compute.Compute.InstanceGroups 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 in project cloudbreak by hortonworks.
the class GcpInstanceResourceBuilderTest method addInstanceGroupFromUpscale.
@Test
public void addInstanceGroupFromUpscale() throws Exception {
// GIVEN
Group group = newGroupWithParams(ImmutableMap.of());
List<CloudResource> buildableResources = builder.create(context, group.getInstances().get(0), privateId, authenticatedContext, group, image);
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");
CloudResource instanceGroup = CloudResource.builder().type(ResourceType.GCP_INSTANCE_GROUP).status(CommonStatus.CREATED).name(group.getName()).group(group.getName()).build();
CloudResource instanceGroup2 = CloudResource.builder().type(ResourceType.GCP_INSTANCE_GROUP).status(CommonStatus.CREATED).name("gateway").build();
CloudResource instanceGroup3 = CloudResource.builder().type(ResourceType.GCP_INSTANCE_GROUP).status(CommonStatus.CREATED).name("idbroker").build();
CloudResource instanceGroup4 = CloudResource.builder().type(ResourceType.GCP_INSTANCE_GROUP).status(CommonStatus.CREATED).name("free-master0").build();
context.addGroupResources(group.getName(), List.of(instanceGroup4, instanceGroup2, instanceGroup, instanceGroup3));
when(compute.instanceGroups()).thenReturn(instanceGroups);
ArgumentCaptor<String> groupName = ArgumentCaptor.forClass(String.class);
when(instanceGroups.addInstances(anyString(), anyString(), groupName.capture(), 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);
builder.build(context, group.getInstances().get(0), privateId, authenticatedContext, group, buildableResources, cloudStack);
// THEN
verify(compute).instances();
assertEquals("master", groupName.getValue());
verify(instances).insert(anyString(), anyString(), instanceArg.capture());
assertNull(instanceArg.getValue().getHostname());
}
use of com.google.api.services.compute.Compute.InstanceGroups in project cloudbreak by hortonworks.
the class GcpInstanceGroupResourceBuilderTest method testBuildNoPermission.
@Test
public void testBuildNoPermission() throws Exception {
CloudResource resource = new CloudResource.Builder().type(ResourceType.GCP_INSTANCE_GROUP).status(CommonStatus.CREATED).group("master").name("super").instanceId("id-123").params(new HashMap<>()).persistent(true).build();
Compute.InstanceGroups.Insert instanceGroupsInsert = mock(Compute.InstanceGroups.Insert.class);
when(gcpContext.getCompute()).thenReturn(compute);
when(gcpContext.getProjectId()).thenReturn("id");
when(gcpContext.getLocation()).thenReturn(location);
when(location.getAvailabilityZone()).thenReturn(availabilityZone);
when(availabilityZone.value()).thenReturn("zone");
when(compute.instanceGroups()).thenReturn(instanceGroups);
when(instanceGroups.insert(anyString(), anyString(), any())).thenReturn(instanceGroupsInsert);
when(instanceGroupsInsert.execute()).thenReturn(operation);
when(operation.getHttpErrorStatusCode()).thenReturn(401);
when(operation.getHttpErrorMessage()).thenReturn("Not Authorized");
Assert.assertThrows("Not Authorized", GcpResourceException.class, () -> underTest.build(gcpContext, authenticatedContext, group, network, security, resource));
}
use of com.google.api.services.compute.Compute.InstanceGroups in project cloudbreak by hortonworks.
the class GcpInstanceGroupResourceBuilderTest method testBuildWithItemsInGroup.
@Test
public void testBuildWithItemsInGroup() throws Exception {
CloudResource resource = new CloudResource.Builder().type(ResourceType.GCP_INSTANCE_GROUP).status(CommonStatus.CREATED).group("master").name("super").instanceId("id-123").params(new HashMap<>()).persistent(true).build();
Compute.InstanceGroups.Insert instanceGroupsInsert = mock(Compute.InstanceGroups.Insert.class);
when(gcpContext.getCompute()).thenReturn(compute);
when(gcpContext.getProjectId()).thenReturn("id");
when(gcpContext.getLocation()).thenReturn(location);
when(location.getAvailabilityZone()).thenReturn(availabilityZone);
when(availabilityZone.value()).thenReturn("zone");
when(compute.instanceGroups()).thenReturn(instanceGroups);
when(instanceGroups.insert(anyString(), anyString(), any())).thenReturn(instanceGroupsInsert);
when(instanceGroupsInsert.execute()).thenReturn(operation);
when(operation.getName()).thenReturn("name");
when(operation.getHttpErrorStatusCode()).thenReturn(null);
CloudResource cloudResource = underTest.build(gcpContext, authenticatedContext, group, network, security, resource);
Assert.assertEquals("super", cloudResource.getName());
}
Aggregations