Java Code Examples for com.google.api.services.compute.model.Operation#getHttpErrorStatusCode()

The following examples show how to use com.google.api.services.compute.model.Operation#getHttpErrorStatusCode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GcpNetworkResourceBuilder.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Override
public CloudResource build(GcpContext context, AuthenticatedContext auth, Network network, Security security, CloudResource resource) throws Exception {
    if (!isExistingNetwork(network)) {
        Compute compute = context.getCompute();
        String projectId = context.getProjectId();

        com.google.api.services.compute.model.Network gcpNetwork = new com.google.api.services.compute.model.Network();
        gcpNetwork.setName(resource.getName());
        gcpNetwork.setAutoCreateSubnetworks(false);
        Insert networkInsert = compute.networks().insert(projectId, gcpNetwork);
        try {
            Operation operation = networkInsert.execute();
            if (operation.getHttpErrorStatusCode() != null) {
                throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), resource.getName());
            }
            context.putParameter(NETWORK_NAME, resource.getName());
            return createOperationAwareCloudResource(resource, operation);
        } catch (GoogleJsonResponseException e) {
            throw new GcpResourceException(checkException(e), resourceType(), resource.getName());
        }
    }
    context.putParameter(NETWORK_NAME, resource.getName());
    return new Builder().cloudResource(resource).persistent(false).build();
}
 
Example 2
Source File: GcpFirewallInResourceBuilder.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@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 = StringUtils.isNotBlank(security.getCloudSecurityId()) && isExistingNetwork(network)
            ? updateExistingFirewallForNewTargets(context, auth, group, security)
            : createNewFirewallRule(context, auth, group, network, 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());
    }
}
 
Example 3
Source File: GcpStackUtil.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private static String checkForErrors(Operation operation) {
    if (operation == null) {
        LOGGER.info("Operation is null!");
        return null;
    }
    String msg = null;
    if (operation.getError() != null) {
        StringBuilder error = new StringBuilder();
        if (operation.getError().getErrors() != null) {
            for (Errors errors : operation.getError().getErrors()) {
                error.append(String.format("code: %s -> message: %s %s", errors.getCode(), errors.getMessage(), System.lineSeparator()));
            }
            msg = error.toString();
        } else {
            LOGGER.debug("No errors found, Error: {}", operation.getError());
        }
    }
    if (operation.getHttpErrorStatusCode() != null) {
        msg += String.format(" HTTP error message: %s, HTTP error status code: %s", operation.getHttpErrorMessage(), operation.getHttpErrorStatusCode());
    }
    return msg;
}
 
Example 4
Source File: GcpInstanceResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void verifyOperation(Operation operation, List<CloudResource> buildableResource) {
    if (operation.getHttpErrorStatusCode() != null) {
        throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), buildableResource.get(0).getName());
    }
    if (operation.getError() != null && !operation.getError().isEmpty()) {
        throw new GcpResourceException(operation.getError().getErrors().stream()
                .map(Operation.Error.Errors::getMessage).collect(Collectors.joining(",")), resourceType(), buildableResource.get(0).getName());
    }
}
 
Example 5
Source File: GcpDiskResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group,
        List<CloudResource> buildableResources, CloudStack cloudStack) throws Exception {
    String projectId = context.getProjectId();
    Location location = context.getLocation();

    Disk disk = new Disk();
    disk.setSizeGb((long) group.getRootVolumeSize());
    disk.setName(buildableResources.get(0).getName());
    disk.setKind(GcpDiskType.HDD.getUrl(projectId, location.getAvailabilityZone()));

    InstanceTemplate template = group.getReferenceInstanceConfiguration().getTemplate();
    gcpDiskEncryptionService.addEncryptionKeyToDisk(template, disk);

    Map<String, String> customTags = new HashMap<>();
    customTags.putAll(cloudStack.getTags());
    disk.setLabels(customTags);

    Insert insDisk = context.getCompute().disks().insert(projectId, location.getAvailabilityZone().value(), disk);
    insDisk.setSourceImage(GcpStackUtil.getAmbariImage(projectId, cloudStack.getImage().getImageName()));
    try {
        Operation operation = insDisk.execute();
        if (operation.getHttpErrorStatusCode() != null) {
            throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), buildableResources.get(0).getName());
        }
        return Collections.singletonList(createOperationAwareCloudResource(buildableResources.get(0), operation));
    } catch (GoogleJsonResponseException e) {
        throw new GcpResourceException(checkException(e), resourceType(), buildableResources.get(0).getName());
    }
}
 
Example 6
Source File: GcpReservedIpResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group,
        List<CloudResource> buildableResource, CloudStack cloudStack) 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, Object> customTags = new HashMap<>();
        customTags.putAll(cloudStack.getTags());
        address.setUnknownKeys(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;
}
 
Example 7
Source File: GcpSubnetResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@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);
        if (isNotEmpty(getSharedProjectId(network))) {
            gcpSubnet.setNetwork(String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s",
                    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();
}