Search in sources :

Example 41 with Compute

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

the class GceAutoScaler method ipToIdLookup.

/**
 * Converts the IPs to IDs
 */
@Override
public List<String> ipToIdLookup(List<String> ips) {
    log.info("Asked IPs -> IDs for: [%s]", String.join(",", ips));
    if (ips.isEmpty()) {
        return new ArrayList<>();
    }
    // actually IPs and can send IDs to this function instead
    if (!InetAddresses.isInetAddress(ips.get(0))) {
        log.debug("Not IPs, doing nothing");
        return ips;
    }
    final String project = envConfig.getProjectId();
    final String zone = envConfig.getZoneName();
    try {
        Compute computeService = createComputeService();
        Compute.Instances.List request = computeService.instances().list(project, zone);
        // Cannot filter by IP atm, see below
        // request.setFilter(GceUtils.buildFilter(ips, "networkInterfaces[0].networkIP"));
        List<String> instanceIds = new ArrayList<>();
        InstanceList response;
        do {
            response = request.execute();
            if (response.getItems() == null) {
                continue;
            }
            for (Instance instance : response.getItems()) {
                // by IP, see https://issuetracker.google.com/issues/73455339
                for (NetworkInterface ni : instance.getNetworkInterfaces()) {
                    if (ips.contains(ni.getNetworkIP())) {
                        instanceIds.add(instance.getName());
                    }
                }
            }
            request.setPageToken(response.getNextPageToken());
        } while (response.getNextPageToken() != null);
        log.debug("Converted to [%s]", String.join(",", instanceIds));
        return instanceIds;
    } catch (Exception e) {
        log.error(e, "Unable to convert IPs to IDs.");
    }
    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) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) InstanceList(com.google.api.services.compute.model.InstanceList) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 42 with Compute

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

the class GceAutoScaler method getRunningInstances.

// Returns the list of the IDs of the machines running in the MIG
private List<String> getRunningInstances() {
    // 500 is sadly the max, see below
    final long maxResults = 500L;
    ArrayList<String> ids = new ArrayList<>();
    try {
        final String project = envConfig.getProjectId();
        final String zone = envConfig.getZoneName();
        final String managedInstanceGroupName = envConfig.getManagedInstanceGroupName();
        Compute computeService = createComputeService();
        Compute.InstanceGroupManagers.ListManagedInstances request = computeService.instanceGroupManagers().listManagedInstances(project, zone, managedInstanceGroupName);
        // Notice that while the doc says otherwise, there is not nextPageToken to page
        // through results and so everything needs to be in the same page
        request.setMaxResults(maxResults);
        InstanceGroupManagersListManagedInstancesResponse response = request.execute();
        for (ManagedInstance mi : response.getManagedInstances()) {
            ids.add(GceUtils.extractNameFromInstance(mi.getInstance()));
        }
        log.debug("Found running instances [%s]", String.join(",", ids));
    } catch (Exception e) {
        log.error(e, "Unable to get instances.");
    }
    return ids;
}
Also used : Compute(com.google.api.services.compute.Compute) ArrayList(java.util.ArrayList) ManagedInstance(com.google.api.services.compute.model.ManagedInstance) InstanceGroupManagersListManagedInstancesResponse(com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 43 with Compute

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

the class GoogleComputeClientFactory method getComputeClient.

public GoogleComputeClient getComputeClient(GoogleCloud cloud) throws OpsException {
    KeyParser parser = new KeyParser();
    Object parsed = parser.parse(cloud.serviceAccountKey.plaintext());
    PrivateKey privateKey;
    if (parsed == null) {
        throw new OpsException("Cannot parse private key");
    } else if (parsed instanceof PrivateKey) {
        privateKey = (PrivateKey) parsed;
    } else {
        throw new OpsException("Expected private key, found: " + parsed.getClass().getSimpleName());
    }
    // Build service account credential.
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY).setServiceAccountId(cloud.serviceAccountId).setServiceAccountScopes(ComputeScopes.COMPUTE).setServiceAccountPrivateKey(privateKey).build();
    Compute compute = new Compute(HTTP_TRANSPORT, JSON_FACTORY, credential);
    return new GoogleComputeClient(platformLayerClient, compute, cloud.projectId);
}
Also used : OpsException(org.platformlayer.ops.OpsException) PrivateKey(java.security.PrivateKey) Compute(com.google.api.services.compute.Compute) KeyParser(org.platformlayer.crypto.KeyParser) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential)

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