From c392a2044ee36ea468f28095ad6a4686f5350aef Mon Sep 17 00:00:00 2001 From: JesseLovelace <43148100+JesseLovelace@users.noreply.github.com> Date: Thu, 9 May 2024 14:15:53 -0700 Subject: [PATCH] feat(storageinsights): Adds samples for storage insights (#2612) --- .../CreateInventoryReportConfigTest.cs | 59 ++++++++++ .../DeleteInventoryReportConfigTest.cs | 52 +++++++++ .../EditInventoryReportConfigTest.cs | 61 ++++++++++ .../GetInventoryReportNamesTest.cs | 65 +++++++++++ .../ListInventoryReportConfigsTest.cs | 61 ++++++++++ .../StorageFixture.cs | 110 ++++++++++++++++++ .../StorageInsights.Samples.Tests.csproj | 22 ++++ .../CreateInventoryReportConfig.cs | 63 ++++++++++ .../DeleteInventoryReportConfig.cs | 35 ++++++ .../EditInventoryReportConfig.cs | 51 ++++++++ .../GetInventoryReportNames.cs | 46 ++++++++ .../ListInventoryReportConfigs.cs | 41 +++++++ .../StorageInsights.Samples.csproj | 10 ++ storageinsights/api/StorageInsights.sln | 28 +++++ storageinsights/api/runTests.ps1 | 16 +++ 15 files changed, 720 insertions(+) create mode 100644 storageinsights/api/StorageInsights.Samples.Tests/CreateInventoryReportConfigTest.cs create mode 100644 storageinsights/api/StorageInsights.Samples.Tests/DeleteInventoryReportConfigTest.cs create mode 100644 storageinsights/api/StorageInsights.Samples.Tests/EditInventoryReportConfigTest.cs create mode 100644 storageinsights/api/StorageInsights.Samples.Tests/GetInventoryReportNamesTest.cs create mode 100644 storageinsights/api/StorageInsights.Samples.Tests/ListInventoryReportConfigsTest.cs create mode 100644 storageinsights/api/StorageInsights.Samples.Tests/StorageFixture.cs create mode 100644 storageinsights/api/StorageInsights.Samples.Tests/StorageInsights.Samples.Tests.csproj create mode 100644 storageinsights/api/StorageInsights.Samples/CreateInventoryReportConfig.cs create mode 100644 storageinsights/api/StorageInsights.Samples/DeleteInventoryReportConfig.cs create mode 100644 storageinsights/api/StorageInsights.Samples/EditInventoryReportConfig.cs create mode 100644 storageinsights/api/StorageInsights.Samples/GetInventoryReportNames.cs create mode 100644 storageinsights/api/StorageInsights.Samples/ListInventoryReportConfigs.cs create mode 100644 storageinsights/api/StorageInsights.Samples/StorageInsights.Samples.csproj create mode 100644 storageinsights/api/StorageInsights.sln create mode 100644 storageinsights/api/runTests.ps1 diff --git a/storageinsights/api/StorageInsights.Samples.Tests/CreateInventoryReportConfigTest.cs b/storageinsights/api/StorageInsights.Samples.Tests/CreateInventoryReportConfigTest.cs new file mode 100644 index 00000000000..24055cd1078 --- /dev/null +++ b/storageinsights/api/StorageInsights.Samples.Tests/CreateInventoryReportConfigTest.cs @@ -0,0 +1,59 @@ +// Copyright 2024 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 Google.Cloud.StorageInsights.V1; +using GoogleCloudSamples; +using System; +using Xunit; + +namespace StorageInsights.Samples.Tests +{ + [Collection(nameof(StorageFixture))] + public class CreateInventoryReportConfigTest : IDisposable + { + private readonly StorageFixture _fixture; + private string _reportConfigName; + + public CreateInventoryReportConfigTest(StorageFixture fixture) => _fixture = fixture; + + [Fact] + public void TestCreateInventoryReportConfigSample() + { + // The permissions the service account needs sometimes needs a few minutes to propagate, so retry permission issues + RetryRobot robot = new RetryRobot() { ShouldRetry = (e) => e.Message.Contains("PermissionDenied") }; + robot.Eventually(() => + { + CreateInventoryReportConfigSample sample = new CreateInventoryReportConfigSample(); + var reportConfig = sample.CreateInventoryReportConfig(_fixture.ProjectId, _fixture.BucketLocation, + _fixture.BucketNameSource, + _fixture.BucketNameSink); + Assert.NotNull(reportConfig.DisplayName); + _reportConfigName = reportConfig.Name; + } + ); + } + + public void Dispose() + { + try + { + _fixture.StorageInsights.DeleteReportConfig(_reportConfigName); + } + catch (Exception) + { + // Do nothing, we delete on a best effort basis. + } + } + } +} diff --git a/storageinsights/api/StorageInsights.Samples.Tests/DeleteInventoryReportConfigTest.cs b/storageinsights/api/StorageInsights.Samples.Tests/DeleteInventoryReportConfigTest.cs new file mode 100644 index 00000000000..efb1a441954 --- /dev/null +++ b/storageinsights/api/StorageInsights.Samples.Tests/DeleteInventoryReportConfigTest.cs @@ -0,0 +1,52 @@ +// Copyright 2024 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 Google.Api.Gax; +using Google.Api.Gax.ResourceNames; +using Google.Cloud.StorageInsights.V1; +using GoogleCloudSamples; +using Xunit; + +namespace StorageInsights.Samples.Tests; + +[Collection(nameof(StorageFixture))] +public class DeleteInventoryReportConfigTest +{ + private readonly StorageFixture _fixture; + + public DeleteInventoryReportConfigTest(StorageFixture fixture) => _fixture = fixture; + + [Fact] + public void TestDeleteInventoryReportConfig() + { + // The permissions the service account needs sometimes needs a few minutes to propagate, so retry permission issues + RetryRobot robot = new RetryRobot() { ShouldRetry = (e) => e.Message.Contains("PermissionDenied") }; + ReportConfig reportConfig = null; + robot.Eventually(() => + { + reportConfig = new CreateInventoryReportConfigSample().CreateInventoryReportConfig(_fixture.ProjectId, + _fixture.BucketLocation, _fixture.BucketNameSource, + _fixture.BucketNameSink); + }); + + DeleteInventoryReportConfigSample sample = new DeleteInventoryReportConfigSample(); + sample.DeleteInventoryReportConfig(_fixture.ProjectId, _fixture.BucketLocation, + reportConfig.ReportConfigName.ReportConfigId); + + PagedEnumerable reportConfigs = _fixture.StorageInsights.ListReportConfigs( + LocationName.Format(_fixture.ProjectId, _fixture.BucketLocation)); + + Assert.DoesNotContain(reportConfigs, config => config.Name.Equals(reportConfig.Name)); + } +} diff --git a/storageinsights/api/StorageInsights.Samples.Tests/EditInventoryReportConfigTest.cs b/storageinsights/api/StorageInsights.Samples.Tests/EditInventoryReportConfigTest.cs new file mode 100644 index 00000000000..ec2b9c07366 --- /dev/null +++ b/storageinsights/api/StorageInsights.Samples.Tests/EditInventoryReportConfigTest.cs @@ -0,0 +1,61 @@ +// Copyright 2024 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 Google.Cloud.StorageInsights.V1; +using GoogleCloudSamples; +using System; +using Xunit; + +namespace StorageInsights.Samples.Tests; + +[Collection(nameof(StorageFixture))] +public class EditInventoryReportConfigTest : IDisposable +{ + private readonly StorageFixture _fixture; + private string _reportConfigName; + + public EditInventoryReportConfigTest(StorageFixture fixture) => _fixture = fixture; + + [Fact] + public void TestEditInventoryReportConfig() + { + ReportConfig reportConfig = null; + // The permissions the service account needs sometimes needs a few minutes to propagate, so retry permission issues + RetryRobot robot = new RetryRobot() { ShouldRetry = (e) => e.Message.Contains("PermissionDenied") }; + robot.Eventually(() => + { + reportConfig = new CreateInventoryReportConfigSample().CreateInventoryReportConfig(_fixture.ProjectId, + _fixture.BucketLocation, _fixture.BucketNameSource, + _fixture.BucketNameSink); + _reportConfigName = reportConfig.Name; + }); + EditInventoryReportConfigSample sample = new EditInventoryReportConfigSample(); + var updatedConfig = sample.EditInventoryReportConfig(_fixture.ProjectId, _fixture.BucketLocation, + reportConfig.ReportConfigName.ReportConfigId); + + Assert.NotEqual(reportConfig.DisplayName, updatedConfig.DisplayName); + } + + public void Dispose() + { + try + { + _fixture.StorageInsights.DeleteReportConfig(_reportConfigName); + } + catch (Exception) + { + // Do nothing, we delete on a best effort basis. + } + } +} diff --git a/storageinsights/api/StorageInsights.Samples.Tests/GetInventoryReportNamesTest.cs b/storageinsights/api/StorageInsights.Samples.Tests/GetInventoryReportNamesTest.cs new file mode 100644 index 00000000000..4ac1cf189a7 --- /dev/null +++ b/storageinsights/api/StorageInsights.Samples.Tests/GetInventoryReportNamesTest.cs @@ -0,0 +1,65 @@ +// Copyright 2024 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 Google.Cloud.StorageInsights.V1; +using GoogleCloudSamples; +using System; +using Xunit; + +namespace StorageInsights.Samples.Tests; + +[Collection(nameof(StorageFixture))] +public class GetInventoryReportNamesTest : IDisposable +{ + private readonly StorageFixture _fixture; + private string _reportConfigName; + + public GetInventoryReportNamesTest(StorageFixture fixture) => _fixture = fixture; + + [Fact] + public void TestGetInventoryReportNames() + { + ReportConfig reportConfig = null; + // The permissions the service account needs sometimes needs a few minutes to propagate, so retry permission issues + RetryRobot robot = new RetryRobot() { ShouldRetry = (e) => e.Message.Contains("PermissionDenied") }; + robot.Eventually(() => + { + reportConfig = new CreateInventoryReportConfigSample().CreateInventoryReportConfig(_fixture.ProjectId, + _fixture.BucketLocation, _fixture.BucketNameSource, + _fixture.BucketNameSink); + _reportConfigName = reportConfig.Name; + }); + GetInventoryReportNamesSample sample = new GetInventoryReportNamesSample(); + sample.GetInventoryReportNames(_fixture.ProjectId, _fixture.BucketLocation, + reportConfig.ReportConfigName.ReportConfigId); + /* We can't actually test for a report config name showing up here, because we create + * the bucket and inventory configs for this test, and it takes 24 hours for an + * inventory report to actually get written to the bucket. + * We could set up a hard-coded bucket, but that would probably introduce flakes. + * The best we can do is make sure the test runs without throwing an error + */ + } + + public void Dispose() + { + try + { + _fixture.StorageInsights.DeleteReportConfig(_reportConfigName); + } + catch (Exception) + { + // Do nothing, we delete on a best effort basis. + } + } +} diff --git a/storageinsights/api/StorageInsights.Samples.Tests/ListInventoryReportConfigsTest.cs b/storageinsights/api/StorageInsights.Samples.Tests/ListInventoryReportConfigsTest.cs new file mode 100644 index 00000000000..985f8bc5424 --- /dev/null +++ b/storageinsights/api/StorageInsights.Samples.Tests/ListInventoryReportConfigsTest.cs @@ -0,0 +1,61 @@ +// Copyright 2024 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 Google.Cloud.StorageInsights.V1; +using GoogleCloudSamples; +using System; +using System.Linq; +using Xunit; + +namespace StorageInsights.Samples.Tests; + +[Collection(nameof(StorageFixture))] +public class ListInventoryReportConfigsTest : IDisposable +{ + private readonly StorageFixture _fixture; + private string _reportConfigName; + + public ListInventoryReportConfigsTest(StorageFixture fixture) => _fixture = fixture; + + [Fact] + public void TestListInventoryReportConfigs() + { + // The permissions the service account needs sometimes needs a few minutes to propagate, so retry permission issues + RetryRobot robot = new RetryRobot() { ShouldRetry = (e) => e.Message.Contains("PermissionDenied") }; + ReportConfig reportConfig = null; + robot.Eventually(() => + { + reportConfig = new CreateInventoryReportConfigSample().CreateInventoryReportConfig(_fixture.ProjectId, + _fixture.BucketLocation, _fixture.BucketNameSource, + _fixture.BucketNameSink); + _reportConfigName = reportConfig.Name; + }); + + ListInventoryReportConfigsSample sample = new ListInventoryReportConfigsSample(); + var reportConfigs = sample.ListInventoryReportConfigs(_fixture.ProjectId, _fixture.BucketLocation); + Assert.Contains(reportConfigs, config => config.Name.Contains(reportConfig.ReportConfigName.ReportConfigId)); + } + + public void Dispose() + { + try + { + _fixture.StorageInsights.DeleteReportConfig(_reportConfigName); + } + catch (Exception) + { + // Do nothing, we delete on a best effort basis. + } + } +} diff --git a/storageinsights/api/StorageInsights.Samples.Tests/StorageFixture.cs b/storageinsights/api/StorageInsights.Samples.Tests/StorageFixture.cs new file mode 100644 index 00000000000..e2f0b7decb0 --- /dev/null +++ b/storageinsights/api/StorageInsights.Samples.Tests/StorageFixture.cs @@ -0,0 +1,110 @@ +// Copyright 2024 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 Google.Api.Gax.ResourceNames; +using Google.Apis.Storage.v1.Data; +using Google.Cloud.ResourceManager.V3; +using Google.Cloud.Storage.V1; +using Google.Cloud.StorageInsights.V1; +using System; +using System.Collections.Generic; +using Xunit; + +namespace StorageInsights.Samples.Tests +{ + [CollectionDefinition(nameof(StorageFixture))] + public class StorageFixture : IDisposable, ICollectionFixture + { + public string ProjectId { get; } + public string BucketNameSource { get; } = Guid.NewGuid().ToString(); + public string BucketNameSink { get; } = Guid.NewGuid().ToString(); + public string BucketLocation => "us-west1"; + public StorageClient Storage { get; } = StorageClient.Create(); + public StorageInsightsClient StorageInsights { get; } = StorageInsightsClient.Create(); + + public StorageFixture() + { + ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID"); + if (string.IsNullOrWhiteSpace(ProjectId)) + { + throw new Exception("You need to set the Environment variable 'GOOGLE_PROJECT_ID' with your Google Cloud Project's project id."); + } + + ProjectsClient projectsClient = ProjectsClient.Create(); + Project project = projectsClient.GetProject(ProjectName.Format(ProjectId)); + string projectNumber = project.Name.Split("/")[1]; + String insightsServiceAccount = "service-" + projectNumber + "@gcp-sa-storageinsights.iam.gserviceaccount.com"; + + CreateBucketAndGrantInsightsPermissions(BucketNameSource, insightsServiceAccount); + CreateBucketAndGrantInsightsPermissions(BucketNameSink, insightsServiceAccount); + } + + + private void CreateBucketAndGrantInsightsPermissions(string bucketName, string email) + { + + Storage.CreateBucket(ProjectId, new Bucket + { + Name = bucketName, + Location = BucketLocation + }); + + string member = "serviceAccount:" + email; + string insightsCollectorService = "roles/storage.insightsCollectorService"; + string objectCreator = "roles/storage.objectCreator"; + + var policy = Storage.GetBucketIamPolicy(bucketName, new GetBucketIamPolicyOptions + { + RequestedPolicyVersion = 3 + }); + // Set the policy schema version. For more information, please refer to https://cloud.google.com/iam/docs/policies#versions. + policy.Version = 3; + + Policy.BindingsData insightsCollectorServiceBinding = new Policy.BindingsData + { + Role = insightsCollectorService, + Members = new List { member } + }; + Policy.BindingsData objectCreatorBinding = new Policy.BindingsData + { + Role = objectCreator, + Members = new List { member } + }; + + policy.Bindings.Add(insightsCollectorServiceBinding); + policy.Bindings.Add(objectCreatorBinding); + Storage.SetBucketIamPolicy(bucketName, policy); + } + + public void Dispose() + { + try + { + Storage.DeleteBucket(BucketNameSink); + } + catch (Exception) + { + // Do nothing, we delete on a best effort basis. + } + try + { + Storage.DeleteBucket(BucketNameSource); + } + catch (Exception) + { + // Do nothing, we delete on a best effort basis. + } + } + } +} diff --git a/storageinsights/api/StorageInsights.Samples.Tests/StorageInsights.Samples.Tests.csproj b/storageinsights/api/StorageInsights.Samples.Tests/StorageInsights.Samples.Tests.csproj new file mode 100644 index 00000000000..ace4dd83c2f --- /dev/null +++ b/storageinsights/api/StorageInsights.Samples.Tests/StorageInsights.Samples.Tests.csproj @@ -0,0 +1,22 @@ + + + + + net6.0 + false + + + + + + + + + + + + + + + + diff --git a/storageinsights/api/StorageInsights.Samples/CreateInventoryReportConfig.cs b/storageinsights/api/StorageInsights.Samples/CreateInventoryReportConfig.cs new file mode 100644 index 00000000000..348b5ea99e8 --- /dev/null +++ b/storageinsights/api/StorageInsights.Samples/CreateInventoryReportConfig.cs @@ -0,0 +1,63 @@ +// Copyright 2024 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 storageinsights_create_inventory_report_config] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.StorageInsights.V1; +using Google.Type; +using System; +using DateTime = System.DateTime; + +public class CreateInventoryReportConfigSample +{ + public ReportConfig CreateInventoryReportConfig( + string projectId = "your-project-id", + string bucketLocation = "us-west-1", + string sourceBucket = "your-gcs-source-bucket", + string destinationBucket = "your-gcs-destination-bucket") + { + ReportConfig reportConfig = new ReportConfig + { + DisplayName = "Example inventory report configuration", + FrequencyOptions = new FrequencyOptions + { + Frequency = FrequencyOptions.Types.Frequency.Weekly, + StartDate = Date.FromDateTime(DateTime.UtcNow.AddDays(1)), + EndDate = Date.FromDateTime(DateTime.UtcNow.AddDays(2)) + }, + CsvOptions = new CSVOptions { RecordSeparator = "\n", Delimiter = ",", HeaderRequired = true }, + ObjectMetadataReportOptions = new ObjectMetadataReportOptions + { + MetadataFields = { "project", "name", "bucket" }, + StorageFilters = new CloudStorageFilters { Bucket = sourceBucket }, + StorageDestinationOptions = new CloudStorageDestinationOptions { Bucket = destinationBucket } + }, + }; + + CreateReportConfigRequest request = new CreateReportConfigRequest + { + Parent = LocationName.Format(projectId, bucketLocation), ReportConfig = reportConfig + }; + + StorageInsightsClient client = StorageInsightsClient.Create(); + + ReportConfig response = client.CreateReportConfig(request); + + Console.WriteLine($"Created a new Inventory Report Config with name {response.Name}"); + + return response; + } +} +// [END storageinsights_create_inventory_report_config] diff --git a/storageinsights/api/StorageInsights.Samples/DeleteInventoryReportConfig.cs b/storageinsights/api/StorageInsights.Samples/DeleteInventoryReportConfig.cs new file mode 100644 index 00000000000..96cadca9402 --- /dev/null +++ b/storageinsights/api/StorageInsights.Samples/DeleteInventoryReportConfig.cs @@ -0,0 +1,35 @@ +// Copyright 2024 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 storageinsights_delete_inventory_report_config] + +using Google.Cloud.StorageInsights.V1; +using System; + +public class DeleteInventoryReportConfigSample +{ + public void DeleteInventoryReportConfig( + string projectId = "your-project-id", + string bucketLocation = "us-west-1", + string inventoryReportConfigUuid = "2b90d21c-f2f4-40b5-9519-e29a78f2b09f") + { + StorageInsightsClient storageInsightsClient = StorageInsightsClient.Create(); + ReportConfigName reportConfigName = + new ReportConfigName(projectId, bucketLocation, inventoryReportConfigUuid); + storageInsightsClient.DeleteReportConfig(reportConfigName); + + Console.WriteLine($"Deleted inventory report config with name {reportConfigName}"); + } +} +// [END storageinsights_delete_inventory_report_config] diff --git a/storageinsights/api/StorageInsights.Samples/EditInventoryReportConfig.cs b/storageinsights/api/StorageInsights.Samples/EditInventoryReportConfig.cs new file mode 100644 index 00000000000..f54a26b7f1c --- /dev/null +++ b/storageinsights/api/StorageInsights.Samples/EditInventoryReportConfig.cs @@ -0,0 +1,51 @@ +// Copyright 2024 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 storageinsights_edit_inventory_report_config] + +using Google.Cloud.StorageInsights.V1; +using Google.Protobuf.WellKnownTypes; +using System; +using System.Collections.Generic; + +public class EditInventoryReportConfigSample +{ + public ReportConfig EditInventoryReportConfig( + string projectId = "your-project-id", + string bucketLocation = "us-west-1", + string inventoryReportConfigUuid = "2b90d21c-f2f4-40b5-9519-e29a78f2b09f") + { + StorageInsightsClient storageInsightsClient = StorageInsightsClient.Create(); + ReportConfigName reportConfigName = + new ReportConfigName(projectId, bucketLocation, inventoryReportConfigUuid); + ReportConfig reportConfig = storageInsightsClient.GetReportConfig(reportConfigName); + + // Modify any field you want to update + reportConfig.DisplayName = "Updated display name"; + + var fields = new List() + // Add any other field you want to update to this list, in snake case + { + "display_name" + }; + + var updatedConfig = + storageInsightsClient.UpdateReportConfig(reportConfig, FieldMask.FromStringEnumerable(fields)); + + Console.WriteLine($"Edited report config with name {reportConfigName}"); + + return updatedConfig; + } +} +// [END storageinsights_edit_inventory_report_config] diff --git a/storageinsights/api/StorageInsights.Samples/GetInventoryReportNames.cs b/storageinsights/api/StorageInsights.Samples/GetInventoryReportNames.cs new file mode 100644 index 00000000000..940b6ac7950 --- /dev/null +++ b/storageinsights/api/StorageInsights.Samples/GetInventoryReportNames.cs @@ -0,0 +1,46 @@ +// Copyright 2024 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 storageinsights_get_inventory_report_names] + +using Google.Cloud.StorageInsights.V1; +using System; +using System.Collections.Generic; + +public class GetInventoryReportNamesSample +{ + public void GetInventoryReportNames( + string projectId = "your-project-id", + string bucketLocation = "us-west-1", + string inventoryReportConfigUuid = "2b90d21c-f2f4-40b5-9519-e29a78f2b09f") + { + StorageInsightsClient storageInsightsClient = StorageInsightsClient.Create(); + ReportConfigName reportConfigName = + new ReportConfigName(projectId, bucketLocation, inventoryReportConfigUuid); + ReportConfig reportConfig = storageInsightsClient.GetReportConfig(reportConfigName); + string extension = reportConfig.CsvOptions is null ? "csv" : "parquet"; + + Console.WriteLine("You can use the Google Cloud Storage Client to download the following objects from " + + "Google Cloud Storage:"); + foreach (ReportDetail reportDetail in storageInsightsClient.ListReportDetails(reportConfigName)) + { + for (long index = reportDetail.ShardsCount - 1; index >= 0; index--) + { + String reportName = reportDetail.ReportPathPrefix + index + "." + extension; + Console.WriteLine(reportName); + } + } + } +} +// [END storageinsights_get_inventory_report_names] diff --git a/storageinsights/api/StorageInsights.Samples/ListInventoryReportConfigs.cs b/storageinsights/api/StorageInsights.Samples/ListInventoryReportConfigs.cs new file mode 100644 index 00000000000..02ef333a7b1 --- /dev/null +++ b/storageinsights/api/StorageInsights.Samples/ListInventoryReportConfigs.cs @@ -0,0 +1,41 @@ +// Copyright 2024 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 storageinsights_list_inventory_report_configs] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.StorageInsights.V1; +using System; +using System.Collections.Generic; + +public class ListInventoryReportConfigsSample +{ + public IEnumerable ListInventoryReportConfigs( + string projectId = "your-project-id", + string bucketLocation = "us-west1") + { + StorageInsightsClient storageInsightsClient = StorageInsightsClient.Create(); + Console.WriteLine($"Listing Inventory report configs in project {projectId} in location {bucketLocation}:"); + + IEnumerable reportConfigs = + storageInsightsClient.ListReportConfigs(LocationName.Format(projectId, bucketLocation)); + foreach (ReportConfig reportConfig in reportConfigs) + { + Console.WriteLine(reportConfig.Name); + } + + return reportConfigs; + } +} +// [END storageinsights_list_inventory_report_configs] diff --git a/storageinsights/api/StorageInsights.Samples/StorageInsights.Samples.csproj b/storageinsights/api/StorageInsights.Samples/StorageInsights.Samples.csproj new file mode 100644 index 00000000000..6e2404fa9ed --- /dev/null +++ b/storageinsights/api/StorageInsights.Samples/StorageInsights.Samples.csproj @@ -0,0 +1,10 @@ + + + + netstandard2.1 + + + + + + \ No newline at end of file diff --git a/storageinsights/api/StorageInsights.sln b/storageinsights/api/StorageInsights.sln new file mode 100644 index 00000000000..660e6e278e8 --- /dev/null +++ b/storageinsights/api/StorageInsights.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StorageInsights.Samples", "StorageInsights.Samples\StorageInsights.Samples.csproj", "{DDBFEB4C-288C-40D4-9344-024DB20827B1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StorageInsights.Samples.Tests", "StorageInsights.Samples.Tests\StorageInsights.Samples.Tests.csproj", "{D43569BE-CD55-42B7-A096-EF61DB81AF6F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "testutil", "..\..\testutil\testutil.csproj", "{9714C126-DE7B-4DC5-95AD-A3B3665D1285}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DDBFEB4C-288C-40D4-9344-024DB20827B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DDBFEB4C-288C-40D4-9344-024DB20827B1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DDBFEB4C-288C-40D4-9344-024DB20827B1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DDBFEB4C-288C-40D4-9344-024DB20827B1}.Release|Any CPU.Build.0 = Release|Any CPU + {D43569BE-CD55-42B7-A096-EF61DB81AF6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D43569BE-CD55-42B7-A096-EF61DB81AF6F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D43569BE-CD55-42B7-A096-EF61DB81AF6F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D43569BE-CD55-42B7-A096-EF61DB81AF6F}.Release|Any CPU.Build.0 = Release|Any CPU + {9714C126-DE7B-4DC5-95AD-A3B3665D1285}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9714C126-DE7B-4DC5-95AD-A3B3665D1285}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9714C126-DE7B-4DC5-95AD-A3B3665D1285}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9714C126-DE7B-4DC5-95AD-A3B3665D1285}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/storageinsights/api/runTests.ps1 b/storageinsights/api/runTests.ps1 new file mode 100644 index 00000000000..531e60d0392 --- /dev/null +++ b/storageinsights/api/runTests.ps1 @@ -0,0 +1,16 @@ +# Copyright(c) 2024 Google Inc. +# +# 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 +# +# http://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. + +dotnet restore --force +dotnet test --no-restore --test-adapter-path:. --logger:junit 2>&1 | %{ "$_" }