-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
data_source_storage_bucket_object_content.go
66 lines (51 loc) · 1.78 KB
/
data_source_storage_bucket_object_content.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package storage
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"google.golang.org/api/storage/v1"
)
func DataSourceGoogleStorageBucketObjectContent() *schema.Resource {
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceStorageBucketObject().Schema)
tpgresource.AddRequiredFieldsToSchema(dsSchema, "bucket")
tpgresource.AddRequiredFieldsToSchema(dsSchema, "name")
tpgresource.AddOptionalFieldsToSchema(dsSchema, "content")
return &schema.Resource{
Read: dataSourceGoogleStorageBucketObjectContentRead,
Schema: dsSchema,
}
}
func dataSourceGoogleStorageBucketObjectContentRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.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
}