Search in sources :

Example 56 with Operation

use of com.google.api.services.compute.model.Operation in project halyard by spinnaker.

the class GoogleProviderUtils method ensureSpinnakerNetworkExists.

static String ensureSpinnakerNetworkExists(AccountDeploymentDetails<GoogleAccount> details) {
    String networkName = getNetworkName();
    String project = details.getAccount().getProject();
    Compute compute = getCompute(details);
    boolean exists = true;
    try {
        compute.networks().get(project, networkName).execute();
    } catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == 404) {
            exists = false;
        } else {
            throw new HalException(FATAL, "Google error encountered retrieving network: " + e.getMessage(), e);
        }
    } catch (IOException e) {
        throw new HalException(FATAL, "Failed to check if spinnaker network exists: " + e.getMessage(), e);
    }
    if (!exists) {
        String networkUrl;
        Network network = new Network().setAutoCreateSubnetworks(true).setName(networkName).setDescription("Spinnaker network auto-created by Halyard");
        try {
            DaemonTaskHandler.message("Creating a spinnaker network...");
            Operation operation = compute.networks().insert(project, network).execute();
            networkUrl = operation.getTargetLink();
            GoogleProviderUtils.waitOnGlobalOperation(compute, project, operation);
        } catch (IOException e) {
            throw new HalException(FATAL, "Failed to create Spinnaker network: " + e.getMessage(), e);
        }
        Firewall.Allowed allowSsh = new Firewall.Allowed().setPorts(Collections.singletonList("22")).setIPProtocol("tcp");
        Firewall firewallSsh = new Firewall().setNetwork(networkUrl).setAllowed(Collections.singletonList(allowSsh)).setName(networkName + "-allow-ssh").setSourceRanges(Collections.singletonList("0.0.0.0/0"));
        Firewall.Allowed allowInternalTcp = new Firewall.Allowed().setPorts(Collections.singletonList("1-65535")).setIPProtocol("tcp");
        Firewall.Allowed allowInternalUdp = new Firewall.Allowed().setPorts(Collections.singletonList("1-65535")).setIPProtocol("udp");
        Firewall.Allowed allowInternalIcmp = new Firewall.Allowed().setIPProtocol("icmp");
        List<Firewall.Allowed> allowInteral = new ArrayList<>();
        allowInteral.add(allowInternalTcp);
        allowInteral.add(allowInternalUdp);
        allowInteral.add(allowInternalIcmp);
        Firewall firewallInternal = new Firewall().setNetwork(networkUrl).setAllowed(allowInteral).setName(networkName + "-allow-internal").setSourceRanges(Collections.singletonList("10.0.0.0/8"));
        try {
            DaemonTaskHandler.message("Adding firewall rules...");
            compute.firewalls().insert(project, firewallSsh).execute();
            compute.firewalls().insert(project, firewallInternal).execute();
        } catch (IOException e) {
            throw new HalException(FATAL, "Failed to create Firewall rule network: " + e.getMessage(), e);
        }
    }
    return String.format("projects/%s/global/networks/%s", project, networkName);
}
Also used : HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Operation(com.google.api.services.compute.model.Operation) Firewall(com.google.api.services.compute.model.Firewall) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Compute(com.google.api.services.compute.Compute) Network(com.google.api.services.compute.model.Network)

Example 57 with Operation

use of com.google.api.services.compute.model.Operation in project halyard by spinnaker.

the class GoogleProviderUtils method waitOnOperation.

private static void waitOnOperation(Supplier<Operation> operationSupplier) {
    Operation operation = operationSupplier.get();
    while (!operation.getStatus().equals("DONE")) {
        if (operation.getError() != null) {
            throw new HalException(FATAL, String.join("\n", operation.getError().getErrors().stream().map(e -> e.getCode() + ": " + e.getMessage()).collect(Collectors.toList())));
        }
        operation = operationSupplier.get();
        DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(1));
    }
}
Also used : Socket(java.net.Socket) Instance(com.google.api.services.compute.model.Instance) URISyntaxException(java.net.URISyntaxException) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) Network(com.google.api.services.compute.model.Network) ConfigProblemSetBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder) Supplier(java.util.function.Supplier) StringUtils(org.apache.commons.lang3.StringUtils) DaemonTaskHandler(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskHandler) ArrayList(java.util.ArrayList) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) AccountDeploymentDetails(com.netflix.spinnaker.halyard.deploy.deployment.v1.AccountDeploymentDetails) AccessConfig(com.google.api.services.compute.model.AccessConfig) Firewall(com.google.api.services.compute.model.Firewall) GoogleAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount) JobExecutor(com.netflix.spinnaker.halyard.core.job.v1.JobExecutor) URI(java.net.URI) GoogleNamedAccountCredentials(com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials) URIBuilder(org.apache.http.client.utils.URIBuilder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) Operation(com.google.api.services.compute.model.Operation) Collectors(java.util.stream.Collectors) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) FATAL(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL) Paths(java.nio.file.Paths) SocketUtils(org.springframework.util.SocketUtils) Data(lombok.Data) Optional(java.util.Optional) DaemonTaskInterrupted(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) Compute(com.google.api.services.compute.Compute) JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) Collections(java.util.Collections) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) Operation(com.google.api.services.compute.model.Operation)

Example 58 with Operation

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

the class GcpAttachedDiskResourceBuilderTest method setUpBuild.

