Skip to content

Commit

Permalink
Merge pull request #14905 from kurtmc/feature/lightsail-public-ports
Browse files Browse the repository at this point in the history
Add cidrs attribute to aws_lightsail_instance_public_ports resource
  • Loading branch information
YakDriver authored Mar 25, 2021
2 parents dfc6bd9 + 4d85170 commit 69e0f43
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .changelog/14905.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_light_instance_public_ports: Add `cidrs` argument to `port_info`
```
17 changes: 17 additions & 0 deletions aws/resource_aws_lightsail_instance_public_ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func resourceAwsLightsailInstancePublicPorts() *schema.Resource {
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cidrs": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateCIDRNetworkAddress,
},
},
"from_port": {
Type: schema.TypeInt,
Required: true,
Expand Down Expand Up @@ -152,6 +161,10 @@ func expandLightsailPortInfo(tfMap map[string]interface{}) *lightsail.PortInfo {
Protocol: aws.String(tfMap["protocol"].(string)),
}

if v, ok := tfMap["cidrs"].(*schema.Set); ok && v.Len() > 0 {
apiObject.Cidrs = expandStringSet(v)
}

return apiObject
}

Expand Down Expand Up @@ -192,6 +205,10 @@ func flattenLightsailInstancePortState(apiObject *lightsail.InstancePortState) m
tfMap["to_port"] = aws.Int64Value(apiObject.ToPort)
tfMap["protocol"] = aws.StringValue(apiObject.Protocol)

if v := apiObject.Cidrs; v != nil {
tfMap["cidrs"] = aws.StringValueSlice(v)
}

return tfMap
}

Expand Down
88 changes: 79 additions & 9 deletions aws/resource_aws_lightsail_instance_public_ports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}),
),
},
},
Expand All @@ -59,12 +61,49 @@ 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",
}),
),
},
},
})
}

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.TestCheckTypeSetElemNestedAttrs(resourceName, "port_info.*", map[string]string{
"protocol": "tcp",
"from_port": "125",
"to_port": "125",
"cidrs.#": "2",
}),
resource.TestCheckTypeSetElemAttr(resourceName, "port_info.*.cidrs.*", "1.1.1.1/32"),
resource.TestCheckTypeSetElemAttr(resourceName, "port_info.*.cidrs.*", "192.168.1.0/24"),
),
},
},
Expand Down Expand Up @@ -189,3 +228,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)
}
6 changes: 6 additions & 0 deletions website/docs/r/lightsail_instance_public_ports.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ The following arguments are required:

### port_info

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:
Expand Down

0 comments on commit 69e0f43

Please sign in to comment.