use of com.google.api.services.compute.Compute.Instances in project photon-model by vmware.
the class GCPTestUtil method getStaleInstanceNames.
/**
* Get the names of all stale instances remaining from previous runs due to read timeout
* failures.
* @param compute The GCE client object.
* @param projectId The project id.
* @param zoneId The zone id.
* @return Names of stale instances
* @throws Throwable Exception during querying the instances
*/
public static List<String> getStaleInstanceNames(Compute compute, String projectId, String zoneId) throws Throwable {
Instances instanceList = compute.instances();
Instances.List list = instanceList.list(projectId, zoneId);
InstanceList ins = list.execute();
List<Instance> instances = ins.getItems();
List<String> names = new ArrayList<String>();
if (instances == null) {
return null;
}
SimpleDateFormat dateFormat = new SimpleDateFormat(GCPConstants.VM_CREATION_TIMESTAMP_FORMAT);
dateFormat.setTimeZone(TimeZone.getTimeZone(GCPConstants.UTC_TIMEZONE_ID));
for (Instance i : instances) {
Date date = dateFormat.parse(i.getCreationTimestamp());
long time = TimeUnit.MILLISECONDS.toMicros(date.getTime());
if (Utils.getNowMicrosUtc() - time > ONE_HOUR_DIFFERENCE_MICROS && i.getName().startsWith("adapter-test-instance")) {
names.add(i.getName());
}
}
return names;
}
use of com.google.api.services.compute.Compute.Instances in project cloudbreak by hortonworks.
the class TagsUtil method checkTagsGcp.
protected static void checkTagsGcp(ApplicationContext applicationContext, String applicationName, String projectId, String serviceAccountId, String p12File, String availabilityZone, Iterable<String> instanceIdList, Map<String, String> tagsToCheckMap) throws Exception {
String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, p12File);
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(serviceAccountId).setServiceAccountScopes(Collections.singletonList(ComputeScopes.COMPUTE)).setServiceAccountPrivateKey(privateKey).build();
Compute compute = new Builder(httpTransport, jsonFactory, null).setApplicationName(applicationName).setHttpRequestInitializer(googleCredential).build();
Instances instances = compute.instances();
for (String id : instanceIdList) {
Get response = instances.get(projectId, availabilityZone, id);
com.google.api.services.compute.model.Instance instance = response.execute();
Tags gcpTags = instance.getTags();
Map<String, String> extractedTags = new HashMap<>();
List<String> tagList = gcpTags.getItems();
for (String i : tagList) {
String[] tmpTagList = i.split("-");
if (tmpTagList.length > 1) {
extractedTags.put(tmpTagList[0], tmpTagList[1]);
}
}
checkTags(tagsToCheckMap, extractedTags);
extractedTags.clear();
}
}
use of com.google.api.services.compute.Compute.Instances in project photon-model by vmware.
the class GCPTestUtil method getInstanceNumber.
/**
* Get the number of instances on specified GCP project and zone.
* @param compute The GCE client object.
* @param projectId The project id.
* @param zoneId The zone id.
* @return The number of instances.
* @throws Throwable Exception during querying the instances
*/
public static int getInstanceNumber(Compute compute, String projectId, String zoneId) throws Throwable {
Instances instanceList = compute.instances();
Instances.List list = instanceList.list(projectId, zoneId);
InstanceList ins = list.execute();
List<Instance> instances = ins.getItems();
if (instances == null) {
return 0;
}
return instances.size();
}
Aggregations