Search in sources :

Example 31 with Compute

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

the class GcpPlatformResources method regions.

@Override
@Cacheable(cacheNames = "cloudResourceRegionCache", key = "#cloudCredential?.id")
public CloudRegions regions(CloudCredential cloudCredential, Region region, Map<String, String> filters) throws Exception {
    Compute compute = GcpStackUtil.buildCompute(cloudCredential);
    String projectId = GcpStackUtil.getProjectId(cloudCredential);
    Map<Region, List<AvailabilityZone>> regionListMap = new HashMap<>();
    Map<Region, String> displayNames = new HashMap<>();
    String defaultRegion = gcpZoneParameterDefault;
    RegionList regionList = compute.regions().list(projectId).execute();
    for (com.google.api.services.compute.model.Region gcpRegion : regionList.getItems()) {
        if (region == null || Strings.isNullOrEmpty(region.value()) || gcpRegion.getName().equals(region.value())) {
            List<AvailabilityZone> availabilityZones = new ArrayList<>();
            for (String s : gcpRegion.getZones()) {
                String[] split = s.split("/");
                if (split.length > 0) {
                    availabilityZones.add(AvailabilityZone.availabilityZone(split[split.length - 1]));
                }
            }
            regionListMap.put(region(gcpRegion.getName()), availabilityZones);
            displayNames.put(region(gcpRegion.getName()), displayName(gcpRegion.getName()));
        }
    }
    if (region != null && !Strings.isNullOrEmpty(region.value())) {
        defaultRegion = region.value();
    }
    return new CloudRegions(regionListMap, displayNames, defaultRegion);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) CloudRegions(com.sequenceiq.cloudbreak.cloud.model.CloudRegions) Compute(com.google.api.services.compute.Compute) Region(com.sequenceiq.cloudbreak.cloud.model.Region) ArrayList(java.util.ArrayList) NetworkList(com.google.api.services.compute.model.NetworkList) RegionList(com.google.api.services.compute.model.RegionList) List(java.util.List) MachineTypeList(com.google.api.services.compute.model.MachineTypeList) FirewallList(com.google.api.services.compute.model.FirewallList) RegionList(com.google.api.services.compute.model.RegionList) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 32 with Compute

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

Example 33 with Compute

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

the class GcpNetworkResourceBuilder method build.

@Override
public CloudResource build(GcpContext context, AuthenticatedContext auth, Network network, Security security, CloudResource resource) throws Exception {
    if (!isExistingNetwork(network)) {
        Compute compute = context.getCompute();
        String projectId = context.getProjectId();
        com.google.api.services.compute.model.Network gcpNetwork = new com.google.api.services.compute.model.Network();
        gcpNetwork.setName(resource.getName());
        gcpNetwork.setAutoCreateSubnetworks(false);
        Insert networkInsert = compute.networks().insert(projectId, gcpNetwork);
        try {
            Operation operation = networkInsert.execute();
            if (operation.getHttpErrorStatusCode() != null) {
                throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), resource.getName());
            }
            context.putParameter(NETWORK_NAME, resource.getName());
            return createOperationAwareCloudResource(resource, operation);
        } catch (GoogleJsonResponseException e) {
            throw new GcpResourceException(checkException(e), resourceType(), resource.getName());
        }
    }
    context.putParameter(NETWORK_NAME, resource.getName());
    return new Builder().cloudResource(resource).persistent(false).build();
}
Also used : Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Networks.Insert) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Compute(com.google.api.services.compute.Compute) GcpStackUtil.isExistingNetwork(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil.isExistingNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)

Example 34 with Compute

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

the class GcpSubnetResourceBuilder method delete.

@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource, Network network) throws Exception {
    if (isNewNetworkAndSubnet(network) || isNewSubnetInExistingNetwork(network)) {
        Compute compute = context.getCompute();
        String projectId = context.getProjectId();
        try {
            Operation operation = compute.subnetworks().delete(projectId, context.getLocation().getRegion().value(), resource.getName()).execute();
            return createOperationAwareCloudResource(resource, operation);
        } catch (GoogleJsonResponseException e) {
            exceptionHandler(e, resource.getName(), resourceType());
            return null;
        }
    }
    return null;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Compute(com.google.api.services.compute.Compute) Operation(com.google.api.services.compute.model.Operation)

Example 35 with Compute

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

the class GcpInstanceConnector method check.

@Override
public List<CloudVmInstanceStatus> check(AuthenticatedContext ac, List<CloudInstance> vms) {
    List<CloudVmInstanceStatus> statuses = new ArrayList<>();
    CloudCredential credential = ac.getCloudCredential();
    CloudContext cloudContext = ac.getCloudContext();
    Compute compute = GcpStackUtil.buildCompute(credential);
    for (CloudInstance instance : vms) {
        InstanceStatus status = InstanceStatus.UNKNOWN;
        try {
            Instance executeInstance = getInstance(cloudContext, credential, compute, instance.getInstanceId());
            if ("RUNNING".equals(executeInstance.getStatus())) {
                status = InstanceStatus.STARTED;
            } else if ("TERMINATED".equals(executeInstance.getStatus())) {
                status = InstanceStatus.STOPPED;
            }
        } catch (GoogleJsonResponseException e) {
            if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                status = InstanceStatus.TERMINATED;
            } else {
                LOGGER.warn(String.format("Instance %s is not reachable", instance), e);
            }
        } catch (IOException e) {
            LOGGER.warn(String.format("Instance %s is not reachable", instance), e);
        }
        statuses.add(new CloudVmInstanceStatus(instance, status));
    }
    return statuses;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Instance(com.google.api.services.compute.model.Instance) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Compute(com.google.api.services.compute.Compute) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) IOException(java.io.IOException)

Aggregations

Compute (com.google.api.services.compute.Compute)43 IOException (java.io.IOException)26 Operation (com.google.api.services.compute.model.Operation)16 ArrayList (java.util.ArrayList)13 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)12 HashMap (java.util.HashMap)9 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)8 GeneralSecurityException (java.security.GeneralSecurityException)8 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)7 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)7 Instance (com.google.api.services.compute.model.Instance)6 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)6 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)6 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)4 HttpRequest (com.google.api.client.http.HttpRequest)4 HttpTransport (com.google.api.client.http.HttpTransport)4 AttachedDisk (com.google.api.services.compute.model.AttachedDisk)4 ManagedInstance (com.google.api.services.compute.model.ManagedInstance)4 NetworkInterface (com.google.api.services.compute.model.NetworkInterface)4 Subnetwork (com.google.api.services.compute.model.Subnetwork)4