Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cidrs attribute to aws_lightsail_instance_public_ports resource #14905

Merged
merged 8 commits into from
Mar 25, 2021
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 @@ -33,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,
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 @@ -44,6 +44,7 @@ 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.
Expand Down