Пример #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();

        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);
    }
    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);
    }