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<>();
}
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;
}
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);
}
Aggregations