Skip to content

Commit

Permalink
feat(cos): Add new data source tencentcloud_cos_object_signed_url
Browse files Browse the repository at this point in the history
  • Loading branch information
lwvjhm committed Nov 22, 2024
1 parent 5f3a2c2 commit 3f06e2e
Show file tree
Hide file tree
Showing 43 changed files with 4,154 additions and 376 deletions.
3 changes: 3 additions & 0 deletions .changelog/2967.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
tencentcloud_cos_object_signed_url
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.1037
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wedata v1.0.792
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199
github.com/tencentyun/cos-go-sdk-v5 v0.7.42-0.20230629101357-7edd77448a0f
github.com/tencentyun/cos-go-sdk-v5 v0.7.58
github.com/yangwenmai/ratelimit v0.0.0-20180104140304-44221c2292e1
gopkg.in/yaml.v2 v2.4.0
)
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14j
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -1064,6 +1065,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199 h1:hMBLtiJ
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199/go.mod h1:nnY91/H3j/Gu7V/oCA6Zeg8T5D3q36EUdBh4EjmHwqY=
github.com/tencentyun/cos-go-sdk-v5 v0.7.42-0.20230629101357-7edd77448a0f h1:yTMDoBvFuXjZat10d98DIKbPnN9FQG+drt1SbNfMW5U=
github.com/tencentyun/cos-go-sdk-v5 v0.7.42-0.20230629101357-7edd77448a0f/go.mod h1:LUFnaqRmGk6pEHOaRmdn2dCZR2j0cSsM5xowWFPTPao=
github.com/tencentyun/cos-go-sdk-v5 v0.7.58 h1:YoZTqaRUcv75P4GSvyam93aEtdgOZWGUJWzXzyCV5EM=
github.com/tencentyun/cos-go-sdk-v5 v0.7.58/go.mod h1:8+hG+mQMuRP/OIS9d83syAvXvrMj9HhkND6Q1fLghw0=
github.com/tenntenn/modver v1.0.1 h1:2klLppGhDgzJrScMpkj9Ujy3rXPUspSjAcev9tSEBgA=
github.com/tenntenn/modver v1.0.1/go.mod h1:bePIyQPb7UeioSRkw3Q0XeMhYZSMx9B8ePqg6SAMGH0=
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpRQGxTSkNYKJ51yaw6ChIqO+Je8UqsTKN/cDag=
Expand Down
1 change: 1 addition & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ func Provider() *schema.Provider {
"tencentcloud_cos_batchs": cos.DataSourceTencentCloudCosBatchs(),
"tencentcloud_cos_bucket_inventorys": cos.DataSourceTencentCloudCosBucketInventorys(),
"tencentcloud_cos_bucket_multipart_uploads": cos.DataSourceTencentCloudCosBucketMultipartUploads(),
"tencentcloud_cos_object_signed_url": cos.DataSourceTencentCloudCosObjectSignedUrl(),
"tencentcloud_cfs_file_systems": cfs.DataSourceTencentCloudCfsFileSystems(),
"tencentcloud_cfs_access_groups": cfs.DataSourceTencentCloudCfsAccessGroups(),
"tencentcloud_cfs_access_rules": cfs.DataSourceTencentCloudCfsAccessRules(),
Expand Down
1 change: 1 addition & 0 deletions tencentcloud/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ Cloud Object Storage(COS)
tencentcloud_cos_batchs
tencentcloud_cos_bucket_inventorys
tencentcloud_cos_bucket_multipart_uploads
tencentcloud_cos_object_signed_url

Resource
tencentcloud_cos_bucket
Expand Down
126 changes: 126 additions & 0 deletions tencentcloud/services/cos/data_source_tc_cos_object_signed_url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package cos

import (
"context"
"time"

tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/tencentyun/cos-go-sdk-v5"

"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func DataSourceTencentCloudCosObjectSignedUrl() *schema.Resource {
return &schema.Resource{
Read: DataSourceTencentCloudCosObjectSignedUrlRead,

Schema: map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Required: true,
Description: "Name of the bucket.",
},
"path": {
Type: schema.TypeString,
Required: true,
Description: "The full path to the object inside the bucket.",
},
"method": {
Type: schema.TypeString,
Optional: true,
Default: "GET",
ValidateFunc: validation.StringInSlice([]string{"GET", "PUT"}, true),
Description: "Method, GET or PUT. Default value is GET.",
},
"duration": {
Type: schema.TypeString,
Optional: true,
Default: "1m",
Description: "Duration of signed url. Default value is 1m.",
},
"headers": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Request headers.",
},
"queries": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Request query parameters.",
},
"signed_url": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "Signed URL.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}

func DataSourceTencentCloudCosObjectSignedUrlRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_cos_object_signed_url.read")()

logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)

bucket := d.Get("bucket").(string)
path := d.Get("path").(string)
method := "GET"
durationString := "1m"
opt := &cos.PresignedURLOptions{}
signHost := true

if v, ok := d.GetOk("method"); ok {
method = v.(string)
}

if v, ok := d.GetOk("duration"); ok {
durationString = v.(string)
}

