Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added data source for storage bucket object content #8016

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/4254.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
`google_storage_bucket_object_content`
```
61 changes: 61 additions & 0 deletions google/data_source_storage_bucket_object_content.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package google

import (
"fmt"
"io/ioutil"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"google.golang.org/api/storage/v1"
)

func dataSourceGoogleStorageBucketObjectContent() *schema.Resource {

dsSchema := datasourceSchemaFromResourceSchema(resourceStorageBucketObject().Schema)

addRequiredFieldsToSchema(dsSchema, "bucket")
addRequiredFieldsToSchema(dsSchema, "name")
addOptionalFieldsToSchema(dsSchema, "content")

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

func dataSourceGoogleStorageBucketObjectContentRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}

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

objectsService := storage.NewObjectsService(config.NewStorageClient(userAgent))
getCall := objectsService.Get(bucket, name)

res, err := getCall.Download()
if err != nil {
return fmt.Errorf("Error downloading storage bucket object: %s", err)
}

defer res.Body.Close()
var bodyString string

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

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

d.SetId(bucket + "-" + name)
return nil
}
52 changes: 52 additions & 0 deletions google/data_source_storage_bucket_object_content_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package google

import (
"fmt"
"testing"

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

func TestAccDataSourceStorageBucketObjectContent_Basic(t *testing.T) {

bucket := "tf-bucket-object-content-" + randString(t, 10)
content := "qwertyuioasdfghjk1234567!!@#$*"

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceStorageBucketObjectContent_Basic(content, bucket),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.google_storage_bucket_object_content.default", "content"),
resource.TestCheckResourceAttr("data.google_storage_bucket_object_content.default", "content", content),
),
},
},
})
}

func testAccDataSourceStorageBucketObjectContent_Basic(content, bucket string) string {
return fmt.Sprintf(`
data "google_storage_bucket_object_content" "default" {
bucket = google_storage_bucket.contenttest.name
name = google_storage_bucket_object.object.name
depends_on = [
google_storage_bucket_object.object,
google_storage_bucket.contenttest,
]
}

resource "google_storage_bucket_object" "object" {
name = "butterfly01"
content = "%s"
bucket = google_storage_bucket.contenttest.name
}

resource "google_storage_bucket" "contenttest" {
name = "%s"
location = "US"
force_destroy = true
}`, content, bucket)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ func Provider() *schema.Provider {
"google_sql_ca_certs": dataSourceGoogleSQLCaCerts(),
"google_sql_database_instance": dataSourceSqlDatabaseInstance(),
"google_storage_bucket_object": dataSourceGoogleStorageBucketObject(),
"google_storage_bucket_object_content": dataSourceGoogleStorageBucketObjectContent(),
"google_storage_object_signed_url": dataSourceGoogleSignedUrl(),
"google_storage_project_service_account": dataSourceGoogleStorageProjectServiceAccount(),
"google_storage_transfer_project_service_account": dataSourceGoogleStorageTransferProjectServiceAccount(),
Expand Down
47 changes: 47 additions & 0 deletions website/docs/d/storage_bucket_object_content.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
subcategory: "Cloud Storage"
layout: "google"
page_title: "Google: google_storage_bucket_object_content"
sidebar_current: "docs-google-datasource-storage-bucket-object_content"
description: |-
Get content of a Google Cloud Storage bucket object.
---


# google\_storage\_bucket\_object\_content

Gets an existing object content 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).

~> **Warning:** The object content will be saved in the state, and visiable to everyone who has access to the state file.

## Example Usage

Example file object stored within a folder.

```hcl
data "google_storage_bucket_object_content" "key" {
name = "encryptedkey"
bucket = "keystore"
}

output "encrypted" {
value = data.google_storage_bucket_object_content.key.content
}
```

## 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:

* `content` - (Computed) [Content-Language](https://tools.ietf.org/html/rfc7231#section-3.1.3.2) of the object content.
4 changes: 4 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,10 @@
<a href="/docs/providers/google/d/storage_bucket_object.html">google_storage_bucket_object</a>
</li>

<li>
<a href="/docs/providers/google/d/storage_bucket_object_content.html">google_storage_bucket_object_content</a>
</li>

<li>
<a href="/docs/providers/google/d/storage_project_service_account.html">google_storage_project_service_account</a>
</li>
Expand Down