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);
    }
Ejemplo n.º 2
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
        }
    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);
    }
        /// <summary>Snippet for DeleteInstance</summary>
        /// <remarks>
        /// This snippet has been automatically generated for illustrative purposes only.
        /// It may require modifications to work in your environment.
        /// </remarks>
        public void DeleteInstanceRequestObject()
        {
            // Create client
            InstancesClient instancesClient = InstancesClient.Create();
            // Initialize request argument(s)
            DeleteInstanceRequest request = new DeleteInstanceRequest {
                Name = "",
            };
            // Make the request
            Operation <Empty, OperationMetadataV1> response = instancesClient.DeleteInstance(request);

            // Poll until the returned long-running operation is complete
            Operation <Empty, OperationMetadataV1> completedResponse = response.PollUntilCompleted();
            // 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 = instancesClient.PollOnceDeleteInstance(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;
            }
        }
Ejemplo n.º 5
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;
            }
        }
Ejemplo n.º 6
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 GetInstance</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void GetInstanceRequestObject()
 {
     // Create client
     InstancesClient instancesClient = InstancesClient.Create();
     // Initialize request argument(s)
     GetInstanceRequest request = new GetInstanceRequest {
         Name = "",
     };
     // Make the request
     Instance response = instancesClient.GetInstance(request);
 }
        /// <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);
        }
Ejemplo n.º 9
0
        private void CleanUpInstances()
        {
            var client           = InstancesClient.Create();
            var list             = client.List(ProjectId, Zone);
            var fixtureInstances = list
                                   .Select(instance => instance.Name)
                                   .Where(name => name.StartsWith(ResourcePrefix));

            foreach (var instanceToDelete in fixtureInstances)
            {
                client.Delete(ProjectId, Zone, instanceToDelete);
            }
        }
Ejemplo n.º 10
0
 /// <summary>Snippet for GetInstance</summary>
 public void GetInstanceRequestObject()
 {
     // Snippet: GetInstance(GetInstanceRequest, CallSettings)
     // Create client
     InstancesClient instancesClient = InstancesClient.Create();
     // Initialize request argument(s)
     GetInstanceRequest request = new GetInstanceRequest {
         Name = "",
     };
     // Make the request
     Instance response = instancesClient.GetInstance(request);
     // End snippet
 }
Ejemplo n.º 11
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();
    }
Ejemplo n.º 12
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
        }
Ejemplo n.º 13
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();
    }
    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);
    }
Ejemplo n.º 15
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
        }
Ejemplo n.º 16
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);
    }
        public void Dispose()
        {
            if (_disposed)
            {
                return;
            }

            BestEffortCleanup(() => StorageClient.DeleteBucket(UsageReportBucketName, new DeleteBucketOptions {
                DeleteObjects = true
            }));

            foreach (var machineName in MachinesToDelete)
            {
                // We don't poll, we delete on a best effort basis.
                BestEffortCleanup(() => InstancesClient.Delete(ProjectId, Zone, machineName));
            }
            _disposed = true;
Ejemplo n.º 18
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);
    }
Ejemplo n.º 19
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> > 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);
    }
    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);
    }
Ejemplo n.º 22
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);
        }