Skip to content

Commit

Permalink
Add cidrs attribute to aws_lightsail_instance_public_ports resource
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmc committed Mar 25, 2021
1 parent c3f18ab commit 690280e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
25 changes: 25 additions & 0 deletions aws/resource_aws_lightsail_instance_public_ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"log"
"sort"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lightsail"
Expand Down Expand Up @@ -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,
},
},
},
},
},
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
61 changes: 61 additions & 0 deletions aws/resource_aws_lightsail_instance_public_ports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 690280e

Please sign in to comment.