Search in sources :

Example 1 with Instances

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;
}
Also used : Instances(com.google.api.services.compute.Compute.Instances) Instance(com.google.api.services.compute.model.Instance) ArrayList(java.util.ArrayList) InstanceList(com.google.api.services.compute.model.InstanceList) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 2 with Instances

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();
    }
}
Also used : PrivateKey(java.security.PrivateKey) HashMap(java.util.HashMap) Builder(com.google.api.services.compute.Compute.Builder) AmazonEC2ClientBuilder(com.amazonaws.services.ec2.AmazonEC2ClientBuilder) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) Instances(com.google.api.services.compute.Compute.Instances) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) ByteArrayInputStream(java.io.ByteArrayInputStream) Compute(com.google.api.services.compute.Compute) Get(com.google.api.services.compute.Compute.Instances.Get) Tags(com.google.api.services.compute.model.Tags)

Example 3 with Instances

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();
}
Also used : Instances(com.google.api.services.compute.Compute.Instances) Instance(com.google.api.services.compute.model.Instance) InstanceList(com.google.api.services.compute.model.InstanceList)

Aggregations

Instances (com.google.api.services.compute.Compute.Instances)3 Instance (com.google.api.services.compute.model.Instance)2 InstanceList (com.google.api.services.compute.model.InstanceList)2 AmazonEC2ClientBuilder (com.amazonaws.services.ec2.AmazonEC2ClientBuilder)1 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1 HttpTransport (com.google.api.client.http.HttpTransport)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 Compute (com.google.api.services.compute.Compute)1 Builder (com.google.api.services.compute.Compute.Builder)1 Get (com.google.api.services.compute.Compute.Instances.Get)1 Tags (com.google.api.services.compute.model.Tags)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 PrivateKey (java.security.PrivateKey)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1