Skip to content

Commit

Permalink
Compute content_base64 for google_storage_bucket_object_content
Browse files Browse the repository at this point in the history
This returns the same content present in the return object but exposed
by using Base64 encoding. This is mostly useful for reading binary
content.

Our particular use case is to push the content of a WASM file to a
Fastly Compute@Edge service. But this is also useful for other
serverless services like AWS Lambda as well.

This alongside including the hash sums for the source object content
proposed in
hashicorp/terraform-provider-google#13002,
would allow us pushing content directly from Google Storage Object
Content data source to Fastly Compute@Edge. AWS Lambda also uses the
same version/content discrimination method of sending the content hash
sum within the `source_code_hash` parameter.

Sign-off-by: Gorka Lerchundi Osa <glertxundi@gmail.com>
  • Loading branch information
glerchundi committed Dec 3, 2022
1 parent 34ba4e8 commit e89e2f9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package google

import (
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -16,6 +17,13 @@ func dataSourceGoogleStorageBucketObjectContent() *schema.Resource {
addRequiredFieldsToSchema(dsSchema, "bucket")
addRequiredFieldsToSchema(dsSchema, "name")
addOptionalFieldsToSchema(dsSchema, "content")
dsSchema["content_base64"] = &schema.Schema{
Type: schema.TypeString,
Description: "Base64 encoded version of the object content. Use this when dealing with binary data.",
Computed: true,
Optional: false,
Required: true,
}

return &schema.Resource{
Read: dataSourceGoogleStorageBucketObjectContentRead,
Expand All @@ -40,22 +48,25 @@ func dataSourceGoogleStorageBucketObjectContentRead(d *schema.ResourceData, meta
if err != nil {
return fmt.Errorf("Error downloading storage bucket object: %s", err)
}

defer res.Body.Close()
var bodyString string

var bodyBytes []byte
if res.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(res.Body)
candidateBodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("Error reading all from res.Body: %s", err)
}
bodyString = string(bodyBytes)
bodyBytes = candidateBodyBytes
}

if err := d.Set("content", bodyString); err != nil {
if err := d.Set("content", string(bodyBytes)); err != nil {
return fmt.Errorf("Error setting content: %s", err)
}

if err := d.Set("content_base64", base64.StdEncoding.EncodeToString(bodyBytes)); err != nil {
return fmt.Errorf("Error setting content_base64: %s", err)
}

d.SetId(bucket + "-" + name)
return nil
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package google

import (
"encoding/base64"
"fmt"
"testing"

Expand All @@ -21,6 +22,8 @@ func TestAccDataSourceStorageBucketObjectContent_Basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.google_storage_bucket_object_content.default", "content"),
resource.TestCheckResourceAttr("data.google_storage_bucket_object_content.default", "content", content),
resource.TestCheckResourceAttrSet("data.google_storage_bucket_object_content.default", "content_base64"),
resource.TestCheckResourceAttr("data.google_storage_bucket_object_content.default", "content_base64", base64.StdEncoding.EncodeToString([]byte(content))),
),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ The following arguments are supported:

The following attributes are exported:

* `content` - (Computed) [Content-Language](https://tools.ietf.org/html/rfc7231#section-3.1.3.2) of the object content.
* `content` - (Computed) Raw content of the object content, assumed by Terraform
to be UTF-8 encoded string. Files that do not contain UTF-8 text will have
invalid UTF-8 sequences in `content` replaced with the Unicode replacement
character.

* `content_base64` - (Computed) Base64 encoded version of the object content.
Use this when dealing with binary data.

0 comments on commit e89e2f9

Please sign in to comment.