Search in sources :

Example 51 with Instance

use of com.google.cloud.redis.v1.Instance in project java-bigtable by googleapis.

the class BaseBigtableInstanceAdminClientTest method createInstanceExceptionTest2.

@Test
public void createInstanceExceptionTest2() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockBigtableInstanceAdmin.addException(exception);
    try {
        String parent = "parent-995424086";
        String instanceId = "instanceId902024336";
        Instance instance = Instance.newBuilder().build();
        Map<String, Cluster> clusters = new HashMap<>();
        client.createInstanceAsync(parent, instanceId, instance, clusters).get();
        Assert.fail("No exception raised");
    } catch (ExecutionException e) {
        Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
        InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
        Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) Instance(com.google.bigtable.admin.v2.Instance) HashMap(java.util.HashMap) StatusRuntimeException(io.grpc.StatusRuntimeException) Cluster(com.google.bigtable.admin.v2.Cluster) ByteString(com.google.protobuf.ByteString) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 52 with Instance

use of com.google.cloud.redis.v1.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;
}
Also used : Instance(com.scaleset.cfbuilder.ec2.Instance) UserData(com.scaleset.cfbuilder.ec2.UserData) SecurityGroup(com.scaleset.cfbuilder.ec2.SecurityGroup)

Example 53 with Instance

use of com.google.cloud.redis.v1.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);
    }
}
Also used : AuthenticationUtils.getS3InstanceRole(org.opentosca.toscana.plugins.cloudformation.util.AuthenticationUtils.getS3InstanceRole) Role(com.scaleset.cfbuilder.iam.Role) Instance(com.scaleset.cfbuilder.ec2.Instance) UserData(com.scaleset.cfbuilder.ec2.UserData) AuthenticationUtils.getS3Authentication(org.opentosca.toscana.plugins.cloudformation.util.AuthenticationUtils.getS3Authentication) Authentication(com.scaleset.cfbuilder.cloudformation.Authentication) Resource(com.scaleset.cfbuilder.core.Resource) CFNInit(com.scaleset.cfbuilder.ec2.metadata.CFNInit) HashMap(java.util.HashMap) Map(java.util.Map)

Example 54 with Instance

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

the class CreateWindowsServerInstanceInternalIp method createWindowsServerInstanceInternalIp.

// Creates a new Windows Server instance that has only an internal IP address.
public static void createWindowsServerInstanceInternalIp(String projectId, String zone, String instanceName, String networkLink, String subnetworkLink) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // machineType - Machine type you want to create in following format:
    // *    "zones/{zone}/machineTypes/{type_name}". For example:
    // *    "zones/europe-west3-c/machineTypes/f1-micro"
    // *    You can find the list of available machine types using:
    // *    https://cloud.google.com/sdk/gcloud/reference/compute/machine-types/list
    String machineType = "n1-standard-1";
    // sourceImageFamily - Name of the public image family for Windows Server or SQL Server images.
    // *    https://cloud.google.com/compute/docs/images#os-compute-support
    String sourceImageFamily = "windows-2012-r2";
    // Instantiates a client.
    try (InstancesClient instancesClient = InstancesClient.create()) {
        AttachedDisk attachedDisk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setDiskSizeGb(64).setSourceImage(String.format("projects/windows-cloud/global/images/family/%s", sourceImageFamily)).build()).setAutoDelete(true).setBoot(true).setType(AttachedDisk.Type.PERSISTENT.toString()).build();
        Instance instance = Instance.newBuilder().setName(instanceName).setMachineType(String.format("zones/%s/machineTypes/%s", zone, machineType)).addDisks(attachedDisk).addNetworkInterfaces(NetworkInterface.newBuilder().setName(networkLink).setSubnetwork(subnetworkLink).build()).build();
        InsertInstanceRequest request = InsertInstanceRequest.newBuilder().setProject(projectId).setZone(zone).setInstanceResource(instance).build();
        // Wait for the operation to complete.
        Operation operation = instancesClient.insertAsync(request).get(3, TimeUnit.MINUTES);
        if (operation.hasError()) {
            System.out.printf("Error in creating instance %s", operation.getError());
            return;
        }
        System.out.printf("Instance created %s", instanceName);
    }
}
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)

Example 55 with Instance

use of com.google.cloud.redis.v1.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, TimeoutException {
    /* 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(3, TimeUnit.MINUTES);
        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)

Aggregations

Test (org.junit.Test)41 Instance (com.google.cloud.redis.v1beta1.Instance)34 CloudRedisClient (com.google.cloud.redis.v1beta1.CloudRedisClient)30 Instance (com.google.cloud.compute.v1.Instance)25 InstancesClient (com.google.cloud.compute.v1.InstancesClient)19 ByteString (com.google.protobuf.ByteString)17 AbstractMessage (com.google.protobuf.AbstractMessage)15 Operation (com.google.cloud.compute.v1.Operation)13 Instance (com.google.spanner.admin.instance.v1.Instance)11 Instance (com.google.bigtable.admin.v2.Instance)10 InsertInstanceRequest (com.google.cloud.compute.v1.InsertInstanceRequest)10 AttachedDisk (com.google.cloud.compute.v1.AttachedDisk)9 Instance (com.google.cloud.notebooks.v1beta1.Instance)9 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)7 Any (com.google.protobuf.Any)7 FieldMask (com.google.protobuf.FieldMask)7 StatusRuntimeException (io.grpc.StatusRuntimeException)7 ArrayList (java.util.ArrayList)7 NetworkInterface (com.google.cloud.compute.v1.NetworkInterface)6 Operation (com.google.longrunning.Operation)6