use of com.google.cloud.compute.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())));
}
}
use of com.google.cloud.compute.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);
}
}
use of com.google.cloud.compute.v1.Instance in project java-docs-samples by GoogleCloudPlatform.
the class CreateInstanceTemplate method createInstanceTemplateWithDiskType.
public static void createInstanceTemplateWithDiskType(String projectId, String templateName) throws IOException, ExecutionException, InterruptedException {
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
AttachedDisk disk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setDiskSizeGb(10).setSourceImage("projects/debian-cloud/global/images/family/debian-10").build()).setAutoDelete(true).setBoot(true).setType(AttachedDisk.Type.PERSISTENT.toString()).build();
InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder().setName(templateName).setProperties(InstanceProperties.newBuilder().setMachineType("n1-standard-1").addDisks(disk).addNetworkInterfaces(NetworkInterface.newBuilder().setName("global/networks/default").build()).build()).build();
InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplateResource(instanceTemplate).build();
Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest).get();
if (response.hasError()) {
System.out.println("Instance Template creation failed ! ! " + response);
return;
}
System.out.printf("Instance Template Operation Status %s: %s", templateName, response.getStatus());
}
}
use of com.google.cloud.compute.v1.Instance in project java-docs-samples by GoogleCloudPlatform.
the class CreateTemplateFromInstance method createTemplateFromInstance.
// Create a new instance template based on an existing instance.
// This new template specifies a different boot disk.
public static void createTemplateFromInstance(String projectId, String templateName, String instance) throws IOException, ExecutionException, InterruptedException {
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
SourceInstanceParams sourceInstanceParams = SourceInstanceParams.newBuilder().addDiskConfigs(DiskInstantiationConfig.newBuilder().setDeviceName("disk-1").setInstantiateFrom(InstantiateFrom.CUSTOM_IMAGE.toString()).setCustomImage(String.format("projects/%s/global/images/family/%s", "rocky-linux-cloud", "rocky-linux-8")).setAutoDelete(true).build()).build();
InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder().setName(templateName).setSourceInstance(instance).setSourceInstanceParams(sourceInstanceParams).build();
InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplateResource(instanceTemplate).build();
Operation operation = instanceTemplatesClient.insertCallable().futureCall(insertInstanceTemplateRequest).get();
Operation response = globalOperationsClient.wait(projectId, operation.getName());
if (response.hasError()) {
System.out.println("Instance Template creation failed ! ! " + response);
return;
}
System.out.printf("Instance Template creation operation status %s: %s", templateName, response.getStatus());
}
}
use of com.google.cloud.compute.v1.Instance in project java-docs-samples by GoogleCloudPlatform.
the class DeleteInstanceTemplate method deleteInstanceTemplate.
// Delete an instance template.
public static void deleteInstanceTemplate(String projectId, String templateName) throws IOException, ExecutionException, InterruptedException {
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
DeleteInstanceTemplateRequest deleteInstanceTemplateRequest = DeleteInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplate(templateName).build();
Operation response = instanceTemplatesClient.deleteAsync(deleteInstanceTemplateRequest).get();
if (response.hasError()) {
System.out.println("Instance template deletion failed ! ! " + response);
return;
}
System.out.printf("Instance template deletion operation status for %s: %s ", templateName, response.getStatus());
}
}
Aggregations