예제 #1
0
    public async Task CreateInstanceAsync(
        // TODO(developer): Set your own default values for these parameters or pass different values when calling this method.
        string projectId   = "your-project-id",
        string zone        = "us-central1-a",
        string machineName = "test-machine",
        string machineType = "n1-standard-1",
        string diskImage   = "projects/debian-cloud/global/images/family/debian-10",
        long diskSizeGb    = 10,
        string networkName = "default")
    {
        Instance instance = new Instance
        {
            Name = machineName,
            // See https://cloud.google.com/compute/docs/machine-types for more information on machine types.
            MachineType = $"zones/{zone}/machineTypes/{machineType}",
            // Instance creation requires at least one persistent disk.
            Disks =
            {
                new AttachedDisk
                {
                    AutoDelete       = true,
                    Boot             = true,
                    Type             = ComputeEnumConstants.AttachedDisk.Type.Persistent,
                    InitializeParams = new AttachedDiskInitializeParams
                    {
                        // See https://cloud.google.com/compute/docs/images for more information on available images.
                        SourceImage = diskImage,
                        DiskSizeGb  = diskSizeGb
                    }
                }
            },
            NetworkInterfaces = { new NetworkInterface {
                                      Name = networkName
                                  } }
        };

        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests.
        InstancesClient client = await InstancesClient.CreateAsync();

        // Insert the instance in the specified project and zone.
        var instanceCreation = await client.InsertAsync(projectId, zone, instance);

        // Wait for the operation to complete using client-side polling.
        // The server-side operation is not affected by polling,
        // and might finish successfully even if polling times out.
        await instanceCreation.PollUntilCompletedAsync();
    }
예제 #2
0
    public async Task CreateInstanceAsync(
        string projectId   = "your-project-id",
        string zone        = "us-central1-a",
        string machineName = "test-machine",
        string machineType = "n1-standard-1",
        string diskImage   = "projects/debian-cloud/global/images/family/debian-10",
        string diskSizeGb  = "10")
    {
        Instance instance = new Instance
        {
            Name = machineName,
            // See https://cloud.google.com/compute/docs/machine-types for more information on machine types.
            MachineType = $"zones/{zone}/machineTypes/{machineType}",
            // Instance creation requires at least one persistent disk.
            Disks =
            {
                new AttachedDisk
                {
                    AutoDelete       = true,
                    Boot             = true,
                    Type             = AttachedDisk.Types.Type.Persistent,
                    InitializeParams = new AttachedDiskInitializeParams
                    {
                        // See https://cloud.google.com/compute/docs/images for more information on available images.
                        SourceImage = diskImage,
                        DiskSizeGb  = diskSizeGb
                    }
                }
            },
            // Instance creation requires at least one network interface.
            // The "default" network interface is created automatically for every project.
            NetworkInterfaces = { new NetworkInterface {
                                      Name = "default"
                                  } }
        };

        // Initialize the client that will be used to send instance-related requests.
        // You should reuse the same client for multiple requests.
        InstancesClient client = await InstancesClient.CreateAsync();

        // Make the request to create a VM instance.
        Operation instanceCreation = await client.InsertAsync(projectId, zone, instance);

        // You may poll the operation until it completes or fails, or for a given amount of time.
        // If polling times out, the operation may still finish successfully after.
        await instanceCreation.PollUntilCompletedAsync(projectId, zone);
    }