diff --git a/google/data_source_google_compute_backend_bucket.go b/google/data_source_google_compute_backend_bucket.go new file mode 100644 index 00000000000..94c6ebf9954 --- /dev/null +++ b/google/data_source_google_compute_backend_bucket.go @@ -0,0 +1,37 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func dataSourceGoogleComputeBackendBucket() *schema.Resource { + dsSchema := datasourceSchemaFromResourceSchema(resourceComputeBackendBucket().Schema) + + // Set 'Required' schema elements + addRequiredFieldsToSchema(dsSchema, "name") + + // Set 'Optional' schema elements + addOptionalFieldsToSchema(dsSchema, "project") + + return &schema.Resource{ + Read: dataSourceComputeBackendBucketRead, + Schema: dsSchema, + } +} + +func dataSourceComputeBackendBucketRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + serviceName := d.Get("name").(string) + + project, err := getProject(d, config) + if err != nil { + return err + } + + d.SetId(fmt.Sprintf("projects/%s/global/backendBuckets/%s", project, serviceName)) + + return resourceComputeBackendBucketRead(d, meta) +} diff --git a/google/data_source_google_compute_backend_bucket_test.go b/google/data_source_google_compute_backend_bucket_test.go new file mode 100644 index 00000000000..6b729f2e091 --- /dev/null +++ b/google/data_source_google_compute_backend_bucket_test.go @@ -0,0 +1,48 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccDataSourceComputeBackendBucket_basic(t *testing.T) { + t.Parallel() + + serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + bucketName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeBackendBucketDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceComputeBackendBucket_basic(serviceName, bucketName), + Check: checkDataSourceStateMatchesResourceState("data.google_compute_backend_bucket.baz", "google_compute_backend_bucket.foobar"), + }, + }, + }) +} + +func testAccDataSourceComputeBackendBucket_basic(serviceName, bucketName string) string { + return fmt.Sprintf(` +resource "google_compute_backend_bucket" "foobar" { + name = "%s" + description = "Contains beautiful images" + bucket_name = google_storage_bucket.image_bucket.name + enable_cdn = true +} + +resource "google_storage_bucket" "image_bucket" { + name = "%s" + location = "EU" +} + +data "google_compute_backend_bucket" "baz" { + name = google_compute_backend_bucket.foobar.name +} +`, serviceName, bucketName) +} diff --git a/google/provider.go b/google/provider.go index cf2221ca50f..70798db2f39 100644 --- a/google/provider.go +++ b/google/provider.go @@ -435,6 +435,7 @@ func Provider() terraform.ResourceProvider { "google_cloudfunctions_function": dataSourceGoogleCloudFunctionsFunction(), "google_composer_image_versions": dataSourceGoogleComposerImageVersions(), "google_compute_address": dataSourceGoogleComputeAddress(), + "google_compute_backend_bucket": dataSourceGoogleComputeBackendBucket(), "google_compute_backend_service": dataSourceGoogleComputeBackendService(), "google_compute_default_service_account": dataSourceGoogleComputeDefaultServiceAccount(), "google_compute_forwarding_rule": dataSourceGoogleComputeForwardingRule(),