Search in sources :

Example 1 with AddInstances

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());
}
Also used : Arrays(java.util.Arrays) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) GcpResourceNameService(com.sequenceiq.cloudbreak.cloud.gcp.service.GcpResourceNameService) Collections.singletonList(java.util.Collections.singletonList) InstanceGroupList(com.google.api.services.compute.model.InstanceGroupList) GroupNetwork(com.sequenceiq.cloudbreak.cloud.model.GroupNetwork) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) Location(com.sequenceiq.cloudbreak.cloud.model.Location) PortDefinition(com.sequenceiq.cloudbreak.cloud.model.PortDefinition) CustomGcpDiskEncryptionCreatorService(com.sequenceiq.cloudbreak.cloud.gcp.service.CustomGcpDiskEncryptionCreatorService) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) GcpStackUtil(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) Instances(com.google.api.services.compute.Compute.Instances) CloudVolumeUsageType(com.sequenceiq.cloudbreak.cloud.model.CloudVolumeUsageType) Assert.assertFalse(org.junit.Assert.assertFalse) Get(com.google.api.services.compute.Compute.Instances.Get) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) Region(com.sequenceiq.cloudbreak.cloud.model.Region) Mock(org.mockito.Mock) AddInstances(com.google.api.services.compute.Compute.InstanceGroups.AddInstances) RunWith(org.junit.runner.RunWith) Start(com.google.api.services.compute.Compute.Instances.Start) SpiFileSystem(com.sequenceiq.cloudbreak.cloud.model.SpiFileSystem) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) InstanceGroup(com.google.api.services.compute.model.InstanceGroup) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Network(com.sequenceiq.cloudbreak.cloud.model.Network) CloudGcsView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudGcsView) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) CloudFileSystemView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudFileSystemView) InstanceGroupType(com.sequenceiq.common.api.type.InstanceGroupType) CustomerEncryptionKeyProtectedDisk(com.google.api.services.compute.model.CustomerEncryptionKeyProtectedDisk) Assert.assertNull(org.junit.Assert.assertNull) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) FileSystemType(com.sequenceiq.common.model.FileSystemType) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) InstanceGroups(com.google.api.services.compute.Compute.InstanceGroups) Assert(org.junit.Assert) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) CLOUD_STACK_TYPE_PARAMETER(com.sequenceiq.cloudbreak.cloud.PlatformParametersConsts.CLOUD_STACK_TYPE_PARAMETER) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GcpLabelUtil(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpLabelUtil) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) AttachedDisk(com.google.api.services.compute.model.AttachedDisk) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CustomGcpDiskEncryptionService(com.sequenceiq.cloudbreak.cloud.gcp.service.CustomGcpDiskEncryptionService) GcpContext(com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext) Insert(com.google.api.services.compute.Compute.Instances.Insert) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.emptyList(java.util.Collections.emptyList) Operation(com.google.api.services.compute.model.Operation) Collectors(java.util.stream.Collectors) TemporaryStorage(com.sequenceiq.cloudbreak.common.type.TemporaryStorage) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) EncryptionType(com.sequenceiq.common.api.type.EncryptionType) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Optional(java.util.Optional) OutboundInternetTraffic(com.sequenceiq.common.api.type.OutboundInternetTraffic) CloudIdentityType(com.sequenceiq.common.model.CloudIdentityType) Compute(com.google.api.services.compute.Compute) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Instance(com.google.api.services.compute.model.Instance) HashMap(java.util.HashMap) CustomerEncryptionKey(com.google.api.services.compute.model.CustomerEncryptionKey) InstancesStartWithEncryptionKeyRequest(com.google.api.services.compute.model.InstancesStartWithEncryptionKeyRequest) Disk(com.google.api.services.compute.model.Disk) Image(com.sequenceiq.cloudbreak.cloud.model.Image) Captor(org.mockito.Captor) HashSet(java.util.HashSet) StartWithEncryptionKey(com.google.api.services.compute.Compute.Instances.StartWithEncryptionKey) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) ArgumentCaptor(org.mockito.ArgumentCaptor) FREEIPA_STACK_TYPE(com.sequenceiq.cloudbreak.cloud.PlatformParametersConsts.FREEIPA_STACK_TYPE) Security(com.sequenceiq.cloudbreak.cloud.model.Security) ResourceType(com.sequenceiq.common.api.type.ResourceType) Collections.emptyMap(java.util.Collections.emptyMap) Assert.assertNotNull(org.junit.Assert.assertNotNull) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) SecurityRule(com.sequenceiq.cloudbreak.cloud.model.SecurityRule) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Mockito.when(org.mockito.Mockito.when) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DISCOVERY_NAME(com.sequenceiq.cloudbreak.cloud.model.CloudInstance.DISCOVERY_NAME) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) Collections(java.util.Collections) InstanceGroup(com.google.api.services.compute.model.InstanceGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) InstanceGroupList(com.google.api.services.compute.model.InstanceGroupList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.google.api.services.compute.model.Instance) InstanceGroups(com.google.api.services.compute.Compute.InstanceGroups) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Operation(com.google.api.services.compute.model.Operation) InstanceGroup(com.google.api.services.compute.model.InstanceGroup) Test(org.junit.Test)

Example 2 with AddInstances

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

Compute (com.google.api.services.compute.Compute)2 AddInstances (com.google.api.services.compute.Compute.InstanceGroups.AddInstances)2 Operation (com.google.api.services.compute.model.Operation)2 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 InstanceGroups (com.google.api.services.compute.Compute.InstanceGroups)1 Instances (com.google.api.services.compute.Compute.Instances)1 Get (com.google.api.services.compute.Compute.Instances.Get)1 Insert (com.google.api.services.compute.Compute.Instances.Insert)1 Start (com.google.api.services.compute.Compute.Instances.Start)1 StartWithEncryptionKey (com.google.api.services.compute.Compute.Instances.StartWithEncryptionKey)1 AttachedDisk (com.google.api.services.compute.model.AttachedDisk)1 CustomerEncryptionKey (com.google.api.services.compute.model.CustomerEncryptionKey)1 CustomerEncryptionKeyProtectedDisk (com.google.api.services.compute.model.CustomerEncryptionKeyProtectedDisk)1 Disk (com.google.api.services.compute.model.Disk)1 Instance (com.google.api.services.compute.model.Instance)1 InstanceGroup (com.google.api.services.compute.model.InstanceGroup)1 InstanceGroupList (com.google.api.services.compute.model.InstanceGroupList)1 InstanceGroupsAddInstancesRequest (com.google.api.services.compute.model.InstanceGroupsAddInstancesRequest)1