Skip to content

Commit

Permalink
features: add data_source_google_compute_backend_bucket (#5700)
Browse files Browse the repository at this point in the history
Link #5690
  • Loading branch information
bdronneau authored Feb 19, 2020
1 parent 6494ef4 commit a5c6f97
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
37 changes: 37 additions & 0 deletions google/data_source_google_compute_backend_bucket.go
Original file line number Diff line number Diff line change
@@ -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)
}
48 changes: 48 additions & 0 deletions google/data_source_google_compute_backend_bucket_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit a5c6f97

Please sign in to comment.