Search in sources :

Example 1 with Instance

use of com.google.cloud.redis.v1.Instance in project TOSCAna by StuPro-TOSCAna.

the class CapabilityMapper method mapDiskSize.

/**
 *     Maps the disk_size property of a {@link ComputeCapability} to an EC2 Instance.
 *
 *     @param computeCapability {@link ComputeCapability} containing the disk_size property
 *     @param cfnModule         {@link CloudFormationModule} containing the Instance
 *     @param nodeName          name of the Instance
 */
public void mapDiskSize(ComputeCapability computeCapability, CloudFormationModule cfnModule, String nodeName) {
    // If disk_size is not set, default to 8000 Mb
    Integer diskSizeInMb = computeCapability.getDiskSizeInMb().orElse(8000);
    // Convert disk_size to Gb
    Integer diskSizeInGb = diskSizeInMb / 1000;
    logger.debug("Check diskSize: '{}' Gb", diskSizeInGb);
    if (diskSizeInGb < 8) {
        logger.warn("Disk size of '{}' smaller than the minimum value required by EC2 Instances. Setting the disk size of '{}' to the minimum allowed value of 8 Gb.", nodeName, nodeName);
        diskSizeInGb = 8;
    }
    // Add BlockDeviceMapping if needed
    if (diskSizeInGb > 8) {
        logger.debug("Disk size of '{}' bigger than the default value of EC2 Instances. Adding a BlockDeviceMapping to '{}'.", nodeName, nodeName);
        Instance computeAsInstance = (Instance) cfnModule.getResource(nodeName);
        computeAsInstance.blockDeviceMappings(new EC2BlockDeviceMapping().deviceName("/dev/sda1").ebs(new EC2EBSBlockDevice().volumeSize(diskSizeInGb.toString())));
    }
}
Also used : EC2BlockDeviceMapping(com.scaleset.cfbuilder.ec2.instance.EC2BlockDeviceMapping) EC2EBSBlockDevice(com.scaleset.cfbuilder.ec2.instance.ec2blockdevicemapping.EC2EBSBlockDevice) Instance(com.scaleset.cfbuilder.ec2.Instance)

Example 2 with Instance

use of com.google.cloud.redis.v1.Instance in project TOSCAna by StuPro-TOSCAna.

the class TransformModelNodeVisitor method visit.

/**
 *     Transforms the {@link Compute} node into a EC2 Instance.
 *
 *     @param node the {@link Compute} node to visit
 */
