diff --git a/edgecenter/data_source_edgecenter_securitygroup.go b/edgecenter/data_source_edgecenter_securitygroup.go index daf585c5..47ee12d2 100644 --- a/edgecenter/data_source_edgecenter_securitygroup.go +++ b/edgecenter/data_source_edgecenter_securitygroup.go @@ -223,11 +223,12 @@ func dataSourceSecurityGroupRead(_ context.Context, d *schema.ResourceData, m in r["protocol"] = sgr.Protocol.String() } - r["port_range_max"] = 0 + r["port_range_max"] = 65535 if sgr.PortRangeMax != nil { r["port_range_max"] = *sgr.PortRangeMax } - r["port_range_min"] = 0 + + r["port_range_min"] = 1 if sgr.PortRangeMin != nil { r["port_range_min"] = *sgr.PortRangeMin } diff --git a/edgecenter/resource_edgecenter_securitygroup.go b/edgecenter/resource_edgecenter_securitygroup.go index fced88d7..6c73156e 100644 --- a/edgecenter/resource_edgecenter_securitygroup.go +++ b/edgecenter/resource_edgecenter_securitygroup.go @@ -10,6 +10,7 @@ import ( "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" "github.com/Edge-Center/edgecentercloud-go/edgecenter/securitygroup/v1/securitygrouprules" "github.com/Edge-Center/edgecentercloud-go/edgecenter/securitygroup/v1/securitygroups" @@ -149,16 +150,16 @@ func resourceSecurityGroup() *schema.Resource { Description: fmt.Sprintf("Available value is %s", strings.Join(types.Protocol("").StringList(), ",")), }, "port_range_min": { - Type: schema.TypeInt, - Optional: true, - Default: 0, - ValidateDiagFunc: validatePortRange, + Type: schema.TypeInt, + Optional: true, + Default: 1, + ValidateFunc: validation.IntBetween(1, 65535), }, "port_range_max": { - Type: schema.TypeInt, - Optional: true, - Default: 0, - ValidateDiagFunc: validatePortRange, + Type: schema.TypeInt, + Optional: true, + Default: 65535, + ValidateFunc: validation.IntBetween(1, 65535), }, "description": { Type: schema.TypeString, @@ -221,8 +222,6 @@ func resourceSecurityGroupCreate(ctx context.Context, d *schema.ResourceData, m for i, r := range rawRules { rule := r.(map[string]interface{}) - portRangeMax := rule["port_range_max"].(int) - portRangeMin := rule["port_range_min"].(int) descr := rule["description"].(string) remoteIPPrefix := rule["remote_ip_prefix"].(string) @@ -237,11 +236,16 @@ func resourceSecurityGroupCreate(ctx context.Context, d *schema.ResourceData, m sgrOpts.RemoteIPPrefix = &remoteIPPrefix } - if portRangeMax != 0 && portRangeMin != 0 { - sgrOpts.PortRangeMax = &portRangeMax - sgrOpts.PortRangeMin = &portRangeMin + portRangeMin := rule["port_range_min"].(int) + portRangeMax := rule["port_range_max"].(int) + + if portRangeMin > portRangeMax { + return diag.FromErr(fmt.Errorf("value of the port_range_min cannot be greater than port_range_max")) } + sgrOpts.PortRangeMax = &portRangeMax + sgrOpts.PortRangeMin = &portRangeMin + rules[i] = sgrOpts } @@ -332,11 +336,11 @@ func resourceSecurityGroupRead(_ context.Context, d *schema.ResourceData, m inte r["protocol"] = sgr.Protocol.String() } - r["port_range_max"] = 0 + r["port_range_max"] = 65535 if sgr.PortRangeMax != nil { r["port_range_max"] = *sgr.PortRangeMax } - r["port_range_min"] = 0 + r["port_range_min"] = 1 if sgr.PortRangeMin != nil { r["port_range_min"] = *sgr.PortRangeMin } diff --git a/edgecenter/test/.env b/edgecenter/test/.env index f3aa4157..fb0a07e5 100644 --- a/edgecenter/test/.env +++ b/edgecenter/test/.env @@ -6,6 +6,6 @@ EC_PASSWORD=Test-1234 EC_PLATFORM=https://api.edgecenter.online/iam EC_STORAGE_API=https://api.edgecenter.online/storage EC_USERNAME=test-cloud-common@edgecenter.ru -TEST_PROJECT_ID=8272 +TEST_PROJECT_ID=31203 TEST_REGION_ID=8 diff --git a/edgecenter/utils_securitygroup.go b/edgecenter/utils_securitygroup.go index c670d9f4..9530acd3 100644 --- a/edgecenter/utils_securitygroup.go +++ b/edgecenter/utils_securitygroup.go @@ -6,18 +6,10 @@ import ( "io" "strconv" - "github.com/hashicorp/go-cty/cty" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/Edge-Center/edgecentercloud-go/edgecenter/securitygroup/v1/securitygroups" typesSG "github.com/Edge-Center/edgecentercloud-go/edgecenter/securitygroup/v1/types" ) -const ( - minPort = 0 - maxPort = 65535 -) - // secGroupUniqueID generates a unique ID for a security group rule using its properties. func secGroupUniqueID(i interface{}) int { e := i.(map[string]interface{}) @@ -35,15 +27,6 @@ func secGroupUniqueID(i interface{}) int { return int(binary.BigEndian.Uint64(h.Sum(nil))) } -// validatePortRange checks if the provided port value is within the valid range. -func validatePortRange(v interface{}, _ cty.Path) diag.Diagnostics { - val := v.(int) - if val >= minPort && val <= maxPort { - return nil - } - return diag.Errorf("available range %d-%d", minPort, maxPort) -} - // extractSecurityGroupRuleMap creates a security group rule from the provided map and security group ID. func extractSecurityGroupRuleMap(r interface{}, gid string) securitygroups.CreateSecurityGroupRuleOpts { rule := r.(map[string]interface{})