Search in sources :

Example 36 with Compute

use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.

the class GcpReservedIpResourceBuilder method delete.

@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource) throws Exception {
    Compute compute = context.getCompute();
    String projectId = context.getProjectId();
    String region = context.getLocation().getRegion().value();
    try {
        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 37 with Compute

use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.

the class GcpContextBuilder method contextInit.

@Override
public GcpContext contextInit(CloudContext context, AuthenticatedContext auth, Network network, List<CloudResource> resources, boolean build) {
    CloudCredential credential = auth.getCloudCredential();
    String projectId = GcpStackUtil.getProjectId(credential);
    String serviceAccountId = GcpStackUtil.getServiceAccountId(credential);
    Compute compute = GcpStackUtil.buildCompute(credential);
    Location location = context.getLocation();
    boolean noPublicIp = network != null ? GcpStackUtil.noPublicIp(network) : false;
    return new GcpContext(context.getName(), location, projectId, serviceAccountId, compute, noPublicIp, PARALLEL_RESOURCE_REQUEST, build);
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Compute(com.google.api.services.compute.Compute) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

Example 38 with Compute

use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.

the class GcpFirewallInResourceBuilder method update.

@Override
public CloudResourceStatus update(GcpContext context, AuthenticatedContext auth, Group group, Network network, Security security, CloudResource resource) {
    String projectId = context.getProjectId();
    Compute compute = context.getCompute();
    String resourceName = resource.getName();
    try {
        Firewall fireWall = compute.firewalls().get(projectId, resourceName).execute();
        List<String> sourceRanges = getSourceRanges(security);
        fireWall.setSourceRanges(sourceRanges);
        Operation operation = compute.firewalls().update(projectId, resourceName, fireWall).execute();
        CloudResource cloudResource = createOperationAwareCloudResource(resource, operation);
        return checkResources(context, auth, Collections.singletonList(cloudResource)).get(0);
    } catch (IOException e) {
        throw new GcpResourceException("Failed to update resource!", GCP_FIREWALL_IN, resourceName, e);
    }
}
Also used : Compute(com.google.api.services.compute.Compute) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) Operation(com.google.api.services.compute.model.Operation) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) IOException(java.io.IOException) Firewall(com.google.api.services.compute.model.Firewall)

Example 39 with Compute

use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.

the class GcpDeleteVpcTest method deleteNetwork.

@AfterSuite
@Parameters("vpcName")
public void deleteNetwork(@Optional("it-vpc") String vpcName) throws Exception {
    springTestContextPrepareTestInstance();
    String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, defaultP12File);
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), new ByteArrayInputStream(Base64.decodeBase64(serviceAccountPrivateKey)), "notasecret", "privatekey", "notasecret");
    JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    GoogleCredential googleCredential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(defaultServiceAccountId).setServiceAccountScopes(Collections.singletonList(ComputeScopes.COMPUTE)).setServiceAccountPrivateKey(privateKey).build();
    Compute compute = new Builder(httpTransport, jsonFactory, null).setApplicationName(defaultName).setHttpRequestInitializer(googleCredential).build();
    Delete delete = compute.networks().delete(defaultProjectId, vpcName);
    Operation operation = delete.execute();
    if (operation.getHttpErrorStatusCode() != null) {
        throw new IllegalStateException("gcp operation failed: " + operation.getHttpErrorMessage());
    }
}
Also used : Delete(com.google.api.services.compute.Compute.Networks.Delete) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) PrivateKey(java.security.PrivateKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Builder(com.google.api.services.compute.Compute.Builder) Compute(com.google.api.services.compute.Compute) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) Operation(com.google.api.services.compute.model.Operation) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) Parameters(org.testng.annotations.Parameters) AfterSuite(org.testng.annotations.AfterSuite)

Example 40 with Compute

use of com.google.api.services.compute.Compute in project druid by druid-io.

the class GceAutoScaler method idToIpLookup.

/**
 * Converts the IDs to IPs - this is actually never called from the outside but it is called once
 * from inside the class if terminate is used instead of terminateWithIds
 */
@Override
public List<String> idToIpLookup(List<String> nodeIds) {
    log.info("Asked IDs -> IPs for: [%s]", String.join(",", nodeIds));
    if (nodeIds.isEmpty()) {
        return new ArrayList<>();
    }
    final String project = envConfig.getProjectId();
    final String zone = envConfig.getZoneName();
    try {
        Compute computeService = createComputeService();
        Compute.Instances.List request = computeService.instances().list(project, zone);
        request.setFilter(GceUtils.buildFilter(nodeIds, "name"));
        List<String> instanceIps = new ArrayList<>();
        InstanceList response;
        do {
            response = request.execute();
            if (response.getItems() == null) {
                continue;
            }
            for (Instance instance : response.getItems()) {
                // Assuming that every server has at least one network interface...
                String ip = instance.getNetworkInterfaces().get(0).getNetworkIP();
                // it for maxScalingDuration time before doing anything else
                if (ip != null && !"null".equals(ip)) {
                    instanceIps.add(ip);
                } else {
                    // log and skip it
                    log.warn("Call returned null IP for %s, skipping", instance.getName());
                }
            }
            request.setPageToken(response.getNextPageToken());
        } while (response.getNextPageToken() != null);
        return instanceIps;
    } catch (Exception e) {
        log.error(e, "Unable to convert IDs to IPs.");
    }
    return new ArrayList<>();
}
Also used : Instance(com.google.api.services.compute.model.Instance) ManagedInstance(com.google.api.services.compute.model.ManagedInstance) Compute(com.google.api.services.compute.Compute) ArrayList(java.util.ArrayList) InstanceList(com.google.api.services.compute.model.InstanceList) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Aggregations

Compute (com.google.api.services.compute.Compute)43 IOException (java.io.IOException)26 Operation (com.google.api.services.compute.model.Operation)16 ArrayList (java.util.ArrayList)13 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)12 HashMap (java.util.HashMap)9 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)8 GeneralSecurityException (java.security.GeneralSecurityException)8 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)7 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)7 Instance (com.google.api.services.compute.model.Instance)6 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)6 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)6 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)4 HttpRequest (com.google.api.client.http.HttpRequest)4 HttpTransport (com.google.api.client.http.HttpTransport)4 AttachedDisk (com.google.api.services.compute.model.AttachedDisk)4 ManagedInstance (com.google.api.services.compute.model.ManagedInstance)4 NetworkInterface (com.google.api.services.compute.model.NetworkInterface)4 Subnetwork (com.google.api.services.compute.model.Subnetwork)4