Search in sources :

Example 1 with Tags

use of com.google.api.services.compute.model.Tags 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 2 with Tags

use of com.google.api.services.compute.model.Tags in project cloudbreak by hortonworks.

the class GcpInstanceResourceBuilder method build.

@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> customTags) throws Exception {
    InstanceTemplate template = group.getReferenceInstanceConfiguration().getTemplate();
    String projectId = context.getProjectId();
    Location location = context.getLocation();
    boolean noPublicIp = context.getNoPublicIp();
    Compute compute = context.getCompute();
    List<CloudResource> computeResources = context.getComputeResources(privateId);
    List<AttachedDisk> listOfDisks = new ArrayList<>();
    listOfDisks.addAll(getBootDiskList(computeResources, projectId, location.getAvailabilityZone()));
    listOfDisks.addAll(getAttachedDisks(computeResources, projectId, location.getAvailabilityZone()));
    Instance instance = new Instance();
    instance.setMachineType(String.format("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/%s", projectId, location.getAvailabilityZone().value(), template.getFlavor()));
    instance.setName(buildableResource.get(0).getName());
    instance.setCanIpForward(Boolean.TRUE);
    instance.setNetworkInterfaces(getNetworkInterface(context.getNetworkResources(), computeResources, location.getRegion(), group, compute, projectId, noPublicIp));
    instance.setDisks(listOfDisks);
    Scheduling scheduling = new Scheduling();
    boolean preemptible = false;
    if (template.getParameter(PREEMPTIBLE, Boolean.class) != null) {
        preemptible = template.getParameter(PREEMPTIBLE, Boolean.class);
    }
    scheduling.setPreemptible(preemptible);
    instance.setScheduling(scheduling);
    Tags tags = new Tags();
    List<String> tagList = new ArrayList<>();
    Map<String, String> labels = new HashMap<>();
    String groupname = group.getName().toLowerCase().replaceAll("[^A-Za-z0-9 ]", "");
    tagList.add(groupname);
    Map<String, String> instanceTag = defaultCostTaggingService.prepareInstanceTagging();
    for (Entry<String, String> entry : instanceTag.entrySet()) {
        tagList.add(String.format("%s-%s", entry.getKey(), entry.getValue()));
        labels.put(entry.getKey(), entry.getValue());
    }
    tagList.add(GcpStackUtil.getClusterTag(auth.getCloudContext()));
    tagList.add(GcpStackUtil.getGroupClusterTag(auth.getCloudContext(), group));
    customTags.forEach((key, value) -> tagList.add(key + '-' + value));
    labels.putAll(customTags);
    tags.setItems(tagList);
    instance.setTags(tags);
    instance.setLabels(labels);
    Metadata metadata = new Metadata();
    metadata.setItems(new ArrayList<>());
    Items sshMetaData = new Items();
    sshMetaData.setKey("ssh-keys");
    sshMetaData.setValue(group.getInstanceAuthentication().getLoginUserName() + ':' + group.getInstanceAuthentication().getPublicKey());
    Items blockProjectWideSsh = new Items();
    blockProjectWideSsh.setKey("block-project-ssh-keys");
    blockProjectWideSsh.setValue("TRUE");
    Items startupScript = new Items();
    startupScript.setKey("startup-script");
    startupScript.setValue(image.getUserDataByType(group.getType()));
    metadata.getItems().add(sshMetaData);
    metadata.getItems().add(startupScript);
    metadata.getItems().add(blockProjectWideSsh);
    instance.setMetadata(metadata);
    Insert insert = compute.instances().insert(projectId, location.getAvailabilityZone().value(), instance);
    insert.setPrettyPrint(Boolean.TRUE);
    try {
        Operation operation = insert.execute();
        if (operation.getHttpErrorStatusCode() != null) {
            throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), buildableResource.get(0).getName());
        }
        return Collections.singletonList(createOperationAwareCloudResource(buildableResource.get(0), operation));
    } catch (GoogleJsonResponseException e) {
        throw new GcpResourceException(checkException(e), resourceType(), buildableResource.get(0).getName());
    }
}
Also used : Instance(com.google.api.services.compute.model.Instance) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Metadata(com.google.api.services.compute.model.Metadata) AttachedDisk(com.google.api.services.compute.model.AttachedDisk) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Instances.Insert) Items(com.google.api.services.compute.model.Metadata.Items) Tags(com.google.api.services.compute.model.Tags) Scheduling(com.google.api.services.compute.model.Scheduling) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Compute(com.google.api.services.compute.Compute) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

Aggregations

Compute (com.google.api.services.compute.Compute)2 Tags (com.google.api.services.compute.model.Tags)2 HashMap (java.util.HashMap)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 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)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 Get (com.google.api.services.compute.Compute.Instances.Get)1 Insert (com.google.api.services.compute.Compute.Instances.Insert)1 AttachedDisk (com.google.api.services.compute.model.AttachedDisk)1 Instance (com.google.api.services.compute.model.Instance)1 Metadata (com.google.api.services.compute.model.Metadata)1 Items (com.google.api.services.compute.model.Metadata.Items)1 Operation (com.google.api.services.compute.model.Operation)1 Scheduling (com.google.api.services.compute.model.Scheduling)1 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)1