Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Improve resource Service #96

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type Range struct {
LowerValue int `json:"lowerValue"`
UpperValue int `json:"upperValue"`
Value int `json:"value"`
}

type CustomServiceType struct {
Expand Down
83 changes: 65 additions & 18 deletions openvpncloud/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -88,18 +89,51 @@ 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),
},
"port": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"lower_value": {
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,
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",
},
},
},
},
"icmp_type": {
Type: schema.TypeList,
Required: true,
Optional: true,
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),
},
},
},
Expand Down Expand Up @@ -181,14 +215,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(
Expand Down Expand Up @@ -249,21 +285,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,
},
)
}
Expand All @@ -284,3 +315,19 @@ 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),
Value: rangeElem["value"].(int),
},
)
}
return ranges
}
Loading