-
Notifications
You must be signed in to change notification settings - Fork 135
/
data_source_tc_cos_bucket_object.go
131 lines (118 loc) · 4.08 KB
/
data_source_tc_cos_bucket_object.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package cos
import (
"context"
"strings"
"time"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudCosBucketObject() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudCosBucketObjectsRead,
Schema: map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Required: true,
Description: "Name of the bucket that contains the objects to query.",
},
"key": {
Type: schema.TypeString,
Required: true,
Description: "The full path to the object inside the bucket.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
"cache_control": {
Type: schema.TypeString,
Computed: true,
Description: "Specifies caching behavior along the request/reply chain.",
},
"content_disposition": {
Type: schema.TypeString,
Computed: true,
Description: "Specifies presentational information for the object.",
},
"content_encoding": {
Type: schema.TypeString,
Computed: true,
Description: "Specifies what content encodings have been applied to the object and thus what decoding mechanisms must be applied to obtain the media-type referenced by the Content-Type header field.",
},
"content_type": {
Type: schema.TypeString,
Computed: true,
Description: "A standard MIME type describing the format of the object data.",
},
"etag": {
Type: schema.TypeString,
Computed: true,
Description: "ETag generated for the object, which is may not equal to MD5 value.",
},
"last_modified": {
Type: schema.TypeString,
Computed: true,
Description: "Last modified date of the object.",
},
"storage_class": {
Type: schema.TypeString,
Computed: true,
Description: "Object storage type such as STANDARD.",
},
},
}
}
func dataSourceTencentCloudCosBucketObjectsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_cos_bucket_object.read")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
bucket := d.Get("bucket").(string)
key := d.Get("key").(string)
outputMap := make(map[string]string)
outputMap["bucket"] = bucket
outputMap["key"] = key
cosService := CosService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
info, err := cosService.HeadObject(ctx, bucket, key)
if err != nil {
return err
}
ids := []string{bucket, key}
d.SetId(helper.DataResourceIdsHash(ids))
_ = d.Set("cache_control", info.CacheControl)
outputMap["cache_control"] = getStringValue(info.CacheControl)
_ = d.Set("content_disposition", info.ContentDisposition)
outputMap["content_disposition"] = getStringValue(info.ContentDisposition)
_ = d.Set("content_encoding", info.ContentEncoding)
outputMap["content_encoding"] = getStringValue(info.ContentEncoding)
_ = d.Set("content_type", info.ContentType)
outputMap["content_type"] = getStringValue(info.ContentType)
etag := getStringValue(info.ETag)
_ = d.Set("etag", strings.Trim(etag, `"`))
outputMap["etag"] = strings.Trim(etag, `"`)
_ = d.Set("last_modified", info.LastModified.Format(time.RFC1123))
outputMap["last_modified"] = info.LastModified.Format(time.RFC1123)
_ = d.Set("storage_class", s3.StorageClassStandard)
outputMap["storage_class"] = s3.StorageClassStandard
if info.StorageClass != nil {
_ = d.Set("storage_class", info.StorageClass)
outputMap["storage_class"] = getStringValue(info.StorageClass)
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if err = tccommon.WriteToFile(output.(string), outputMap); err != nil {
return err
}
}
return nil
}
func getStringValue(p *string) string {
if p == nil {
return ""
}
return *p
}