Skip to content

Commit a26f575

Browse files
tongyimingmikatong
and
mikatong
authored
fix(vod): [116206298] support event config (#2575)
* support event config * add changelog --------- Co-authored-by: mikatong <[email protected]>
1 parent e1869e0 commit a26f575

9 files changed

+376
-0
lines changed

.changelog/2575.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_vod_event_config
3+
```

tencentcloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,7 @@ func Provider() *schema.Provider {
14301430
"tencentcloud_vod_sample_snapshot_template": vod.ResourceTencentCloudVodSampleSnapshotTemplate(),
14311431
"tencentcloud_vod_transcode_template": vod.ResourceTencentCloudVodTranscodeTemplate(),
14321432
"tencentcloud_vod_watermark_template": vod.ResourceTencentCloudVodWatermarkTemplate(),
1433+
"tencentcloud_vod_event_config": vod.ResourceTencentCloudVodEventConfig(),
14331434
"tencentcloud_sqlserver_publish_subscribe": sqlserver.ResourceTencentCloudSqlserverPublishSubscribe(),
14341435
"tencentcloud_api_gateway_usage_plan": apigateway.ResourceTencentCloudAPIGatewayUsagePlan(),
14351436
"tencentcloud_api_gateway_usage_plan_attachment": apigateway.ResourceTencentCloudAPIGatewayUsagePlanAttachment(),

tencentcloud/provider.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@ Video on Demand(VOD)
11221122
tencentcloud_vod_sample_snapshot_template
11231123
tencentcloud_vod_transcode_template
11241124
tencentcloud_vod_watermark_template
1125+
tencentcloud_vod_event_config
11251126

11261127
Oceanus
11271128
Data Source
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package vod
2+
3+
import (
4+
"context"
5+
"log"
6+
"strconv"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
11+
vod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod/v20180717"
12+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
13+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
14+
)
15+
16+
func ResourceTencentCloudVodEventConfig() *schema.Resource {
17+
return &schema.Resource{
18+
Create: resourceTencentCloudVodEventConfigCreate,
19+
Read: resourceTencentCloudVodEventConfigRead,
20+
Update: resourceTencentCloudVodEventConfigUpdate,
21+
Delete: resourceTencentCloudVodEventConfigDelete,
22+
Importer: &schema.ResourceImporter{
23+
State: schema.ImportStatePassthrough,
24+
},
25+
Schema: map[string]*schema.Schema{
26+
"sub_app_id": {
27+
Required: true,
28+
Type: schema.TypeInt,
29+
Description: "Sub app id.",
30+
},
31+
32+
"mode": {
33+
Optional: true,
34+
Computed: true,
35+
Type: schema.TypeString,
36+
Description: "How to receive event notifications. Valid values:\n" +
37+
"- Push: HTTP callback notification;\n" +
38+
"- PULL: Reliable notification based on message queuing.",
39+
},
40+
41+
"notification_url": {
42+
Optional: true,
43+
Type: schema.TypeString,
44+
Description: "The address used to receive 3.0 format callbacks when receiving HTTP callback notifications. Note: If you take the NotificationUrl parameter and the value is an empty string, the 3.0 format callback address is cleared.",
45+
},
46+
47+
"upload_media_complete_event_switch": {
48+
Optional: true,
49+
Computed: true,
50+
Type: schema.TypeString,
51+
Description: "Whether to receive video upload completion event notification, default `OFF` means to ignore the event notification, `ON` means to receive event notification.",
52+
},
53+
54+
"delete_media_complete_event_switch": {
55+
Optional: true,
56+
Computed: true,
57+
Type: schema.TypeString,
58+
Description: "Whether to receive video deletion completion event notification, default `OFF` is to ignore the event notification, `ON` is to receive event notification.",
59+
},
60+
},
61+
}
62+
}
63+
64+
func resourceTencentCloudVodEventConfigCreate(d *schema.ResourceData, meta interface{}) error {
65+
defer tccommon.LogElapsed("resource.tencentcloud_vod_event_config.create")()
66+
defer tccommon.InconsistentCheck(d, meta)()
67+
68+
var subAppId int
69+
if v, ok := d.GetOk("sub_app_id"); ok {
70+
subAppId = v.(int)
71+
}
72+
73+
d.SetId(strconv.Itoa(subAppId))
74+
75+
return resourceTencentCloudVodEventConfigUpdate(d, meta)
76+
}
77+
78+
func resourceTencentCloudVodEventConfigRead(d *schema.ResourceData, meta interface{}) error {
79+
defer tccommon.LogElapsed("resource.tencentcloud_vod_event_config.read")()
80+
defer tccommon.InconsistentCheck(d, meta)()
81+
82+
logId := tccommon.GetLogId(tccommon.ContextNil)
83+
84+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
85+
86+
service := VodService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
87+
88+
subAppId, err := strconv.Atoi(d.Id())
89+
if err != nil {
90+
return err
91+
}
92+
_ = d.Set("sub_app_id", subAppId)
93+
94+
eventConfig, err := service.DescribeVodEventConfig(ctx, uint64(subAppId))
95+
if err != nil {
96+
return err
97+
}
98+
99+
if eventConfig == nil {
100+
d.SetId("")
101+
log.Printf("[WARN]%s resource `VodEventConfig` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
102+
return nil
103+
}
104+
105+
if eventConfig.Mode != nil {
106+
_ = d.Set("mode", eventConfig.Mode)
107+
}
108+
109+
if eventConfig.NotificationUrl != nil {
110+
_ = d.Set("notification_url", eventConfig.NotificationUrl)
111+
}
112+
113+
if eventConfig.UploadMediaCompleteEventSwitch != nil {
114+
_ = d.Set("upload_media_complete_event_switch", eventConfig.UploadMediaCompleteEventSwitch)
115+
}
116+
117+
if eventConfig.DeleteMediaCompleteEventSwitch != nil {
118+
_ = d.Set("delete_media_complete_event_switch", eventConfig.DeleteMediaCompleteEventSwitch)
119+
}
120+
121+
return nil
122+
}
123+
124+
func resourceTencentCloudVodEventConfigUpdate(d *schema.ResourceData, meta interface{}) error {
125+
defer tccommon.LogElapsed("resource.tencentcloud_vod_event_config.update")()
126+
defer tccommon.InconsistentCheck(d, meta)()
127+
128+
logId := tccommon.GetLogId(tccommon.ContextNil)
129+
130+
request := vod.NewModifyEventConfigRequest()
131+
132+
subAppId, err := strconv.Atoi(d.Id())
133+
if err != nil {
134+
return err
135+
}
136+
137+
request.SubAppId = helper.IntUint64(subAppId)
138+
139+
if v, ok := d.GetOk("mode"); ok {
140+
request.Mode = helper.String(v.(string))
141+
}
142+
143+
if v, ok := d.GetOk("notification_url"); ok {
144+
request.NotificationUrl = helper.String(v.(string))
145+
}
146+
147+
if v, ok := d.GetOk("upload_media_complete_event_switch"); ok {
148+
request.UploadMediaCompleteEventSwitch = helper.String(v.(string))
149+
}
150+
151+
if v, ok := d.GetOk("delete_media_complete_event_switch"); ok {
152+
request.DeleteMediaCompleteEventSwitch = helper.String(v.(string))
153+
}
154+
155+
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
156+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVodClient().ModifyEventConfig(request)
157+
if e != nil {
158+
if sdkError, ok := e.(*sdkErrors.TencentCloudSDKError); ok {
159+
if sdkError.Code == "FailedOperation" && sdkError.Message == "invalid vod user" {
160+
return resource.RetryableError(e)
161+
}
162+
}
163+
return resource.NonRetryableError(e)
164+
} else {
165+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
166+
}
167+
return nil
168+
})
169+
if err != nil {
170+
log.Printf("[CRITAL]%s update vod eventConfig failed, reason:%+v", logId, err)
171+
return err
172+
}
173+
174+
return resourceTencentCloudVodEventConfigRead(d, meta)
175+
}
176+
177+
func resourceTencentCloudVodEventConfigDelete(d *schema.ResourceData, meta interface{}) error {
178+
defer tccommon.LogElapsed("resource.tencentcloud_vod_event_config.delete")()
179+
defer tccommon.InconsistentCheck(d, meta)()
180+
181+
return nil
182+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Provide a resource to create a vod event config.
2+
3+
Example Usage
4+
5+
```hcl
6+
resource "tencentcloud_vod_sub_application" "sub_application" {
7+
name = "eventconfig-subapplication"
8+
status = "On"
9+
description = "this is sub application"
10+
}
11+
12+
resource "tencentcloud_vod_event_config" "event_config" {
13+
mode = "PUSH"
14+
notification_url = "http://mydemo.com/receiveevent"
15+
upload_media_complete_event_switch = "ON"
16+
delete_media_complete_event_switch = "ON"
17+
sub_app_id = tonumber(split("#", tencentcloud_vod_sub_application.sub_application.id)[1])
18+
}
19+
```
20+
21+
Import
22+
23+
VOD event config can be imported using the subAppId, e.g.
24+
25+
```
26+
$ terraform import tencentcloud_vod_event_config.foo $subAppId
27+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package vod_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
8+
)
9+
10+
func TestAccTencentCloudVodEventConfigResource_basic(t *testing.T) {
11+
t.Parallel()
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { tcacctest.AccPreCheck(t) },
14+
Providers: tcacctest.AccProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAccVodEventConfig,
18+
Check: resource.ComposeTestCheckFunc(
19+
resource.TestCheckResourceAttrSet("tencentcloud_vod_event_config.event_config", "id"),
20+
resource.TestCheckResourceAttrSet("tencentcloud_vod_event_config.event_config", "sub_app_id"),
21+
resource.TestCheckResourceAttr("tencentcloud_vod_event_config.event_config", "mode", "PUSH"),
22+
resource.TestCheckResourceAttr("tencentcloud_vod_event_config.event_config", "notification_url", "http://mydemo.com/receiveevent"),
23+
resource.TestCheckResourceAttr("tencentcloud_vod_event_config.event_config", "upload_media_complete_event_switch", "OFF"),
24+
resource.TestCheckResourceAttr("tencentcloud_vod_event_config.event_config", "delete_media_complete_event_switch", "OFF"),
25+
),
26+
},
27+
{
28+
Config: testAccVodEventConfigUpdate,
29+
Check: resource.ComposeTestCheckFunc(
30+
resource.TestCheckResourceAttr("tencentcloud_vod_event_config.event_config", "upload_media_complete_event_switch", "ON"),
31+
resource.TestCheckResourceAttr("tencentcloud_vod_event_config.event_config", "delete_media_complete_event_switch", "ON"),
32+
),
33+
},
34+
{
35+
ResourceName: "tencentcloud_vod_event_config.event_config",
36+
ImportState: true,
37+
ImportStateVerify: true,
38+
},
39+
},
40+
})
41+
}
42+
43+
const testAccVodEventConfig = `
44+
45+
resource "tencentcloud_vod_sub_application" "sub_application" {
46+
name = "eventconfig-subapplication"
47+
status = "On"
48+
description = "this is sub application"
49+
}
50+
51+
resource "tencentcloud_vod_event_config" "event_config" {
52+
mode = "PUSH"
53+
notification_url = "http://mydemo.com/receiveevent"
54+
sub_app_id = tonumber(split("#", tencentcloud_vod_sub_application.sub_application.id)[1])
55+
}
56+
57+
`
58+
59+
const testAccVodEventConfigUpdate = `
60+
61+
resource "tencentcloud_vod_sub_application" "sub_application" {
62+
name = "eventconfig-subapplication"
63+
status = "On"
64+
description = "this is sub application"
65+
}
66+
67+
resource "tencentcloud_vod_event_config" "event_config" {
68+
mode = "PUSH"
69+
notification_url = "http://mydemo.com/receiveevent"
70+
upload_media_complete_event_switch = "ON"
71+
delete_media_complete_event_switch = "ON"
72+
sub_app_id = tonumber(split("#", tencentcloud_vod_sub_application.sub_application.id)[1])
73+
}
74+
75+
`

