diff --git a/compute/api/Compute.Samples.Tests/ComputeFixture.cs b/compute/api/Compute.Samples.Tests/ComputeFixture.cs index 56a7cd0b62d..8fbdeb8f77c 100644 --- a/compute/api/Compute.Samples.Tests/ComputeFixture.cs +++ b/compute/api/Compute.Samples.Tests/ComputeFixture.cs @@ -34,13 +34,15 @@ public class ComputeFixture : IDisposable, ICollectionFixture public string DiskImage => "projects/debian-cloud/global/images/family/debian-10"; - public string DiskSizeGb => "10"; + public long DiskSizeGb => 10; public string NetworkName => "default"; public string UsageReportBucketName { get; } = GenerateName("b"); - public string UsageReportPrefix { get; } = "test-usage"; + public string UsageReportPrefix => "test-usage"; + + public string PublicImagesProjectId => "windows-sql-cloud"; private IList MachinesToDelete { get; } = new List(); diff --git a/compute/api/Compute.Samples.Tests/ListImagesAsyncTest.cs b/compute/api/Compute.Samples.Tests/ListImagesAsyncTest.cs new file mode 100644 index 00000000000..cd9cd4bd97b --- /dev/null +++ b/compute/api/Compute.Samples.Tests/ListImagesAsyncTest.cs @@ -0,0 +1,34 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System.Threading.Tasks; +using Xunit; + +namespace Compute.Samples.Tests +{ + [Collection(nameof(ComputeFixture))] + public class ListImagesAsyncTest + { + private readonly ComputeFixture _fixture; + private readonly ListImagesAsyncSample _listSample = new ListImagesAsyncSample(); + + public ListImagesAsyncTest(ComputeFixture fixture) => _fixture = fixture; + + [Fact] + public async Task ListsImages() => // Just test that it doesn't fail. + await _listSample.ListImagesAsync(_fixture.PublicImagesProjectId); + } +} diff --git a/compute/api/Compute.Samples.Tests/ListImagesPerPageAsyncTest.cs b/compute/api/Compute.Samples.Tests/ListImagesPerPageAsyncTest.cs new file mode 100644 index 00000000000..25d0463f8a6 --- /dev/null +++ b/compute/api/Compute.Samples.Tests/ListImagesPerPageAsyncTest.cs @@ -0,0 +1,35 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System.Threading.Tasks; +using Xunit; + +namespace Compute.Samples.Tests +{ + [Collection(nameof(ComputeFixture))] + public class ListImagesPerPageAsyncTest + { + private readonly ComputeFixture _fixture; + private readonly ListImagesPerPageAsyncSample _listSample = new ListImagesPerPageAsyncSample(); + + public ListImagesPerPageAsyncTest(ComputeFixture fixture) => _fixture = fixture; + + [Fact] + public async Task ListsImagesPerPage() => // Just test that it doesn't fail. + await _listSample.ListImagesPerPageAsync(_fixture.PublicImagesProjectId); + + } +} diff --git a/compute/api/Compute.Samples/Compute.Samples.csproj b/compute/api/Compute.Samples/Compute.Samples.csproj index 2a735f87f0a..ab614cfe9a3 100644 --- a/compute/api/Compute.Samples/Compute.Samples.csproj +++ b/compute/api/Compute.Samples/Compute.Samples.csproj @@ -5,7 +5,7 @@ - + diff --git a/compute/api/Compute.Samples/CreateInstanceAsync.cs b/compute/api/Compute.Samples/CreateInstanceAsync.cs index 40a0156913c..73c4ca9bed0 100644 --- a/compute/api/Compute.Samples/CreateInstanceAsync.cs +++ b/compute/api/Compute.Samples/CreateInstanceAsync.cs @@ -28,7 +28,7 @@ public async Task CreateInstanceAsync( string machineName = "test-machine", string machineType = "n1-standard-1", string diskImage = "projects/debian-cloud/global/images/family/debian-10", - string diskSizeGb = "10", + long diskSizeGb = 10, string networkName = "default") { Instance instance = new Instance diff --git a/compute/api/Compute.Samples/ListAllInstancesAsync.cs b/compute/api/Compute.Samples/ListAllInstancesAsync.cs index f3f10675aec..b52cbd89bdd 100644 --- a/compute/api/Compute.Samples/ListAllInstancesAsync.cs +++ b/compute/api/Compute.Samples/ListAllInstancesAsync.cs @@ -30,31 +30,20 @@ public async Task> ListAllInstancesAsync( // 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 allInstances = new List(); // Make the request to list all VM instances in a project. - AggregatedListInstancesRequest request = new AggregatedListInstancesRequest { Project = projectId }; - do + await foreach (var instancesByZone in client.AggregatedListAsync(projectId)) { - 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($"Instances for zone: {instancesByZone.Key}"); - foreach (var instance in instancesByZone.Value.Instances) - { - Console.WriteLine($"-- Name: {instance.Name}"); - allInstances.Add(instance); - } + 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; } diff --git a/compute/api/Compute.Samples/ListImagesAsync.cs b/compute/api/Compute.Samples/ListImagesAsync.cs new file mode 100644 index 00000000000..0f53004f723 --- /dev/null +++ b/compute/api/Compute.Samples/ListImagesAsync.cs @@ -0,0 +1,54 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START compute_images_list] + +using Google.Cloud.Compute.V1; +using System; +using System.Threading.Tasks; + +public class ListImagesAsyncSample +{ + public async Task ListImagesAsync( + // 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. + ImagesClient client = await ImagesClient.CreateAsync(); + + // Make the request to list all non-deprecated images in a project. + ListImagesRequest request = new ListImagesRequest + { + Project = projectId, + // Listing only non-deprecated images to reduce the size of the reply. + Filter = "deprecated.state != DEPRECATED", + // MaxResults indicates the maximum number of items that will be returned per page. + MaxResults = 100 + }; + + // Although the MaxResults parameter is specified in the request, the sequence returned + // by the ListAsync() method hides the pagination mechanic. The library makes multiple + // requests to the API for you, so you can simply iterate over all the images. + await foreach (var image in client.ListAsync(request)) + { + // The result is an Image collection. + Console.WriteLine($"Image: {image.Name}"); + } + } +} + +// [END compute_images_list] diff --git a/compute/api/Compute.Samples/ListImagesPerPageAsync.cs b/compute/api/Compute.Samples/ListImagesPerPageAsync.cs new file mode 100644 index 00000000000..3eee6fa79c1 --- /dev/null +++ b/compute/api/Compute.Samples/ListImagesPerPageAsync.cs @@ -0,0 +1,60 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START compute_images_list_page] + +using Google.Cloud.Compute.V1; +using System; +using System.Threading.Tasks; + +public class ListImagesPerPageAsyncSample +{ + public async Task ListImagesPerPageAsync( + // 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. + ImagesClient client = await ImagesClient.CreateAsync(); + + // Make the request to list all non-deprecated images in a project. + ListImagesRequest request = new ListImagesRequest + { + Project = projectId, + // Listing only non-deprecated images to reduce the size of the reply. + Filter = "deprecated.state != DEPRECATED", + // MaxResults indicates the maximum number of items that will be returned per page. + MaxResults = 10 + }; + + // Call the AsRawResponses() method of the returned image sequence to access the page sequece instead + // This allows you to have more granular control of iteration over paginated results from the API. + // Each time you access the next page, the library retrieves that page from the API. + int pageIndex = 0; + await foreach (var page in client.ListAsync(request).AsRawResponses()) + { + Console.WriteLine($"Page index: {pageIndex}"); + pageIndex++; + foreach (var image in page) + { + // The result is an Image collection. + Console.WriteLine($"Image: {image.Name}"); + } + } + } +} + +// [END compute_images_list_page] diff --git a/compute/api/Compute.Samples/ListZoneInstancesAsync.cs b/compute/api/Compute.Samples/ListZoneInstancesAsync.cs index 9eb62182e8a..e71c35f4392 100644 --- a/compute/api/Compute.Samples/ListZoneInstancesAsync.cs +++ b/compute/api/Compute.Samples/ListZoneInstancesAsync.cs @@ -31,30 +31,15 @@ public async Task> ListZoneInstancesAsync( // 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 allInstances = new List(); // 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 + await foreach(var instance in client.ListAsync(projectId, zone)) { - 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); + Console.WriteLine($"Instance: {instance.Name}"); + allInstances.Add(instance); + } return allInstances; }