Skip to content

Commit 1d08a84

Browse files
authored
add scf res (#1829)
* add scf res * add changelog
1 parent f1314f9 commit 1d08a84

10 files changed

+712
-0
lines changed

.changelog/1829.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-resource
2+
tencentcloud_scf_function_version
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_scf_function_event_invoke_config
7+
```

tencentcloud/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,8 @@ Serverless Cloud Function(SCF)
635635
636636
Resource
637637
tencentcloud_scf_function
638+
tencentcloud_scf_function_version
639+
tencentcloud_scf_function_event_invoke_config
638640
tencentcloud_scf_namespace
639641
tencentcloud_scf_layer
640642
tencentcloud_scf_function_alias
@@ -1971,6 +1973,8 @@ func Provider() *schema.Provider {
19711973
"tencentcloud_cam_service_linked_role": resourceTencentCloudCamServiceLinkedRole(),
19721974
"tencentcloud_cam_user_saml_config": resourceTencentCloudCamUserSamlConfig(),
19731975
"tencentcloud_scf_function": resourceTencentCloudScfFunction(),
1976+
"tencentcloud_scf_function_version": resourceTencentCloudScfFunctionVersion(),
1977+
"tencentcloud_scf_function_event_invoke_config": resourceTencentCloudScfFunctionEventInvokeConfig(),
19741978
"tencentcloud_scf_namespace": resourceTencentCloudScfNamespace(),
19751979
"tencentcloud_scf_layer": resourceTencentCloudScfLayer(),
19761980
"tencentcloud_scf_function_alias": resourceTencentCloudScfFunctionAlias(),
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
Provides a resource to create a scf function_event_invoke_config
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_scf_function_event_invoke_config" "function_event_invoke_config" {
8+
function_name = "keep-1676351130"
9+
namespace = "default"
10+
async_trigger_config {
11+
retry_config {
12+
retry_num = 2
13+
}
14+
msg_ttl = 24
15+
}
16+
}
17+
```
18+
19+
Import
20+
21+
scf function_event_invoke_config can be imported using the id, e.g.
22+
23+
```
24+
terraform import tencentcloud_scf_function_event_invoke_config.function_event_invoke_config function_name#namespace
25+
```
26+
*/
27+
package tencentcloud
28+
29+
import (
30+
"context"
31+
"fmt"
32+
"log"
33+
"strings"
34+
35+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
36+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
37+
scf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf/v20180416"
38+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
39+
)
40+
41+
func resourceTencentCloudScfFunctionEventInvokeConfig() *schema.Resource {
42+
return &schema.Resource{
43+
Create: resourceTencentCloudScfFunctionEventInvokeConfigCreate,
44+
Read: resourceTencentCloudScfFunctionEventInvokeConfigRead,
45+
Update: resourceTencentCloudScfFunctionEventInvokeConfigUpdate,
46+
Delete: resourceTencentCloudScfFunctionEventInvokeConfigDelete,
47+
Importer: &schema.ResourceImporter{
48+
State: schema.ImportStatePassthrough,
49+
},
50+
Schema: map[string]*schema.Schema{
51+
"function_name": {
52+
Required: true,
53+
Type: schema.TypeString,
54+
Description: "Function name.",
55+
},
56+
57+
"namespace": {
58+
Optional: true,
59+
Type: schema.TypeString,
60+
Default: "default",
61+
Description: "Function namespace. Default value: default.",
62+
},
63+
"async_trigger_config": {
64+
Required: true,
65+
Type: schema.TypeList,
66+
MaxItems: 1,
67+
Description: "Async retry configuration information.",
68+
Elem: &schema.Resource{
69+
Schema: map[string]*schema.Schema{
70+
"retry_config": {
71+
Type: schema.TypeList,
72+
Required: true,
73+
Description: "Async retry configuration of function upon user error.",
74+
Elem: &schema.Resource{
75+
Schema: map[string]*schema.Schema{
76+
"retry_num": {
77+
Type: schema.TypeInt,
78+
Required: true,
79+
Description: "Number of retry attempts.",
80+
},
81+
},
82+
},
83+
},
84+
"msg_ttl": {
85+
Type: schema.TypeInt,
86+
Required: true,
87+
Description: "Message retention period.",
88+
},
89+
},
90+
},
91+
},
92+
},
93+
}
94+
}
95+
96+
func resourceTencentCloudScfFunctionEventInvokeConfigCreate(d *schema.ResourceData, meta interface{}) error {
97+
defer logElapsed("resource.tencentcloud_scf_function_event_invoke_config.create")()
98+
defer inconsistentCheck(d, meta)()
99+
100+
functionName := d.Get("function_name").(string)
101+
namespace := d.Get("namespace").(string)
102+
103+
d.SetId(functionName + FILED_SP + namespace)
104+
105+
return resourceTencentCloudScfFunctionEventInvokeConfigUpdate(d, meta)
106+
}
107+
108+
func resourceTencentCloudScfFunctionEventInvokeConfigRead(d *schema.ResourceData, meta interface{}) error {
109+
defer logElapsed("resource.tencentcloud_scf_function_event_invoke_config.read")()
110+
defer inconsistentCheck(d, meta)()
111+
112+
logId := getLogId(contextNil)
113+
114+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
115+
116+
service := ScfService{client: meta.(*TencentCloudClient).apiV3Conn}
117+
118+
idSplit := strings.Split(d.Id(), FILED_SP)
119+
if len(idSplit) != 2 {
120+
return fmt.Errorf("id is broken,%s", d.Id())
121+
}
122+
functionName := idSplit[0]
123+
namespace := idSplit[1]
124+
125+
FunctionEventInvokeConfig, err := service.DescribeScfFunctionEventInvokeConfigById(ctx, namespace, functionName)
126+
if err != nil {
127+
return err
128+
}
129+
130+
if FunctionEventInvokeConfig == nil {
131+
d.SetId("")
132+
log.Printf("[WARN]%s resource `ScfFunctionEventInvokeConfig` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
133+
return nil
134+
}
135+
136+
if FunctionEventInvokeConfig != nil {
137+
asyncTriggerConfigMap := map[string]interface{}{}
138+
139+
if FunctionEventInvokeConfig.RetryConfig != nil {
140+
retryConfigList := []interface{}{}
141+
for _, retryConfig := range FunctionEventInvokeConfig.RetryConfig {
142+
retryConfigMap := map[string]interface{}{}
143+
144+
if retryConfig.RetryNum != nil {
145+
retryConfigMap["retry_num"] = retryConfig.RetryNum
146+
}
147+
148+
retryConfigList = append(retryConfigList, retryConfigMap)
149+
}
150+
151+
asyncTriggerConfigMap["retry_config"] = retryConfigList
152+
}
153+
154+
if FunctionEventInvokeConfig.MsgTTL != nil {
155+
asyncTriggerConfigMap["msg_ttl"] = FunctionEventInvokeConfig.MsgTTL
156+
}
157+
158+
_ = d.Set("async_trigger_config", []interface{}{asyncTriggerConfigMap})
159+
}
160+
161+
_ = d.Set("function_name", functionName)
162+
163+
_ = d.Set("namespace", namespace)
164+
165+
return nil
166+
}
167+
168+
func resourceTencentCloudScfFunctionEventInvokeConfigUpdate(d *schema.ResourceData, meta interface{}) error {
169+
defer logElapsed("resource.tencentcloud_scf_function_event_invoke_config.update")()
170+
defer inconsistentCheck(d, meta)()
171+
172+
logId := getLogId(contextNil)
173+
174+
request := scf.NewUpdateFunctionEventInvokeConfigRequest()
175+
176+
idSplit := strings.Split(d.Id(), FILED_SP)
177+
if len(idSplit) != 2 {
178+
return fmt.Errorf("id is broken,%s", d.Id())
179+
}
180+
functionName := idSplit[0]
181+
namespace := idSplit[1]
182+
183+
request.Namespace = &namespace
184+
request.FunctionName = &functionName
185+
186+
if dMap, ok := helper.InterfacesHeadMap(d, "async_trigger_config"); ok {
187+
asyncTriggerConfig := scf.AsyncTriggerConfig{}
188+
if v, ok := dMap["retry_config"]; ok {
189+
for _, item := range v.([]interface{}) {
190+
retryConfigMap := item.(map[string]interface{})
191+
retryConfig := scf.RetryConfig{}
192+
if v, ok := retryConfigMap["retry_num"]; ok {
193+
retryConfig.RetryNum = helper.IntInt64(v.(int))
194+
}
195+
asyncTriggerConfig.RetryConfig = append(asyncTriggerConfig.RetryConfig, &retryConfig)
196+
}
197+
}
198+
if v, ok := dMap["msg_ttl"]; ok {
199+
asyncTriggerConfig.MsgTTL = helper.IntInt64(v.(int))
200+
}
201+
request.AsyncTriggerConfig = &asyncTriggerConfig
202+
}
203+
204+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
205+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseScfClient().UpdateFunctionEventInvokeConfig(request)
206+
if e != nil {
207+
return retryError(e)
208+
} else {
209+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
210+
}
211+
return nil
212+
})
213+
if err != nil {
214+
log.Printf("[CRITAL]%s update scf FunctionEventInvokeConfig failed, reason:%+v", logId, err)
215+
return err
216+
}
217+
218+
return resourceTencentCloudScfFunctionEventInvokeConfigRead(d, meta)
219+
}
220+
221+
func resourceTencentCloudScfFunctionEventInvokeConfigDelete(d *schema.ResourceData, meta interface{}) error {
222+
defer logElapsed("resource.tencentcloud_scf_function_event_invoke_config.delete")()
223+
defer inconsistentCheck(d, meta)()
224+
225+
return nil
226+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudNeedFixScfFunctionEventInvokeConfigResource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccScfFunctionEventInvokeConfig,
19+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_scf_function_event_invoke_config.function_event_invoke_config", "id")),
20+
},
21+
{
22+
ResourceName: "tencentcloud_scf_function_event_invoke_config.function_event_invoke_config",
23+
ImportState: true,
24+
ImportStateVerify: true,
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccScfFunctionEventInvokeConfig = `
31+
32+
resource "tencentcloud_scf_function_event_invoke_config" "function_event_invoke_config" {
33+
function_name = "keep-1676351130"
34+
namespace = "default"
35+
async_trigger_config {
36+
retry_config {
37+
retry_num = 2
38+
}
39+
msg_ttl = 24
40+
}
41+
}
42+
43+
`

0 commit comments

Comments
 (0)