Skip to content

Commit 549ad71

Browse files
committed
scf - add public net config and eip config
1 parent c193ebf commit 549ad71

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

tencentcloud/resource_tc_scf_function.go

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"encoding/base64"
3333
"encoding/json"
3434
"fmt"
35+
scf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf/v20180416"
3536
"io/ioutil"
3637
"log"
3738
"os"
@@ -190,7 +191,19 @@ func resourceTencentCloudScfFunction() *schema.Resource {
190191
Optional: true,
191192
Description: "Tags of the SCF function.",
192193
},
193-
194+
"enable_public_net": {
195+
Type: schema.TypeBool,
196+
Optional: true,
197+
Default: false,
198+
Description: "Indicates whether public net config enabled.",
199+
},
200+
"enable_eip_config": {
201+
Type: schema.TypeBool,
202+
Optional: true,
203+
Default: false,
204+
AtLeastOneOf: []string{"enable_public_net"},
205+
Description: "Indicates whether EIP config set to `ENABLE` when `enable_public_net` was true.",
206+
},
194207
// cos code
195208
"cos_bucket_name": {
196209
Type: schema.TypeString,
@@ -433,6 +446,37 @@ func resourceTencentCloudScfFunctionCreate(d *schema.ResourceData, m interface{}
433446
functionInfo.cosBucketRegion = helper.String(raw.(string))
434447
}
435448

449+
enablePublicNet, enablePublicNetOk := d.GetOk("enable_public_net")
450+
enableEipConfig, enableEipConfigOk := d.GetOk("enable_eip_config")
451+
452+
if enablePublicNetOk {
453+
enable := enablePublicNet.(bool)
454+
publicNetStatus := helper.String("ENABLE")
455+
if !enable {
456+
publicNetStatus = helper.String("DISABLE")
457+
}
458+
functionInfo.publicNetConfig = &scf.PublicNetConfigIn{
459+
PublicNetStatus: publicNetStatus,
460+
EipConfig: &scf.EipConfigIn{
461+
EipStatus: helper.String("DISABLE"),
462+
},
463+
}
464+
}
465+
466+
if enableEipConfigOk {
467+
enableEip := enableEipConfig.(bool)
468+
eipStatus := "DISABLE"
469+
if enableEip {
470+
if !enablePublicNet.(bool) {
471+
return fmt.Errorf("cannot set enable_eip_config to true if enable_public_net was disable")
472+
}
473+
eipStatus = "ENABLE"
474+
}
475+
functionInfo.publicNetConfig.EipConfig = &scf.EipConfigIn{
476+
EipStatus: helper.String(eipStatus),
477+
}
478+
}
479+
436480
if raw, ok := d.GetOk("zip_file"); ok {
437481
path, err := homedir.Expand(raw.(string))
438482
if err != nil {
@@ -606,6 +650,10 @@ func resourceTencentCloudScfFunctionRead(d *schema.ResourceData, m interface{})
606650
_ = d.Set("eips", resp.EipConfig.Eips)
607651
_ = d.Set("host", resp.AccessInfo.Host)
608652
_ = d.Set("vip", resp.AccessInfo.Vip)
653+
if resp.PublicNetConfig != nil {
654+
_ = d.Set("enable_public_net", *resp.PublicNetConfig.PublicNetStatus == "ENABLE")
655+
_ = d.Set("enable_eip_config", *resp.PublicNetConfig.EipConfig.EipStatus == "ENABLE")
656+
}
609657

610658
triggers := make([]map[string]interface{}, 0, len(resp.Triggers))
611659
for _, trigger := range resp.Triggers {
@@ -806,6 +854,42 @@ func resourceTencentCloudScfFunctionUpdate(d *schema.ResourceData, m interface{}
806854
functionInfo.l5Enable = helper.Bool(d.Get("l5_enable").(bool))
807855
}
808856

857+
if d.HasChange("enable_public_net") {
858+
updateAttrs = append(updateAttrs, "enable_public_net")
859+
}
860+
861+
if d.HasChange("enable_eip_config") {
862+
updateAttrs = append(updateAttrs, "enable_eip_config")
863+
}
864+
865+
if raw, ok := d.GetOk("enable_public_net"); ok {
866+
enablePublicNet := raw.(bool)
867+
publicNetStatus := helper.String("ENABLE")
868+
if !enablePublicNet {
869+
publicNetStatus = helper.String("DISABLE")
870+
}
871+
functionInfo.publicNetConfig = &scf.PublicNetConfigIn{
872+
PublicNetStatus: publicNetStatus,
873+
EipConfig: &scf.EipConfigIn{
874+
EipStatus: helper.String("DISABLE"),
875+
},
876+
}
877+
}
878+
879+
if raw, ok := d.GetOk("enable_eip_config"); ok {
880+
status := "DISABLE"
881+
enablePublicNet := d.Get("enable_public_net").(bool)
882+
if raw.(bool) {
883+
if !enablePublicNet {
884+
return fmt.Errorf("cannot set enable_eip_config to true if enable_public_net was disable")
885+
}
886+
status = "ENABLE"
887+
}
888+
functionInfo.publicNetConfig.EipConfig = &scf.EipConfigIn{
889+
EipStatus: helper.String(status),
890+
}
891+
}
892+
809893
// update function configuration
810894
if len(updateAttrs) > 0 {
811895
if err := scfService.ModifyFunctionConfig(ctx, functionInfo); err != nil {

tencentcloud/service_tencentcloud_scf.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type scfFunctionInfo struct {
2828
clsTopicId *string
2929
namespace *string
3030
l5Enable *bool
31+
publicNetConfig *scf.PublicNetConfigIn
3132

3233
cosBucketName *string
3334
cosObjectName *string
@@ -54,6 +55,7 @@ func (me *ScfService) CreateFunction(ctx context.Context, info scfFunctionInfo)
5455
request.Handler = info.handler
5556
request.Description = info.desc
5657
request.MemorySize = helper.IntInt64(*info.memSize)
58+
request.PublicNetConfig = info.publicNetConfig
5759
request.Timeout = helper.IntInt64(*info.timeout)
5860
for k, v := range info.environment {
5961
if request.Environment == nil {
@@ -249,6 +251,9 @@ func (me *ScfService) ModifyFunctionConfig(ctx context.Context, info scfFunction
249251
}
250252
request.VpcConfig.SubnetId = info.subnetId
251253
}
254+
if info.publicNetConfig != nil {
255+
request.PublicNetConfig = info.publicNetConfig
256+
}
252257
request.Role = info.role
253258
request.ClsLogsetId = info.clsLogsetId
254259
request.ClsTopicId = info.clsTopicId

0 commit comments

Comments
 (0)