Search in sources :

Example 1 with InstanceGroup

use of com.google.api.services.compute.model.InstanceGroup 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 InstanceGroup

use of com.google.api.services.compute.model.InstanceGroup 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());
}
Also used : 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) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InstanceGroup(com.google.api.services.compute.model.InstanceGroup) Test(org.junit.Test)

Example 3 with InstanceGroup

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

the class GcpInstanceGroupResourceBuilder method build.

@Override
public CloudResource build(GcpContext context, AuthenticatedContext auth, Group group, Network network, Security security, CloudResource resource) throws Exception {
    LOGGER.info("Building GCP instancegroup {} for project {}", group.getName(), context.getProjectId());
    Insert insert = context.getCompute().instanceGroups().insert(context.getProjectId(), context.getLocation().getAvailabilityZone().value(), new InstanceGroup().setName(resource.getName()));
    return executeOperationalRequest(resource, insert);
}
Also used : Insert(com.google.api.services.compute.Compute.InstanceGroups.Insert) InstanceGroup(com.google.api.services.compute.model.InstanceGroup)

Aggregations

InstanceGroup (com.google.api.services.compute.model.InstanceGroup)3 InstanceGroups (com.google.api.services.compute.Compute.InstanceGroups)2 Instance (com.google.api.services.compute.model.Instance)2 InstanceGroupList (com.google.api.services.compute.model.InstanceGroupList)2 Operation (com.google.api.services.compute.model.Operation)2 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2 Group (com.sequenceiq.cloudbreak.cloud.model.Group)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Compute (com.google.api.services.compute.Compute)1 AddInstances (com.google.api.services.compute.Compute.InstanceGroups.AddInstances)1 Insert (com.google.api.services.compute.Compute.InstanceGroups.Insert)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