|
| 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 | +} |
0 commit comments