Search in sources :

Example 21 with Operation

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());
}
Also used : GcpContext(com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext) Network(com.sequenceiq.cloudbreak.cloud.model.Network) Compute(com.google.api.services.compute.Compute) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Operation(com.google.api.services.compute.model.Operation) Test(org.junit.jupiter.api.Test)

Example 22 with Operation

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;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Compute(com.google.api.services.compute.Compute) Operation(com.google.api.services.compute.model.Operation)

Example 23 with Operation

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();
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Subnetwork(com.google.api.services.compute.model.Subnetwork) Compute(com.google.api.services.compute.Compute) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Subnetworks.Insert)

Example 24 with Operation

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;
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Compute(com.google.api.services.compute.Compute) Operation(com.google.api.services.compute.model.Operation)

Example 25 with Operation

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;
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Operation(com.google.api.services.compute.model.Operation)

Aggregations

Operation (com.google.api.services.compute.model.Operation)94 Compute (com.google.api.services.compute.Compute)38 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)37 IOException (java.io.IOException)35 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 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)15 Instance (com.google.api.services.compute.model.Instance)14 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