@Override
public void visit(Compute node) {
    try {
        if (cfnModule.checkComputeToEc2(node)) {
            logger.debug("Compute '{}' will be transformed to EC2", node.getEntityName());
            String nodeName = toAlphanumerical(node.getEntityName());
            // default security group the EC2 Instance
            SecurityGroup webServerSecurityGroup = cfnModule.resource(SecurityGroup.class, nodeName + SECURITY_GROUP).groupDescription("Enables ports for " + nodeName + ".");
            // open endpoint port
            node.getEndpoint().getPort().ifPresent(port -> webServerSecurityGroup.ingress(ingress -> ingress.cidrIp(IP_OPEN), PROTOCOL_TCP, port.port));
            // check what image id should be taken
            CapabilityMapper capabilityMapper = createCapabilityMapper();
            OsCapability computeOs = node.getOs();
            String imageId = capabilityMapper.mapOsCapabilityToImageId(computeOs);
            ComputeCapability computeCompute = node.getHost();
            String instanceType = capabilityMapper.mapComputeCapabilityToInstanceType(computeCompute, CapabilityMapper.EC2_DISTINCTION);
            // create CFN init and store it
            CFNInit init = new CFNInit(CONFIG_SETS);
            cfnModule.putCFNInit(nodeName, init);
            cfnModule.resource(Instance.class, nodeName).securityGroupIds(webServerSecurityGroup).imageId(imageId).instanceType(instanceType);
            capabilityMapper.mapDiskSize(computeCompute, cfnModule, nodeName);
            // Add Reference to keyName if KeyPair needed and open Port 22 (Allows SSH access)
            if (cfnModule.hasKeyPair()) {
                Instance instance = (Instance) cfnModule.getResource(nodeName);
                instance.keyName(cfnModule.getKeyNameVar());
                webServerSecurityGroup.ingress(ingress -> ingress.cidrIp(IP_OPEN), PROTOCOL_TCP, 22);
            }
        } else {
            logger.debug("Compute '{}' will not be transformed to EC2", node.getEntityName());
        }
    } catch (SdkClientException se) {
        logger.error("SDKClient failed, no valid credentials or no internet connection");
        throw new TransformationFailureException("Failed", se);
    } catch (Exception e) {
        logger.error("Error while creating EC2Instance resource.");
        throw new TransformationFailureException("Failed at Compute node " + node.getEntityName(), e);
    }
}
Also used : Application(com.scaleset.cfbuilder.beanstalk.Application) Apache(org.opentosca.toscana.model.node.Apache) Environment(com.scaleset.cfbuilder.beanstalk.Environment) CFNInit(com.scaleset.cfbuilder.ec2.metadata.CFNInit) CONFIG_CONFIGURE(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.CONFIG_CONFIGURE) NodeVisitor(org.opentosca.toscana.model.visitor.NodeVisitor) CapabilityMapper(org.opentosca.toscana.plugins.cloudformation.mapper.CapabilityMapper) SECURITY_GROUP(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.SECURITY_GROUP) ApplicationVersion(com.scaleset.cfbuilder.beanstalk.ApplicationVersion) ArrayList(java.util.ArrayList) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) CFNPackage(com.scaleset.cfbuilder.ec2.metadata.CFNPackage) JavaRuntimeMapper(org.opentosca.toscana.plugins.cloudformation.mapper.JavaRuntimeMapper) MysqlDatabase(org.opentosca.toscana.model.node.MysqlDatabase) CFNCommand(com.scaleset.cfbuilder.ec2.metadata.CFNCommand) CONFIG_CREATE(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.CONFIG_CREATE) CONFIG_SETS(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.CONFIG_SETS) APACHE_RESTART_COMMAND(org.opentosca.toscana.plugins.cloudformation.handler.OperationHandler.APACHE_RESTART_COMMAND) WebApplication(org.opentosca.toscana.model.node.WebApplication) FILEPATH_NODEJS_CREATE(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.FILEPATH_NODEJS_CREATE) APACHE_ENV_IMPORT(org.opentosca.toscana.plugins.cloudformation.handler.EnvironmentHandler.APACHE_ENV_IMPORT) SourceBundle(com.scaleset.cfbuilder.beanstalk.SourceBundle) Artifact(org.opentosca.toscana.model.artifact.Artifact) Compute(org.opentosca.toscana.model.node.Compute) ComputeCapability(org.opentosca.toscana.model.capability.ComputeCapability) Database(org.opentosca.toscana.model.node.Database) OperationHandler(org.opentosca.toscana.plugins.cloudformation.handler.OperationHandler) SecurityGroup(com.scaleset.cfbuilder.ec2.SecurityGroup) Dbms(org.opentosca.toscana.model.node.Dbms) Nodejs(org.opentosca.toscana.model.node.Nodejs) Instance(com.scaleset.cfbuilder.ec2.Instance) Set(java.util.Set) JavaApplication(org.opentosca.toscana.model.node.custom.JavaApplication) OptionSetting(com.scaleset.cfbuilder.beanstalk.OptionSetting) OsCapability(org.opentosca.toscana.model.capability.OsCapability) List(java.util.List) SdkClientException(com.amazonaws.SdkClientException) ConfigurationTemplate(com.scaleset.cfbuilder.beanstalk.ConfigurationTemplate) CloudFormationLifecycle.toAlphanumerical(org.opentosca.toscana.plugins.cloudformation.CloudFormationLifecycle.toAlphanumerical) TransformationContext(org.opentosca.toscana.core.transformation.TransformationContext) DBInstance(com.scaleset.cfbuilder.rds.DBInstance) CONFIG_START(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.CONFIG_START) CloudFormationModule(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule) SdkClientException(com.amazonaws.SdkClientException) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) Instance(com.scaleset.cfbuilder.ec2.Instance) DBInstance(com.scaleset.cfbuilder.rds.DBInstance) OsCapability(org.opentosca.toscana.model.capability.OsCapability) CFNInit(com.scaleset.cfbuilder.ec2.metadata.CFNInit) SecurityGroup(com.scaleset.cfbuilder.ec2.SecurityGroup) CapabilityMapper(org.opentosca.toscana.plugins.cloudformation.mapper.CapabilityMapper) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) SdkClientException(com.amazonaws.SdkClientException) ComputeCapability(org.opentosca.toscana.model.capability.ComputeCapability)

