Skip to content

Commit b4c9e78

Browse files
authored
fix(cos): [127892203] tencentcloud_cos_object_download_operation support custom timeouts (#3568)
* add * add
1 parent fcc2630 commit b4c9e78

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

.changelog/3568.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_cos_object_download_operation: support custom timeouts
3+
```

tencentcloud/connectivity/client.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,19 @@ func (me *TencentCloudClient) UseTencentCosClientNew(bucket string, cdcId ...str
349349
}
350350

351351
// UseTencentCosClient tencent cloud own client for service instead of aws
352-
func (me *TencentCloudClient) UseTencentCosClient(bucket string) *cos.Client {
352+
func (me *TencentCloudClient) UseTencentCosClient(bucket string, clientTimeout ...time.Duration) *cos.Client {
353353
cosUrl := fmt.Sprintf("https://%s.cos.%s.myqcloud.com", bucket, me.Region)
354354
if me.CosDomain != "" {
355355
parsedURL, _ := url.Parse(me.CosDomain)
356356
parsedURL.Host = bucket + "." + parsedURL.Host
357357
cosUrl = parsedURL.String()
358358
}
359359

360+
tmpTimeout := 100 * time.Second
361+
if len(clientTimeout) > 0 {
362+
tmpTimeout = clientTimeout[0]
363+
}
364+
360365
u, _ := url.Parse(cosUrl)
361366

362367
if me.tencentCosConn != nil && me.tencentCosConn.BaseURL.BucketURL == u {
@@ -368,7 +373,7 @@ func (me *TencentCloudClient) UseTencentCosClient(bucket string) *cos.Client {
368373
}
369374

370375
me.tencentCosConn = cos.NewClient(baseUrl, &http.Client{
371-
Timeout: 100 * time.Second,
376+
Timeout: tmpTimeout,
372377
Transport: &cos.AuthorizationTransport{
373378
SecretID: me.Credential.SecretId,
374379
SecretKey: me.Credential.SecretKey,

tencentcloud/services/cos/resource_tc_cos_object_download_operation.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"log"
77
"os"
8+
"time"
89

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

@@ -16,7 +17,9 @@ func ResourceTencentCloudCosObjectDownloadOperation() *schema.Resource {
1617
Create: resourceTencentCloudCosObjectDownloadOperationCreate,
1718
Read: resourceTencentCloudCosObjectDownloadOperationRead,
1819
Delete: resourceTencentCloudCosObjectDownloadOperationDelete,
19-
20+
Timeouts: &schema.ResourceTimeout{
21+
Create: schema.DefaultTimeout(3 * time.Minute),
22+
},
2023
Schema: map[string]*schema.Schema{
2124
"bucket": {
2225
Required: true,
@@ -44,16 +47,22 @@ func resourceTencentCloudCosObjectDownloadOperationCreate(d *schema.ResourceData
4447
defer tccommon.LogElapsed("resource.tencentcloud_cos_object_download_operation.create")()
4548
defer tccommon.InconsistentCheck(d, meta)()
4649

47-
logId := tccommon.GetLogId(tccommon.ContextNil)
48-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
50+
var (
51+
logId = tccommon.GetLogId(tccommon.ContextNil)
52+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
53+
)
54+
4955
bucket := d.Get("bucket").(string)
5056
key := d.Get("key").(string)
5157
downloadPath := d.Get("download_path").(string)
52-
resp, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTencentCosClient(bucket).Object.Get(ctx, key, nil)
58+
59+
timeout := d.Timeout(schema.TimeoutCreate)
60+
resp, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTencentCosClient(bucket, timeout).Object.Get(ctx, key, nil)
5361
if err != nil {
5462
log.Printf("[CRITAL]%s object download failed, reason:%+v", logId, err)
5563
return err
5664
}
65+
5766
defer resp.Body.Close()
5867

5968
fd, err := os.OpenFile(downloadPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
@@ -68,7 +77,6 @@ func resourceTencentCloudCosObjectDownloadOperationCreate(d *schema.ResourceData
6877
}
6978

7079
d.SetId(bucket + tccommon.FILED_SP + key)
71-
7280
return resourceTencentCloudCosObjectDownloadOperationRead(d, meta)
7381
}
7482

tencentcloud/services/cos/resource_tc_cos_object_download_operation.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ Provides a resource to download object
33
Example Usage
44

55
```hcl
6-
resource "tencentcloud_cos_object_download_operation" "object_download" {
7-
bucket = "xxxxxxx"
8-
key = "test.txt"
9-
download_path = "/tmp/test.txt"
6+
resource "tencentcloud_cos_object_download_operation" "example" {
7+
bucket = "private-bucket-1309116523"
8+
key = "demo.txt"
9+
download_path = "/tmp/demo.txt"
10+
11+
timeouts {
12+
create = "10m"
13+
}
1014
}
1115
```

website/docs/r/cos_object_download_operation.html.markdown

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ Provides a resource to download object
1414
## Example Usage
1515

1616
```hcl
17-
resource "tencentcloud_cos_object_download_operation" "object_download" {
18-
bucket = "xxxxxxx"
19-
key = "test.txt"
20-
download_path = "/tmp/test.txt"
17+
resource "tencentcloud_cos_object_download_operation" "example" {
18+
bucket = "private-bucket-1309116523"
19+
key = "demo.txt"
20+
download_path = "/tmp/demo.txt"
21+
22+
timeouts {
23+
create = "10m"
24+
}
2125
}
2226
```
2327

@@ -36,4 +40,9 @@ In addition to all arguments above, the following attributes are exported:
3640
* `id` - ID of the resource.
3741

3842

43+
## Timeouts
44+
45+
The `timeouts` block allows you to specify [timeouts](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts) for certain actions:
46+
47+
* `create` - (Defaults to `3m`) Used when creating the resource.
3948

0 commit comments

Comments
 (0)