From 690280e045083a1b72d2b4bb1fa89bb44b688e83 Mon Sep 17 00:00:00 2001 From: Kurt McAlpine Date: Sat, 29 Aug 2020 13:52:14 +1200 Subject: [PATCH 1/8] Add cidrs attribute to aws_lightsail_instance_public_ports resource --- ...rce_aws_lightsail_instance_public_ports.go | 25 ++++++++ ...ws_lightsail_instance_public_ports_test.go | 61 +++++++++++++++++++ ...htsail_instance_public_ports.html.markdown | 1 + 3 files changed, 87 insertions(+) diff --git a/aws/resource_aws_lightsail_instance_public_ports.go b/aws/resource_aws_lightsail_instance_public_ports.go index 5116ce73b222..4019948f569d 100644 --- a/aws/resource_aws_lightsail_instance_public_ports.go +++ b/aws/resource_aws_lightsail_instance_public_ports.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "log" + "sort" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/lightsail" @@ -51,6 +52,15 @@ func resourceAwsLightsailInstancePublicPorts() *schema.Resource { ForceNew: true, ValidateFunc: validation.IntBetween(0, 65535), }, + "cidrs": { + Type: schema.TypeList, + Optional: true, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validateCIDRNetworkAddress, + }, + }, }, }, }, @@ -151,6 +161,15 @@ func expandLightsailPortInfo(tfMap map[string]interface{}) *lightsail.PortInfo { ToPort: aws.Int64((int64)(tfMap["to_port"].(int))), Protocol: aws.String(tfMap["protocol"].(string)), } + if cidrs, ok := tfMap["cidrs"]; ok { + for _, v := range cidrs.([]interface{}) { + apiObject.Cidrs = append(apiObject.Cidrs, aws.String(v.(string))) + } + + sort.Slice(apiObject.Cidrs, func(i, j int) bool { + return *apiObject.Cidrs[i] > *apiObject.Cidrs[j] + }) + } return apiObject } @@ -192,6 +211,12 @@ func flattenLightsailInstancePortState(apiObject *lightsail.InstancePortState) m tfMap["to_port"] = aws.Int64Value(apiObject.ToPort) tfMap["protocol"] = aws.StringValue(apiObject.Protocol) + cidrs := apiObject.Cidrs + sort.Slice(cidrs, func(i, j int) bool { + return *cidrs[i] > *cidrs[j] + }) + tfMap["cidrs"] = aws.StringValueSlice(cidrs) + return tfMap } diff --git a/aws/resource_aws_lightsail_instance_public_ports_test.go b/aws/resource_aws_lightsail_instance_public_ports_test.go index d22363706e16..5907a45c526f 100644 --- a/aws/resource_aws_lightsail_instance_public_ports_test.go +++ b/aws/resource_aws_lightsail_instance_public_ports_test.go @@ -71,6 +71,36 @@ func TestAccAWSLightsailInstancePublicPorts_multiple(t *testing.T) { }) } +func TestAccAWSLightsailInstancePublicPorts_cidrs(t *testing.T) { + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_lightsail_instance_public_ports.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccPartitionHasServicePreCheck(lightsail.EndpointsID, t) + testAccPreCheckAWSLightsail(t) + }, + ErrorCheck: testAccErrorCheck(t, lightsail.EndpointsID), + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLightsailInstancePublicPortsDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLightsailInstancePublicPortsConfig_cidrs(rName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLightsailInstancePublicPortsExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "port_info.#", "1"), + resource.TestCheckResourceAttr(resourceName, "port_info.0.protocol", "tcp"), + resource.TestCheckResourceAttr(resourceName, "port_info.0.from_port", "125"), + resource.TestCheckResourceAttr(resourceName, "port_info.0.to_port", "125"), + resource.TestCheckResourceAttr(resourceName, "port_info.0.cidrs.0", "192.168.1.0/24"), + resource.TestCheckResourceAttr(resourceName, "port_info.0.cidrs.1", "1.1.1.1/32"), + ), + }, + }, + }) +} + func testAccCheckAWSLightsailInstancePublicPortsExists(resourceName string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] @@ -189,3 +219,34 @@ resource "aws_lightsail_instance_public_ports" "test" { } `, rName) } + +func testAccAWSLightsailInstancePublicPortsConfig_cidrs(rName string) string { + return fmt.Sprintf(` +data "aws_availability_zones" "available" { + state = "available" + + filter { + name = "opt-in-status" + values = ["opt-in-not-required"] + } +} + +resource "aws_lightsail_instance" "test" { + name = %[1]q + availability_zone = data.aws_availability_zones.available.names[0] + blueprint_id = "amazon_linux" + bundle_id = "nano_1_0" +} + +resource "aws_lightsail_instance_public_ports" "test" { + instance_name = aws_lightsail_instance.test.name + + port_info { + protocol = "tcp" + from_port = 125 + to_port = 125 + cidrs = ["192.168.1.0/24", "1.1.1.1/32"] + } +} +`, rName) +} diff --git a/website/docs/r/lightsail_instance_public_ports.html.markdown b/website/docs/r/lightsail_instance_public_ports.html.markdown index 320f7d51957b..c2bbc343363a 100644 --- a/website/docs/r/lightsail_instance_public_ports.html.markdown +++ b/website/docs/r/lightsail_instance_public_ports.html.markdown @@ -47,6 +47,7 @@ The following arguments are required: * `from_port` - (Required) First port in a range of open ports on an instance. * `protocol` - (Required) IP protocol name. Valid values are `tcp`, `all`, `udp`, and `icmp`. * `to_port` - (Required) Last port in a range of open ports on an instance. +* `cidrs` - (Optional) List of CIDR blocks. ## Attributes Reference From 9f292958bccd667febea9fa5cba861ea90a288b8 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Thu, 25 Mar 2021 09:44:34 -0400 Subject: [PATCH 2/8] r/lightsail_instance_public_ports: Alphabetize args --- ...urce_aws_lightsail_instance_public_ports.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/aws/resource_aws_lightsail_instance_public_ports.go b/aws/resource_aws_lightsail_instance_public_ports.go index 4019948f569d..c7552cf43b53 100644 --- a/aws/resource_aws_lightsail_instance_public_ports.go +++ b/aws/resource_aws_lightsail_instance_public_ports.go @@ -34,6 +34,15 @@ func resourceAwsLightsailInstancePublicPorts() *schema.Resource { MinItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ + "cidrs": { + Type: schema.TypeList, + Optional: true, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validateCIDRNetworkAddress, + }, + }, "from_port": { Type: schema.TypeInt, Required: true, @@ -52,15 +61,6 @@ func resourceAwsLightsailInstancePublicPorts() *schema.Resource { ForceNew: true, ValidateFunc: validation.IntBetween(0, 65535), }, - "cidrs": { - Type: schema.TypeList, - Optional: true, - Computed: true, - Elem: &schema.Schema{ - Type: schema.TypeString, - ValidateFunc: validateCIDRNetworkAddress, - }, - }, }, }, }, From d8619a40b55671b50ae0b493a4d1304813a65497 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Thu, 25 Mar 2021 09:45:00 -0400 Subject: [PATCH 3/8] docs/r/lightsail_instance_public_ports: Minor fix --- website/docs/r/lightsail_instance_public_ports.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/lightsail_instance_public_ports.html.markdown b/website/docs/r/lightsail_instance_public_ports.html.markdown index c2bbc343363a..c53f92b9e614 100644 --- a/website/docs/r/lightsail_instance_public_ports.html.markdown +++ b/website/docs/r/lightsail_instance_public_ports.html.markdown @@ -44,10 +44,10 @@ The following arguments are required: ### port_info +* `cidrs` - (Optional) List of CIDR blocks. * `from_port` - (Required) First port in a range of open ports on an instance. * `protocol` - (Required) IP protocol name. Valid values are `tcp`, `all`, `udp`, and `icmp`. * `to_port` - (Required) Last port in a range of open ports on an instance. -* `cidrs` - (Optional) List of CIDR blocks. ## Attributes Reference From 3d5b25ccaed393a38eb08f385f5eefc438c884d9 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Thu, 25 Mar 2021 10:31:09 -0400 Subject: [PATCH 4/8] r/lightsail_instance_public_ports: Change CIDRs to set --- ...rce_aws_lightsail_instance_public_ports.go | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/aws/resource_aws_lightsail_instance_public_ports.go b/aws/resource_aws_lightsail_instance_public_ports.go index c7552cf43b53..4531f19dd9d0 100644 --- a/aws/resource_aws_lightsail_instance_public_ports.go +++ b/aws/resource_aws_lightsail_instance_public_ports.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "log" - "sort" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/lightsail" @@ -35,7 +34,7 @@ func resourceAwsLightsailInstancePublicPorts() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "cidrs": { - Type: schema.TypeList, + Type: schema.TypeSet, Optional: true, Computed: true, Elem: &schema.Schema{ @@ -161,14 +160,9 @@ func expandLightsailPortInfo(tfMap map[string]interface{}) *lightsail.PortInfo { ToPort: aws.Int64((int64)(tfMap["to_port"].(int))), Protocol: aws.String(tfMap["protocol"].(string)), } - if cidrs, ok := tfMap["cidrs"]; ok { - for _, v := range cidrs.([]interface{}) { - apiObject.Cidrs = append(apiObject.Cidrs, aws.String(v.(string))) - } - sort.Slice(apiObject.Cidrs, func(i, j int) bool { - return *apiObject.Cidrs[i] > *apiObject.Cidrs[j] - }) + if v, ok := tfMap["cidrs"].(*schema.Set); ok && v.Len() > 0 { + apiObject.Cidrs = expandStringSet(v) } return apiObject @@ -211,11 +205,9 @@ func flattenLightsailInstancePortState(apiObject *lightsail.InstancePortState) m tfMap["to_port"] = aws.Int64Value(apiObject.ToPort) tfMap["protocol"] = aws.StringValue(apiObject.Protocol) - cidrs := apiObject.Cidrs - sort.Slice(cidrs, func(i, j int) bool { - return *cidrs[i] > *cidrs[j] - }) - tfMap["cidrs"] = aws.StringValueSlice(cidrs) + if v := apiObject.Cidrs; v != nil { + tfMap["cidrs"] = aws.StringValueSlice(v) + } return tfMap } From bfd07db496026cffdad263ce975a7d9bcb61cbe7 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Thu, 25 Mar 2021 10:31:45 -0400 Subject: [PATCH 5/8] tests/r/lightsail_instance_public_ports: Nested attr check --- ...ource_aws_lightsail_instance_public_ports_test.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/aws/resource_aws_lightsail_instance_public_ports_test.go b/aws/resource_aws_lightsail_instance_public_ports_test.go index 5907a45c526f..c49b810a6258 100644 --- a/aws/resource_aws_lightsail_instance_public_ports_test.go +++ b/aws/resource_aws_lightsail_instance_public_ports_test.go @@ -90,11 +90,13 @@ func TestAccAWSLightsailInstancePublicPorts_cidrs(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSLightsailInstancePublicPortsExists(resourceName), resource.TestCheckResourceAttr(resourceName, "port_info.#", "1"), - resource.TestCheckResourceAttr(resourceName, "port_info.0.protocol", "tcp"), - resource.TestCheckResourceAttr(resourceName, "port_info.0.from_port", "125"), - resource.TestCheckResourceAttr(resourceName, "port_info.0.to_port", "125"), - resource.TestCheckResourceAttr(resourceName, "port_info.0.cidrs.0", "192.168.1.0/24"), - resource.TestCheckResourceAttr(resourceName, "port_info.0.cidrs.1", "1.1.1.1/32"), + resource.TestCheckTypeSetElemNestedAttrs(resourceName, "port_info.*", map[string]string{ + "protocol": "tcp", + "from_port": "125", + "to_port": "125", + "cidrs.0": "1.1.1.1/32", + "cidrs.1": "192.168.1.0/24", + }), ), }, }, From 29afc7cd014136e4248185848f8be43616cc7470 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Thu, 25 Mar 2021 10:32:10 -0400 Subject: [PATCH 6/8] r/lightsail_instance_public_ports: Add changelog --- .changelog/14905.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/14905.txt diff --git a/.changelog/14905.txt b/.changelog/14905.txt new file mode 100644 index 000000000000..55bc3a980016 --- /dev/null +++ b/.changelog/14905.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_light_instance_public_ports: Add `cidrs` argument to `port_info` +``` \ No newline at end of file From 5fce8f950ffe1a0bcf2727264539b4955bc8934b Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Thu, 25 Mar 2021 10:35:00 -0400 Subject: [PATCH 7/8] docs/r/lightsail_instance_public_ports: Fix docs --- .../docs/r/lightsail_instance_public_ports.html.markdown | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/website/docs/r/lightsail_instance_public_ports.html.markdown b/website/docs/r/lightsail_instance_public_ports.html.markdown index c53f92b9e614..3e9b98ce0f6c 100644 --- a/website/docs/r/lightsail_instance_public_ports.html.markdown +++ b/website/docs/r/lightsail_instance_public_ports.html.markdown @@ -44,11 +44,16 @@ The following arguments are required: ### port_info -* `cidrs` - (Optional) List of CIDR blocks. +The following arguments are required: + * `from_port` - (Required) First port in a range of open ports on an instance. * `protocol` - (Required) IP protocol name. Valid values are `tcp`, `all`, `udp`, and `icmp`. * `to_port` - (Required) Last port in a range of open ports on an instance. +The following arguments are optional: + +* `cidrs` - (Optional) Set of CIDR blocks. + ## Attributes Reference In addition to all arguments above, the following attributes are exported: From 4d8517094d4e7e1760a6d233ef0278ff5f8ee70e Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Thu, 25 Mar 2021 11:04:56 -0400 Subject: [PATCH 8/8] tests/r/lightsail_instance_public_ports: Use set testing --- ...ws_lightsail_instance_public_ports_test.go | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/aws/resource_aws_lightsail_instance_public_ports_test.go b/aws/resource_aws_lightsail_instance_public_ports_test.go index c49b810a6258..ac152a39cba3 100644 --- a/aws/resource_aws_lightsail_instance_public_ports_test.go +++ b/aws/resource_aws_lightsail_instance_public_ports_test.go @@ -31,9 +31,11 @@ func TestAccAWSLightsailInstancePublicPorts_basic(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSLightsailInstancePublicPortsExists(resourceName), resource.TestCheckResourceAttr(resourceName, "port_info.#", "1"), - resource.TestCheckResourceAttr(resourceName, "port_info.0.protocol", "tcp"), - resource.TestCheckResourceAttr(resourceName, "port_info.0.from_port", "80"), - resource.TestCheckResourceAttr(resourceName, "port_info.0.to_port", "80"), + resource.TestCheckTypeSetElemNestedAttrs(resourceName, "port_info.*", map[string]string{ + "protocol": "tcp", + "from_port": "80", + "to_port": "80", + }), ), }, }, @@ -59,12 +61,16 @@ func TestAccAWSLightsailInstancePublicPorts_multiple(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSLightsailInstancePublicPortsExists(resourceName), resource.TestCheckResourceAttr(resourceName, "port_info.#", "2"), - resource.TestCheckResourceAttr(resourceName, "port_info.1.protocol", "tcp"), - resource.TestCheckResourceAttr(resourceName, "port_info.1.from_port", "80"), - resource.TestCheckResourceAttr(resourceName, "port_info.1.to_port", "80"), - resource.TestCheckResourceAttr(resourceName, "port_info.0.protocol", "tcp"), - resource.TestCheckResourceAttr(resourceName, "port_info.0.from_port", "443"), - resource.TestCheckResourceAttr(resourceName, "port_info.0.to_port", "443"), + resource.TestCheckTypeSetElemNestedAttrs(resourceName, "port_info.*", map[string]string{ + "protocol": "tcp", + "from_port": "80", + "to_port": "80", + }), + resource.TestCheckTypeSetElemNestedAttrs(resourceName, "port_info.*", map[string]string{ + "protocol": "tcp", + "from_port": "443", + "to_port": "443", + }), ), }, }, @@ -94,9 +100,10 @@ func TestAccAWSLightsailInstancePublicPorts_cidrs(t *testing.T) { "protocol": "tcp", "from_port": "125", "to_port": "125", - "cidrs.0": "1.1.1.1/32", - "cidrs.1": "192.168.1.0/24", + "cidrs.#": "2", }), + resource.TestCheckTypeSetElemAttr(resourceName, "port_info.*.cidrs.*", "1.1.1.1/32"), + resource.TestCheckTypeSetElemAttr(resourceName, "port_info.*.cidrs.*", "192.168.1.0/24"), ), }, },