コード例 #1
0
    public async Task <IList <Instance> > ListAllInstancesAsync(
        // TODO(developer): Set your own default values for these parameters or pass different values when calling this method.
        string projectId = "your-project-id")
    {
        // 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();

        IList <Instance> allInstances = new List <Instance>();

        // Make the request to list all VM instances in a project.
        await foreach (var instancesByZone in client.AggregatedListAsync(projectId))
        {
            // The result contains a KeyValuePair collection, where the key is a zone and the value
            // is a collection of instances in that zone.
            Console.WriteLine($"Instances for zone: {instancesByZone.Key}");
            foreach (var instance in instancesByZone.Value.Instances)
            {
                Console.WriteLine($"-- Name: {instance.Name}");
                allInstances.Add(instance);
            }
        }

        return(allInstances);
    }
コード例 #2
0
        /// <summary>Snippet for DebugInstanceAsync</summary>
        /// <remarks>
        /// This snippet has been automatically generated for illustrative purposes only.
        /// It may require modifications to work in your environment.
        /// </remarks>
        public async Task DebugInstanceRequestObjectAsync()
        {
            // Create client
            InstancesClient instancesClient = await InstancesClient.CreateAsync();

            // Initialize request argument(s)
            DebugInstanceRequest request = new DebugInstanceRequest {
                Name = "", SshKey = "",
            };
            // Make the request
            Operation <Instance, OperationMetadataV1> response = await instancesClient.DebugInstanceAsync(request);

            // Poll until the returned long-running operation is complete
            Operation <Instance, OperationMetadataV1> completedResponse = await response.PollUntilCompletedAsync();

            // Retrieve the operation result
            Instance result = completedResponse.Result;

            // Or get the name of the operation
            string operationName = response.Name;
            // This name can be stored, then the long-running operation retrieved later by name
            Operation <Instance, OperationMetadataV1> retrievedResponse = await instancesClient.PollOnceDebugInstanceAsync(operationName);

            // Check if the retrieved long-running operation has completed
            if (retrievedResponse.IsCompleted)
            {
                // If it has completed, then access the result
                Instance retrievedResult = retrievedResponse.Result;
            }
        }
コード例 #3
0
    public async Task <IList <Instance> > ListZoneInstancesAsync(string projectId = "your-project-id", string zone = "us-central1-a")
    {
        // 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();

        InstanceList     instanceList;
        IList <Instance> allInstances = new List <Instance>();

        // Make the requests to list all VM instances.
        ListInstancesRequest request = new ListInstancesRequest
        {
            Project = projectId,
            Zone    = zone,
        };

        do
        {
            instanceList = await client.ListAsync(request);

            // The result is an Instance collection.
            foreach (var instance in instanceList.Items)
            {
                Console.WriteLine($"-- Name: {instance.Name}");
                allInstances.Add(instance);
            }
            // Use the NextPageToken value on the request result to make subsequent requests
            // until all instances have been listed.
            request.PageToken = instanceList.NextPageToken;

            // When all instances are listed the last result NextPageToken is not set.
        } while (instanceList.HasNextPageToken);

        return(allInstances);
    }
コード例 #4
0
        /// <summary>Snippet for DeleteInstanceAsync</summary>
        public async Task DeleteInstanceRequestObjectAsync()
        {
            // Snippet: DeleteInstanceAsync(DeleteInstanceRequest, CallSettings)
            // Additional: DeleteInstanceAsync(DeleteInstanceRequest, CancellationToken)
            // Create client
            InstancesClient instancesClient = await InstancesClient.CreateAsync();

            // Initialize request argument(s)
            DeleteInstanceRequest request = new DeleteInstanceRequest {
                Name = "",
            };
            // Make the request
            Operation <Empty, OperationMetadataV1> response = await instancesClient.DeleteInstanceAsync(request);

            // Poll until the returned long-running operation is complete
            Operation <Empty, OperationMetadataV1> completedResponse = await response.PollUntilCompletedAsync();

            // Retrieve the operation result
            Empty result = completedResponse.Result;

            // Or get the name of the operation
            string operationName = response.Name;
            // This name can be stored, then the long-running operation retrieved later by name
            Operation <Empty, OperationMetadataV1> retrievedResponse = await instancesClient.PollOnceDeleteInstanceAsync(operationName);

            // Check if the retrieved long-running operation has completed
            if (retrievedResponse.IsCompleted)
            {
                // If it has completed, then access the result
                Empty retrievedResult = retrievedResponse.Result;
            }
            // End snippet
        }
