From 593156e918513d779312e7e562a740a1f6f4dcb2 Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 14 Dec 2018 11:59:57 -0800 Subject: [PATCH] Add data source for cloud bucket object (#2657) /cc @chrisst --- ...ata_source_google_storage_bucket_object.go | 48 +++++++++++++++ google/provider.go | 1 + google/resource_storage_bucket_object.go | 9 ++- ...e_google_cloud_bucket_object.html.markdown | 60 +++++++++++++++++++ .../r/storage_bucket_object.html.markdown | 2 + 5 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 google/data_source_google_storage_bucket_object.go create mode 100644 website/docs/d/datasource_google_cloud_bucket_object.html.markdown diff --git a/google/data_source_google_storage_bucket_object.go b/google/data_source_google_storage_bucket_object.go new file mode 100644 index 00000000..b97de262 --- /dev/null +++ b/google/data_source_google_storage_bucket_object.go @@ -0,0 +1,48 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceGoogleStorageBucketObject() *schema.Resource { + + dsSchema := datasourceSchemaFromResourceSchema(resourceStorageBucketObject().Schema) + + addOptionalFieldsToSchema(dsSchema, "bucket") + addOptionalFieldsToSchema(dsSchema, "name") + + return &schema.Resource{ + Read: dataSourceGoogleStorageBucketObjectRead, + Schema: dsSchema, + } +} + +func dataSourceGoogleStorageBucketObjectRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + bucket := d.Get("bucket").(string) + name := d.Get("name").(string) + + // Using REST apis because the storage go client doesn't support folders + url := fmt.Sprintf("https://www.googleapis.com/storage/v1/b/%s/o/%s", bucket, name) + + res, err := sendRequest(config, "GET", url, nil) + if err != nil { + return fmt.Errorf("Error retrieving storage bucket object: %s", err) + } + + d.Set("cache_control", res["cacheControl"]) + d.Set("content_disposition", res["contentDisposition"]) + d.Set("content_encoding", res["contentEncoding"]) + d.Set("content_language", res["contentLanguage"]) + d.Set("content_type", res["contentType"]) + d.Set("crc32c", res["crc32c"]) + d.Set("self_link", res["selfLink"]) + d.Set("storage_class", res["storageClass"]) + d.Set("md5hash", res["md5Hash"]) + + d.SetId(bucket + "-" + name) + + return nil +} diff --git a/google/provider.go b/google/provider.go index 3b0cffe5..1ab735e8 100644 --- a/google/provider.go +++ b/google/provider.go @@ -96,6 +96,7 @@ func Provider() terraform.ResourceProvider { "google_project_services": dataSourceGoogleProjectServices(), "google_service_account": dataSourceGoogleServiceAccount(), "google_service_account_key": dataSourceGoogleServiceAccountKey(), + "google_storage_bucket_object": dataSourceGoogleStorageBucketObject(), "google_storage_object_signed_url": dataSourceGoogleSignedUrl(), "google_storage_project_service_account": dataSourceGoogleStorageProjectServiceAccount(), }, diff --git a/google/resource_storage_bucket_object.go b/google/resource_storage_bucket_object.go index 7f756346..77289efd 100644 --- a/google/resource_storage_bucket_object.go +++ b/google/resource_storage_bucket_object.go @@ -11,9 +11,10 @@ import ( "crypto/md5" "encoding/base64" + "io/ioutil" + "google.golang.org/api/googleapi" "google.golang.org/api/storage/v1" - "io/ioutil" ) func resourceStorageBucketObject() *schema.Resource { @@ -143,6 +144,11 @@ func resourceStorageBucketObject() *schema.Resource { ForceNew: true, Computed: true, }, + + "self_link": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -234,6 +240,7 @@ func resourceStorageBucketObjectRead(d *schema.ResourceData, meta interface{}) e d.Set("content_language", res.ContentLanguage) d.Set("content_type", res.ContentType) d.Set("storage_class", res.StorageClass) + d.Set("self_link", res.SelfLink) d.SetId(objectGetId(res)) diff --git a/website/docs/d/datasource_google_cloud_bucket_object.html.markdown b/website/docs/d/datasource_google_cloud_bucket_object.html.markdown new file mode 100644 index 00000000..b18a74e1 --- /dev/null +++ b/website/docs/d/datasource_google_cloud_bucket_object.html.markdown @@ -0,0 +1,60 @@ +--- +layout: "google" +page_title: "Google: google_storage_bucket_object" +sidebar_current: "docs-google-datasource-storage-bucket-object" +description: |- + Get information about a Google Cloud Storage bucket object. +--- + + +# google\_storage\_bucket\_object + +Gets an existing object inside an existing bucket in Google Cloud Storage service (GCS). +See [the official documentation](https://cloud.google.com/storage/docs/key-terms#objects) +and +[API](https://cloud.google.com/storage/docs/json_api/v1/objects). + + +## Example Usage + +Example picture stored within a folder. + +```hcl +data "google_storage_bucket_object" "picture" { + name = "folder/butterfly01.jpg" + bucket = "image-store" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `bucket` - (Required) The name of the containing bucket. + +* `name` - (Required) The name of the object. + +## Attributes Reference + +The following attributes are exported: + +* `cache_control` - (Computed) [Cache-Control](https://tools.ietf.org/html/rfc7234#section-5.2) + directive to specify caching behavior of object data. If omitted and object is accessible to all anonymous users, the default will be public, max-age=3600 + +* `content_disposition` - (Computed) [Content-Disposition](https://tools.ietf.org/html/rfc6266) of the object data. + +* `content_encoding` - (Computed) [Content-Encoding](https://tools.ietf.org/html/rfc7231#section-3.1.2.2) of the object data. + +* `content_language` - (Computed) [Content-Language](https://tools.ietf.org/html/rfc7231#section-3.1.3.2) of the object data. + +* `content_type` - (Computed) [Content-Type](https://tools.ietf.org/html/rfc7231#section-3.1.1.5) of the object data. Defaults to "application/octet-stream" or "text/plain; charset=utf-8". + +* `crc32c` - (Computed) Base 64 CRC32 hash of the uploaded data. + +* `md5hash` - (Computed) Base 64 MD5 hash of the uploaded data. + +* `self_link` - (Computed) A url reference to this object. + +* `storage_class` - (Computed) The [StorageClass](https://cloud.google.com/storage/docs/storage-classes) of the new bucket object. + Supported values include: `MULTI_REGIONAL`, `REGIONAL`, `NEARLINE`, `COLDLINE`. If not provided, this defaults to the bucket's default + storage class or to a [standard](https://cloud.google.com/storage/docs/storage-classes#standard) class. diff --git a/website/docs/r/storage_bucket_object.html.markdown b/website/docs/r/storage_bucket_object.html.markdown index ceadcb68..790a5e74 100644 --- a/website/docs/r/storage_bucket_object.html.markdown +++ b/website/docs/r/storage_bucket_object.html.markdown @@ -69,3 +69,5 @@ exported: * `crc32c` - (Computed) Base 64 CRC32 hash of the uploaded data. * `md5hash` - (Computed) Base 64 MD5 hash of the uploaded data. + +* `self_link` - (Computed) A url reference to this object.