Example 3 with Instance

use of com.google.cloud.redis.v1.Instance in project java-docs-samples by GoogleCloudPlatform.

the class CreateInstance method createInstance.

// Create a new instance with the provided "instanceName" value in the specified project and zone.
public static void createInstance(String project, String zone, String instanceName) throws IOException, InterruptedException, ExecutionException {
    // Below are sample values that can be replaced.
    // machineType: machine type of the VM being created. This value uses the format zones/{zone}/machineTypes/{type_name}. For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
    // sourceImage: path to the operating system image to mount. For details about images you can mount, see https://cloud.google.com/compute/docs/images
    // diskSizeGb: storage size of the boot disk to attach to the instance.
    // networkName: network interface to associate with the instance.
    String machineType = String.format("zones/%s/machineTypes/n1-standard-1", zone);
    String sourceImage = String.format("projects/debian-cloud/global/images/family/%s", "debian-10");
    long diskSizeGb = 10L;
    String networkName = "default";
    // clean up any remaining background resources.
    try (InstancesClient instancesClient = InstancesClient.create()) {
        // Instance creation requires at least one persistent disk and one network interface.
        AttachedDisk disk = AttachedDisk.newBuilder().setBoot(true).setAutoDelete(true).setType(Type.PERSISTENT.toString()).setDeviceName("disk-1").setInitializeParams(AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage).setDiskSizeGb(diskSizeGb).build()).build();
        // Use the network interface provided in the networkName argument.
        NetworkInterface networkInterface = NetworkInterface.newBuilder().setName(networkName).build();
        // Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
        Instance instanceResource = Instance.newBuilder().setName(instanceName).setMachineType(machineType).addDisks(disk).addNetworkInterfaces(networkInterface).build();
        System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
        // Insert the instance in the specified project and zone.
        InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstanceResource(instanceResource).build();
        OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(insertInstanceRequest);
        // Wait for the operation to complete.
        Operation response = operation.get();
        if (response.hasError()) {
            System.out.println("Instance creation failed ! ! " + response);
            return;
        }
        System.out.println("Operation Status: " + response.getStatus());
    }
}
Also used : InsertInstanceRequest(com.google.cloud.compute.v1.InsertInstanceRequest) Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient) AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) NetworkInterface(com.google.cloud.compute.v1.NetworkInterface) Operation(com.google.cloud.compute.v1.Operation)

Example 4 with Instance

use of com.google.cloud.redis.v1.Instance in project java-docs-samples by GoogleCloudPlatform.

the class CreateInstanceFromTemplateWithOverrides method createInstanceFromTemplateWithOverrides.