コード例 #5
0
        private static async Task <Instance> GetInstance(string project, string zone, string instance)
        {
            var client = await InstancesClient.CreateAsync();

            var currentInstance = await client.GetAsync(project, zone, instance);

            return(currentInstance);
        }
        /// <summary>Snippet for GetInstanceAsync</summary>
        /// <remarks>
        /// This snippet has been automatically generated for illustrative purposes only.
        /// It may require modifications to work in your environment.
        /// </remarks>
        public async Task GetInstanceRequestObjectAsync()
        {
            // Create client
            InstancesClient instancesClient = await InstancesClient.CreateAsync();

            // Initialize request argument(s)
            GetInstanceRequest request = new GetInstanceRequest {
                Name = "",
            };
            // Make the request
            Instance response = await instancesClient.GetInstanceAsync(request);
        }
コード例 #7
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();
    }
コード例 #8
0
        /// <summary>Snippet for GetInstanceAsync</summary>
        public async Task GetInstanceRequestObjectAsync()
        {
            // Snippet: GetInstanceAsync(GetInstanceRequest, CallSettings)
            // Additional: GetInstanceAsync(GetInstanceRequest, CancellationToken)
            // Create client
            InstancesClient instancesClient = await InstancesClient.CreateAsync();

            // Initialize request argument(s)
            GetInstanceRequest request = new GetInstanceRequest {
                Name = "",
            };
            // Make the request
            Instance response = await instancesClient.GetInstanceAsync(request);

            // End snippet
        }
コード例 #9
0
    public async Task DeleteInstanceAsync(
        // 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")
    {
        // 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();

        // Make the request to delete a VM instance.
        var instanceDeletion = await client.DeleteAsync(projectId, zone, machineName);

        // Wait for the operation to complete using client-side polling.
        await instanceDeletion.PollUntilCompletedAsync();
    }
コード例 #10
0
    public async Task DeleteInstanceAsync(
        string projectId   = "your-project-id",
        string zone        = "us-central1-a",
        string machineName = "test-machine")
    {
        // 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 delete a VM instance.
        Operation instanceDeletion = await client.DeleteAsync(projectId, zone, machineName);

        // 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 instanceDeletion.PollUntilCompletedAsync(projectId, zone);
    }
コード例 #11
0
        /// <summary>Snippet for ListInstancesAsync</summary>
        public async Task ListInstancesRequestObjectAsync()
        {
            // Snippet: ListInstancesAsync(ListInstancesRequest, CallSettings)
            // Create client
            InstancesClient instancesClient = await InstancesClient.CreateAsync();

            // Initialize request argument(s)
            ListInstancesRequest request = new ListInstancesRequest {
                Parent = "",
            };
            // Make the request
            PagedAsyncEnumerable <ListInstancesResponse, Instance> response = instancesClient.ListInstancesAsync(request);

            // Iterate over all response items, lazily performing RPCs as required
            await response.ForEachAsync((Instance item) =>
            {
                // Do something with each item
                Console.WriteLine(item);
            });

            // Or iterate over pages (of server-defined size), performing one RPC per page
            await response.AsRawResponses().ForEachAsync((ListInstancesResponse page) =>
            {
                // Do something with each page of items
                Console.WriteLine("A page of results:");
                foreach (Instance item in page)
                {
                    // Do something with each item
                    Console.WriteLine(item);
                }
            });

            // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
            int             pageSize   = 10;
            Page <Instance> singlePage = await response.ReadPageAsync(pageSize);

            // Do something with the page of items
            Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
            foreach (Instance item in singlePage)
            {
                // Do something with each item
                Console.WriteLine(item);
            }
            // Store the pageToken, for when the next page is required.
            string nextPageToken = singlePage.NextPageToken;
            // End snippet
        }
コード例 #12
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);
    }
