-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cos): Add new data source
tencentcloud_cos_object_signed_url
- Loading branch information
lwvjhm
committed
Nov 20, 2024
1 parent
5f3a2c2
commit 430095f
Showing
43 changed files
with
4,154 additions
and
376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-data-source | ||
tencentcloud_cos_object_signed_url | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
tencentcloud/services/cos/data_source_tc_cos_object_signed_url.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
signed_url := result.String() | ||
|
||
d.SetId(helper.DataResourceIdHash(path)) | ||
_ = d.Set("signed_url", signed_url) | ||
output, ok := d.GetOk("result_output_file") | ||
if ok && output.(string) != "" { | ||
if err := tccommon.WriteToFile(output.(string), signed_url); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
10 changes: 10 additions & 0 deletions
10
tencentcloud/services/cos/data_source_tc_cos_object_signed_url.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
``` |
41 changes: 41 additions & 0 deletions
41
tencentcloud/services/cos/data_source_tc_cos_object_signed_url_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.