use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.
the class GcpAttachedDiskResourceBuilder method checkResources.
@Override
public List<CloudResourceStatus> checkResources(GcpContext context, AuthenticatedContext auth, List<CloudResource> resources) {
List<CloudResourceStatus> result = new ArrayList<>();
for (CloudResource resource : resources) {
LOGGER.debug("Check {} resource: {}", resourceType(), resource);
List<String> operationIds = Optional.ofNullable(resource.getParameter(OPERATION_ID, List.class)).orElse(List.of());
boolean finished = operationIds.isEmpty() || operationIds.stream().allMatch(operationId -> {
try {
Operation operation = getResourceChecker().check(context, operationId, resources);
return operation == null || gcpStackUtil.isOperationFinished(operation);
} catch (Exception e) {
CloudContext cloudContext = auth.getCloudContext();
throw new GcpResourceException("Error during status check", resourceType(), cloudContext.getName(), cloudContext.getId(), resource.getName(), e);
}
});
ResourceStatus successStatus = context.isBuild() ? ResourceStatus.CREATED : ResourceStatus.DELETED;
result.add(new CloudResourceStatus(resource, finished ? successStatus : ResourceStatus.IN_PROGRESS));
if (finished) {
if (successStatus == ResourceStatus.CREATED) {
LOGGER.debug("Creation of {} was successful", resource);
} else {
LOGGER.debug("Deletion of {} was successful", resource);
}
}
}
return result;
}
use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.
the class GcpAttachedDiskResourceBuilder method build.
@Override
public List<CloudResource> build(GcpContext context, CloudInstance instance, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> resources, CloudStack cloudStack) throws Exception {
InstanceTemplate template = group.getReferenceInstanceTemplate();
List<String> operations = new ArrayList<>();
List<String> syncedOperations = Collections.synchronizedList(operations);
String projectId = context.getProjectId();
Compute compute = context.getCompute();
Collection<Future<Void>> futures = new ArrayList<>();
List<CloudResource> buildableResource = resources.stream().filter(cloudResource -> CommonStatus.REQUESTED.equals(cloudResource.getStatus())).collect(Collectors.toList());
List<CloudResource> result = new ArrayList<>();
for (CloudResource volumeSetResource : buildableResource) {
VolumeSetAttributes volumeSetAttributes = volumeSetResource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class);
for (VolumeSetAttributes.Volume volume : volumeSetAttributes.getVolumes()) {
Map<String, String> labels = gcpLabelUtil.createLabelsFromTags(cloudStack);
Disk disk = createDisk(projectId, volume, labels, volumeSetAttributes);
customGcpDiskEncryptionService.addEncryptionKeyToDisk(template, disk);
Future<Void> submit = intermediateBuilderExecutor.submit(() -> {
Insert insDisk = compute.disks().insert(projectId, volumeSetAttributes.getAvailabilityZone(), disk);
try {
Operation operation = insDisk.execute();
syncedOperations.add(operation.getName());
if (operation.getHttpErrorStatusCode() != null) {
throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), disk.getName());
}
} catch (TokenResponseException e) {
throw gcpStackUtil.getMissingServiceAccountKeyError(e, projectId);
} catch (GoogleJsonResponseException e) {
throw new GcpResourceException(checkException(e), resourceType(), disk.getName());
}
return null;
});
futures.add(submit);
}
volumeSetResource.putParameter(OPERATION_ID, operations);
result.add(new Builder().cloudResource(volumeSetResource).status(CommonStatus.CREATED).params(volumeSetResource.getParameters()).build());
}
for (Future<Void> future : futures) {
future.get();
}
result.addAll(resources.stream().filter(cloudResource -> CommonStatus.CREATED.equals(cloudResource.getStatus())).collect(Collectors.toList()));
return result;
}
use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.
the class GcpFirewallInResourceBuilderTest method testDeleteWhenEverythingGoesFine.
@Test
public void testDeleteWhenEverythingGoesFine() throws Exception {
CloudResource resource = new CloudResource.Builder().type(ResourceType.GCP_FIREWALL_IN).status(CommonStatus.CREATED).group("master").name("super").instanceId("id-123").params(new HashMap<>()).persistent(true).build();
GcpContext gcpContext = mock(GcpContext.class);
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
Network network = mock(Network.class);
Compute compute = mock(Compute.class);
Compute.Firewalls firewalls = mock(Compute.Firewalls.class);
Compute.Firewalls.Delete firewallsDelete = mock(Compute.Firewalls.Delete.class);
Operation operation = mock(Operation.class);
when(gcpContext.getCompute()).thenReturn(compute);
when(gcpContext.getProjectId()).thenReturn("id");
when(compute.firewalls()).thenReturn(firewalls);
when(firewalls.delete(anyString(), anyString())).thenReturn(firewallsDelete);
when(firewallsDelete.execute()).thenReturn(operation);
when(operation.getName()).thenReturn("name");
CloudResource delete = underTest.delete(gcpContext, authenticatedContext, resource, network);
Assert.assertEquals(ResourceType.GCP_FIREWALL_IN, delete.getType());
Assert.assertEquals(CommonStatus.CREATED, delete.getStatus());
Assert.assertEquals("super", delete.getName());
Assert.assertEquals("master", delete.getGroup());
Assert.assertEquals("id-123", delete.getInstanceId());
}
use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.
the class GcpFirewallInternalResourceBuilderTest method testDeleteWhenEverythingGoesFine.
@Test
public void testDeleteWhenEverythingGoesFine() throws Exception {
CloudResource resource = new CloudResource.Builder().type(ResourceType.GCP_FIREWALL_INTERNAL).status(CommonStatus.CREATED).group("master").name("super").instanceId("id-123").params(new HashMap<>()).persistent(true).build();
GcpContext gcpContext = mock(GcpContext.class);
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
Network network = mock(Network.class);
Compute compute = mock(Compute.class);
Compute.Firewalls firewalls = mock(Compute.Firewalls.class);
Compute.Firewalls.Delete firewallsDelete = mock(Compute.Firewalls.Delete.class);
Operation operation = mock(Operation.class);
when(gcpContext.getCompute()).thenReturn(compute);
when(gcpContext.getProjectId()).thenReturn("id");
when(compute.firewalls()).thenReturn(firewalls);
when(firewalls.delete(anyString(), anyString())).thenReturn(firewallsDelete);
when(firewallsDelete.execute()).thenReturn(operation);
when(operation.getName()).thenReturn("name");
CloudResource delete = underTest.delete(gcpContext, authenticatedContext, resource, network);
Assert.assertEquals(ResourceType.GCP_FIREWALL_INTERNAL, delete.getType());
Assert.assertEquals(CommonStatus.CREATED, delete.getStatus());
Assert.assertEquals("super", delete.getName());
Assert.assertEquals("master", delete.getGroup());
Assert.assertEquals("id-123", delete.getInstanceId());
}
use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.
the class GcpSubnetResourceBuilderTest method testDeleteWhenEverythingGoesFine.
@Test
public void testDeleteWhenEverythingGoesFine() throws Exception {
CloudResource resource = new CloudResource.Builder().type(ResourceType.GCP_NETWORK).status(CommonStatus.CREATED).group("master").name("super").instanceId("id-123").params(new HashMap<>()).persistent(true).build();
GcpContext gcpContext = mock(GcpContext.class);
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
Network network = mock(Network.class);
Compute compute = mock(Compute.class);
Location location = mock(Location.class);
Compute.Subnetworks networks = mock(Compute.Subnetworks.class);
Compute.Subnetworks.Delete networksDelete = mock(Compute.Subnetworks.Delete.class);
Operation operation = mock(Operation.class);
when(gcpStackUtil.isNewNetworkAndSubnet(network)).thenReturn(true);
when(gcpContext.getLocation()).thenReturn(location);
when(location.getRegion()).thenReturn(Region.region("us-west-1"));
when(gcpContext.getCompute()).thenReturn(compute);
when(gcpContext.getProjectId()).thenReturn("id");
when(compute.subnetworks()).thenReturn(networks);
when(networks.delete(anyString(), anyString(), anyString())).thenReturn(networksDelete);
when(networksDelete.execute()).thenReturn(operation);
when(operation.getName()).thenReturn("name");
CloudResource delete = underTest.delete(gcpContext, authenticatedContext, resource, network);
Assert.assertEquals(ResourceType.GCP_NETWORK, delete.getType());
Assert.assertEquals(CommonStatus.CREATED, delete.getStatus());
Assert.assertEquals("super", delete.getName());
Assert.assertEquals("master", delete.getGroup());
Assert.assertEquals("id-123", delete.getInstanceId());
}
Aggregations