@BeforeEach
void setUpBuild() throws Exception {
    privateCrn = "crn";
    CloudContext cloudContext = CloudContext.Builder.builder().withId(privateId).withName("testname").withCrn("crn").withPlatform("GCP").withWorkspaceId(WORKSPACE_ID).build();
    CloudCredential cloudCredential = new CloudCredential(privateCrn, "credentialname", "account");
    cloudCredential.putParameter("projectId", "projectId");
    cloudInstance = new CloudInstance(instanceId, new InstanceTemplate("flavor", "group", 1L, new ArrayList<>(), InstanceStatus.CREATE_REQUESTED, new HashMap<>(), 1L, "img", TemporaryStorage.ATTACHED_VOLUMES, 0L), new InstanceAuthentication("pub", "pub", "cb"), "subnet1", "az1");
    Location location = Location.location(Region.region("region"), AvailabilityZone.availabilityZone("az"));
    context = new GcpContext(cloudContext.getName(), location, "projectId", "serviceAccountId", compute, false, 30, false);
    List<CloudResource> networkResources = Collections.singletonList(new Builder().type(ResourceType.GCP_NETWORK).name("network-test").build());
    context.addNetworkResources(networkResources);
    privateId = 1L;
    name = "master";
    flavor = "m1.medium";
    instanceId = "SOME_ID";
    auth = new AuthenticatedContext(cloudContext, cloudCredential);
    params = Map.of();
    volumes = Arrays.asList(new Volume("/hadoop/fs1", "HDD", 1, CloudVolumeUsageType.GENERAL), new Volume("/hadoop/fs2", "HDD", 1, CloudVolumeUsageType.GENERAL));
    List<SecurityRule> rules = Collections.singletonList(new SecurityRule("0.0.0.0/0", new PortDefinition[] { new PortDefinition("22", "22"), new PortDefinition("443", "443") }, "tcp"));
    security = new Security(rules, emptyList());
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    InstanceTemplate instanceTemplate = new InstanceTemplate(flavor, name, privateId, volumes, InstanceStatus.CREATE_REQUESTED, params, 0L, "cb-centos66-amb200-2015-05-25", TemporaryStorage.ATTACHED_VOLUMES, 0L);
    CloudInstance cloudInstance = new CloudInstance(instanceId, instanceTemplate, instanceAuthentication, "subnet-1", "az1");
    group = new Group(name, InstanceGroupType.CORE, Collections.singletonList(cloudInstance), security, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), 50, Optional.empty(), createGroupNetwork(), emptyMap());
    List<VolumeSetAttributes.Volume> volumes = new ArrayList<>();
    volumes.add(new VolumeSetAttributes.Volume("1234", "noop", 0, "eph", CloudVolumeUsageType.GENERAL));
    VolumeSetAttributes attributes = new VolumeSetAttributes("Ireland", true, "", volumes, 0, "eph");
    Map<String, Object> params = new HashMap<>();
    params.put(CloudResource.ATTRIBUTES, attributes);
    buildableResource = List.of(CloudResource.builder().type(ResourceType.GCP_DISK).status(CommonStatus.REQUESTED).name("disk").params(params).build());
    Map<InstanceGroupType, String> userData = ImmutableMap.of(InstanceGroupType.CORE, "CORE", InstanceGroupType.GATEWAY, "GATEWAY");
    Image image = new Image("cb-centos66-amb200-2015-05-25", userData, "redhat6", "redhat6", "", "default", "default-id", new HashMap<>());
    cloudStack = new CloudStack(Collections.emptyList(), null, image, emptyMap(), emptyMap(), null, null, null, null, null);
    when(intermediateBuilderExecutor.submit(any(Callable.class))).thenAnswer(invocation -> {
        Callable<Void> callable = invocation.getArgument(0);
        return new MockFuture(callable);
    });
    operation = new Operation();
    operation.setName("operation");
    operation.setHttpErrorStatusCode(null);
    when(compute.disks()).thenReturn(disks);
    when(disks.insert(anyString(), anyString(), any(Disk.class))).thenReturn(insert);
    when(insert.execute()).thenReturn(operation);
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) InstanceGroupType(com.sequenceiq.common.api.type.InstanceGroupType) HashMap(java.util.HashMap) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) SecurityRule(com.sequenceiq.cloudbreak.cloud.model.SecurityRule) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Operation(com.google.api.services.compute.model.Operation) Security(com.sequenceiq.cloudbreak.cloud.model.Security) Image(com.sequenceiq.cloudbreak.cloud.model.Image) Callable(java.util.concurrent.Callable) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) GcpContext(com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext) Disk(com.google.api.services.compute.model.Disk) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) PortDefinition(com.sequenceiq.cloudbreak.cloud.model.PortDefinition) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Location(com.sequenceiq.cloudbreak.cloud.model.Location) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 59 with Operation

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

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

Aggregations

Operation (com.google.api.services.compute.model.Operation)96 Compute (com.google.api.services.compute.Compute)40 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)37 IOException (java.io.IOException)37 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)23 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)21 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)17 Instance (com.google.api.services.compute.model.Instance)15 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)15 AutoScalingData (org.apache.druid.indexing.overlord.autoscaling.AutoScalingData)14 GcpContext (com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext)12 InstanceGroupManagersListManagedInstancesResponse (com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse)10 Location (com.sequenceiq.cloudbreak.cloud.model.Location)10 HashMap (java.util.HashMap)10 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)9 Disk (com.google.api.services.compute.model.Disk)8 InstanceGroupManagersDeleteInstancesRequest (com.google.api.services.compute.model.InstanceGroupManagersDeleteInstancesRequest)8 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)8 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)8