duration, err := time.ParseDuration(durationString)
if err != nil {
return err
}

if v, ok := d.GetOk("headers"); ok {
for key, value := range v.(map[string]string) {
opt.Header.Set(key, value)
}
}

if v, ok := d.GetOk("queries"); ok {
for key, value := range v.(map[string]string) {
opt.Query.Set(key, value)
}
}

result, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTencentCosClient(bucket).Object.GetPresignedURL2(ctx, method, path, duration, opt, signHost)
if err != nil {
return err
}

signedUrl := result.String()

d.SetId(helper.DataResourceIdHash(path))
_ = d.Set("signed_url", signedUrl)
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if err := tccommon.WriteToFile(output.(string), signedUrl); err != nil {
return err
}
}

return nil
}
10 changes: 10 additions & 0 deletions tencentcloud/services/cos/data_source_tc_cos_object_signed_url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Use this data source to query the signed url of the COS object.

Example Usage

```hcl
data "tencentcloud_cos_object_signed_url" "cos_object_signed_url" {
bucket = "xxxxxx"
path = "path/to/file"
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cos_test

import (
"testing"

tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"

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

func TestAccTencentCloudCosObjectSignedUrlDataSource_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() {
tcacctest.AccPreCheck(t)
},
Providers: tcacctest.AccProviders,
Steps: []resource.TestStep{
{
Config: testAccCosObjectSignedUrlDataSource,
Check: resource.ComposeTestCheckFunc(
tcacctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_cos_object_signed_url.cos_object_signed_url"),
resource.TestCheckResourceAttrSet("data.tencentcloud_cos_object_signed_url.cos_object_signed_url", "signed_url"),
),
},
},
})
}

const testAccCosObjectSignedUrlDataSource = `
data "tencentcloud_cos_object_signed_url" "cos_object_signed_url" {
bucket = "keep-test-1308919341"
path = "path/to/file"
headers = {
Content-Type = "text/plain"
}
queries = {
prefix = "xxx"
}
}
`
17 changes: 3 additions & 14 deletions tencentcloud/services/cos/resource_tc_cos_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1582,11 +1582,6 @@ func resourceTencentCloudCosBucketOriginPullUpdate(ctx context.Context, service
CopyOriginData: true,
HttpHeader: &cos.BucketOriginHttpHeader{},
},
OriginInfo: &cos.BucketOriginInfo{
FileInfo: &cos.BucketOriginFileInfo{
PrefixDirective: false,
},
},
}
)

Expand All @@ -1606,7 +1601,7 @@ func resourceTencentCloudCosBucketOriginPullUpdate(ctx context.Context, service
item.OriginParameter.Protocol = v.(string)
}
if v, ok := dMap["host"]; ok {
item.OriginInfo.HostInfo = v.(string)
item.OriginInfo.HostInfo.HostName = v.(string)
}
if v, ok := dMap["follow_query_string"]; ok {
item.OriginParameter.FollowQueryString = v.(bool)
Expand All @@ -1619,17 +1614,11 @@ func resourceTencentCloudCosBucketOriginPullUpdate(ctx context.Context, service
//}
if v, ok := dMap["redirect_prefix"]; ok {
value := v.(string)
if value != "" {
item.OriginInfo.FileInfo.PrefixDirective = true
}
item.OriginInfo.FileInfo.Prefix = value
item.OriginInfo.FileInfo.PrefixConfiguration.Prefix = value
}
if v, ok := dMap["redirect_suffix"]; ok {
value := v.(string)
if value != "" {
item.OriginInfo.FileInfo.PrefixDirective = true
}
item.OriginInfo.FileInfo.Suffix = value
item.OriginInfo.FileInfo.SuffixConfiguration.Suffix = value
}
if v, ok := dMap["custom_http_headers"]; ok {
var customHeaders []cos.OriginHttpHeader
Expand Down
4 changes: 2 additions & 2 deletions tencentcloud/services/cos/service_tencentcloud_cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ func (me *CosService) GetBucketPullOrigin(ctx context.Context, bucket string) (r
for _, rule := range originConfig.Rule {
item := make(map[string]interface{})
item["priority"] = helper.Int(rule.RulePriority)
item["host"] = helper.String(rule.OriginInfo.HostInfo)
item["host"] = helper.String(rule.OriginInfo.HostInfo.HostName)

if rule.OriginCondition != nil {
item["prefix"] = helper.String(rule.OriginCondition.Prefix)
Expand Down Expand Up @@ -1311,7 +1311,7 @@ func (me *CosService) GetBucketPullOrigin(ctx context.Context, bucket string) (r
}

if rule.OriginInfo.FileInfo != nil {
item["host"] = helper.String(rule.OriginInfo.HostInfo)
item["host"] = helper.String(rule.OriginInfo.HostInfo.HostName)
//item["redirect_prefix"] = helper.String(rule.OriginInfo.FileInfo.Prefix)
//item["redirect_suffix"] = helper.String(rule.OriginInfo.FileInfo.Suffix)
}
Expand Down
8 changes: 7 additions & 1 deletion vendor/github.com/tencentyun/cos-go-sdk-v5/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3f06e2e

Please sign in to comment.