Skip to content

Commit

Permalink
Added storage bucket datasource (#5251)
Browse files Browse the repository at this point in the history
* addeed storage bucket datasource

* fix typo
  • Loading branch information
shuyama1 authored Sep 29, 2021
1 parent 703a6b3 commit fd701ca
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package google

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGoogleStorageBucket() *schema.Resource {

dsSchema := datasourceSchemaFromResourceSchema(resourceStorageBucket().Schema)

addRequiredFieldsToSchema(dsSchema, "name")

return &schema.Resource{
Read: dataSourceGoogleStorageBucketRead,
Schema: dsSchema,
}
}

func dataSourceGoogleStorageBucketRead(d *schema.ResourceData, meta interface{}) error {

bucket := d.Get("name").(string)
d.SetId(bucket)

return resourceStorageBucketRead(d, meta)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceGoogleStorageBucket_basic(t *testing.T) {
t.Parallel()

bucket := "tf-bucket-" + randString(t, 10)

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccStorageBucketDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleStorageBucketConfig(bucket),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceStateWithIgnores("data.google_storage_bucket.bar", "google_storage_bucket.foo", map[string]struct{}{"force_destroy": {}}),
),
},
},
})
}

func testAccDataSourceGoogleStorageBucketConfig(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "foo" {
name = "%s"
}
data "google_storage_bucket" "bar" {
name = google_storage_bucket.foo.name
depends_on = [
google_storage_bucket.foo,
]
}
`, bucketName)
}
1 change: 1 addition & 0 deletions mmv1/third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ func Provider() *schema.Provider {
"google_sql_ca_certs": dataSourceGoogleSQLCaCerts(),
"google_sql_backup_run": dataSourceSqlBackupRun(),
"google_sql_database_instance": dataSourceSqlDatabaseInstance(),
"google_storage_bucket": dataSourceGoogleStorageBucket(),
"google_storage_bucket_object": dataSourceGoogleStorageBucketObject(),
"google_storage_bucket_object_content": dataSourceGoogleStorageBucketObjectContent(),
"google_storage_object_signed_url": dataSourceGoogleSignedUrl(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
subcategory: "Cloud Storage"
layout: "google"
page_title: "Google: google_storage_bucket"
sidebar_current: "docs-google-datasource-storage-bucket"
description: |-
Get information about a Google Cloud Storage bucket.
---

# google\_storage\_bucket

Gets an existing bucket in Google Cloud Storage service (GCS).
See [the official documentation](https://cloud.google.com/storage/docs/key-terms#buckets)
and
[API](https://cloud.google.com/storage/docs/json_api/v1/buckets).


## Example Usage

```hcl
data "google_storage_bucket" "my-bucket" {
name = "my-bucket"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the bucket.

## Attributes Reference

See [google_storage_bucket](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket#argument-reference) resource for details of the available attributes.

0 comments on commit fd701ca

Please sign in to comment.