Search in sources :

Example 26 with Operation

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

the class GcpBackendServiceResourceBuilder method delete.

@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource) throws Exception {
    String regionName = context.getLocation().getRegion().getRegionName();
    LOGGER.info("deleting backend service {} for {}", resource.getName(), context.getProjectId());
    Delete delete = context.getCompute().regionBackendServices().delete(context.getProjectId(), regionName, resource.getName());
    try {
        Operation operation = delete.execute();
        return createOperationAwareCloudResource(resource, operation);
    } catch (GoogleJsonResponseException e) {
        exceptionHandler(e, resource.getName(), resourceType());
        return null;
    }
}
Also used : Delete(com.google.api.services.compute.Compute.RegionBackendServices.Delete) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Operation(com.google.api.services.compute.model.Operation)

Example 27 with Operation

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

the class GcpInstanceResourceBuilder method stopStart.

private CloudVmInstanceStatus stopStart(GcpContext context, AuthenticatedContext auth, CloudInstance instance, boolean stopRequest) {
    String projectId = gcpStackUtil.getProjectId(auth.getCloudCredential());
    String availabilityZone = instance.getAvailabilityZone();
    Compute compute = context.getCompute();
    String instanceId = instance.getInstanceId();
    try {
        LOGGER.info("Gcp operations are preparing: instanceId: {}, projectId: {}, availabilityZone: {}", instanceId, projectId, availabilityZone);
        Get get = compute.instances().get(projectId, availabilityZone, instanceId);
        Instance instanceResponse = get.execute();
        Operation operation;
        if (stopRequest) {
            LOGGER.info("Stop operation executed");
            operation = compute.instances().stop(projectId, availabilityZone, instanceId).setPrettyPrint(true).execute();
        } else {
            LOGGER.info("Start operation executed");
            operation = executeStartOperation(projectId, availabilityZone, compute, instanceId, instance.getTemplate(), instanceResponse.getDisks());
        }
        CloudInstance operationAwareCloudInstance = createOperationAwareCloudInstance(instance, operation);
        return new CloudVmInstanceStatus(operationAwareCloudInstance, InstanceStatus.IN_PROGRESS);
    } catch (TokenResponseException e) {
        throw gcpStackUtil.getMissingServiceAccountKeyError(e, context.getProjectId());
    } catch (IOException e) {
        throw new GcpResourceException(String.format("An error occurred while stopping the vm '%s'", instanceId), e);
    }
}
Also used : CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.google.api.services.compute.model.Instance) Compute(com.google.api.services.compute.Compute) Get(com.google.api.services.compute.Compute.Instances.Get) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) Operation(com.google.api.services.compute.model.Operation) IOException(java.io.IOException) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException)

Example 28 with Operation

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

the class GcpDiskResourceBuilderTest method setUp.

@BeforeEach
void setUp() 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");
    Location location = Location.location(Region.region("region"), AvailabilityZone.availabilityZone("az"));
    String projectId = "projectId";
    String serviceAccountId = "serviceAccountId";
    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";
    String flavor = "m1.medium";
    instanceId = "SOME_ID";
    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");
    auth = new AuthenticatedContext(cloudContext, cloudCredential);
    Map<String, Object> params = Map.of();
    List<Volume> 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 = new InstanceAuthentication("sshkey", "", "cloudbreak");
    instanceTemplate = new InstanceTemplate(flavor, name, privateId, volumes, InstanceStatus.CREATE_REQUESTED, params, 0L, "cb-centos66-amb200-2015-05-25", TemporaryStorage.ATTACHED_VOLUMES, 0L);
    group = createGroup(50);
    buildableResource = List.of(CloudResource.builder().type(ResourceType.GCP_DISK).status(CommonStatus.REQUESTED).name("disk").params(Map.of()).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);
    Operation 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 : InstanceGroupType(com.sequenceiq.common.api.type.InstanceGroupType) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) SecurityRule(com.sequenceiq.cloudbreak.cloud.model.SecurityRule) Operation(com.google.api.services.compute.model.Operation) Security(com.sequenceiq.cloudbreak.cloud.model.Security) Image(com.sequenceiq.cloudbreak.cloud.model.Image) 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) Location(com.sequenceiq.cloudbreak.cloud.model.Location) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 29 with Operation

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

