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