Search in sources :

Example 61 with Operation

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

the class GcpInstanceStateCheckerTest method checkBasedOnOperationWhenGetOperationFailsWithNotFoundGoogleJsonExceptionAndInstanceCouldBeGet.

@ParameterizedTest(name = "checkBasedOnOperation when operation get fails with {0} status code and GCP instance status: {1} " + "and expected instance status: {2} ")
@MethodSource("checkBasedOnOperationWhenGetOperationFailsWithNotFoundGoogleJsonExceptionData")
void checkBasedOnOperationWhenGetOperationFailsWithNotFoundGoogleJsonExceptionAndInstanceCouldBeGet(int operationErrorStatusCode, String gcpInstanceStatus, InstanceStatus expectedInstanceStatus) throws IOException {
    stubGcpContext();
    String instanceId = "instanceId";
    CloudInstance cloudInstance = getCloudInstanceWithOperation(AVAILABILITY_ZONE, OPERATION_ID, instanceId);
    HttpResponseException.Builder builder = new HttpResponseException.Builder(HttpStatusCodes.STATUS_CODE_BAD_GATEWAY, "Bad gateway: internal server error", new HttpHeaders());
    GoogleJsonError googleErrorDetail = new GoogleJsonError();
    googleErrorDetail.setCode(operationErrorStatusCode);
    GoogleJsonResponseException googleJsonResponseException = new GoogleJsonResponseException(builder, googleErrorDetail);
    when(gcpStackUtil.zoneOperation(gcpCompute, PROJECT_ID, OPERATION_ID, AVAILABILITY_ZONE)).thenThrow(googleJsonResponseException);
    Instance instance = new Instance();
    instance.setStatus(gcpInstanceStatus);
    when(gcpStackUtil.getComputeInstanceWithId(gcpCompute, PROJECT_ID, AVAILABILITY_ZONE, instanceId)).thenReturn(instance);
    List<CloudVmInstanceStatus> vmStatuses = underTest.checkBasedOnOperation(gcpContext, List.of(cloudInstance));
    verify(gcpStackUtil).zoneOperation(gcpCompute, PROJECT_ID, OPERATION_ID, AVAILABILITY_ZONE);
    verify(gcpStackUtil).getComputeInstanceWithId(gcpCompute, PROJECT_ID, AVAILABILITY_ZONE, instanceId);
    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) HttpHeaders(com.google.api.client.http.HttpHeaders) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Instance(com.google.api.services.compute.model.Instance) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) HttpResponseException(com.google.api.client.http.HttpResponseException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 62 with Operation

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

the class GcpInstanceStateCheckerTest method getOperation.

@NotNull
private Operation getOperation(String anOperationIdentifier, String status, String operationType) {
    Operation operation = new Operation();
    operation.setName(anOperationIdentifier);
    operation.setStatus(status);
    operation.setOperationType(operationType);
    return operation;
}
Also used : Operation(com.google.api.services.compute.model.Operation) NotNull(org.jetbrains.annotations.NotNull)

Example 63 with Operation

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

the class GcpAttachedDiskResourceBuilder method delete.

@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource) throws Exception {
    VolumeSetAttributes volumeSetAttributes = resource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class);
    if (!volumeSetAttributes.getDeleteOnTermination()) {
        resource.setStatus(CommonStatus.DETACHED);
        volumeSetAttributes.setDeleteOnTermination(Boolean.TRUE);
        resource.putParameter(CloudResource.ATTRIBUTES, volumeSetAttributes);
        resourceNotifier.notifyUpdate(resource, auth.getCloudContext());
        throw new InterruptedException("Resource will be preserved for later reattachment.");
    }
    List<String> operations = new ArrayList<>();
    List<String> syncedOperations = Collections.synchronizedList(operations);
    Collection<Future<Void>> futures = new ArrayList<>();
    for (VolumeSetAttributes.Volume volume : volumeSetAttributes.getVolumes()) {
        Future<Void> submit = intermediateBuilderExecutor.submit(() -> {
            try {
                LOGGER.info("Going to delete volume [id: {}] from project [id: {}] in the following availability zone: {}", volume.getId(), context.getProjectId(), volumeSetAttributes.getAvailabilityZone());
                Operation operation = context.getCompute().disks().delete(context.getProjectId(), volumeSetAttributes.getAvailabilityZone(), volume.getId()).execute();
                syncedOperations.add(operation.getName());
                if (operation.getHttpErrorStatusCode() != null) {
                    String message = String.format("%s [code: %d, message: %s, error: %s]", VOLUME_DELETION_FAILED_BASE_MSG, operation.getHttpErrorStatusCode(), operation.getHttpErrorMessage(), operation.getError());
                    LOGGER.warn(message);
                    throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), volume.getId());
                }
            } catch (TokenResponseException e) {
                logVolumeDeletionProblem(e);
                gcpStackUtil.getMissingServiceAccountKeyError(e, context.getProjectId());
            } catch (GoogleJsonResponseException e) {
                logVolumeDeletionProblem(e);
                exceptionHandler(e, resource.getName(), resourceType());
            } catch (IOException e) {
                logVolumeDeletionProblem(e);
                throw new GcpResourceException(e.getMessage(), e);
            }
            return null;
        });
        futures.add(submit);
    }
    for (Future<Void> future : futures) {
        future.get();
    }
    resource.putParameter(OPERATION_ID, operations);
    return resource;
}
Also used : ArrayList(java.util.ArrayList) Operation(com.google.api.services.compute.model.Operation) IOException(java.io.IOException) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Future(java.util.concurrent.Future) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException)

