Skip to content

Commit

Permalink
Merge pull request #32 from Edge-Center/fix/CLOUDDEV-332-sg-rules-por…
Browse files Browse the repository at this point in the history
…t-ranges

fix(CLOUDDEV-332): change sg rules port ranges
  • Loading branch information
anaxaim authored Nov 21, 2023
2 parents c5f7841 + 06e9ef3 commit 359460c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 35 deletions.
5 changes: 3 additions & 2 deletions edgecenter/data_source_edgecenter_securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
34 changes: 19 additions & 15 deletions edgecenter/resource_edgecenter_securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion edgecenter/test/.env
Original file line number Diff line number Diff line change
Expand Up @@ -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=[email protected]
TEST_PROJECT_ID=8272
TEST_PROJECT_ID=31203
TEST_REGION_ID=8

17 changes: 0 additions & 17 deletions edgecenter/utils_securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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{})
Expand Down

0 comments on commit 359460c

Please sign in to comment.