Search in sources :

Example 1 with Get

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

the class GcpInstanceResourceBuilder method stopStart.

private CloudVmInstanceStatus stopStart(GcpContext context, AuthenticatedContext auth, CloudInstance instance, boolean stopRequest) {
    String projectId = GcpStackUtil.getProjectId(auth.getCloudCredential());
    String availabilityZone = context.getLocation().getAvailabilityZone().value();
    Compute compute = context.getCompute();
    String instanceId = instance.getInstanceId();
    try {
        Get get = compute.instances().get(projectId, availabilityZone, instanceId);
        String state = stopRequest ? "RUNNING" : "TERMINATED";
        if (state.equals(get.execute().getStatus())) {
            Operation operation = stopRequest ? compute.instances().stop(projectId, availabilityZone, instanceId).setPrettyPrint(true).execute() : compute.instances().start(projectId, availabilityZone, instanceId).setPrettyPrint(true).execute();
            CloudInstance operationAwareCloudInstance = createOperationAwareCloudInstance(instance, operation);
            return checkInstances(context, auth, Collections.singletonList(operationAwareCloudInstance)).get(0);
        } else {
            LOGGER.info("Instance {} is not in {} state - won't start it.", state, instanceId);
            return null;
        }
    } catch (IOException e) {
        throw new GcpResourceException(String.format("An error occurred while stopping the vm '%s'", instanceId), e);
    }
}
Also used : Compute(com.google.api.services.compute.Compute) Get(com.google.api.services.compute.Compute.Instances.Get) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) Operation(com.google.api.services.compute.model.Operation) IOException(java.io.IOException)

Example 2 with Get

use of com.google.api.services.compute.Compute.Instances.Get 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)

Aggregations

Compute (com.google.api.services.compute.Compute)2 Get (com.google.api.services.compute.Compute.Instances.Get)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 Builder (com.google.api.services.compute.Compute.Builder)1 Instances (com.google.api.services.compute.Compute.Instances)1 Operation (com.google.api.services.compute.model.Operation)1 Tags (com.google.api.services.compute.model.Tags)1 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)1 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 PrivateKey (java.security.PrivateKey)1 HashMap (java.util.HashMap)1