Skip to content

Commit

Permalink
Merge pull request #239 from NaverCloudPlatform/feature/acg
Browse files Browse the repository at this point in the history
feat: Support protocol number
  • Loading branch information
youngmn authored Nov 29, 2022
2 parents 7f41624 + 33811bb commit 59f5f08
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions ncloud/resource_ncloud_access_control_group_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"log"
"regexp"
"strconv"
"time"
)

Expand All @@ -34,9 +36,12 @@ func resourceNcloudAccessControlGroupRule() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: ToDiagFunc(validation.StringInSlice([]string{"TCP", "UDP", "ICMP"}, false)),
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: ToDiagFunc(validation.All(
validation.StringMatch(regexp.MustCompile(`TCP|UDP|ICMP|\b([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-2])\b`), "only TCP, UDP, ICMP and 1-252 are valid values."),
validation.StringNotInSlice([]string{"1", "6", "17"}, false),
)),
},
"port_range": {
Type: schema.TypeString,
Expand Down Expand Up @@ -71,9 +76,12 @@ func resourceNcloudAccessControlGroupRule() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: ToDiagFunc(validation.StringInSlice([]string{"TCP", "UDP", "ICMP"}, false)),
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: ToDiagFunc(validation.All(
validation.StringMatch(regexp.MustCompile(`TCP|UDP|ICMP|\b([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-2])\b`), "only TCP, UDP, ICMP and 1-252 are valid values."),
validation.StringNotInSlice([]string{"1", "6", "17"}, false),
)),
},
"port_range": {
Type: schema.TypeString,
Expand Down Expand Up @@ -148,8 +156,15 @@ func resourceNcloudAccessControlGroupRuleRead(d *schema.ResourceData, meta inter
oSet := schema.NewSet(schema.HashResource(resourceNcloudAccessControlGroupRule().Schema["outbound"].Elem.(*schema.Resource)), []interface{}{})

for _, r := range rules {
var protocol string
if allowedProtocolCodes[*r.ProtocolType.Code] {
protocol = *r.ProtocolType.Code
} else {
protocol = strconv.Itoa(int(*r.ProtocolType.Number))
}

m := map[string]interface{}{
"protocol": *r.ProtocolType.Code,
"protocol": protocol,
"port_range": *r.PortRange,
"ip_block": *r.IpBlock,
"source_access_control_group_no": *r.AccessControlGroupSequence,
Expand Down Expand Up @@ -444,3 +459,9 @@ func expandRemoveAccessControlGroupRule(rules []interface{}) []*vserver.RemoveAc

return acgRuleList
}

var allowedProtocolCodes = map[string]bool{
"TCP": true,
"UDP": true,
"ICMP": true,
}

0 comments on commit 59f5f08

Please sign in to comment.