From 0a3bafa38bff0e0664df231edef0446eba64a12c Mon Sep 17 00:00:00 2001 From: George Demin Date: Sat, 2 Nov 2024 14:24:17 +0300 Subject: [PATCH 1/8] fix(CLOUDDEV-1134): fix add sg without port --- edgecenter/resource_edgecenter_securitygroup.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/edgecenter/resource_edgecenter_securitygroup.go b/edgecenter/resource_edgecenter_securitygroup.go index a416fd8c..99efde41 100644 --- a/edgecenter/resource_edgecenter_securitygroup.go +++ b/edgecenter/resource_edgecenter_securitygroup.go @@ -158,13 +158,13 @@ func resourceSecurityGroup() *schema.Resource { "port_range_min": { Type: schema.TypeInt, Optional: true, - Default: 1, + Computed: true, ValidateFunc: validation.IntBetween(1, 65535), }, "port_range_max": { Type: schema.TypeInt, Optional: true, - Default: 65535, + Computed: true, ValidateFunc: validation.IntBetween(1, 65535), }, "description": { @@ -250,8 +250,13 @@ func resourceSecurityGroupCreate(ctx context.Context, d *schema.ResourceData, m return diag.FromErr(fmt.Errorf("value of the port_range_min cannot be greater than port_range_max")) } - sgrOpts.PortRangeMax = &portRangeMax - sgrOpts.PortRangeMin = &portRangeMin + if portRangeMax != 0 { + sgrOpts.PortRangeMax = &portRangeMax + } + + if portRangeMin != 0 { + sgrOpts.PortRangeMin = &portRangeMin + } rules[i] = sgrOpts } From 51364c61e42bab5f4d00ae6855fc3d183b1e5809 Mon Sep 17 00:00:00 2001 From: George Demin Date: Wed, 13 Nov 2024 09:33:12 +0300 Subject: [PATCH 2/8] fix(CLOUDDEV-1134): changed resource sg port type from int to string --- .../resource_edgecenter_securitygroup.go | 43 ++++++++++++------- edgecenter/utils_securitygroup.go | 26 +++++++---- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/edgecenter/resource_edgecenter_securitygroup.go b/edgecenter/resource_edgecenter_securitygroup.go index 99efde41..4877638f 100644 --- a/edgecenter/resource_edgecenter_securitygroup.go +++ b/edgecenter/resource_edgecenter_securitygroup.go @@ -6,6 +6,8 @@ import ( "fmt" "log" "net/http" + "regexp" + "strconv" "strings" "time" @@ -156,16 +158,16 @@ func resourceSecurityGroup() *schema.Resource { Description: fmt.Sprintf("Available value is %s", strings.Join(edgecloudV2.SecurityGroupRuleProtocol("").StringList(), ",")), }, "port_range_min": { - Type: schema.TypeInt, + Type: schema.TypeString, Optional: true, Computed: true, - ValidateFunc: validation.IntBetween(1, 65535), + ValidateFunc: validation.StringMatch(regexp.MustCompile(`^$|^[1-9][0-9]{0,4}$`), "must be a valid integer between 1 and 65535 or empty"), }, "port_range_max": { - Type: schema.TypeInt, + Type: schema.TypeString, Optional: true, Computed: true, - ValidateFunc: validation.IntBetween(1, 65535), + ValidateFunc: validation.StringMatch(regexp.MustCompile(`^$|^[1-9][0-9]{0,4}$`), "must be a valid integer between 1 and 65535 or empty"), }, "description": { Type: schema.TypeString, @@ -243,19 +245,30 @@ func resourceSecurityGroupCreate(ctx context.Context, d *schema.ResourceData, m sgrOpts.RemoteIPPrefix = &remoteIPPrefix } - portRangeMin := rule["port_range_min"].(int) - portRangeMax := rule["port_range_max"].(int) + portRangeMin := rule["port_range_min"].(string) + portRangeMax := rule["port_range_max"].(string) - if portRangeMin > portRangeMax { + portRangeMinItn, err := strconv.Atoi(portRangeMin) + if err != nil { + return diag.FromErr(err) + } + + portRangeMaxItn, err := strconv.Atoi(portRangeMax) + + if err != nil { + return diag.FromErr(err) + } + + if portRangeMinItn > portRangeMaxItn { return diag.FromErr(fmt.Errorf("value of the port_range_min cannot be greater than port_range_max")) } - if portRangeMax != 0 { - sgrOpts.PortRangeMax = &portRangeMax + if portRangeMax != "" { + sgrOpts.PortRangeMax = &portRangeMinItn } - if portRangeMin != 0 { - sgrOpts.PortRangeMin = &portRangeMin + if portRangeMin != "" { + sgrOpts.PortRangeMin = &portRangeMaxItn } rules[i] = sgrOpts @@ -350,13 +363,13 @@ func resourceSecurityGroupRead(ctx context.Context, d *schema.ResourceData, m in r["protocol"] = sgr.Protocol.String() } - r["port_range_max"] = 65535 + r["port_range_max"] = "" if sgr.PortRangeMax != nil { - r["port_range_max"] = *sgr.PortRangeMax + r["port_range_max"] = strconv.Itoa(*sgr.PortRangeMax) } - r["port_range_min"] = 1 + r["port_range_min"] = "" if sgr.PortRangeMin != nil { - r["port_range_min"] = *sgr.PortRangeMin + r["port_range_min"] = strconv.Itoa(*sgr.PortRangeMin) } r["description"] = "" diff --git a/edgecenter/utils_securitygroup.go b/edgecenter/utils_securitygroup.go index b5752de0..9813db15 100644 --- a/edgecenter/utils_securitygroup.go +++ b/edgecenter/utils_securitygroup.go @@ -3,10 +3,10 @@ package edgecenter import ( "crypto/md5" "encoding/binary" + "fmt" + edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2" "io" "strconv" - - edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2" ) // secGroupUniqueID generates a unique ID for a security group rule using its properties. @@ -18,8 +18,8 @@ func secGroupUniqueID(i interface{}) int { io.WriteString(h, e["direction"].(string)) io.WriteString(h, e["ethertype"].(string)) io.WriteString(h, proto) - io.WriteString(h, strconv.Itoa(e["port_range_min"].(int))) - io.WriteString(h, strconv.Itoa(e["port_range_max"].(int))) + io.WriteString(h, e["port_range_min"].(string)) + io.WriteString(h, e["port_range_max"].(string)) io.WriteString(h, e["description"].(string)) io.WriteString(h, e["remote_ip_prefix"].(string)) @@ -37,10 +37,20 @@ func extractSecurityGroupRuleCreateRequestV2(r interface{}, gid string) edgeclou SecurityGroupID: &gid, } - minP, maxP := rule["port_range_min"].(int), rule["port_range_max"].(int) - if minP != 0 && maxP != 0 { - opts.PortRangeMin = &minP - opts.PortRangeMax = &maxP + minP, maxP := rule["port_range_min"].(string), rule["port_range_max"].(string) + if minP != "" && maxP != "" { + minPort, err := strconv.Atoi(minP) + if err != nil { + fmt.Errorf("error convert string to int in minPort: %w", err) + } + + maxPort, err := strconv.Atoi(maxP) + if err != nil { + fmt.Errorf("error convert string to int in maxPort: %w", err) + } + + opts.PortRangeMin = &minPort + opts.PortRangeMax = &maxPort } description, _ := rule["description"].(string) From 8de6b715841839035644345068ea7807880f5a32 Mon Sep 17 00:00:00 2001 From: George Demin Date: Wed, 13 Nov 2024 09:53:34 +0300 Subject: [PATCH 3/8] fix(CLOUDDEV-1134): changed resource sg port type from int to string --- .../resource_edgecenter_securitygroup.go | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/edgecenter/resource_edgecenter_securitygroup.go b/edgecenter/resource_edgecenter_securitygroup.go index 4877638f..e3b0839e 100644 --- a/edgecenter/resource_edgecenter_securitygroup.go +++ b/edgecenter/resource_edgecenter_securitygroup.go @@ -6,17 +6,14 @@ import ( "fmt" "log" "net/http" - "regexp" "strconv" "strings" "time" + edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2" "github.com/hashicorp/go-cty/cty" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" - - edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2" ) const ( @@ -25,6 +22,19 @@ const ( var ErrCannotDeleteSGRule = errors.New("error when deleting security group rule") +func validatePortRange(val interface{}, key string) (warns []string, errs []error) { + v, ok := val.(string) + if !ok || v == "" { + return + } + + port, err := strconv.Atoi(v) + if err != nil || port < 1 || port > 65535 { + errs = append(errs, fmt.Errorf("%q must be an integer between 1 and 65535, got %q", key, v)) + } + return +} + func resourceSecurityGroup() *schema.Resource { return &schema.Resource{ CreateContext: resourceSecurityGroupCreate, @@ -160,14 +170,12 @@ func resourceSecurityGroup() *schema.Resource { "port_range_min": { Type: schema.TypeString, Optional: true, - Computed: true, - ValidateFunc: validation.StringMatch(regexp.MustCompile(`^$|^[1-9][0-9]{0,4}$`), "must be a valid integer between 1 and 65535 or empty"), + ValidateFunc: validatePortRange, }, "port_range_max": { Type: schema.TypeString, Optional: true, - Computed: true, - ValidateFunc: validation.StringMatch(regexp.MustCompile(`^$|^[1-9][0-9]{0,4}$`), "must be a valid integer between 1 and 65535 or empty"), + ValidateFunc: validatePortRange, }, "description": { Type: schema.TypeString, From f4952ff304f7abd6aa0f7bd2fd9948e6a8f349cd Mon Sep 17 00:00:00 2001 From: George Demin Date: Wed, 13 Nov 2024 10:51:49 +0300 Subject: [PATCH 4/8] fix(CLOUDDEV-1134): changed resource sg port type from int to string --- .../resource_edgecenter_securitygroup.go | 43 ++++++++++--------- edgecenter/utils_securitygroup.go | 15 ++----- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/edgecenter/resource_edgecenter_securitygroup.go b/edgecenter/resource_edgecenter_securitygroup.go index e3b0839e..80d41fbd 100644 --- a/edgecenter/resource_edgecenter_securitygroup.go +++ b/edgecenter/resource_edgecenter_securitygroup.go @@ -10,10 +10,11 @@ import ( "strings" "time" - edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2" "github.com/hashicorp/go-cty/cty" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2" ) const ( @@ -22,19 +23,6 @@ const ( var ErrCannotDeleteSGRule = errors.New("error when deleting security group rule") -func validatePortRange(val interface{}, key string) (warns []string, errs []error) { - v, ok := val.(string) - if !ok || v == "" { - return - } - - port, err := strconv.Atoi(v) - if err != nil || port < 1 || port > 65535 { - errs = append(errs, fmt.Errorf("%q must be an integer between 1 and 65535, got %q", key, v)) - } - return -} - func resourceSecurityGroup() *schema.Resource { return &schema.Resource{ CreateContext: resourceSecurityGroupCreate, @@ -168,14 +156,28 @@ func resourceSecurityGroup() *schema.Resource { Description: fmt.Sprintf("Available value is %s", strings.Join(edgecloudV2.SecurityGroupRuleProtocol("").StringList(), ",")), }, "port_range_min": { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validatePortRange, + Type: schema.TypeString, + Optional: true, + ValidateDiagFunc: func(v interface{}, path cty.Path) diag.Diagnostics { + val := v.(string) + port, _ := strconv.Atoi(val) + if port < 1 || port > 65535 { + return diag.Errorf("wrong port_range_min %q, available value between 1 and 65535", val) + } + return nil + }, }, "port_range_max": { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validatePortRange, + Type: schema.TypeString, + Optional: true, + ValidateDiagFunc: func(v interface{}, path cty.Path) diag.Diagnostics { + val := v.(string) + port, _ := strconv.Atoi(val) + if port < 1 || port > 65535 { + return diag.Errorf("wrong port_range_max %q, available value between 1 and 65535", val) + } + return nil + }, }, "description": { Type: schema.TypeString, @@ -262,7 +264,6 @@ func resourceSecurityGroupCreate(ctx context.Context, d *schema.ResourceData, m } portRangeMaxItn, err := strconv.Atoi(portRangeMax) - if err != nil { return diag.FromErr(err) } diff --git a/edgecenter/utils_securitygroup.go b/edgecenter/utils_securitygroup.go index 9813db15..fddd5206 100644 --- a/edgecenter/utils_securitygroup.go +++ b/edgecenter/utils_securitygroup.go @@ -3,10 +3,10 @@ package edgecenter import ( "crypto/md5" "encoding/binary" - "fmt" - edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2" "io" "strconv" + + edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2" ) // secGroupUniqueID generates a unique ID for a security group rule using its properties. @@ -39,15 +39,8 @@ func extractSecurityGroupRuleCreateRequestV2(r interface{}, gid string) edgeclou minP, maxP := rule["port_range_min"].(string), rule["port_range_max"].(string) if minP != "" && maxP != "" { - minPort, err := strconv.Atoi(minP) - if err != nil { - fmt.Errorf("error convert string to int in minPort: %w", err) - } - - maxPort, err := strconv.Atoi(maxP) - if err != nil { - fmt.Errorf("error convert string to int in maxPort: %w", err) - } + minPort, _ := strconv.Atoi(minP) + maxPort, _ := strconv.Atoi(maxP) opts.PortRangeMin = &minPort opts.PortRangeMax = &maxPort From 7f2b7d6c3b38ad557267f2e7a9f5b70f15135b37 Mon Sep 17 00:00:00 2001 From: George Demin Date: Wed, 13 Nov 2024 11:00:49 +0300 Subject: [PATCH 5/8] fix(CLOUDDEV-1134): changed resource sg port type from int to string --- edgecenter/utils_securitygroup.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/edgecenter/utils_securitygroup.go b/edgecenter/utils_securitygroup.go index fddd5206..fa49e6ca 100644 --- a/edgecenter/utils_securitygroup.go +++ b/edgecenter/utils_securitygroup.go @@ -18,8 +18,8 @@ func secGroupUniqueID(i interface{}) int { io.WriteString(h, e["direction"].(string)) io.WriteString(h, e["ethertype"].(string)) io.WriteString(h, proto) - io.WriteString(h, e["port_range_min"].(string)) - io.WriteString(h, e["port_range_max"].(string)) + io.WriteString(h, strconv.Itoa(e["port_range_min"].(int))) + io.WriteString(h, strconv.Itoa(e["port_range_max"].(int))) io.WriteString(h, e["description"].(string)) io.WriteString(h, e["remote_ip_prefix"].(string)) From 4827dd6df99f0390147efca2a12f1d3c97dfd432 Mon Sep 17 00:00:00 2001 From: George Demin Date: Wed, 13 Nov 2024 13:37:45 +0300 Subject: [PATCH 6/8] fix(CLOUDDEV-1134): changed resource sg port type from int to string --- edgecenter/utils_securitygroup.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/edgecenter/utils_securitygroup.go b/edgecenter/utils_securitygroup.go index fa49e6ca..fddd5206 100644 --- a/edgecenter/utils_securitygroup.go +++ b/edgecenter/utils_securitygroup.go @@ -18,8 +18,8 @@ func secGroupUniqueID(i interface{}) int { io.WriteString(h, e["direction"].(string)) io.WriteString(h, e["ethertype"].(string)) io.WriteString(h, proto) - io.WriteString(h, strconv.Itoa(e["port_range_min"].(int))) - io.WriteString(h, strconv.Itoa(e["port_range_max"].(int))) + io.WriteString(h, e["port_range_min"].(string)) + io.WriteString(h, e["port_range_max"].(string)) io.WriteString(h, e["description"].(string)) io.WriteString(h, e["remote_ip_prefix"].(string)) From f6db4abd58eb5ebf8bdba36778e8a7aaed7abe54 Mon Sep 17 00:00:00 2001 From: George Demin Date: Wed, 13 Nov 2024 14:18:37 +0300 Subject: [PATCH 7/8] fix(CLOUDDEV-1134): changed resource sg port type from int to string --- edgecenter/data_source_edgecenter_securitygroup.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/edgecenter/data_source_edgecenter_securitygroup.go b/edgecenter/data_source_edgecenter_securitygroup.go index fa0668b2..acc3a63a 100644 --- a/edgecenter/data_source_edgecenter_securitygroup.go +++ b/edgecenter/data_source_edgecenter_securitygroup.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "log" + "strconv" "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -114,11 +115,11 @@ func dataSourceSecurityGroup() *schema.Resource { Description: fmt.Sprintf("Available value is %s", strings.Join(utilV2.SecurityGroupRuleProtocol("").StringList(), ",")), }, "port_range_min": { - Type: schema.TypeInt, + Type: schema.TypeString, Computed: true, }, "port_range_max": { - Type: schema.TypeInt, + Type: schema.TypeString, Computed: true, }, "description": { @@ -227,14 +228,13 @@ func dataSourceSecurityGroupRead(ctx context.Context, d *schema.ResourceData, m r["protocol"] = string(*sgr.Protocol) } - r["port_range_max"] = 65535 + r["port_range_max"] = "" if sgr.PortRangeMax != nil { - r["port_range_max"] = *sgr.PortRangeMax + r["port_range_max"] = strconv.Itoa(*sgr.PortRangeMax) } - - r["port_range_min"] = 1 + r["port_range_min"] = "" if sgr.PortRangeMin != nil { - r["port_range_min"] = *sgr.PortRangeMin + r["port_range_min"] = strconv.Itoa(*sgr.PortRangeMin) } r["description"] = "" From d87db82a3b565a7057fa36857bf01a87337690ab Mon Sep 17 00:00:00 2001 From: George Demin Date: Thu, 14 Nov 2024 12:08:46 +0300 Subject: [PATCH 8/8] fix(CLOUDDEV-1134): changed resource sg port type from int to string --- .../resource_edgecenter_securitygroup.go | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/edgecenter/resource_edgecenter_securitygroup.go b/edgecenter/resource_edgecenter_securitygroup.go index 80d41fbd..8fd1974d 100644 --- a/edgecenter/resource_edgecenter_securitygroup.go +++ b/edgecenter/resource_edgecenter_securitygroup.go @@ -258,17 +258,23 @@ func resourceSecurityGroupCreate(ctx context.Context, d *schema.ResourceData, m portRangeMin := rule["port_range_min"].(string) portRangeMax := rule["port_range_max"].(string) - portRangeMinItn, err := strconv.Atoi(portRangeMin) - if err != nil { - return diag.FromErr(err) + var portRangeMinItn, portRangeMaxItn int + + if portRangeMin != "" { + portRangeMinItn, err = strconv.Atoi(portRangeMin) + if err != nil { + return diag.FromErr(err) + } } - portRangeMaxItn, err := strconv.Atoi(portRangeMax) - if err != nil { - return diag.FromErr(err) + if portRangeMax != "" { + portRangeMaxItn, err = strconv.Atoi(portRangeMax) + if err != nil { + return diag.FromErr(err) + } } - if portRangeMinItn > portRangeMaxItn { + if portRangeMin != "" && portRangeMax != "" && portRangeMinItn > portRangeMaxItn { return diag.FromErr(fmt.Errorf("value of the port_range_min cannot be greater than port_range_max")) }