use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.
the class GcpNetworkResourceBuilderTest 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);
Compute.Networks networks = mock(Compute.Networks.class);
Compute.Networks.Delete networksDelete = mock(Compute.Networks.Delete.class);
Operation operation = mock(Operation.class);
when(gcpStackUtil.isExistingNetwork(network)).thenReturn(false);
when(gcpContext.getCompute()).thenReturn(compute);
when(gcpContext.getProjectId()).thenReturn("id");
when(compute.networks()).thenReturn(networks);
when(networks.delete(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());
}
use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.
the class GcpNetworkResourceBuilder method delete.
@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource, Network network) throws Exception {
if (!gcpStackUtil.isExistingNetwork(network)) {
Compute compute = context.getCompute();
String projectId = context.getProjectId();
try {
Operation operation = compute.networks().delete(projectId, resource.getName()).execute();
return createOperationAwareCloudResource(resource, operation);
} catch (GoogleJsonResponseException e) {
exceptionHandler(e, resource.getName(), resourceType());
return null;
}
}
return null;
}
use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.
the class GcpSubnetResourceBuilder method build.
@Override
public CloudResource build(GcpContext context, AuthenticatedContext auth, Network network, Security security, CloudResource resource) throws Exception {
if (gcpStackUtil.isNewNetworkAndSubnet(network) || gcpStackUtil.isNewSubnetInExistingNetwork(network)) {
Compute compute = context.getCompute();
String projectId = context.getProjectId();
Subnetwork gcpSubnet = new Subnetwork();
gcpSubnet.setName(resource.getName());
gcpSubnet.setDescription(description());
gcpSubnet.setIpCidrRange(network.getSubnet().getCidr());
String networkName = context.getStringParameter(GcpNetworkResourceBuilder.NETWORK_NAME);
if (isNotEmpty(gcpStackUtil.getSharedProjectId(network))) {
gcpSubnet.setNetwork(String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", gcpStackUtil.getSharedProjectId(network), networkName));
} else {
gcpSubnet.setNetwork(String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", projectId, networkName));
}
Insert snInsert = compute.subnetworks().insert(projectId, auth.getCloudContext().getLocation().getRegion().value(), gcpSubnet);
try {
Operation operation = snInsert.execute();
if (operation.getHttpErrorStatusCode() != null) {
throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), resource.getName());
}
context.putParameter(SUBNET_NAME, resource.getName());
return createOperationAwareCloudResource(resource, operation);
} catch (GoogleJsonResponseException e) {
throw new GcpResourceException(checkException(e), resourceType(), resource.getName());
}
}
context.putParameter(SUBNET_NAME, resource.getName());
return new Builder().cloudResource(resource).persistent(false).build();
}
use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.
the class GcpReservedIpResourceBuilder method deleteReservedIP.
public CloudResource deleteReservedIP(GcpContext context, CloudResource resource) throws Exception {
Compute compute = context.getCompute();
String projectId = context.getProjectId();
String region = context.getLocation().getRegion().value();
try {
LOGGER.info("About to delete GCP reserved ip address in: [projectID: {}, region: {}, resource: {}]", projectId, region, resource.getName());
Operation operation = compute.addresses().delete(projectId, region, resource.getName()).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 GcpForwardingRuleResourceBuilder method delete.
@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource) throws Exception {
String regionName = context.getLocation().getRegion().getRegionName();
LOGGER.info("Deleting forwarding rule {} for {}", resource.getName(), context.getProjectId());
Compute.ForwardingRules.Delete delete = context.getCompute().forwardingRules().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;
}
}
Aggregations