use of com.google.cloud.redis.v1beta1.Instance in project TOSCAna by StuPro-TOSCAna.
the class CloudFormationVisitor method createSqlEc2.
/**
* Creates a EC2 for a {@link MysqlDatabase} that runs an initial sql query.
*
* @param mysqlDatabase the {@link MysqlDatabase} this sql query should be run on
* @param sqlQuery the sql query to run
* @return the instance name of the created EC2 Instance
*/
protected String createSqlEc2(MysqlDatabase mysqlDatabase, String sqlQuery) {
String ec2Name = toAlphanumerical(mysqlDatabase.getEntityName()) + "TmpSqlServer";
SecurityGroup webServerSecurityGroup = cfnModule.resource(SecurityGroup.class, ec2Name + SECURITY_GROUP).groupDescription("Temporary group for accessing mysqlDatabase" + toAlphanumerical(mysqlDatabase.getEntityName()) + " with SQLRequest");
cfnModule.resource(Instance.class, ec2Name).securityGroupIds(webServerSecurityGroup).imageId("ami-79873901").instanceType("t2.micro").instanceInitiatedShutdownBehavior("terminate").userData(new UserData(StackUtils.getUserDataDBConnFn(mysqlDatabase, sqlQuery)));
return ec2Name;
}
use of com.google.cloud.redis.v1beta1.Instance in project TOSCAna by StuPro-TOSCAna.
the class CloudFormationModule method build.
/**
* Build the template.
* <br>
* Following steps are taken:
* <ol>
* <li>Add CFNInit to corresponding instance resource </li>
* <li>Check if EC2 instances need access to S3. If yes, then
* <ol>
* <li>Add necessary IAM resources to the module</li>
* <li>Add <tt>Authentication<tt> and <tt>IamInstanceProfile<tt> to corresponding instance resource</li>
* </ol>
* </li>
* <li>Add the KeyPair to the template if it is needed</li>
* </ol>
*/
@Override
public void build() {
for (Map.Entry<String, CFNInit> pair : cfnInitMap.entrySet()) {
Resource res = this.getResource(pair.getKey());
if (res instanceof Instance) {
Instance instance = (Instance) res;
if (!pair.getValue().getConfigs().isEmpty()) {
instance.addCFNInit(pair.getValue()).userData(new UserData(getUserDataFn(pair.getKey(), CONFIG_SETS, this)));
}
}
}
if (!fileUploadList.isEmpty()) {
Role instanceRole = getS3InstanceRole(this);
getS3Policy(this).roles(instanceRole);
getS3InstanceProfile(this).roles(instanceRole);
Authentication s3authentication = getS3Authentication(bucketName);
for (String instanceName : authenticationSet) {
Resource res = this.getResource(instanceName);
if (res instanceof Instance) {
Instance instance = (Instance) res;
instance.authentication(s3authentication).iamInstanceProfile(ref(INSTANCE_PROFILE));
}
}
}
if (this.hasKeyPair()) {
strParam(KEYNAME).type(KEYNAME_TYPE).description(KEYNAME_DESCRIPTION).constraintDescription(KEYNAME_CONSTRAINT_DESCRIPTION);
}
}
use of com.google.cloud.redis.v1beta1.Instance in project java-docs-samples by GoogleCloudPlatform.
the class ListInstance method listInstances.
// List all instances in the given zone in the specified project ID.
public static void listInstances(String project, String zone) throws IOException {
// safely clean up any remaining background resources.
try (InstancesClient instancesClient = InstancesClient.create()) {
// Set the project and zone to retrieve instances present in the zone.
System.out.printf("Listing instances from %s in %s:", project, zone);
for (Instance zoneInstance : instancesClient.list(project, zone).iterateAll()) {
System.out.println(zoneInstance.getName());
}
System.out.println("####### Listing instances complete #######");
}
}
use of com.google.cloud.redis.v1beta1.Instance in project java-docs-samples by GoogleCloudPlatform.
the class CreateEncryptedInstance method createEncryptedInstance.
// Create a new encrypted instance with the provided "instanceName" value and encryption key
// in the specified project and zone.
public static void createEncryptedInstance(String project, String zone, String instanceName, String diskEncryptionKey) 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-11");
long diskSizeGb = 10L;
String networkName = "default";
/* Initialize client that will be used to send requests. This client only needs to be created
once, and can be reused for multiple requests. After completing all of your requests, call
the `instancesClient.close()` method on the client to safely
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()).setInitializeParams(AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage).setDiskSizeGb(diskSizeGb).build()).setDiskEncryptionKey(CustomerEncryptionKey.newBuilder().setRawKey(diskEncryptionKey).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 ", 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());
}
}
use of com.google.cloud.redis.v1beta1.Instance in project java-docs-samples by GoogleCloudPlatform.
the class CreateInstancesAdvanced method createWithDisks.
// [END compute_instances_create_from_image_plus_snapshot_disk]
// [END compute_instances_create_from_snapshot]
// [START compute_instances_create_with_subnet]
// [START compute_instances_create_from_image_plus_snapshot_disk]
// [START compute_instances_create_from_snapshot]
// [START compute_instances_create_from_image_plus_empty_disk]
// [START compute_instances_create_from_custom_image]
// [START compute_instances_create_from_image]
/**
* Send an instance creation request to the Compute Engine API and wait for it to complete.
*
* @param project project ID or project number of the Cloud project you want to use.
* @param zone name of the zone to create the instance in. For example: "us-west3-b"
* @param instanceName name of the new virtual machine (VM) instance.
* @param disks a list of compute_v1.AttachedDisk objects describing the disks you want to attach
* to your new instance.
* @param machineType machine type of the VM being created. This value uses the following format:
* "zones/{zone}/machineTypes/{type_name}".
* For example: "zones/europe-west3-c/machineTypes/f1-micro"
* @param network name of the network you want the new instance to use. For example:
* "global/networks/default" represents the network named "default", which is created
* automatically for each project.
* @param subnetwork name of the subnetwork you want the new instance to use. This value uses the
* following format: "regions/{region}/subnetworks/{subnetwork_name}"
* @return Instance object.
*/
private static Instance createWithDisks(String project, String zone, String instanceName, Vector<AttachedDisk> disks, String machineType, String network, String subnetwork) throws IOException, InterruptedException, ExecutionException {
try (InstancesClient instancesClient = InstancesClient.create()) {
// Use the network interface provided in the networkName argument.
NetworkInterface networkInterface;
if (subnetwork != null) {
networkInterface = NetworkInterface.newBuilder().setName(network).setSubnetwork(subnetwork).build();
} else {
networkInterface = NetworkInterface.newBuilder().setName(network).build();
}
machineType = String.format("zones/%s/machineTypes/%s", zone, machineType);
// Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
Instance instanceResource = Instance.newBuilder().setName(instanceName).setMachineType(machineType).addAllDisks(disks).addNetworkInterfaces(networkInterface).build();
System.out.printf("Creating instance: %s at %s ", 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 null;
}
System.out.println("Operation Status: " + response.getStatus());
return instancesClient.get(project, zone, instanceName);
}
}
Aggregations