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