-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Tavasyag/storagebucket list #17500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BBBmau
merged 11 commits into
GoogleCloudPlatform:main
from
tavasyag:tavasyag/storagebucket_list
May 21, 2026
+404
−4
Merged
Tavasyag/storagebucket list #17500
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b4f2bec
Added identity and list implem
tavasyaganpati f1359ae
resource fix
tavasyaganpati 65e693a
ListPages fix
tavasyaganpati f7956e1
Add docs
tavasyag c7ea6d2
Add test for include resources=true
tavasyag 8dca29f
Add bucket limit for vcr test
tavasyag 5a501aa
Add identity test and fix docs
tavasyag b262701
Fix list test
tavasyag 739c9bb
Add listPages
tavasyag 12bd3ee
Ignore labels
tavasyag fe3085b
Fix vcr test
tavasyag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
mmv1/third_party/terraform/services/storage/list_google_storage_bucket.go.tmpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| // Copyright (c) IBM Corp. 2014, 2026 | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package storage | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/diag" | ||
| "github.com/hashicorp/terraform-plugin-framework/list" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
|
|
||
| "google.golang.org/api/storage/v1" | ||
|
|
||
| "github.com/hashicorp/terraform-provider-google/google/registry" | ||
| "github.com/hashicorp/terraform-provider-google/google/tpgresource" | ||
| transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" | ||
| ) | ||
|
|
||
| func init() { | ||
| registry.FrameworkListResource{ | ||
| Name: "google_storage_bucket", | ||
| ProductName: "storage", | ||
| Func: NewGoogleStorageBucketListResource, | ||
| }.Register() | ||
| } | ||
|
|
||
|
|
||
| type GoogleStorageBucketListResource struct { | ||
| tpgresource.ListResourceMetadata | ||
| } | ||
|
|
||
| type GoogleStorageBucketListModel struct { | ||
| Project types.String `tfsdk:"project"` | ||
| } | ||
|
|
||
| func NewGoogleStorageBucketListResource() list.ListResource { | ||
| listR := &GoogleStorageBucketListResource{} | ||
| listR.TypeName = "google_storage_bucket" | ||
| listR.SDKv2Resource = ResourceStorageBucket() | ||
| listR.ListConfigFields = []tpgresource.ListConfigField{ | ||
| {Name: "project", Kind: tpgresource.ListConfigKindString, Optional: true}, | ||
| } | ||
| return listR | ||
| } | ||
|
|
||
| func (listR *GoogleStorageBucketListResource) List(ctx context.Context, listReq list.ListRequest, stream *list.ListResultsStream) { | ||
| errStorageBucketListStreamClosed := errors.New("stream closed") | ||
|
|
||
| var data GoogleStorageBucketListModel | ||
| diags := listReq.Config.Get(ctx, &data) | ||
| if diags.HasError() { | ||
| stream.Results = list.ListResultsStreamDiagnostics(diags) | ||
| return | ||
| } | ||
| if listR.Client == nil { | ||
| diags = append(diags, diag.NewErrorDiagnostic( | ||
| "Provider not configured", | ||
| "The Google provider client is not available; ensure the provider is configured (e.g. credentials and default project).", | ||
| )) | ||
| stream.Results = list.ListResultsStreamDiagnostics(diags) | ||
| return | ||
| } | ||
| project := listR.GetProject(data.Project) | ||
|
|
||
| stream.Results = func(push func(list.ListResult) bool) { | ||
| err := ListStorageBuckets(listR.Client, project, func(rd *schema.ResourceData) error { | ||
| result := listReq.NewListResult(ctx) | ||
|
|
||
| if err := listR.SetResult(ctx, listReq.IncludeResource, &result, rd, "name"); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !push(result) { | ||
| return errStorageBucketListStreamClosed | ||
| } | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| if errors.Is(err, errStorageBucketListStreamClosed) { | ||
| return | ||
| } | ||
| diags.AddError("API Error", err.Error()) | ||
| result := listReq.NewListResult(ctx) | ||
| result.Diagnostics = diags | ||
| push(result) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func flattenStorageBucketListItem(res map[string]interface{}, d *schema.ResourceData, config *transport_tpg.Config) error { | ||
| var bucket storage.Bucket | ||
| if err := tpgresource.Convert(res, &bucket); err != nil { | ||
| return fmt.Errorf("error converting storage bucket list response: %w", err) | ||
| } | ||
| if bucket.Name == "" { | ||
| return fmt.Errorf("missing name in storage bucket list response") | ||
| } | ||
| if err := d.Set("name", bucket.Name); err != nil { | ||
| return err | ||
| } | ||
| userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return setStorageBucket(d, config, &bucket, bucket.Name, userAgent) | ||
| } | ||
|
|
||
| func ListStorageBuckets(config *transport_tpg.Config, project string, callback func(rd *schema.ResourceData) error) error { | ||
| if config == nil { | ||
| return fmt.Errorf("provider client is not configured") | ||
| } | ||
| d := ResourceStorageBucket().Data(&terraform.InstanceState{}) | ||
|
|
||
| if project != "" { | ||
| if err := d.Set("project", project); err != nil { | ||
| return fmt.Errorf("error setting project on temporary resource data: %w", err) | ||
| } | ||
| } | ||
| url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}StorageBasePath{{"}}"}}b?project={{"{{"}}project{{"}}"}}") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| billingProject := "" | ||
| if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
| billingProject = bp | ||
| } | ||
|
|
||
| userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return transport_tpg.ListPages(transport_tpg.ListPagesOptions{ | ||
| Config: config, | ||
| TempData: d, | ||
| Resource: ResourceStorageBucket(), | ||
| ListURL: url, | ||
| BillingProject: billingProject, | ||
| UserAgent: userAgent, | ||
| Flattener: flattenStorageBucketListItem, | ||
| Callback: callback, | ||
| }) | ||
| } | ||
|
|
||
|
|
||
|
|
103 changes: 103 additions & 0 deletions
103
mmv1/third_party/terraform/services/storage/list_google_storage_bucket_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright (c) IBM Corp. 2014, 2026 | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package storage_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
| "github.com/hashicorp/terraform-plugin-testing/querycheck" | ||
| "github.com/hashicorp/terraform-plugin-testing/querycheck/queryfilter" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
|
|
||
| "github.com/hashicorp/terraform-provider-google/google/acctest" | ||
| "github.com/hashicorp/terraform-provider-google/google/envvar" | ||
| ) | ||
|
|
||
| func TestAccStorageBucketListResource_queryIdentity(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| project := envvar.GetTestProjectFromEnv() | ||
| bucketName := "tf-test-" + acctest.RandString(t, 10) | ||
|
|
||
| acctest.VcrTest(t, resource.TestCase{ | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_14_0), | ||
| }, | ||
| PreCheck: func() { acctest.AccTestPreCheck(t) }, | ||
| ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: testAccStorageBucketBasic(bucketName, project), | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("google_storage_bucket.test", "project", project), | ||
| resource.TestCheckResourceAttr("google_storage_bucket.test", "name", bucketName), | ||
| ), | ||
| }, | ||
| { | ||
| Query: true, | ||
| Config: testAccStorageBucketListQuery(project, true), | ||
| QueryResultChecks: []querycheck.QueryResultCheck{ | ||
| querycheck.ExpectIdentity("google_storage_bucket.all_in_project", map[string]knownvalue.Check{ | ||
| "name": knownvalue.StringExact(bucketName), | ||
| "project": knownvalue.StringExact(project), | ||
| }), | ||
| querycheck.ExpectLengthAtLeast("google_storage_bucket.all_in_project", 1), | ||
| querycheck.ExpectResourceKnownValues( | ||
| "google_storage_bucket.all_in_project", | ||
| queryfilter.ByDisplayName(knownvalue.StringExact(bucketName)), | ||
| []querycheck.KnownValueCheck{ | ||
| { | ||
| Path: tfjsonpath.New("name"), | ||
| KnownValue: knownvalue.StringExact(bucketName), | ||
| }, | ||
| { | ||
| Path: tfjsonpath.New("project"), | ||
| KnownValue: knownvalue.StringExact(project), | ||
| }, | ||
| { | ||
| Path: tfjsonpath.New("location"), | ||
| KnownValue: knownvalue.StringExact("US"), | ||
| }, | ||
| }, | ||
| ), | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func testAccStorageBucketBasic(name, project string) string { | ||
| return fmt.Sprintf(` | ||
| resource "google_storage_bucket" "test" { | ||
| name = %q | ||
| location = "US" | ||
| project = %q | ||
| } | ||
| `, name, project) | ||
| } | ||
|
|
||
| func testAccStorageBucketListQuery(project string, includeResource bool) string { | ||
| includeResourceBlock := "" | ||
| if includeResource { | ||
| includeResourceBlock = " include_resource = true" | ||
| } | ||
|
|
||
| return fmt.Sprintf(` | ||
| provider "google" {} | ||
|
|
||
| list "google_storage_bucket" "all_in_project" { | ||
| provider = google | ||
| %s | ||
| limit = 1000 | ||
|
|
||
| config { | ||
| project = %q | ||
| } | ||
| } | ||
| `, includeResourceBlock, project) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.