Example 64 with Operation

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

the class GcpDiskResourceBuilder method build.

@Override
public List<CloudResource> build(GcpContext context, CloudInstance instance, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> buildableResources, CloudStack cloudStack) throws Exception {
    String projectId = context.getProjectId();
    String location = getLocation(instance, context);
    Disk disk = new Disk();
    disk.setDescription(description());
    disk.setSizeGb((long) group.getRootVolumeSize());
    disk.setName(buildableResources.get(0).getName());
    disk.setType(GcpDiskType.SSD.getUrl(projectId, location));
    InstanceTemplate template = group.getReferenceInstanceTemplate();
    customGcpDiskEncryptionService.addEncryptionKeyToDisk(template, disk);
    Map<String, String> labels = gcpLabelUtil.createLabelsFromTags(cloudStack);
    disk.setLabels(labels);
    Insert insDisk = context.getCompute().disks().insert(projectId, location, disk);
    insDisk.setSourceImage(gcpStackUtil.getCDPImage(projectId, cloudStack.getImage().getImageName()));
    try {
        Operation operation = insDisk.execute();
        if (operation.getHttpErrorStatusCode() != null) {
            throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), buildableResources.get(0).getName());
        }
        return Collections.singletonList(createOperationAwareCloudResource(buildableResources.get(0), operation));
    } catch (GoogleJsonResponseException e) {
        throw new GcpResourceException(checkException(e), resourceType(), buildableResources.get(0).getName());
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Disks.Insert) Disk(com.google.api.services.compute.model.Disk) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 65 with Operation

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

the class GcpDiskResourceBuilder method delete.

@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource) throws Exception {
    String resourceName = resource.getName();
    VolumeSetAttributes volumeSetAttributes = resource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class);
    String zone;
    if (volumeSetAttributes != null && volumeSetAttributes.getAvailabilityZone() != null) {
        zone = volumeSetAttributes.getAvailabilityZone();
    } else {
        zone = context.getLocation().getAvailabilityZone().value();
    }
    try {
        LOGGER.info("Creating operation to delete disk [name: {}] in project [id: {}] in the following availability zone: {}", resourceName, context.getProjectId(), zone);
        Operation operation = context.getCompute().disks().delete(context.getProjectId(), zone, resourceName).execute();
        return createOperationAwareCloudResource(resource, operation);
    } catch (GoogleJsonResponseException e) {
        exceptionHandler(e, resourceName, resourceType());
    }
    return null;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Operation(com.google.api.services.compute.model.Operation) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes)

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