// Creates a Compute Engine VM instance from an instance template,
// but overrides the disk and machine type options in the template.
public static void createInstanceFromTemplateWithOverrides(String projectId, String zone, String instanceName, String instanceTemplateName) throws IOException, ExecutionException, InterruptedException {
    try (InstancesClient instancesClient = InstancesClient.create();
        InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
        String machineType = "n1-standard-1";
        String newDiskSourceImage = "projects/debian-cloud/global/images/family/debian-10";
        // Retrieve an instance template.
        InstanceTemplate instanceTemplate = instanceTemplatesClient.get(projectId, instanceTemplateName);
        AttachedDisk newdisk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setDiskSizeGb(10).setSourceImage(newDiskSourceImage).build()).setAutoDelete(true).setBoot(false).setType(AttachedDisk.Type.PERSISTENT.toString()).build();
        Instance instance = Instance.newBuilder().setName(instanceName).setMachineType(String.format("zones/%s/machineTypes/%s", zone, machineType)).addAllDisks(instanceTemplate.getProperties().getDisksList()).addDisks(newdisk).build();
        InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder().setProject(projectId).setZone(zone).setInstanceResource(instance).setSourceInstanceTemplate(instanceTemplate.getSelfLink()).build();
        Operation response = instancesClient.insertAsync(insertInstanceRequest).get();
        if (response.hasError()) {
            System.out.println("Instance creation from template with overrides failed ! ! " + response);
            return;
        }
        System.out.printf("Instance creation from template with overrides: Operation Status %s: %s ", instanceName, response.getStatus());
    }
}
Also used : InsertInstanceRequest(com.google.cloud.compute.v1.InsertInstanceRequest) Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient) AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) Operation(com.google.cloud.compute.v1.Operation) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient) InstanceTemplate(com.google.cloud.compute.v1.InstanceTemplate)

Example 5 with Instance

use of com.google.cloud.redis.v1.Instance in project java-docs-samples by GoogleCloudPlatform.

the class ListAllInstances method listAllInstances.

// List all instances in the specified project ID.
public static AggregatedListPagedResponse listAllInstances(String project) throws IOException {
    // safely clean up any remaining background resources.
    try (InstancesClient instancesClient = InstancesClient.create()) {
        // Use the `setMaxResults` parameter to limit the number of results
        // that the API returns per response page.
        AggregatedListInstancesRequest aggregatedListInstancesRequest = AggregatedListInstancesRequest.newBuilder().setProject(project).setMaxResults(5).build();
        InstancesClient.AggregatedListPagedResponse response = instancesClient.aggregatedList(aggregatedListInstancesRequest);
        // automatically, requesting next pages as you iterate over the results.
        for (Map.Entry<String, InstancesScopedList> zoneInstances : response.iterateAll()) {
            // Instances scoped by each zone
            String zone = zoneInstances.getKey();
            if (!zoneInstances.getValue().getInstancesList().isEmpty()) {
                // zoneInstances.getKey() returns the fully qualified address.
                // Hence, strip it to get the zone name only
                System.out.printf("Instances at %s: ", zone.substring(zone.lastIndexOf('/') + 1));
                for (Instance instance : zoneInstances.getValue().getInstancesList()) {
                    System.out.println(instance.getName());
                }
            }
        }
        System.out.println("####### Listing all instances complete #######");
        return response;
    }
}
Also used : InstancesScopedList(com.google.cloud.compute.v1.InstancesScopedList) Instance(com.google.cloud.compute.v1.Instance) AggregatedListPagedResponse(com.google.cloud.compute.v1.InstancesClient.AggregatedListPagedResponse) InstancesClient(com.google.cloud.compute.v1.InstancesClient) AggregatedListInstancesRequest(com.google.cloud.compute.v1.AggregatedListInstancesRequest) Map(java.util.Map)

Aggregations

Test (org.junit.Test)18 Instance (com.google.cloud.compute.v1.Instance)11 InstancesClient (com.google.cloud.compute.v1.InstancesClient)11 Instance (com.google.bigtable.admin.v2.Instance)10 ByteString (com.google.protobuf.ByteString)9 AbstractMessage (com.google.protobuf.AbstractMessage)8 Operation (com.google.cloud.compute.v1.Operation)7 InsertInstanceRequest (com.google.cloud.compute.v1.InsertInstanceRequest)6 AttachedDisk (com.google.cloud.compute.v1.AttachedDisk)5 NetworkInterface (com.google.cloud.compute.v1.NetworkInterface)5 Instance (com.google.cloud.redis.v1.Instance)5 HashMap (java.util.HashMap)5 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)4 Cluster (com.google.bigtable.admin.v2.Cluster)4 Instance (com.google.cloud.redis.v1beta1.Instance)4 FieldMask (com.google.protobuf.FieldMask)4 Instance (com.scaleset.cfbuilder.ec2.Instance)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 Operation (com.google.longrunning.Operation)3 CreateInstanceRequest (com.google.bigtable.admin.v2.CreateInstanceRequest)2