Search in sources :

Example 6 with MachineType

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

the class GcpPlatformResources method virtualMachines.

@Override
@Cacheable(cacheNames = "cloudResourceVmTypeCache", key = "#cloudCredential?.id + #region.getRegionName()")
public CloudVmTypes virtualMachines(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    Compute compute = GcpStackUtil.buildCompute(cloudCredential);
    String projectId = GcpStackUtil.getProjectId(cloudCredential);
    Map<String, Set<VmType>> cloudVmResponses = new HashMap<>();
    Map<String, VmType> defaultCloudVmResponses = new HashMap<>();
    try {
        Set<VmType> types = new HashSet<>();
        VmType defaultVmType = null;
        CloudRegions regions = regions(cloudCredential, region, filters);
        for (AvailabilityZone availabilityZone : regions.getCloudRegions().get(region)) {
            MachineTypeList machineTypeList = compute.machineTypes().list(projectId, availabilityZone.value()).execute();
            for (MachineType machineType : machineTypeList.getItems()) {
                VmTypeMeta vmTypeMeta = VmTypeMetaBuilder.builder().withCpuAndMemory(machineType.getGuestCpus(), machineType.getMemoryMb().floatValue() / THOUSAND).withMagneticConfig(TEN, machineType.getMaximumPersistentDisksSizeGb().intValue(), 1, machineType.getMaximumPersistentDisksSizeGb().intValue()).withSsdConfig(TEN, machineType.getMaximumPersistentDisksSizeGb().intValue(), 1, machineType.getMaximumPersistentDisks()).withMaximumPersistentDisksSizeGb(machineType.getMaximumPersistentDisksSizeGb().toString()).create();
                VmType vmType = VmType.vmTypeWithMeta(machineType.getName(), vmTypeMeta, true);
                types.add(vmType);
                if (machineType.getName().equals(gcpVmDefault)) {
                    defaultVmType = vmType;
                }
            }
            cloudVmResponses.put(availabilityZone.value(), types);
            defaultCloudVmResponses.put(availabilityZone.value(), defaultVmType);
        }
        return new CloudVmTypes(cloudVmResponses, defaultCloudVmResponses);
    } catch (Exception e) {
        return new CloudVmTypes(new HashMap<>(), new HashMap<>());
    }
}
Also used : VmTypeMeta(com.sequenceiq.cloudbreak.cloud.model.VmTypeMeta) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) MachineType(com.google.api.services.compute.model.MachineType) CloudRegions(com.sequenceiq.cloudbreak.cloud.model.CloudRegions) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) IOException(java.io.IOException) CloudVmTypes(com.sequenceiq.cloudbreak.cloud.model.CloudVmTypes) Compute(com.google.api.services.compute.Compute) VmType(com.sequenceiq.cloudbreak.cloud.model.VmType) MachineTypeList(com.google.api.services.compute.model.MachineTypeList) HashSet(java.util.HashSet) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 7 with MachineType

use of com.google.api.services.compute.model.MachineType in project platformlayer by platformlayer.

the class GoogleComputeClient method createInstance.

