Skip to content

Commit 51e5596

Browse files
tongyimingmikatong
andauthored
Feat/cos resource (#1928)
* add cos resource * update * add cos resource * add doc * add changelog * add changelog * update --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 1cce53f commit 51e5596

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+6184
-697
lines changed

.changelog/1928.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
```release-note:new-data-source
2+
tencentcloud_cos_batchs
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_cos_bucket_inventorys
7+
```
8+
9+
```release-note:new-data-source
10+
tencentcloud_cos_bucket_multipart_uploads
11+
```
12+
13+
```release-note:new-resource
14+
tencentcloud_cos_batch
15+
```
16+
17+
```release-note:enhancement
18+
resource/tencentcloud_ci_media_concat_template: adapt to new cos sdk
19+
```
20+
21+
```release-note:enhancement
22+
resource/tencentcloud_ci_media_video_montage_template: adapt to new cos sdk
23+
```
24+
25+
```release-note:enhancement
26+
resource/tencentcloud_ci_media_transcode_template: adapt to new cos sdk
27+
```

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ require (
1111
github.com/fatih/color v1.15.0
1212
github.com/golangci/golangci-lint v1.52.2
1313
github.com/google/go-querystring v1.1.0 // indirect
14+
github.com/google/uuid v1.3.0
1415
github.com/hashicorp/errwrap v1.1.0 // indirect
1516
github.com/hashicorp/go-multierror v1.1.1
1617
github.com/hashicorp/hcl/v2 v2.13.0
1718
github.com/hashicorp/terraform-plugin-sdk/v2 v2.20.0
1819
github.com/katbyte/terrafmt v0.2.0
1920
github.com/mitchellh/go-homedir v1.1.0
2021
github.com/mitchellh/mapstructure v1.5.0 // indirect
21-
github.com/mozillazg/go-httpheader v0.3.1 // indirect
22+
github.com/mozillazg/go-httpheader v0.4.0 // indirect
2223
github.com/pkg/errors v0.9.1
2324
github.com/stretchr/testify v1.8.2
2425
github.com/tencentcloud/tencentcloud-sdk-go-intl-en v3.0.646+incompatible
@@ -52,7 +53,7 @@ require (
5253
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr v1.0.287
5354
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es v1.0.383
5455
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap v1.0.514
55-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.199
56+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563
5657
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.656
5758
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live v1.0.535
5859
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb v1.0.672
@@ -87,7 +88,7 @@ require (
8788
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199
8889
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.669
8990
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199
90-
github.com/tencentyun/cos-go-sdk-v5 v0.7.40
91+
github.com/tencentyun/cos-go-sdk-v5 v0.7.42-0.20230629101357-7edd77448a0f
9192
github.com/yangwenmai/ratelimit v0.0.0-20180104140304-44221c2292e1
9293
gopkg.in/yaml.v2 v2.4.0
9394
)
@@ -163,7 +164,6 @@ require (
163164
github.com/golangci/revgrep v0.0.0-20220804021717-745bb2f7c2e6 // indirect
164165
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 // indirect
165166
github.com/google/go-cmp v0.5.9 // indirect
166-
github.com/google/uuid v1.3.0 // indirect
167167
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
168168
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
169169
github.com/gookit/color v1.5.2 // indirect

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ github.com/moricho/tparallel v0.3.1/go.mod h1:leENX2cUv7Sv2qDgdi0D0fCftN8fRC67Bc
598598
github.com/mozillazg/go-httpheader v0.2.1/go.mod h1:jJ8xECTlalr6ValeXYdOF8fFUISeBAdw6E61aqQma60=
599599
github.com/mozillazg/go-httpheader v0.3.1 h1:IRP+HFrMX2SlwY9riuio7raffXUpzAosHtZu25BSJok=
600600
github.com/mozillazg/go-httpheader v0.3.1/go.mod h1:PuT8h0pw6efvp8ZeUec1Rs7dwjK08bt6gKSReGMqtdA=
601+
github.com/mozillazg/go-httpheader v0.4.0 h1:aBn6aRXtFzyDLZ4VIRLsZbbJloagQfMnCiYgOq6hK4w=
602+
github.com/mozillazg/go-httpheader v0.4.0/go.mod h1:PuT8h0pw6efvp8ZeUec1Rs7dwjK08bt6gKSReGMqtdA=
601603
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
602604
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
603605
github.com/nakabonne/nestif v0.3.1 h1:wm28nZjhQY5HyYPx+weN3Q65k6ilSBxDb8v5S81B81U=
@@ -821,6 +823,7 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.540/go.mod
821823
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.542/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
822824
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.544/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
823825
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.547/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
826+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.563/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
824827
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.571/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
825828
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.572/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
826829
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.578/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
@@ -894,6 +897,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap v1.0.514/go.mod h1
894897
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.194/go.mod h1:yrBKWhChnDqNz1xuXdSbWXG56XawEq0G5j1lg4VwBD4=
895898
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.199 h1:rSDQeqvV4khOJUyg6xmMYF26CRd+WtSYvfwP6N72NP0=
896899
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.199/go.mod h1:yrBKWhChnDqNz1xuXdSbWXG56XawEq0G5j1lg4VwBD4=
900+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563 h1:FoX+MK4vHThvPO6FbP5q98zD8S3n+d5+DbtK7skl++c=
901+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563/go.mod h1:uom4Nvi9W+Qkom0exYiJ9VWJjXwyxtPYTkKkaLMlfE0=
897902
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.656 h1:g//msFpwifqXoHuSIara75o2NWs4lRYHJ5qWQuBcYEs=
898903
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.656/go.mod h1:hvE2a3GX09BXRMTIsk0w0vkAJ4QcagMq+pXCLnFmPNM=
899904
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live v1.0.535 h1:hSwG4ZbcP2Ht+AWHI81EMCvJdDTyTT7zP/qjOe/VcZg=
@@ -966,6 +971,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199 h1:hMBLtiJ
966971
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199/go.mod h1:nnY91/H3j/Gu7V/oCA6Zeg8T5D3q36EUdBh4EjmHwqY=
967972
github.com/tencentyun/cos-go-sdk-v5 v0.7.40 h1:W6vDGKCHe4wBACI1d2UgE6+50sJFhRWU4O8IB2ozzxM=
968973
github.com/tencentyun/cos-go-sdk-v5 v0.7.40/go.mod h1:4dCEtLHGh8QPxHEkgq+nFaky7yZxQuYwgSJM87icDaw=
974+
github.com/tencentyun/cos-go-sdk-v5 v0.7.42-0.20230629101357-7edd77448a0f h1:yTMDoBvFuXjZat10d98DIKbPnN9FQG+drt1SbNfMW5U=
975+
github.com/tencentyun/cos-go-sdk-v5 v0.7.42-0.20230629101357-7edd77448a0f/go.mod h1:LUFnaqRmGk6pEHOaRmdn2dCZR2j0cSsM5xowWFPTPao=
969976
github.com/tenntenn/modver v1.0.1/go.mod h1:bePIyQPb7UeioSRkw3Q0XeMhYZSMx9B8ePqg6SAMGH0=
970977
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY=
971978
github.com/tetafro/godot v1.4.11 h1:BVoBIqAf/2QdbFmSwAWnaIqDivZdOV0ZRwEm6jivLKw=

tencentcloud/connectivity/client.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ type TencentCloudClient struct {
158158
dbbrainConn *dbbrain.Client
159159
dtsConn *dts.Client
160160
ciConn *cos.Client
161+
cosBatchConn *cos.Client
161162
tsfConn *tsf.Client
162163
mpsConn *mps.Client
163164
cwpConn *cwp.Client
@@ -1007,6 +1008,30 @@ func (me *TencentCloudClient) UseDtsClient() *dts.Client {
10071008
return me.dtsConn
10081009
}
10091010

1011+
// UseCosBatchClient returns ci client for service
1012+
func (me *TencentCloudClient) UseCosBatchClient(uin string) *cos.Client {
1013+
u, _ := url.Parse(fmt.Sprintf("https://%s.cos-control.%s.myqcloud.com", uin, me.Region))
1014+
1015+
if me.cosBatchConn != nil && me.cosBatchConn.BaseURL.BatchURL == u {
1016+
return me.cosBatchConn
1017+
}
1018+
1019+
baseUrl := &cos.BaseURL{
1020+
BatchURL: u,
1021+
}
1022+
1023+
me.cosBatchConn = cos.NewClient(baseUrl, &http.Client{
1024+
Timeout: 100 * time.Second,
1025+
Transport: &cos.AuthorizationTransport{
1026+
SecretID: me.Credential.SecretId,
1027+
SecretKey: me.Credential.SecretKey,
1028+
SessionToken: me.Credential.Token,
1029+
},
1030+
})
1031+
1032+
return me.cosBatchConn
1033+
}
1034+
10101035
// UseCiClient returns ci client for service
10111036
func (me *TencentCloudClient) UseCiClient(bucket string) *cos.Client {
10121037
u, _ := url.Parse(fmt.Sprintf("https://%s.ci.%s.myqcloud.com", bucket, me.Region))
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
Use this data source to query the COS batch.
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_cos_batchs" "cos_batchs" {
8+
uin = "xxxxxx"
9+
appid = "xxxxxx"
10+
}
11+
```
12+
*/
13+
package tencentcloud
14+
15+
import (
16+
"context"
17+
"encoding/json"
18+
"log"
19+
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
21+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22+
"github.com/tencentyun/cos-go-sdk-v5"
23+
)
24+
25+
func dataSourceTencentCloudCosBatchs() *schema.Resource {
26+
return &schema.Resource{
27+
Read: dataSourceTencentCloudCosBatchsRead,
28+
29+
Schema: map[string]*schema.Schema{
30+
"uin": {
31+
Type: schema.TypeString,
32+
Required: true,
33+
Description: "Uin.",
34+
},
35+
"appid": {
36+
Type: schema.TypeInt,
37+
Required: true,
38+
Description: "Appid.",
39+
},
40+
"job_statuses": {
41+
Type: schema.TypeString,
42+
Optional: true,
43+
Description: "The task status information you need to query. If you do not specify a task status, COS returns the status of all tasks that have been executed, including those that are in progress. If you specify a task status, COS returns the task in the specified state. Optional task states include: Active, Cancelled, Cancelling, Complete, Completing, Failed, Failing, New, Paused, Pausing, Preparing, Ready, Suspended.",
44+
},
45+
"jobs": {
46+
Type: schema.TypeList,
47+
Computed: true,
48+
Description: "Multiple batch processing task information.",
49+
Elem: &schema.Resource{
50+
Schema: map[string]*schema.Schema{
51+
"creation_time": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
Description: "Job creation time.",
55+
},
56+
"description": {
57+
Type: schema.TypeString,
58+
Computed: true,
59+
Description: "Mission description. The length is limited to 0-256 bytes.",
60+
},
61+
"job_id": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
Description: "Job ID. The length is limited to 1-64 bytes.",
65+
},
66+
"operation": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: "Actions performed on objects in a batch processing job. For example, COSPutObjectCopy.",
70+
},
71+
"priority": {
72+
Type: schema.TypeInt,
73+
Computed: true,
74+
Description: "Mission priority. Tasks with higher values will be given priority. The priority size is limited to 0-2147483647.",
75+
},
76+
"status": {
77+
Type: schema.TypeString,
78+
Computed: true,
79+
Description: "Task execution status. Legal parameter values include Active, Cancelled, Cancelling, Complete, Completing, Failed, Failing, New, Paused, Pausing, Preparing, Ready, Suspended.",
80+
},
81+
"termination_date": {
82+
Type: schema.TypeString,
83+
Computed: true,
84+
Description: "The end time of the batch processing job.",
85+
},
86+
"progress_summary": {
87+
Type: schema.TypeList,
88+
Computed: true,
89+
Description: "Summary of the status of task implementation. Describe the total number of operations performed in this task, the number of successful operations, and the number of failed operations.",
90+
Elem: &schema.Resource{
91+
Schema: map[string]*schema.Schema{
92+
"number_of_tasks_failed": {
93+
Type: schema.TypeInt,
94+
Computed: true,
95+
Description: "The current failed Operand.",
96+
},
97+
"number_of_tasks_succeeded": {
98+
Type: schema.TypeInt,
99+
Computed: true,
100+
Description: "The current successful Operand.",
101+
},
102+
"total_number_of_tasks": {
103+
Type: schema.TypeInt,
104+
Computed: true,
105+
Description: "Total Operand.",
106+
},
107+
},
108+
},
109+
},
110+
},
111+
},
112+
},
113+
"result_output_file": {
114+
Type: schema.TypeString,
115+
Optional: true,
116+
Description: "Used to save results.",
117+
},
118+
},
119+
}
120+
}
121+
122+
func dataSourceTencentCloudCosBatchsRead(d *schema.ResourceData, meta interface{}) error {
123+
defer logElapsed("data_source.tencentcloud_cos_batchs.read")()
124+
125+
logId := getLogId(contextNil)
126+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
127+
uin := d.Get("uin").(string)
128+
appid := d.Get("appid").(int)
129+
jobs := make([]map[string]interface{}, 0)
130+
131+
opt := &cos.BatchListJobsOptions{}
132+
if v, ok := d.GetOk("job_statuses"); ok {
133+
opt.JobStatuses = v.(string)
134+
}
135+
headers := &cos.BatchRequestHeaders{
136+
XCosAppid: appid,
137+
}
138+
ids := make([]string, 0)
139+
for {
140+
req, _ := json.Marshal(opt)
141+
result, response, err := meta.(*TencentCloudClient).apiV3Conn.UseCosBatchClient(uin).Batch.ListJobs(ctx, nil, headers)
142+
responseBody, _ := json.Marshal(response.Body)
143+
log.Printf("[DEBUG]%s api[ListJobs] success, request body [%s], response body [%v]\n", logId, req, responseBody)
144+
if err != nil {
145+
return err
146+
}
147+
for _, item := range result.Jobs.Members {
148+
jobItem := make(map[string]interface{})
149+
jobItem["creation_time"] = item.CreationTime
150+
jobItem["description"] = item.Description
151+
jobItem["job_id"] = item.JobId
152+
jobItem["operation"] = item.Operation
153+
jobItem["priority"] = item.Priority
154+
jobItem["status"] = item.Status
155+
jobItem["termination_date"] = item.TerminationDate
156+
progressSummary := map[string]interface{}{
157+
"number_of_tasks_failed": item.ProgressSummary.NumberOfTasksFailed,
158+
"number_of_tasks_succeeded": item.ProgressSummary.NumberOfTasksSucceeded,
159+
"total_number_of_tasks": item.ProgressSummary.TotalNumberOfTasks,
160+
}
161+
jobItem["progress_summary"] = []interface{}{progressSummary}
162+
ids = append(ids, item.JobId)
163+
jobs = append(jobs, jobItem)
164+
}
165+
if result.NextToken != "" {
166+
opt.NextToken = result.NextToken
167+
} else {
168+
break
169+
}
170+
}
171+
172+
d.SetId(helper.DataResourceIdsHash(ids))
173+
_ = d.Set("jobs", jobs)
174+
output, ok := d.GetOk("result_output_file")
175+
if ok && output.(string) != "" {
176+
if err := writeToFile(output.(string), jobs); err != nil {
177+
return err
178+
}
179+
}
180+
181+
return nil
182+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudCosBatchsDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccCosBatchsDataSource,
19+
Check: resource.ComposeTestCheckFunc(
20+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_cos_batchs.cos_batchs"),
21+
resource.TestCheckResourceAttrSet("data.tencentcloud_cos_batchs.cos_batchs", "jobs.#"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
const testAccCosBatchsDataSource = `
29+
data "tencentcloud_cos_batchs" "cos_batchs" {
30+
uin = "100022975249"
31+
appid = "1308919341"
32+
}
33+
`

0 commit comments

Comments
 (0)