From 3765b5e00b36144196ba18a7ce6b533a4b5a847d Mon Sep 17 00:00:00 2001 From: Olexandr Lytvyn Date: Thu, 7 Sep 2023 11:08:24 +0300 Subject: [PATCH 1/2] Improve resource Service --- openvpncloud/resource_service.go | 63 +++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/openvpncloud/resource_service.go b/openvpncloud/resource_service.go index 43936f5..64048bb 100644 --- a/openvpncloud/resource_service.go +++ b/openvpncloud/resource_service.go @@ -2,6 +2,7 @@ package openvpncloud import ( "context" + "github.com/OpenVPN/terraform-provider-openvpn-cloud/client" "github.com/hashicorp/go-cty/cty" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -88,9 +89,31 @@ func resourceServiceConfig() *schema.Resource { Optional: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ + "protocol": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{"TCP", "UDP", "ICMP"}, false), + Description: "The description for the UI. Defaults to `Managed by Terraform`.", + }, + "port": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "lower_value": { + Type: schema.TypeInt, + Required: true, + }, + "upper_value": { + Type: schema.TypeInt, + Required: true, + }, + }, + }, + }, "icmp_type": { Type: schema.TypeList, - Required: true, + Optional: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "lower_value": { @@ -181,14 +204,16 @@ func flattenCustomServiceTypes(types []*client.CustomServiceType) interface{} { data = append( data, map[string]interface{}{ - "icmp_type": flattenIcmpType(t.IcmpType), + "icmp_type": flattenPorts(t.IcmpType), + "port": flattenPorts(t.Port), + "protocol": t.Protocol, }, ) } return data } -func flattenIcmpType(icmpType []client.Range) interface{} { +func flattenPorts(icmpType []client.Range) interface{} { var data []interface{} for _, t := range icmpType { data = append( @@ -249,21 +274,16 @@ func resourceDataToService(data *schema.ResourceData) *client.Service { mainConfig := configList[0].(map[string]interface{}) for _, r := range mainConfig["custom_service_types"].([]interface{}) { cst := r.(map[string]interface{}) - var icmpTypes []client.Range - for _, r := range cst["icmp_type"].([]interface{}) { - icmpType := r.(map[string]interface{}) - icmpTypes = append( - icmpTypes, - client.Range{ - LowerValue: icmpType["lower_value"].(int), - UpperValue: icmpType["upper_value"].(int), - }, - ) - } + icmpTypes := getPortsFromField(cst, "icmp_type") + ports := getPortsFromField(cst, "port") + protocol := cst["protocol"].(string) + config.CustomServiceTypes = append( config.CustomServiceTypes, &client.CustomServiceType{ + Protocol: protocol, IcmpType: icmpTypes, + Port: ports, }, ) } @@ -284,3 +304,18 @@ func resourceDataToService(data *schema.ResourceData) *client.Service { } return s } + +func getPortsFromField(cst map[string]interface{}, fieldName string) []client.Range { + var ranges []client.Range + for _, r := range cst[fieldName].([]interface{}) { + rangeElem := r.(map[string]interface{}) + ranges = append( + ranges, + client.Range{ + LowerValue: rangeElem["lower_value"].(int), + UpperValue: rangeElem["upper_value"].(int), + }, + ) + } + return ranges +} From d584d4b626cb01938666829e40c7547c4f622854 Mon Sep 17 00:00:00 2001 From: Olexandr Lytvyn Date: Mon, 2 Oct 2023 15:02:42 +0300 Subject: [PATCH 2/2] Add new argument logic ('value') and validation --- client/service.go | 1 + openvpncloud/resource_service.go | 30 +++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/client/service.go b/client/service.go index 3a67580..f2c661e 100644 --- a/client/service.go +++ b/client/service.go @@ -11,6 +11,7 @@ import ( type Range struct { LowerValue int `json:"lowerValue"` UpperValue int `json:"upperValue"` + Value int `json:"value"` } type CustomServiceType struct { diff --git a/openvpncloud/resource_service.go b/openvpncloud/resource_service.go index 64048bb..9cd3b0b 100644 --- a/openvpncloud/resource_service.go +++ b/openvpncloud/resource_service.go @@ -93,7 +93,6 @@ func resourceServiceConfig() *schema.Resource { Type: schema.TypeString, Required: true, ValidateFunc: validation.StringInSlice([]string{"TCP", "UDP", "ICMP"}, false), - Description: "The description for the UI. Defaults to `Managed by Terraform`.", }, "port": { Type: schema.TypeList, @@ -101,12 +100,22 @@ func resourceServiceConfig() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "lower_value": { - Type: schema.TypeInt, - Required: true, + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(0, 65535), + Description: "This argument is designed to be used to define range of ports", }, "upper_value": { - Type: schema.TypeInt, - Required: true, + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(0, 65535), + Description: "This argument is designed to be used to define range of ports", + }, + "value": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(0, 65535), + Description: "This argument is designed to be used to define singular port", }, }, }, @@ -117,12 +126,14 @@ func resourceServiceConfig() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "lower_value": { - Type: schema.TypeInt, - Required: true, + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(0, 255), }, "upper_value": { - Type: schema.TypeInt, - Required: true, + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(0, 255), }, }, }, @@ -314,6 +325,7 @@ func getPortsFromField(cst map[string]interface{}, fieldName string) []client.Ra client.Range{ LowerValue: rangeElem["lower_value"].(int), UpperValue: rangeElem["upper_value"].(int), + Value: rangeElem["value"].(int), }, ) }