コード例 #13
0
    public async Task <IList <Instance> > ListZoneInstancesAsync(
        // 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")
    {
        // 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();

        IList <Instance> allInstances = new List <Instance>();

        // Make the request to list all VM instances in the given zone in the specified project.
        await foreach (var instance in client.ListAsync(projectId, zone))
        {
            // The result is an Instance collection.
            Console.WriteLine($"Instance: {instance.Name}");
            allInstances.Add(instance);
        }

        return(allInstances);
    }
コード例 #14
0
    public async Task <IList <Instance> > ListAllInstancesAsync(
        // TODO(developer): Set your own default values for these parameters or pass different values when calling this method.
        string projectId = "your-project-id")
    {
        // 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();

        InstanceAggregatedList instanceList;
        IList <Instance>       allInstances = new List <Instance>();

        // Make the request to list all VM instances in a project.
        AggregatedListInstancesRequest request = new AggregatedListInstancesRequest {
            Project = projectId
        };

        do
        {
            instanceList = await client.AggregatedListAsync(request);

            // The result contains a KeyValuePair collection, where the key is a zone and the value
            // is a collection of instances in that zone.
            foreach (var instancesByZone in instanceList.Items)
            {
                Console.WriteLine($"Instances for zone: {instancesByZone.Key}");
                foreach (var instance in instancesByZone.Value.Instances)
                {
                    Console.WriteLine($"-- Name: {instance.Name}");
                    allInstances.Add(instance);
                }
            }
            // Use the NextPageToken value on the request result to make subsequent requests
            // until all instances have been listed.
            request.PageToken = instanceList.NextPageToken;

            // When all instances are listed the last result NextPageToken is not set.
        } while (instanceList.HasNextPageToken);

        return(allInstances);
    }
コード例 #15
0
    public async Task <IList <Instance> > ListAllInstancesAsync(string projectId = "your-project-id")
    {
        // 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();

        InstanceAggregatedList instanceList;
        IList <Instance>       allInstances = new List <Instance>();

        // Make the requests to list all VM instances.
        AggregatedListInstancesRequest request = new AggregatedListInstancesRequest {
            Project = projectId
        };

        do
        {
            instanceList = await client.AggregatedListAsync(request);

            // The result contains a KeyValuePair collection, where the key is a zone and the value
            // is a collection of instances in that zone.
            foreach (var instancesByZone in instanceList.Items)
            {
                Console.WriteLine($"Instances for zone: {instancesByZone.Key}");
                foreach (var instance in instancesByZone.Value.Instances)
                {
                    Console.WriteLine($"-- Name: {instance.Name}");
                    allInstances.Add(instance);
                }
            }
            // Use the NextPageToken value on the request result to make subsequent requests
            // until all instances have been listed.
            request.PageToken = instanceList.NextPageToken;

            // When all instances are listed the last result NextPageToken is not set.
        } while (instanceList.HasNextPageToken);

        return(allInstances);
    }
コード例 #16
0
    public async Task <IList <Instance> > ListZoneInstancesAsync(
        // 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")
    {
        // 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();

        InstanceList     instanceList;
        IList <Instance> allInstances = new List <Instance>();

        // Make the request to list all VM instances in the given zone in the specified project.
        ListInstancesRequest request = new ListInstancesRequest
        {
            Project = projectId,
            Zone    = zone,
        };

        do
        {
            instanceList = await client.ListAsync(request);

            // The result is an Instance collection.
            foreach (var instance in instanceList.Items)
            {
                Console.WriteLine($"-- Name: {instance.Name}");
                allInstances.Add(instance);
            }
            // Use the NextPageToken value on the request result to make subsequent requests
            // until all instances have been listed.
            request.PageToken = instanceList.NextPageToken;

            // When all instances are listed the last result NextPageToken is not set.
        } while (instanceList.HasNextPageToken);

        return(allInstances);
    }
コード例 #17
0
        private static async Task SetLabelsAsync(string project, string zone, string instance, string username)
        {
            var currentInstance = await GetInstance(project, zone, instance);

            var labels = currentInstance.Labels;

            labels.Add("username", username);

            var request = new SetLabelsInstanceRequest
            {
                Project  = project,
                Zone     = zone,
                Instance = instance,
                InstancesSetLabelsRequestResource = new InstancesSetLabelsRequest
                {
                    Labels           = { labels },
                    LabelFingerprint = currentInstance.LabelFingerprint
                }
            };

            var client = await InstancesClient.CreateAsync();

            await client.SetLabelsAsync(request);
        }