public Instance createInstance(GoogleCloud cloud, MachineCreationRequest request, PublicKey sshPublicKey) throws OpsException {
    try {
        Image foundImage = null;
        {
            DiskImageRecipe recipe = null;
            if (request.recipeId != null) {
                recipe = platformLayerClient.getItem(request.recipeId, DiskImageRecipe.class);
            }
            OperatingSystemRecipe operatingSystem = null;
            if (recipe != null) {
                operatingSystem = recipe.getOperatingSystem();
            }
            log.info("Listing images to pick best image");
            Iterable<Image> images = listImages(PROJECTID_GOOGLE);
            // TODO: We need a better solution here!!
            log.warn("Hard coding image names");
            Set<String> imageNames = Sets.newHashSet("ubuntu-12-04-v20120621");
            for (Image image : images) {
                if (imageNames.contains(image.getName())) {
                    foundImage = image;
                    break;
                }
            }
            if (foundImage == null) {
                throw new IllegalArgumentException("Could not find image");
            }
        }
        // GCE requires that the name comply with RFC1035, which I think means a valid DNS
        // For now, just use a UUID, with a pl- prefix so it doesn't start with a number
        // TODO: Fix this!
        String instanceName = "pl-" + UUID.randomUUID().toString();
        Operation createServerOperation;
        {
            Instance create = new Instance();
            create.setName(instanceName);
            create.setZone(buildZoneUrl(projectId, ZONE_US_CENTRAL1_A));
            {
                NetworkInterface networkInterface = new NetworkInterface();
                networkInterface.setNetwork(buildNetworkUrl(projectId, "default"));
                AccessConfig networkAccessConfig = new AccessConfig();
                networkAccessConfig.setType("ONE_TO_ONE_NAT");
                networkInterface.setAccessConfigs(Lists.newArrayList(networkAccessConfig));
                create.setNetworkInterfaces(Lists.newArrayList(networkInterface));
            }
            Metadata metadata = new Metadata();
            metadata.setItems(Lists.<Items>newArrayList());
            create.setMetadata(metadata);
            if (request.tags != null) {
                for (Tag tag : request.tags) {
                    Metadata.Items meta = new Metadata.Items();
                    meta.setKey(tag.getKey());
                    meta.setValue(tag.getValue());
                    metadata.getItems().add(meta);
                }
            }
            if (request.sshPublicKey != null) {
                Metadata.Items meta = new Metadata.Items();
                meta.setKey("sshKeys");
                meta.setValue(USER_NAME + ":" + OpenSshUtils.serialize(sshPublicKey));
                metadata.getItems().add(meta);
            }
            create.setImage(foundImage.getSelfLink());
            MachineType flavor = getClosestInstanceType(request);
            if (flavor == null) {
                throw new OpsException("Cannot determine machine type for request");
            }
            create.setMachineType(flavor.getSelfLink());
            if (request.securityGroups != null) {
                // TODO: Reimplement if needed
                throw new UnsupportedOperationException();
            }
            // if (createdSecurityGroup != null) {
            // ServerForCreate.SecurityGroup serverSecurityGroup = new ServerForCreate.SecurityGroup();
            // serverSecurityGroup.setName(createdSecurityGroup.getName());
            // create.getSecurityGroups().add(serverSecurityGroup);
            // }
            // create.setConfigDrive(cloudBehaviours.useConfigDrive());
            log.info("Launching new server: " + instanceName);
            try {
                createServerOperation = compute.instances().insert(projectId, create).execute();
            } catch (IOException e) {
                throw new OpsException("Error launching new instance", e);
            }
        }
        log.info("Waiting for server to be ready");
        createServerOperation = waitComplete(createServerOperation, 10, TimeUnit.MINUTES);
        Instance created;
        InstanceState state = null;
        while (true) {
            created = findInstanceByName(instanceName);
            state = InstanceState.get(created);
            log.info("Instance state: " + state);
            if (state.isRunning()) {
                break;
            }
            Thread.sleep(1000);
        }
        return created;
    } catch (InterruptedException e) {
        ExceptionUtils.handleInterrupted(e);
        throw new OpsException("Error building server", e);
    } catch (TimeoutException e) {
        throw new OpsException("Timeout waiting for server build", e);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Set(java.util.Set) Instance(com.google.api.services.compute.model.Instance) Metadata(com.google.api.services.compute.model.Metadata) MachineType(com.google.api.services.compute.model.MachineType) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) Operation(com.google.api.services.compute.model.Operation) Items(com.google.api.services.compute.model.Metadata.Items) IOException(java.io.IOException) Image(com.google.api.services.compute.model.Image) AccessConfig(com.google.api.services.compute.model.AccessConfig) DiskImageRecipe(org.platformlayer.images.model.DiskImageRecipe) Items(com.google.api.services.compute.model.Metadata.Items) Tag(org.platformlayer.core.model.Tag) OperatingSystemRecipe(org.platformlayer.images.model.OperatingSystemRecipe) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

MachineType (com.google.api.services.compute.model.MachineType)7 IOException (java.io.IOException)5 MachineTypeList (com.google.api.services.compute.model.MachineTypeList)3 Set (java.util.Set)2 OpsException (org.platformlayer.ops.OpsException)2 Compute (com.google.api.services.compute.Compute)1 AccessConfig (com.google.api.services.compute.model.AccessConfig)1 Image (com.google.api.services.compute.model.Image)1 Instance (com.google.api.services.compute.model.Instance)1 MachineTypeAggregatedList (com.google.api.services.compute.model.MachineTypeAggregatedList)1 MachineTypesScopedList (com.google.api.services.compute.model.MachineTypesScopedList)1 Metadata (com.google.api.services.compute.model.Metadata)1 Items (com.google.api.services.compute.model.Metadata.Items)1 NetworkInterface (com.google.api.services.compute.model.NetworkInterface)1 Operation (com.google.api.services.compute.model.Operation)1 ImmutableList (com.google.common.collect.ImmutableList)1 AvailabilityZone (com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone)1 CloudRegions (com.sequenceiq.cloudbreak.cloud.model.CloudRegions)1 CloudVmTypes (com.sequenceiq.cloudbreak.cloud.model.CloudVmTypes)1 VmType (com.sequenceiq.cloudbreak.cloud.model.VmType)1