Skip to content

Commit b4125aa

Browse files
author
hellertang
authored
Feature/support vod sub application (#723)
* support vod sub application * support vod sub application * do some fix * add doc
1 parent d9367b0 commit b4125aa

File tree

7 files changed

+354
-2
lines changed

7 files changed

+354
-2
lines changed

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,6 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.199 h1:Op
494494
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.199/go.mod h1:Rh/4NXBd0aqmaRGDYcW4gL2Zi8JShGZiB23zrfVaS90=
495495
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis v1.0.199 h1:lXCng7HQqvubF7uwa7x5COsDZlJEjEJ/RBpaeYGc0+I=
496496
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis v1.0.199/go.mod h1:5bwboqeXqVnRvUlKn2G9Y9DbOnWMSVQ0zWhhPZKUVZE=
497-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf v1.0.267 h1:ipvvQOzGyUUGohsVLElJnDDZn4Xoq1axk8my+GLyRYs=
498-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf v1.0.267/go.mod h1:Pew6DV5oBGrzHYWZ8ssiHeJS/Z39ggVv1y5ADGWdO4s=
499497
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf v1.0.275 h1:hn5RrN/qkcObnyKfJ+raey/riVeRqHJFvY34l2YgELs=
500498
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf v1.0.275/go.mod h1:Pew6DV5oBGrzHYWZ8ssiHeJS/Z39ggVv1y5ADGWdO4s=
501499
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver v1.0.199 h1:Ms62XLYCuqrdG4mD5S72oj/ZxdNTxJ+Mc4w0Kxqucwo=

tencentcloud/extension_vod.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@ const (
77
VOD_AUDIO_CHANNEL_DUAL = "dual"
88
VOD_AUDIO_CHANNEL_STEREO = "stereo"
99

10+
VOD_SUB_APPLICATION_RUNNING = "On"
11+
VOD_SUB_APPLICATION_STOPPED = "Off"
12+
VOD_SUB_APPLICATION_DESTROY = "Destroyed"
13+
1014
VOD_DEFAULT_OFFSET = 0
1115
VOD_MAX_LIMIT = 100
1216
)
1317

18+
var VOD_SUB_APPLICATION_STATUS = []string{
19+
VOD_SUB_APPLICATION_RUNNING,
20+
VOD_SUB_APPLICATION_STOPPED,
21+
VOD_SUB_APPLICATION_DESTROY,
22+
}
23+
24+
1425
var (
1526
VOD_AUDIO_CHANNEL_TYPE_TO_INT = map[string]int64{
1627
VOD_AUDIO_CHANNEL_MONO: 1,

tencentcloud/internal/helper/transform.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ func PString(pointer *string) string {
5151
return *pointer
5252
}
5353

54+
func PUint64(pointer *uint64) uint64 {
55+
return *pointer
56+
}
57+
5458
// Takes the result of flatmap.Expand for an array of strings
5559
// and returns a []string
5660
func InterfacesStrings(configured []interface{}) []string {
@@ -121,3 +125,14 @@ func StrToInt64(s string) (i int64) {
121125
i, _ = strconv.ParseInt(s, 10, 64)
122126
return
123127
}
128+
129+
func UInt64ToStr(s uint64) (i string) {
130+
i = strconv.FormatUint(s, 10)
131+
return
132+
}
133+
134+
func StrToUInt64(s string) (i uint64) {
135+
intNum, _ := strconv.Atoi(s)
136+
i = uint64(intNum)
137+
return
138+
}

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ Video on Demand(VOD)
484484
tencentcloud_vod_snapshot_by_time_offset_template
485485
tencentcloud_vod_image_sprite_template
486486
tencentcloud_vod_super_player_config
487+
tencentcloud_vod_sub_application
487488
488489
Virtual Private Cloud(VPC)
489490
Data Source
@@ -953,6 +954,7 @@ func Provider() terraform.ResourceProvider {
953954
"tencentcloud_vod_procedure_template": resourceTencentCloudVodProcedureTemplate(),
954955
"tencentcloud_vod_snapshot_by_time_offset_template": resourceTencentCloudVodSnapshotByTimeOffsetTemplate(),
955956
"tencentcloud_vod_super_player_config": resourceTencentCloudVodSuperPlayerConfig(),
957+
"tencentcloud_vod_sub_application": resourceTencentCloudVodSubApplication(),
956958
"tencentcloud_sqlserver_publish_subscribe": resourceTencentCloudSqlserverPublishSubscribe(),
957959
"tencentcloud_api_gateway_usage_plan": resourceTencentCloudAPIGatewayUsagePlan(),
958960
"tencentcloud_api_gateway_usage_plan_attachment": resourceTencentCloudAPIGatewayUsagePlanAttachment(),
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
/*
2+
Provide a resource to create a VOD sub application.
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_vod_sub_application" "foo" {
8+
name = "foo"
9+
status = "On"
10+
description = "this is sub application"
11+
}
12+
```
13+
14+
Import
15+
16+
VOD super player config can be imported using the name+, e.g.
17+
18+
```
19+
$ terraform import tencentcloud_vod_sub_application.foo name+"#"+id
20+
```
21+
*/
22+
package tencentcloud
23+
24+
import (
25+
"fmt"
26+
"log"
27+
"strings"
28+
29+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
30+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
31+
vod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod/v20180717"
32+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
33+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit"
34+
)
35+
36+
func resourceTencentCloudVodSubApplication() *schema.Resource {
37+
return &schema.Resource{
38+
Create: resourceTencentCloudVodSubApplicationCreate,
39+
Read: resourceTencentCloudVodSubApplicationRead,
40+
Update: resourceTencentCloudVodSubApplicationUpdate,
41+
Delete: resourceTencentCloudVodSubApplicationDelete,
42+
Importer: &schema.ResourceImporter{
43+
State: schema.ImportStatePassthrough,
44+
},
45+
46+
Schema: map[string]*schema.Schema{
47+
"name": {
48+
Type: schema.TypeString,
49+
Required: true,
50+
ValidateFunc: validateStringLengthInRange(1, 40),
51+
Description: "Sub application name, which can contain up to 64 letters, digits, underscores, and hyphens (such as test_ABC-123) and must be unique under a user.",
52+
},
53+
"status": {
54+
Type: schema.TypeString,
55+
Required: true,
56+
Description: "Sub appliaction status.",
57+
ValidateFunc: validateAllowedStringValue(VOD_SUB_APPLICATION_STATUS),
58+
},
59+
"description": {
60+
Type: schema.TypeString,
61+
Optional: true,
62+
Description: "Sub application description.",
63+
},
64+
//computed
65+
"create_time": {
66+
Type: schema.TypeString,
67+
Computed: true,
68+
Description: "The time when the sub application was created.",
69+
},
70+
},
71+
}
72+
}
73+
74+
func resourceTencentCloudVodSubApplicationCreate(d *schema.ResourceData, meta interface{}) error {
75+
defer logElapsed("resource.tencentcloud_vod_sub_application.create")()
76+
77+
var (
78+
logId = getLogId(contextNil)
79+
request = vod.NewCreateSubAppIdRequest()
80+
subAppId *uint64
81+
subAppName *string
82+
)
83+
84+
if v, ok := d.GetOk("name"); ok {
85+
subAppName = helper.String(v.(string))
86+
request.Name = subAppName
87+
}
88+
89+
if v, ok := d.GetOk("description"); ok {
90+
request.Description = helper.String(v.(string))
91+
}
92+
93+
if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
94+
ratelimit.Check(request.GetAction())
95+
response, err := meta.(*TencentCloudClient).apiV3Conn.UseVodClient().CreateSubAppId(request)
96+
if err != nil {
97+
log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, request.GetAction(), err.Error())
98+
return retryError(err)
99+
}
100+
subAppId = response.Response.SubAppId
101+
return nil
102+
}); err != nil {
103+
return err
104+
}
105+
106+
d.SetId(*subAppName + FILED_SP + helper.UInt64ToStr(*subAppId))
107+
108+
if v, ok := d.GetOk("status"); ok {
109+
statusResquest := vod.NewModifySubAppIdStatusRequest()
110+
statusResquest.SubAppId = subAppId
111+
statusResquest.Status = helper.String(v.(string))
112+
113+
if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
114+
ratelimit.Check(statusResquest.GetAction())
115+
_, err := meta.(*TencentCloudClient).apiV3Conn.UseVodClient().ModifySubAppIdStatus(statusResquest)
116+
if err != nil {
117+
log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, request.GetAction(), err.Error())
118+
return retryError(err)
119+
}
120+
return nil
121+
}); err != nil {
122+
return err
123+
}
124+
}
125+
126+
return resourceTencentCloudVodSubApplicationRead(d, meta)
127+
}
128+
129+
func resourceTencentCloudVodSubApplicationRead(d *schema.ResourceData, meta interface{}) error {
130+
defer logElapsed("resource.tencentcloud_vod_sub_application.read")()
131+
defer inconsistentCheck(d, meta)()
132+
133+
var (
134+
//logId = getLogId(contextNil)
135+
//ctx = context.WithValue(context.TODO(), logIdKey, logId)
136+
client = meta.(*TencentCloudClient).apiV3Conn
137+
request = vod.NewDescribeSubAppIdsRequest()
138+
appInfo = vod.SubAppIdInfo{}
139+
)
140+
141+
idSplit := strings.Split(d.Id(), FILED_SP)
142+
if len(idSplit) != 2 {
143+
return fmt.Errorf("sub application id is borken, id is %s", d.Id())
144+
}
145+
subAppName := idSplit[0]
146+
subAppId := idSplit[1]
147+
148+
request.Name = &subAppName
149+
150+
response, err := client.UseVodClient().DescribeSubAppIds(request)
151+
if err != nil {
152+
return err
153+
}
154+
infoSet := response.Response.SubAppIdInfoSet
155+
if len(infoSet) == 0 {
156+
d.SetId("")
157+
return nil
158+
}
159+
160+
for _, info := range infoSet {
161+
if helper.UInt64ToStr(helper.PUint64(info.SubAppId)) == subAppId {
162+
appInfo = *info
163+
break
164+
}
165+
}
166+
_ = d.Set("name", appInfo.Name)
167+
_ = d.Set("description", appInfo.Description)
168+
// there may hide a bug, appInfo do not return status. So use the user input
169+
//_ = d.Set("status", appInfo.Status)
170+
_ = d.Set("status", helper.String(d.Get("status").(string)))
171+
_ = d.Set("create_time", appInfo.CreateTime)
172+
return nil
173+
}
174+
175+
func resourceTencentCloudVodSubApplicationUpdate(d *schema.ResourceData, meta interface{}) error {
176+
defer logElapsed("resource.tencentcloud_vod_sub_application.update")()
177+
178+
var (
179+
logId = getLogId(contextNil)
180+
request = vod.NewModifySubAppIdInfoRequest()
181+
changeFlag = false
182+
)
183+
184+
idSplit := strings.Split(d.Id(), FILED_SP)
185+
if len(idSplit) != 2 {
186+
return fmt.Errorf("sub application id is borken, id is %s", d.Id())
187+
}
188+
subAppId := idSplit[1]
189+
190+
if d.HasChange("name") {
191+
changeFlag = true
192+
if v, ok := d.GetOk("name"); ok {
193+
request.Name = helper.String(v.(string))
194+
}
195+
}
196+
if d.HasChange("description") {
197+
changeFlag = true
198+
if v, ok := d.GetOk("description"); ok {
199+
request.Description = helper.String(v.(string))
200+
}
201+
}
202+
request.SubAppId = helper.Uint64(helper.StrToUInt64(subAppId))
203+
204+
if changeFlag {
205+
var err error
206+
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
207+
ratelimit.Check(request.GetAction())
208+
_, err = meta.(*TencentCloudClient).apiV3Conn.UseVodClient().ModifySubAppIdInfo(request)
209+
if err != nil {
210+
log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, request.GetAction(), err.Error())
211+
return retryError(err)
212+
}
213+
return nil
214+
})
215+
if err != nil {
216+
return err
217+
}
218+
}
219+
if d.HasChange("status") {
220+
var statusRequest = vod.NewModifySubAppIdStatusRequest()
221+
if v, ok := d.GetOk("status"); ok {
222+
statusRequest.Status = helper.String(v.(string))
223+
}
224+
statusRequest.SubAppId = helper.Uint64(helper.StrToUInt64(subAppId))
225+
var err error
226+
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
227+
ratelimit.Check(statusRequest.GetAction())
228+
_, err = meta.(*TencentCloudClient).apiV3Conn.UseVodClient().ModifySubAppIdStatus(statusRequest)
229+
if err != nil {
230+
log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, statusRequest.GetAction(), err.Error())
231+
return retryError(err)
232+
}
233+
return nil
234+
})
235+
}
236+
return resourceTencentCloudVodSubApplicationRead(d, meta)
237+
}
238+
239+
func resourceTencentCloudVodSubApplicationDelete(d *schema.ResourceData, meta interface{}) error {
240+
defer logElapsed("resource.tencentcloud_vod_sub_application.delete")()
241+
logId := getLogId(contextNil)
242+
243+
idSplit := strings.Split(d.Id(), FILED_SP)
244+
if len(idSplit) != 2 {
245+
return fmt.Errorf("sub application id is borken, id is %s", d.Id())
246+
}
247+
subAppId := idSplit[1]
248+
var statusRequest = vod.NewModifySubAppIdStatusRequest()
249+
// first turn off
250+
statusRequest.Status = helper.String("Off")
251+
statusRequest.SubAppId = helper.Uint64(helper.StrToUInt64(subAppId))
252+
if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
253+
ratelimit.Check(statusRequest.GetAction())
254+
if _, err := meta.(*TencentCloudClient).apiV3Conn.UseVodClient().ModifySubAppIdStatus(statusRequest); err != nil {
255+
log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, statusRequest.GetAction(), err.Error())
256+
return retryError(err, InternalError)
257+
}
258+
return nil
259+
}); err != nil {
260+
return err
261+
}
262+
// then destroy
263+
statusRequest.Status = helper.String("Destroyed")
264+
if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
265+
ratelimit.Check(statusRequest.GetAction())
266+
if _, err := meta.(*TencentCloudClient).apiV3Conn.UseVodClient().ModifySubAppIdStatus(statusRequest); err != nil {
267+
log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, statusRequest.GetAction(), err.Error())
268+
return retryError(err, InternalError)
269+
}
270+
return nil
271+
}); err != nil {
272+
return err
273+
}
274+
275+
return nil
276+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
subcategory: "Video on Demand(VOD)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_vod_sub_application"
5+
sidebar_current: "docs-tencentcloud-resource-vod_sub_application"
6+
description: |-
7+
Provide a resource to create a VOD sub application.
8+
---
9+
10+
# tencentcloud_vod_sub_application
11+
12+
Provide a resource to create a VOD sub application.
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "tencentcloud_vod_sub_application" "foo" {
18+
name = "foo"
19+
status = "On"
20+
description = "this is sub application"
21+
}
22+
```
23+
24+
## Argument Reference
25+
26+
The following arguments are supported:
27+
28+
* `name` - (Required) Sub application name, which can contain up to 64 letters, digits, underscores, and hyphens (such as test_ABC-123) and must be unique under a user.
29+
* `status` - (Required) Sub appliaction status.
30+
* `description` - (Optional) Sub application description.
31+
32+
## Attributes Reference
33+
34+
In addition to all arguments above, the following attributes are exported:
35+
36+
* `id` - ID of the resource.
37+
* `create_time` - The time when the sub application was created.
38+
39+
40+
## Import
41+
42+
VOD super player config can be imported using the name+, e.g.
43+
44+
```
45+
$ terraform import tencentcloud_vod_sub_application.foo name+"#"+id
46+
```
47+

0 commit comments

Comments
 (0)