|
| 1 | +/* |
| 2 | +Provides a resource to create a scf trigger_config |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_scf_trigger_config" "trigger_config" { |
| 8 | + enable = "OPEN" |
| 9 | + function_name = "keep-1676351130" |
| 10 | + trigger_name = "SCF-timer-1685540160" |
| 11 | + type = "timer" |
| 12 | + qualifier = "$DEFAULT" |
| 13 | + namespace = "default" |
| 14 | +} |
| 15 | +``` |
| 16 | +
|
| 17 | +Import |
| 18 | +
|
| 19 | +scf trigger_config can be imported using the id, e.g. |
| 20 | +
|
| 21 | +``` |
| 22 | +terraform import tencentcloud_scf_trigger_config.trigger_config functionName#namespace#triggerName |
| 23 | +``` |
| 24 | +*/ |
| 25 | +package tencentcloud |
| 26 | + |
| 27 | +import ( |
| 28 | + "context" |
| 29 | + "fmt" |
| 30 | + "log" |
| 31 | + "strings" |
| 32 | + |
| 33 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 34 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 35 | + scf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf/v20180416" |
| 36 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 37 | +) |
| 38 | + |
| 39 | +func resourceTencentCloudScfTriggerConfig() *schema.Resource { |
| 40 | + return &schema.Resource{ |
| 41 | + Create: resourceTencentCloudScfTriggerConfigCreate, |
| 42 | + Read: resourceTencentCloudScfTriggerConfigRead, |
| 43 | + Update: resourceTencentCloudScfTriggerConfigUpdate, |
| 44 | + Delete: resourceTencentCloudScfTriggerConfigDelete, |
| 45 | + Importer: &schema.ResourceImporter{ |
| 46 | + State: schema.ImportStatePassthrough, |
| 47 | + }, |
| 48 | + Schema: map[string]*schema.Schema{ |
| 49 | + "function_name": { |
| 50 | + Required: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Description: "Function name.", |
| 53 | + }, |
| 54 | + |
| 55 | + "trigger_name": { |
| 56 | + Required: true, |
| 57 | + Type: schema.TypeString, |
| 58 | + Description: "Trigger name.", |
| 59 | + }, |
| 60 | + |
| 61 | + "type": { |
| 62 | + Required: true, |
| 63 | + Type: schema.TypeString, |
| 64 | + Description: "Trigger Type.", |
| 65 | + }, |
| 66 | + |
| 67 | + "enable": { |
| 68 | + Required: true, |
| 69 | + Type: schema.TypeString, |
| 70 | + Description: "Initial status of the trigger. Values: `OPEN` (enabled); `CLOSE` disabled).", |
| 71 | + }, |
| 72 | + |
| 73 | + "qualifier": { |
| 74 | + Optional: true, |
| 75 | + Type: schema.TypeString, |
| 76 | + Description: "Function version. It defaults to `$LATEST`. It's recommended to use `[$DEFAULT](https://intl.cloud.tencent.com/document/product/583/36149?from_cn_redirect=1#.E9.BB.98.E8.AE.A4.E5.88.AB.E5.90.8D)` for canary release.", |
| 77 | + }, |
| 78 | + |
| 79 | + "namespace": { |
| 80 | + Optional: true, |
| 81 | + Type: schema.TypeString, |
| 82 | + Default: "default", |
| 83 | + Description: "Function namespace.", |
| 84 | + }, |
| 85 | + |
| 86 | + "trigger_desc": { |
| 87 | + Optional: true, |
| 88 | + Computed: true, |
| 89 | + Type: schema.TypeString, |
| 90 | + Description: "To update a COS trigger, this field is required. It stores the data {event:cos:ObjectCreated:*} in the JSON format. The data content of this field is in the same format as that of SetTrigger. This field is optional if a scheduled trigger or CMQ trigger is to be deleted.", |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func resourceTencentCloudScfTriggerConfigCreate(d *schema.ResourceData, meta interface{}) error { |
| 97 | + defer logElapsed("resource.tencentcloud_scf_trigger_config.create")() |
| 98 | + defer inconsistentCheck(d, meta)() |
| 99 | + |
| 100 | + var ( |
| 101 | + functionName string |
| 102 | + triggerName string |
| 103 | + namespace string |
| 104 | + ) |
| 105 | + if v, ok := d.GetOk("function_name"); ok { |
| 106 | + functionName = v.(string) |
| 107 | + } |
| 108 | + |
| 109 | + if v, ok := d.GetOk("namespace"); ok { |
| 110 | + namespace = v.(string) |
| 111 | + } |
| 112 | + |
| 113 | + if v, ok := d.GetOk("trigger_name"); ok { |
| 114 | + triggerName = v.(string) |
| 115 | + } |
| 116 | + |
| 117 | + d.SetId(functionName + FILED_SP + namespace + FILED_SP + triggerName) |
| 118 | + |
| 119 | + return resourceTencentCloudScfTriggerConfigUpdate(d, meta) |
| 120 | +} |
| 121 | + |
| 122 | +func resourceTencentCloudScfTriggerConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 123 | + defer logElapsed("resource.tencentcloud_scf_trigger_config.read")() |
| 124 | + defer inconsistentCheck(d, meta)() |
| 125 | + |
| 126 | + logId := getLogId(contextNil) |
| 127 | + |
| 128 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 129 | + |
| 130 | + service := ScfService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 131 | + |
| 132 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 133 | + if len(idSplit) != 3 { |
| 134 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 135 | + } |
| 136 | + functionName := idSplit[0] |
| 137 | + namespace := idSplit[1] |
| 138 | + triggerName := idSplit[2] |
| 139 | + |
| 140 | + triggerConfig, err := service.DescribeScfTriggerConfigById(ctx, functionName, namespace, triggerName) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + if triggerConfig == nil { |
| 146 | + d.SetId("") |
| 147 | + log.Printf("[WARN]%s resource `ScfTriggerConfig` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 148 | + return nil |
| 149 | + } |
| 150 | + |
| 151 | + if triggerConfig.Enable != nil { |
| 152 | + if *triggerConfig.Enable == 1 { |
| 153 | + _ = d.Set("enable", "OPEN") |
| 154 | + } else { |
| 155 | + _ = d.Set("enable", "CLOSE") |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + _ = d.Set("function_name", functionName) |
| 160 | + |
| 161 | + _ = d.Set("namespace", namespace) |
| 162 | + |
| 163 | + if triggerConfig.TriggerName != nil { |
| 164 | + _ = d.Set("trigger_name", triggerConfig.TriggerName) |
| 165 | + } |
| 166 | + |
| 167 | + if triggerConfig.Type != nil { |
| 168 | + _ = d.Set("type", triggerConfig.Type) |
| 169 | + } |
| 170 | + |
| 171 | + if triggerConfig.Qualifier != nil { |
| 172 | + _ = d.Set("qualifier", triggerConfig.Qualifier) |
| 173 | + } |
| 174 | + |
| 175 | + if triggerConfig.TriggerDesc != nil { |
| 176 | + _ = d.Set("trigger_desc", triggerConfig.TriggerDesc) |
| 177 | + } |
| 178 | + |
| 179 | + return nil |
| 180 | +} |
| 181 | + |
| 182 | +func resourceTencentCloudScfTriggerConfigUpdate(d *schema.ResourceData, meta interface{}) error { |
| 183 | + defer logElapsed("resource.tencentcloud_scf_trigger_config.update")() |
| 184 | + defer inconsistentCheck(d, meta)() |
| 185 | + |
| 186 | + logId := getLogId(contextNil) |
| 187 | + |
| 188 | + request := scf.NewUpdateTriggerStatusRequest() |
| 189 | + |
| 190 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 191 | + if len(idSplit) != 3 { |
| 192 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 193 | + } |
| 194 | + functionName := idSplit[0] |
| 195 | + namespace := idSplit[1] |
| 196 | + triggerName := idSplit[2] |
| 197 | + |
| 198 | + request.FunctionName = &functionName |
| 199 | + request.Namespace = &namespace |
| 200 | + request.TriggerName = &triggerName |
| 201 | + |
| 202 | + if v, ok := d.GetOk("enable"); ok { |
| 203 | + request.Enable = helper.String(v.(string)) |
| 204 | + } |
| 205 | + |
| 206 | + if v, ok := d.GetOk("type"); ok { |
| 207 | + request.Type = helper.String(v.(string)) |
| 208 | + } |
| 209 | + |
| 210 | + if v, ok := d.GetOk("qualifier"); ok { |
| 211 | + request.Qualifier = helper.String(v.(string)) |
| 212 | + } |
| 213 | + |
| 214 | + if v, ok := d.GetOk("trigger_desc"); ok { |
| 215 | + request.TriggerDesc = helper.String(v.(string)) |
| 216 | + } |
| 217 | + |
| 218 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 219 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseScfClient().UpdateTriggerStatus(request) |
| 220 | + if e != nil { |
| 221 | + return retryError(e) |
| 222 | + } else { |
| 223 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 224 | + } |
| 225 | + return nil |
| 226 | + }) |
| 227 | + if err != nil { |
| 228 | + log.Printf("[CRITAL]%s update scf triggerConfig failed, reason:%+v", logId, err) |
| 229 | + return err |
| 230 | + } |
| 231 | + |
| 232 | + return resourceTencentCloudScfTriggerConfigRead(d, meta) |
| 233 | +} |
| 234 | + |
| 235 | +func resourceTencentCloudScfTriggerConfigDelete(d *schema.ResourceData, meta interface{}) error { |
| 236 | + defer logElapsed("resource.tencentcloud_scf_trigger_config.delete")() |
| 237 | + defer inconsistentCheck(d, meta)() |
| 238 | + |
| 239 | + return nil |
| 240 | +} |
0 commit comments