Search in sources :

Example 11 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException 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 (!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 12 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException 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 (isNewNetworkAndSubnet(network) || isNewSubnetInExistingNetwork(network)) {
        Compute compute = context.getCompute();
        String projectId = context.getProjectId();
        Subnetwork gcpSubnet = new Subnetwork();
        gcpSubnet.setName(resource.getName());
        gcpSubnet.setIpCidrRange(network.getSubnet().getCidr());
        String networkName = context.getStringParameter(GcpNetworkResourceBuilder.NETWORK_NAME);
        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 13 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project cloudbreak by hortonworks.

the class AbstractGcpResourceBuilder method check.

protected Operation check(GcpContext context, DynamicModel resource) throws IOException {
    String operation = resource.getStringParameter(OPERATION_ID);
    if (operation == null) {
        return null;
    }
    try {
        Operation execute = GcpStackUtil.globalOperations(context.getCompute(), context.getProjectId(), operation).execute();
        checkError(execute);
        return execute;
    } catch (GoogleJsonResponseException e) {
        if (e.getDetails().get("code").equals(HttpStatus.SC_NOT_FOUND) || e.getDetails().get("code").equals(HttpStatus.SC_FORBIDDEN)) {
            Location location = context.getLocation();
            try {
                Operation execute = GcpStackUtil.regionOperations(context.getCompute(), context.getProjectId(), operation, location.getRegion()).execute();
                checkError(execute);
                return execute;
            } catch (GoogleJsonResponseException e1) {
                if (e1.getDetails().get("code").equals(HttpStatus.SC_NOT_FOUND) || e1.getDetails().get("code").equals(HttpStatus.SC_FORBIDDEN)) {
                    Operation execute = GcpStackUtil.zoneOperations(context.getCompute(), context.getProjectId(), operation, location.getAvailabilityZone()).execute();
                    checkError(execute);
                    return execute;
                } else {
                    throw e1;
                }
            }
        } else {
            throw e;
        }
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Operation(com.google.api.services.compute.model.Operation) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

Example 14 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project cloudbreak by hortonworks.

the class GcpReservedIpResourceBuilder method build.

@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> tags) throws Exception {
    List<CloudResource> result = buildableResource;
    if (!buildableResource.isEmpty()) {
        CloudResource resource = buildableResource.get(0);
        String projectId = context.getProjectId();
        String region = context.getLocation().getRegion().value();
        Address address = new Address();
        address.setName(resource.getName());
        Map<String, String> customTags = new HashMap<>();
        customTags.putAll(tags);
        customTags.putAll(defaultCostTaggingService.prepareIpTagging());
        address.setLabels(customTags);
        Insert networkInsert = context.getCompute().addresses().insert(projectId, region, address);
        try {
            Operation operation = networkInsert.execute();
            if (operation.getHttpErrorStatusCode() != null) {
                throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), resource.getName());
            }
            result = Collections.singletonList(createOperationAwareCloudResource(resource, operation));
        } catch (GoogleJsonResponseException e) {
            throw new GcpResourceException(checkException(e), resourceType(), resource.getName());
        }
    }
    return result;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Address(com.google.api.services.compute.model.Address) HashMap(java.util.HashMap) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Addresses.Insert)

Example 15 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project cloudbreak by hortonworks.

the class GcpFirewallInResourceBuilder method build.

@Override
public CloudResource build(GcpContext context, AuthenticatedContext auth, Group group, Network network, Security security, CloudResource buildableResource) throws Exception {
    String projectId = context.getProjectId();
    ComputeRequest<Operation> firewallRequest;
    firewallRequest = StringUtils.isNotBlank(security.getCloudSecurityId()) && isExistingNetwork(network) ? updateExistingFirewallForNewTargets(context, auth, group, security) : createNewFirewallRule(context, auth, group, security, buildableResource, projectId);
    try {
        Operation operation = firewallRequest.execute();
        if (operation.getHttpErrorStatusCode() != null) {
            throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), buildableResource.getName());
        }
        return createOperationAwareCloudResource(buildableResource, operation);
    } catch (GoogleJsonResponseException e) {
        throw new GcpResourceException(checkException(e), resourceType(), buildableResource.getName());
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) Operation(com.google.api.services.compute.model.Operation)

Aggregations

GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)90 IOException (java.io.IOException)45 YouTube (com.google.api.services.youtube.YouTube)26 Credential (com.google.api.client.auth.oauth2.Credential)25 Operation (com.google.api.services.compute.model.Operation)12 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)12 Compute (com.google.api.services.compute.Compute)11 Storage (com.google.api.services.storage.Storage)10 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)8 GcsOptions (org.apache.beam.sdk.extensions.gcp.options.GcsOptions)7 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)5 InputStreamContent (com.google.api.client.http.InputStreamContent)5 BackOff (com.google.api.client.util.BackOff)5 HashMap (java.util.HashMap)5 Objects (com.google.api.services.storage.model.Objects)4 GoogleCloudStorage (com.google.cloud.hadoop.gcsio.GoogleCloudStorage)4 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)4 MediaHttpUploader (com.google.api.client.googleapis.media.MediaHttpUploader)3 MediaHttpUploaderProgressListener (com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener)3