the class GcpInstanceStateCheckerTest method checkBasedOnOperationWhenGetOperationSucceedsAndFinishedButTheOperationTypeIsNotSupported.

@Test
void checkBasedOnOperationWhenGetOperationSucceedsAndFinishedButTheOperationTypeIsNotSupported() throws Exception {
    stubGcpContext();
    CloudInstance cloudInstance = getCloudInstanceWithOperation(AVAILABILITY_ZONE, OPERATION_ID);
    Compute.ZoneOperations.Get get = mock(Compute.ZoneOperations.Get.class);
    Operation operation = getOperation(OPERATION_ID, "DONE", "UNKNOWN");
    when(get.execute()).thenReturn(operation);
    when(gcpStackUtil.zoneOperation(gcpCompute, PROJECT_ID, OPERATION_ID, AVAILABILITY_ZONE)).thenReturn(get);
    when(gcpStackUtil.isOperationFinished(operation)).thenReturn(Boolean.TRUE);
    Assertions.assertThrows(GcpResourceException.class, () -> underTest.checkBasedOnOperation(gcpContext, List.of(cloudInstance)));
    verify(gcpStackUtil).zoneOperation(gcpCompute, PROJECT_ID, OPERATION_ID, AVAILABILITY_ZONE);
}
Also used : CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Operation(com.google.api.services.compute.model.Operation) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with Operation

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

the class GcpInstanceStateCheckerTest method checkBasedOnOperationWhenGetOperationReturns.

@ParameterizedTest(name = "checkBasedOnOperationWhenGetOperationReturns with operation status: {0}, type: {1} then the expected instance status is: {3}")
@MethodSource("checkBasedOnOperationWhenGetOperationReturnsData")
void checkBasedOnOperationWhenGetOperationReturns(String operationStatus, String operationType, boolean operationFinished, InstanceStatus expectedInstanceStatus) throws Exception {
    stubGcpContext();
    CloudInstance cloudInstance = getCloudInstanceWithOperation(AVAILABILITY_ZONE, OPERATION_ID);
    Compute.ZoneOperations.Get get = mock(Compute.ZoneOperations.Get.class);
    Operation operation = getOperation(OPERATION_ID, operationStatus, operationType);
    when(get.execute()).thenReturn(operation);
    when(gcpStackUtil.zoneOperation(gcpCompute, PROJECT_ID, OPERATION_ID, AVAILABILITY_ZONE)).thenReturn(get);
    when(gcpStackUtil.isOperationFinished(operation)).thenReturn(operationFinished);
    List<CloudVmInstanceStatus> vmStatuses = underTest.checkBasedOnOperation(gcpContext, List.of(cloudInstance));
    verify(gcpStackUtil).zoneOperation(gcpCompute, PROJECT_ID, OPERATION_ID, AVAILABILITY_ZONE);
    assertFalse(vmStatuses.isEmpty());
    assertTrue(vmStatuses.stream().allMatch(vmStatus -> expectedInstanceStatus.equals(vmStatus.getStatus())));
}
Also used : AbstractGcpComputeBaseResourceChecker(com.sequenceiq.cloudbreak.cloud.gcp.service.checker.AbstractGcpComputeBaseResourceChecker) Instance(com.google.api.services.compute.model.Instance) Mock(org.mockito.Mock) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) GcpContext(com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) MethodSource(org.junit.jupiter.params.provider.MethodSource) GcpStackUtil(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil) InjectMocks(org.mockito.InjectMocks) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) HttpHeaders(com.google.api.client.http.HttpHeaders) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) HttpResponseException(com.google.api.client.http.HttpResponseException) Operation(com.google.api.services.compute.model.Operation) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) HttpStatusCodes(com.google.api.client.http.HttpStatusCodes) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions(org.junit.jupiter.api.Assertions) NotNull(org.jetbrains.annotations.NotNull) Compute(com.google.api.services.compute.Compute) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) Mockito.mock(org.mockito.Mockito.mock) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Operation(com.google.api.services.compute.model.Operation) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Operation (com.google.api.services.compute.model.Operation)94 Compute (com.google.api.services.compute.Compute)38 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)37 IOException (java.io.IOException)35 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 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)15 Instance (com.google.api.services.compute.model.Instance)14 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