tencentcloud/services/vod/service_tencentcloud_vod.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,3 +682,28 @@ func (me *VodService) DeleteVodWatermarkTemplateById(ctx context.Context, subApp
682682

683683
return
684684
}
685+
686+
func (me *VodService) DescribeVodEventConfig(ctx context.Context, subAppId uint64) (eventConfig *vod.DescribeEventConfigResponseParams, errRet error) {
687+
logId := tccommon.GetLogId(tccommon.ContextNil)
688+
689+
request := vod.NewDescribeEventConfigRequest()
690+
request.SubAppId = &subAppId
691+
692+
defer func() {
693+
if errRet != nil {
694+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
695+
}
696+
}()
697+
698+
ratelimit.Check(request.GetAction())
699+
700+
response, err := me.client.UseVodClient().DescribeEventConfig(request)
701+
if err != nil {
702+
errRet = err
703+
return
704+
}
705+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
706+
707+
eventConfig = response.Response
708+
return
709+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
subcategory: "Video on Demand(VOD)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_vod_event_config"
5+
sidebar_current: "docs-tencentcloud-resource-vod_event_config"
6+
description: |-
7+
Provide a resource to create a vod event config.
8+
---
9+
10+
# tencentcloud_vod_event_config
11+
12+
Provide a resource to create a vod event config.
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "tencentcloud_vod_sub_application" "sub_application" {
18+
name = "eventconfig-subapplication"
19+
status = "On"
20+
description = "this is sub application"
21+
}
22+
23+
resource "tencentcloud_vod_event_config" "event_config" {
24+
mode = "PUSH"
25+
notification_url = "http://mydemo.com/receiveevent"
26+
upload_media_complete_event_switch = "ON"
27+
delete_media_complete_event_switch = "ON"
28+
sub_app_id = tonumber(split("#", tencentcloud_vod_sub_application.sub_application.id)[1])
29+
}
30+
```
31+
32+
## Argument Reference
33+
34+
The following arguments are supported:
35+
36+
* `sub_app_id` - (Required, Int) Sub app id.
37+
* `delete_media_complete_event_switch` - (Optional, String) Whether to receive video deletion completion event notification, default `OFF` is to ignore the event notification, `ON` is to receive event notification.
38+
* `mode` - (Optional, String) How to receive event notifications. Valid values:
39+
- Push: HTTP callback notification;
40+
- PULL: Reliable notification based on message queuing.
41+
* `notification_url` - (Optional, String) The address used to receive 3.0 format callbacks when receiving HTTP callback notifications. Note: If you take the NotificationUrl parameter and the value is an empty string, the 3.0 format callback address is cleared.
42+
* `upload_media_complete_event_switch` - (Optional, String) Whether to receive video upload completion event notification, default `OFF` means to ignore the event notification, `ON` means to receive event notification.
43+
44+
## Attributes Reference
45+
46+
In addition to all arguments above, the following attributes are exported:
47+
48+
* `id` - ID of the resource.
49+
50+
51+
52+
## Import
53+
54+
VOD event config can be imported using the subAppId, e.g.
55+
56+
```
57+
$ terraform import tencentcloud_vod_event_config.foo $subAppId
58+
```
59+

0 commit comments

Comments
 (0)