Skip to content

Commit be47deb

Browse files
feat(fgs): supplement attributes for function resources
1 parent 0bab2a4 commit be47deb

File tree

5 files changed

+254
-11
lines changed

5 files changed

+254
-11
lines changed

docs/resources/fgs_function.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,34 @@ The `reserved_instances` block supports:
405405
If this parameter is enabled, reserved instances are initialized and the mode change needs some time to take effect.
406406
You will still be billed at the price of reserved instances for non-idle mode in this period.
407407

408+
* `func_vpc` - (Optional, List) Specifies the configuration of function VPC.
409+
The [func_vpc](#functiongraph_func_vpc) structure is documented below.
410+
411+
<a name="functiongraph_func_vpc"></a>
412+
The `func_vpc` block supports:
413+
414+
* `cidr` - (Optional, String) Specifies the network segment on which the subnet resides.
415+
416+
* `gateway` - (Optional, String) Specifies the gateway.
417+
418+
* `network_controller` - (Optional, List) Specifies the function network configuration.
419+
The [network_controller](#functiongraph_network_controller) structure is documented below.
420+
421+
<a name="functiongraph_network_controller"></a>
422+
The `network_controller` block supports:
423+
424+
* `disable_public_network` - (Optional, Bool) Specifies the prohibit public network access switch.
425+
426+
* `trigger_access_vpcs` - (Optional, List) Specifies the trigger function VPC configuration.
427+
The [trigger_access_vpcs](#network_controller_trigger_access_vpcs) structure is documented below.
428+
429+
<a name="network_controller_trigger_access_vpcs"></a>
430+
The `trigger_access_vpcs` block supports:
431+
432+
* `vpc_id` - (Optional, String) Specifies the VPC ID.
433+
434+
* `vpc_name` - (Optional, String) Specifies the VPC name.
435+
408436
* `tactics_config` - (Optional, List) Specifies the auto scaling policies for reserved instance.
409437
The [tactics_config](#functiongraph_tactics_config) structure is documented below.
410438

huaweicloud/services/acceptance/fgs/resource_huaweicloud_fgs_function_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,3 +1315,75 @@ resource "huaweicloud_fgs_function" "test" {
13151315
}
13161316
`, name)
13171317
}
1318+
1319+
func TestAccFgsV2Function_funcVpcAndNetworkController(t *testing.T) {
1320+
var (
1321+
f function.Function
1322+
1323+
name = acceptance.RandomAccResourceName()
1324+
resourceName = "huaweicloud_fgs_function.test"
1325+
)
1326+
1327+
rc := acceptance.InitResourceCheck(
1328+
resourceName,
1329+
&f,
1330+
getResourceObj,
1331+
)
1332+
1333+
resource.ParallelTest(t, resource.TestCase{
1334+
PreCheck: func() {
1335+
acceptance.TestAccPreCheck(t)
1336+
},
1337+
ProviderFactories: acceptance.TestAccProviderFactories,
1338+
CheckDestroy: rc.CheckResourceDestroy(),
1339+
Steps: []resource.TestStep{
1340+
{
1341+
Config: testAccFunction_funcVpcAndNetworkController(name),
1342+
Check: resource.ComposeTestCheckFunc(
1343+
rc.CheckResourceExists(),
1344+
resource.TestCheckResourceAttr(resourceName, "name", name),
1345+
),
1346+
},
1347+
{
1348+
ResourceName: resourceName,
1349+
ImportState: true,
1350+
ImportStateVerify: true,
1351+
ImportStateVerifyIgnore: []string{
1352+
"app",
1353+
"package",
1354+
"func_code",
1355+
},
1356+
},
1357+
},
1358+
})
1359+
}
1360+
1361+
func testAccFunction_funcVpcAndNetworkController(name string) string {
1362+
return fmt.Sprintf(`
1363+
%[1]s
1364+
1365+
resource "huaweicloud_fgs_function" "test" {
1366+
name = "%[2]s"
1367+
app = "default"
1368+
handler = "bootstrap"
1369+
xrole = "function_all_trust"
1370+
memory_size = 128
1371+
timeout = 3
1372+
runtime = "Custom"
1373+
code_type = "inline"
1374+
vpc_id = huaweicloud_vpc.test.id
1375+
network_id = huaweicloud_vpc_subnet.test.id
1376+
func_vpc{
1377+
cidr = "192.168.0.0/24"
1378+
gateway = "192.168.0.1"
1379+
}
1380+
network_controller{
1381+
disable_public_network = true
1382+
trigger_access_vpcs{
1383+
vpc_id = huaweicloud_vpc.test.id
1384+
vpc_name = huaweicloud_vpc.test.name
1385+
}
1386+
}
1387+
}
1388+
`, common.TestBaseNetwork(name), name)
1389+
}

huaweicloud/services/fgs/resource_huaweicloud_fgs_function.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,55 @@ func ResourceFgsFunctionV2() *schema.Resource {
409409
Optional: true,
410410
Description: "schema: Internal; Specifies the maximum duration that the function can be initialized.",
411411
},
412+
"func_vpc": {
413+
Type: schema.TypeList,
414+
Optional: true,
415+
Elem: &schema.Resource{
416+
Schema: map[string]*schema.Schema{
417+
"cidr": {
418+
Type: schema.TypeString,
419+
Optional: true,
420+
},
421+
"gateway": {
422+
Type: schema.TypeString,
423+
Optional: true,
424+
},
425+
"security_groups": {
426+
Type: schema.TypeList,
427+
Optional: true,
428+
Elem: &schema.Schema{Type: schema.TypeString},
429+
},
430+
},
431+
},
432+
},
433+
"network_controller": {
434+
Type: schema.TypeList,
435+
Optional: true,
436+
Elem: &schema.Resource{
437+
Schema: map[string]*schema.Schema{
438+
"disable_public_network": {
439+
Type: schema.TypeBool,
440+
Optional: true,
441+
},
442+
"trigger_access_vpcs": {
443+
Type: schema.TypeList,
444+
Optional: true,
445+
Elem: &schema.Resource{
446+
Schema: map[string]*schema.Schema{
447+
"vpc_id": {
448+
Type: schema.TypeString,
449+
Optional: true,
450+
},
451+
"vpc_name": {
452+
Type: schema.TypeString,
453+
Optional: true,
454+
},
455+
},
456+
},
457+
},
458+
},
459+
},
460+
},
412461
"version": {
413462
Type: schema.TypeString,
414463
Computed: true,
@@ -497,6 +546,46 @@ func buildCustomImage(imageConfig []interface{}) *function.CustomImage {
497546
}
498547
}
499548

549+
func buildFuncVpc(funcVpcConfig []interface{}) *function.FuncVpc {
550+
if len(funcVpcConfig) < 1 {
551+
return nil
552+
}
553+
554+
cfg := funcVpcConfig[0].(map[string]interface{})
555+
return &function.FuncVpc{
556+
Cidr: cfg["cidr"].(string),
557+
Gateway: cfg["gateway"].(string),
558+
SecurityGroups: utils.ExpandToStringList(cfg["security_groups"].([]interface{})),
559+
}
560+
}
561+
562+
func buildNetworkController(networkControllerConfig []interface{}) *function.NetworkControlConfig {
563+
if len(networkControllerConfig) < 1 {
564+
return nil
565+
}
566+
567+
cfg := networkControllerConfig[0].(map[string]interface{})
568+
return &function.NetworkControlConfig{
569+
DisablePublicNetwork: cfg["disable_public_network"].(bool),
570+
TriggerAccessVpcs: buildTriggerAccessVpcs(cfg["trigger_access_vpcs"].([]interface{})),
571+
}
572+
}
573+
574+
func buildTriggerAccessVpcs(vpcConfig []interface{}) []function.VpcConfig {
575+
if len(vpcConfig) < 1 {
576+
return nil
577+
}
578+
result := make([]function.VpcConfig, len(vpcConfig))
579+
for i, val := range vpcConfig {
580+
cfg := val.(map[string]interface{})
581+
result[i] = function.VpcConfig{
582+
VpcId: cfg["vpc_id"].(string),
583+
VpcName: cfg["vpc_name"].(string),
584+
}
585+
}
586+
return result
587+
}
588+
500589
func buildFgsFunctionParameters(d *schema.ResourceData, cfg *config.Config) (function.CreateOpts, error) {
501590
// check app and package
502591
app, appOk := d.GetOk("app")
@@ -539,6 +628,8 @@ func buildFgsFunctionParameters(d *schema.ResourceData, cfg *config.Config) (fun
539628
GPUType: d.Get("gpu_type").(string),
540629
PreStopHandler: d.Get("pre_stop_handler").(string),
541630
PreStopTimeout: d.Get("pre_stop_timeout").(int),
631+
FuncVpc: buildFuncVpc(d.Get("func_vpc").([]interface{})),
632+
NetworkController: buildNetworkController(d.Get("network_controller").([]interface{})),
542633
}
543634
if v, ok := d.GetOk("func_code"); ok {
544635
funcCode := function.FunctionCodeOpts{
@@ -674,9 +765,45 @@ func setFgsFunctionAgency(d *schema.ResourceData, agency string) error {
674765
}
675766

676767
func setFgsFunctionVpcAccess(d *schema.ResourceData, funcVpc function.FuncVpc) error {
768+
result := []map[string]interface{}{
769+
{
770+
"cidr": funcVpc.Cidr,
771+
"gateway": funcVpc.Gateway,
772+
},
773+
}
774+
677775
mErr := multierror.Append(
678776
d.Set("vpc_id", funcVpc.VpcId),
679777
d.Set("network_id", funcVpc.SubnetId),
778+
d.Set("func_vpc", result),
779+
)
780+
if err := mErr.ErrorOrNil(); err != nil {
781+
return fmt.Errorf("error setting vault fields: %s", err)
782+
}
783+
return nil
784+
}
785+
786+
func setTriggerAccessVpcs(vpcConfigs []function.VpcConfig) interface{} {
787+
result := make([]map[string]interface{}, len(vpcConfigs))
788+
for i, val := range vpcConfigs {
789+
result[i] = map[string]interface{}{
790+
"vpc_id": val.VpcId,
791+
"vpc_name": val.VpcName,
792+
}
793+
}
794+
return result
795+
}
796+
797+
func setNetworkController(d *schema.ResourceData, networkController function.NetworkControlConfig) error {
798+
result := []map[string]interface{}{
799+
{
800+
"disable_public_network": networkController.DisablePublicNetwork,
801+
"trigger_access_vpcs": setTriggerAccessVpcs(networkController.TriggerAccessVpcs),
802+
},
803+
}
804+
805+
mErr := multierror.Append(
806+
d.Set("network_controller", result),
680807
)
681808
if err := mErr.ErrorOrNil(); err != nil {
682809
return fmt.Errorf("error setting vault fields: %s", err)
@@ -901,6 +1028,7 @@ func resourceFgsFunctionRead(_ context.Context, d *schema.ResourceData, meta int
9011028
d.Set("gpu_type", f.GPUType),
9021029
d.Set("pre_stop_handler", f.PreStopHandler),
9031030
d.Set("pre_stop_timeout", f.PreStopTimeout),
1031+
setNetworkController(d, f.NetworkController),
9041032
)
9051033

9061034
reservedInstances, err := getReservedInstanceConfig(fgsClient, d)
@@ -1267,6 +1395,8 @@ func resourceFgsFunctionMetadataUpdate(fgsClient *golangsdk.ServiceClient, urn s
12671395
GPUType: d.Get("gpu_type").(string),
12681396
PreStopHandler: d.Get("pre_stop_handler").(string),
12691397
PreStopTimeout: d.Get("pre_stop_timeout").(int),
1398+
FuncVpc: buildFuncVpc(d.Get("func_vpc").([]interface{})),
1399+
NetworkController: buildNetworkController(d.Get("network_controller").([]interface{})),
12701400
}
12711401

12721402
if _, ok := d.GetOk("vpc_id"); ok {

vendor/github.com/chnsz/golangsdk/openstack/fgs/v2/function/requests.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/chnsz/golangsdk/openstack/fgs/v2/function/results.go

Lines changed: 20 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)