|
| 1 | +/* |
| 2 | +Provides a resource to create a scf invoke_function |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_scf_invoke_function" "invoke_function" { |
| 8 | + function_name = "keep-1676351130" |
| 9 | + qualifier = "2" |
| 10 | + namespace = "default" |
| 11 | +} |
| 12 | +``` |
| 13 | +
|
| 14 | +*/ |
| 15 | +package tencentcloud |
| 16 | + |
| 17 | +import ( |
| 18 | + "log" |
| 19 | + |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 22 | + scf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf/v20180416" |
| 23 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 24 | +) |
| 25 | + |
| 26 | +func resourceTencentCloudScfInvokeFunction() *schema.Resource { |
| 27 | + return &schema.Resource{ |
| 28 | + Create: resourceTencentCloudScfInvokeFunctionCreate, |
| 29 | + Read: resourceTencentCloudScfInvokeFunctionRead, |
| 30 | + Delete: resourceTencentCloudScfInvokeFunctionDelete, |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "function_name": { |
| 33 | + Required: true, |
| 34 | + ForceNew: true, |
| 35 | + Type: schema.TypeString, |
| 36 | + Description: "Function name.", |
| 37 | + }, |
| 38 | + |
| 39 | + "invocation_type": { |
| 40 | + Optional: true, |
| 41 | + ForceNew: true, |
| 42 | + Type: schema.TypeString, |
| 43 | + Description: "Fill in RequestResponse for synchronized invocations (default and recommended) and Event for asychronized invocations. Note that for synchronized invocations, the max timeout period is 300s. Choose asychronized invocations if the required timeout period is longer than 300 seconds. You can also use InvokeFunction for synchronized invocations.", |
| 44 | + }, |
| 45 | + |
| 46 | + "qualifier": { |
| 47 | + Optional: true, |
| 48 | + ForceNew: true, |
| 49 | + Type: schema.TypeString, |
| 50 | + Description: "The version or alias of the triggered function. It defaults to $LATEST.", |
| 51 | + }, |
| 52 | + |
| 53 | + "client_context": { |
| 54 | + Optional: true, |
| 55 | + ForceNew: true, |
| 56 | + Type: schema.TypeString, |
| 57 | + Description: "Function running parameter, which is in the JSON format. The maximum parameter size is 6 MB for synchronized invocations and 128KB for asynchronized invocations. This field corresponds to event input parameter.", |
| 58 | + }, |
| 59 | + |
| 60 | + "log_type": { |
| 61 | + Optional: true, |
| 62 | + ForceNew: true, |
| 63 | + Type: schema.TypeString, |
| 64 | + Description: "Null for async invocations.", |
| 65 | + }, |
| 66 | + |
| 67 | + "namespace": { |
| 68 | + Optional: true, |
| 69 | + ForceNew: true, |
| 70 | + Type: schema.TypeString, |
| 71 | + Description: "Namespace.", |
| 72 | + }, |
| 73 | + |
| 74 | + "routing_key": { |
| 75 | + Optional: true, |
| 76 | + ForceNew: true, |
| 77 | + Type: schema.TypeString, |
| 78 | + Description: "Traffic routing config in json format, e.g., {k:v}. Please note that both k and v must be strings. Up to 1024 bytes allowed.", |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func resourceTencentCloudScfInvokeFunctionCreate(d *schema.ResourceData, meta interface{}) error { |
| 85 | + defer logElapsed("resource.tencentcloud_scf_invoke_function.create")() |
| 86 | + defer inconsistentCheck(d, meta)() |
| 87 | + |
| 88 | + logId := getLogId(contextNil) |
| 89 | + |
| 90 | + var ( |
| 91 | + request = scf.NewInvokeRequest() |
| 92 | + response = scf.NewInvokeResponse() |
| 93 | + ) |
| 94 | + if v, ok := d.GetOk("function_name"); ok { |
| 95 | + request.FunctionName = helper.String(v.(string)) |
| 96 | + } |
| 97 | + |
| 98 | + if v, ok := d.GetOk("invocation_type"); ok { |
| 99 | + request.InvocationType = helper.String(v.(string)) |
| 100 | + } |
| 101 | + |
| 102 | + if v, ok := d.GetOk("qualifier"); ok { |
| 103 | + request.Qualifier = helper.String(v.(string)) |
| 104 | + } |
| 105 | + |
| 106 | + if v, ok := d.GetOk("client_context"); ok { |
| 107 | + request.ClientContext = helper.String(v.(string)) |
| 108 | + } |
| 109 | + |
| 110 | + if v, ok := d.GetOk("log_type"); ok { |
| 111 | + request.LogType = helper.String(v.(string)) |
| 112 | + } |
| 113 | + |
| 114 | + if v, ok := d.GetOk("namespace"); ok { |
| 115 | + request.Namespace = helper.String(v.(string)) |
| 116 | + } |
| 117 | + |
| 118 | + if v, ok := d.GetOk("routing_key"); ok { |
| 119 | + request.RoutingKey = helper.String(v.(string)) |
| 120 | + } |
| 121 | + |
| 122 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 123 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseScfClient().Invoke(request) |
| 124 | + if e != nil { |
| 125 | + return retryError(e) |
| 126 | + } else { |
| 127 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 128 | + } |
| 129 | + response = result |
| 130 | + return nil |
| 131 | + }) |
| 132 | + if err != nil { |
| 133 | + log.Printf("[CRITAL]%s operate scf InvokeFunction failed, reason:%+v", logId, err) |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + functionRequestId := *response.Response.Result.FunctionRequestId |
| 138 | + |
| 139 | + d.SetId(functionRequestId) |
| 140 | + |
| 141 | + return resourceTencentCloudScfInvokeFunctionRead(d, meta) |
| 142 | +} |
| 143 | + |
| 144 | +func resourceTencentCloudScfInvokeFunctionRead(d *schema.ResourceData, meta interface{}) error { |
| 145 | + defer logElapsed("resource.tencentcloud_scf_invoke_function.read")() |
| 146 | + defer inconsistentCheck(d, meta)() |
| 147 | + |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +func resourceTencentCloudScfInvokeFunctionDelete(d *schema.ResourceData, meta interface{}) error { |
| 152 | + defer logElapsed("resource.tencentcloud_scf_invoke_function.delete")() |
| 153 | + defer inconsistentCheck(d, meta)() |
| 154 | + |
| 155 | + return nil |
| 156 | +} |
0 commit comments