Skip to content

Commit

Permalink
feat(fgs): supplement attributes for function resources
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyuancheng01 committed May 28, 2024
1 parent 0bab2a4 commit be47deb
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 11 deletions.
28 changes: 28 additions & 0 deletions docs/resources/fgs_function.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,34 @@ The `reserved_instances` block supports:
If this parameter is enabled, reserved instances are initialized and the mode change needs some time to take effect.
You will still be billed at the price of reserved instances for non-idle mode in this period.

* `func_vpc` - (Optional, List) Specifies the configuration of function VPC.
The [func_vpc](#functiongraph_func_vpc) structure is documented below.

<a name="functiongraph_func_vpc"></a>
The `func_vpc` block supports:

* `cidr` - (Optional, String) Specifies the network segment on which the subnet resides.

* `gateway` - (Optional, String) Specifies the gateway.

* `network_controller` - (Optional, List) Specifies the function network configuration.
The [network_controller](#functiongraph_network_controller) structure is documented below.

<a name="functiongraph_network_controller"></a>
The `network_controller` block supports:

* `disable_public_network` - (Optional, Bool) Specifies the prohibit public network access switch.

* `trigger_access_vpcs` - (Optional, List) Specifies the trigger function VPC configuration.
The [trigger_access_vpcs](#network_controller_trigger_access_vpcs) structure is documented below.

<a name="network_controller_trigger_access_vpcs"></a>
The `trigger_access_vpcs` block supports:

* `vpc_id` - (Optional, String) Specifies the VPC ID.

* `vpc_name` - (Optional, String) Specifies the VPC name.

* `tactics_config` - (Optional, List) Specifies the auto scaling policies for reserved instance.
The [tactics_config](#functiongraph_tactics_config) structure is documented below.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1315,3 +1315,75 @@ resource "huaweicloud_fgs_function" "test" {
}
`, name)
}

func TestAccFgsV2Function_funcVpcAndNetworkController(t *testing.T) {
var (
f function.Function

name = acceptance.RandomAccResourceName()
resourceName = "huaweicloud_fgs_function.test"
)

rc := acceptance.InitResourceCheck(
resourceName,
&f,
getResourceObj,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccFunction_funcVpcAndNetworkController(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(resourceName, "name", name),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"app",
"package",
"func_code",
},
},
},
})
}

func testAccFunction_funcVpcAndNetworkController(name string) string {
return fmt.Sprintf(`
%[1]s
resource "huaweicloud_fgs_function" "test" {
name = "%[2]s"
app = "default"
handler = "bootstrap"
xrole = "function_all_trust"
memory_size = 128
timeout = 3
runtime = "Custom"
code_type = "inline"
vpc_id = huaweicloud_vpc.test.id
network_id = huaweicloud_vpc_subnet.test.id
func_vpc{
cidr = "192.168.0.0/24"
gateway = "192.168.0.1"
}
network_controller{
disable_public_network = true
trigger_access_vpcs{
vpc_id = huaweicloud_vpc.test.id
vpc_name = huaweicloud_vpc.test.name
}
}
}
`, common.TestBaseNetwork(name), name)
}
130 changes: 130 additions & 0 deletions huaweicloud/services/fgs/resource_huaweicloud_fgs_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,55 @@ func ResourceFgsFunctionV2() *schema.Resource {
Optional: true,
Description: "schema: Internal; Specifies the maximum duration that the function can be initialized.",
},
"func_vpc": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cidr": {
Type: schema.TypeString,
Optional: true,
},
"gateway": {
Type: schema.TypeString,
Optional: true,
},
"security_groups": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"network_controller": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"disable_public_network": {
Type: schema.TypeBool,
Optional: true,
},
"trigger_access_vpcs": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"vpc_id": {
Type: schema.TypeString,
Optional: true,
},
"vpc_name": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
},
"version": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -497,6 +546,46 @@ func buildCustomImage(imageConfig []interface{}) *function.CustomImage {
}
}

func buildFuncVpc(funcVpcConfig []interface{}) *function.FuncVpc {
if len(funcVpcConfig) < 1 {
return nil
}

cfg := funcVpcConfig[0].(map[string]interface{})
return &function.FuncVpc{
Cidr: cfg["cidr"].(string),
Gateway: cfg["gateway"].(string),
SecurityGroups: utils.ExpandToStringList(cfg["security_groups"].([]interface{})),

Check failure on line 558 in huaweicloud/services/fgs/resource_huaweicloud_fgs_function.go

View workflow job for this annotation

GitHub Actions / build

unknown field SecurityGroups in struct literal of type "github.com/chnsz/golangsdk/openstack/fgs/v2/function".FuncVpc
}
}

func buildNetworkController(networkControllerConfig []interface{}) *function.NetworkControlConfig {
if len(networkControllerConfig) < 1 {
return nil
}

cfg := networkControllerConfig[0].(map[string]interface{})
return &function.NetworkControlConfig{
DisablePublicNetwork: cfg["disable_public_network"].(bool),
TriggerAccessVpcs: buildTriggerAccessVpcs(cfg["trigger_access_vpcs"].([]interface{})),
}
}

func buildTriggerAccessVpcs(vpcConfig []interface{}) []function.VpcConfig {
if len(vpcConfig) < 1 {
return nil
}
result := make([]function.VpcConfig, len(vpcConfig))
for i, val := range vpcConfig {
cfg := val.(map[string]interface{})
result[i] = function.VpcConfig{
VpcId: cfg["vpc_id"].(string),
VpcName: cfg["vpc_name"].(string),
}
}
return result
}

func buildFgsFunctionParameters(d *schema.ResourceData, cfg *config.Config) (function.CreateOpts, error) {
// check app and package
app, appOk := d.GetOk("app")
Expand Down Expand Up @@ -539,6 +628,8 @@ func buildFgsFunctionParameters(d *schema.ResourceData, cfg *config.Config) (fun
GPUType: d.Get("gpu_type").(string),
PreStopHandler: d.Get("pre_stop_handler").(string),
PreStopTimeout: d.Get("pre_stop_timeout").(int),
FuncVpc: buildFuncVpc(d.Get("func_vpc").([]interface{})),

Check failure on line 631 in huaweicloud/services/fgs/resource_huaweicloud_fgs_function.go

View workflow job for this annotation

GitHub Actions / build

unknown field FuncVpc in struct literal of type "github.com/chnsz/golangsdk/openstack/fgs/v2/function".CreateOpts
NetworkController: buildNetworkController(d.Get("network_controller").([]interface{})),

Check failure on line 632 in huaweicloud/services/fgs/resource_huaweicloud_fgs_function.go

View workflow job for this annotation

GitHub Actions / build

unknown field NetworkController in struct literal of type "github.com/chnsz/golangsdk/openstack/fgs/v2/function".CreateOpts
}
if v, ok := d.GetOk("func_code"); ok {
funcCode := function.FunctionCodeOpts{
Expand Down Expand Up @@ -674,9 +765,45 @@ func setFgsFunctionAgency(d *schema.ResourceData, agency string) error {
}

func setFgsFunctionVpcAccess(d *schema.ResourceData, funcVpc function.FuncVpc) error {
result := []map[string]interface{}{
{
"cidr": funcVpc.Cidr,
"gateway": funcVpc.Gateway,
},
}

mErr := multierror.Append(
d.Set("vpc_id", funcVpc.VpcId),
d.Set("network_id", funcVpc.SubnetId),
d.Set("func_vpc", result),
)
if err := mErr.ErrorOrNil(); err != nil {
return fmt.Errorf("error setting vault fields: %s", err)
}
return nil
}

func setTriggerAccessVpcs(vpcConfigs []function.VpcConfig) interface{} {
result := make([]map[string]interface{}, len(vpcConfigs))
for i, val := range vpcConfigs {
result[i] = map[string]interface{}{
"vpc_id": val.VpcId,
"vpc_name": val.VpcName,
}
}
return result
}

func setNetworkController(d *schema.ResourceData, networkController function.NetworkControlConfig) error {
result := []map[string]interface{}{
{
"disable_public_network": networkController.DisablePublicNetwork,
"trigger_access_vpcs": setTriggerAccessVpcs(networkController.TriggerAccessVpcs),
},
}

mErr := multierror.Append(
d.Set("network_controller", result),
)
if err := mErr.ErrorOrNil(); err != nil {
return fmt.Errorf("error setting vault fields: %s", err)
Expand Down Expand Up @@ -901,6 +1028,7 @@ func resourceFgsFunctionRead(_ context.Context, d *schema.ResourceData, meta int
d.Set("gpu_type", f.GPUType),
d.Set("pre_stop_handler", f.PreStopHandler),
d.Set("pre_stop_timeout", f.PreStopTimeout),
setNetworkController(d, f.NetworkController),
)

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

Check failure on line 1399 in huaweicloud/services/fgs/resource_huaweicloud_fgs_function.go

View workflow job for this annotation

GitHub Actions / build

cannot use buildNetworkController(d.Get("network_controller").([]interface{})) (value of type *"github.com/chnsz/golangsdk/openstack/fgs/v2/function".NetworkControlConfig) as "github.com/chnsz/golangsdk/openstack/fgs/v2/function".NetworkControlConfig value in struct literal
}

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit be47deb

Please sign in to comment.