-
Notifications
You must be signed in to change notification settings - Fork 134
/
resource_tc_scf_invoke_function.go
145 lines (117 loc) · 4.54 KB
/
resource_tc_scf_invoke_function.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package scf
import (
"log"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
scf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf/v20180416"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func ResourceTencentCloudScfInvokeFunction() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudScfInvokeFunctionCreate,
Read: resourceTencentCloudScfInvokeFunctionRead,
Delete: resourceTencentCloudScfInvokeFunctionDelete,
Schema: map[string]*schema.Schema{
"function_name": {
Required: true,
ForceNew: true,
Type: schema.TypeString,
Description: "Function name.",
},
"invocation_type": {
Optional: true,
ForceNew: true,
Type: schema.TypeString,
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.",
},
"qualifier": {
Optional: true,
ForceNew: true,
Type: schema.TypeString,
Description: "The version or alias of the triggered function. It defaults to $LATEST.",
},
"client_context": {
Optional: true,
ForceNew: true,
Type: schema.TypeString,
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.",
},
"log_type": {
Optional: true,
ForceNew: true,
Type: schema.TypeString,
Description: "Null for async invocations.",
},
"namespace": {
Optional: true,
ForceNew: true,
Type: schema.TypeString,
Description: "Namespace.",
},
"routing_key": {
Optional: true,
ForceNew: true,
Type: schema.TypeString,
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.",
},
},
}
}
func resourceTencentCloudScfInvokeFunctionCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_scf_invoke_function.create")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
var (
request = scf.NewInvokeRequest()
response = scf.NewInvokeResponse()
)
if v, ok := d.GetOk("function_name"); ok {
request.FunctionName = helper.String(v.(string))
}
if v, ok := d.GetOk("invocation_type"); ok {
request.InvocationType = helper.String(v.(string))
}
if v, ok := d.GetOk("qualifier"); ok {
request.Qualifier = helper.String(v.(string))
}
if v, ok := d.GetOk("client_context"); ok {
request.ClientContext = helper.String(v.(string))
}
if v, ok := d.GetOk("log_type"); ok {
request.LogType = helper.String(v.(string))
}
if v, ok := d.GetOk("namespace"); ok {
request.Namespace = helper.String(v.(string))
}
if v, ok := d.GetOk("routing_key"); ok {
request.RoutingKey = helper.String(v.(string))
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseScfClient().Invoke(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
response = result
return nil
})
if err != nil {
log.Printf("[CRITAL]%s operate scf InvokeFunction failed, reason:%+v", logId, err)
return err
}
functionRequestId := *response.Response.Result.FunctionRequestId
d.SetId(functionRequestId)
return resourceTencentCloudScfInvokeFunctionRead(d, meta)
}
func resourceTencentCloudScfInvokeFunctionRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_scf_invoke_function.read")()
defer tccommon.InconsistentCheck(d, meta)()
return nil
}
func resourceTencentCloudScfInvokeFunctionDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_scf_invoke_function.delete")()
defer tccommon.InconsistentCheck(d, meta)()
return nil
}