-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
New Resource: aws_network_acl_association #1034
Changes from 3 commits
f467708
e4ee9fb
657a1b0
f749c98
01ed6d2
64014fb
de507c5
d2529da
d8bae10
14cc390
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsNetworkAclAssociation() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsNetworkAclAssociationCreate, | ||
Read: resourceAwsNetworkAclAssociationRead, | ||
Update: resourceAwsNetworkAclAssociationUpdate, | ||
Delete: resourceAwsNetworkAclAssociationDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"subnet_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"network_acl_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsNetworkAclAssociationCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
naclId := d.Get("network_acl_id").(string) | ||
subnetId := d.Get("subnet_id").(string) | ||
|
||
log.Printf( | ||
"[INFO] Creating network acl association: %s => %s", | ||
subnetId, | ||
naclId) | ||
|
||
association, err_association := findNetworkAclAssociation(subnetId, conn) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please prefer camelCase-d variables instead of snake-case-d ones, like |
||
if err_association != nil { | ||
return fmt.Errorf("Failed to create acl %s with nacl %s: %s", d.Id(), naclId, err_association) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This message is not really accurate. What do you think of rewording it to:
|
||
} | ||
|
||
associationOpts := ec2.ReplaceNetworkAclAssociationInput{ | ||
AssociationId: association.NetworkAclAssociationId, | ||
NetworkAclId: aws.String(naclId), | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you move the log instruction from line 42 here, put it all on one line, and perhaps change it to the below?:
This would provide all of the parameters for the function, so that we don't need to Printf them :) |
||
var err error | ||
err = resource.Retry(5*time.Minute, func() *resource.RetryError { | ||
_, err = conn.ReplaceNetworkAclAssociation(&associationOpts) | ||
if err != nil { | ||
if awsErr, ok := err.(awserr.Error); ok { | ||
if awsErr != nil { | ||
return resource.RetryableError(awsErr) | ||
} | ||
} | ||
return resource.NonRetryableError(err) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Set the ID and return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment seems a bit obvious, so would prefer to remove it if you don't mind |
||
d.SetId(naclId) | ||
log.Printf("[INFO] Association ID: %s", d.Id()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you can retrieve this value, this does not seem to be needed. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest... Looking at comments and thinking... naclid can get by d.Get("network_acl_id").(string) I wrote things like 75 lines, |
||
|
||
return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should return |
||
} | ||
|
||
func resourceAwsNetworkAclAssociationRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
// Inspect that the association exists | ||
subnetId := d.Get("subnet_id").(string) | ||
_, err_association := findNetworkAclAssociation(subnetId, conn) | ||
if err_association != nil { | ||
return fmt.Errorf("Failed to read acl %s with subnet %s: %s", d.Id(), subnetId, err_association) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we fail to read the network acl, set the ID to |
||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsNetworkAclAssociationUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
naclId := d.Get("network_acl_id").(string) | ||
subnetId := d.Get("subnet_id").(string) | ||
|
||
log.Printf( | ||
"[INFO] Creating network acl association: %s => %s", | ||
subnetId, | ||
naclId) | ||
|
||
association, err_association := findNetworkAclAssociation(subnetId, conn) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you rename the var |
||
if err_association != nil { | ||
return fmt.Errorf("Failed to update acl %s with subnet %s: %s", d.Id(), naclId, err_association) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This message is not really accurate. What do you think of rewording it to:
At this point, we are only trying to find the association, rather than updating it. What do you think? |
||
} | ||
|
||
req := &ec2.ReplaceNetworkAclAssociationInput{ | ||
AssociationId: association.NetworkAclAssociationId, | ||
NetworkAclId: aws.String(naclId), | ||
} | ||
resp, err := conn.ReplaceNetworkAclAssociation(req) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you move the log instruction from line 42 here, put it all on one line, and perhaps change it to the below?:
This would provide all of the parameters for the function, so that we don't need to Printf them all manually :) |
||
|
||
if err != nil { | ||
ec2err, ok := err.(awserr.Error) | ||
if ok && ec2err.Code() == "InvalidAssociationID.NotFound" { | ||
// Not found, so just create a new one | ||
return resourceAwsNetworkAclAssociationCreate(d, meta) | ||
} | ||
|
||
return err | ||
} | ||
|
||
// Update the ID | ||
d.SetId(*resp.NewAssociationId) | ||
log.Printf("[INFO] Association ID: %s", d.Id()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not seem useful, could you remove it along with the comment line 128? |
||
|
||
return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should return |
||
} | ||
|
||
func resourceAwsNetworkAclAssociationDelete(d *schema.ResourceData, meta interface{}) error { | ||
|
||
log.Printf("[INFO] Do nothing on network acl associatioØ destroy phase: %s", d.Id()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this method should really do something here, as the association would still exist in the AWS-land. Also, when you need to destroy something in the TF-land, set the ID to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for comment |
||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSNetworkAclAssociation(t *testing.T) { | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
IDRefreshName: "aws_network_acl.bar", | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSNetworkAclDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSNetworkAclAssoc, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
testCheckAwsRMNetworkAclAssocExists("aws_network_acl_association.test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testCheckAwsRMNetworkAclAssocExists(name string) resource.TestCheckFunc { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method should check that the association exists, by requesting AWS. |
||
return func(s *terraform.State) error { | ||
|
||
_, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccAWSNetworkAclAssoc = ` | ||
resource "aws_vpc" "testespvpc" { | ||
cidr_block = "10.1.0.0/16" | ||
tags { | ||
Name = "testAccAWSNetworkAclEsp" | ||
} | ||
} | ||
|
||
resource "aws_network_acl" "acl_a" { | ||
vpc_id = "${aws_vpc.testespvpc.id}" | ||
|
||
tags { | ||
Name = "terraform test" | ||
} | ||
} | ||
|
||
resource "aws_subnet" "sunet_a" { | ||
vpc_id = "${aws_vpc.testespvpc.id}" | ||
cidr_block = "10.0.33.0/24" | ||
tags { | ||
Name = "terraform test" | ||
} | ||
} | ||
|
||
resource "aws_network_acl_association" "test" { | ||
network_acl_id = "${aws_network_acl.acl_a.id}" | ||
subnet_id = "${aws_subnet.subnet_a.id}" | ||
} | ||
}` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1405,6 +1405,10 @@ | |
<a href="/docs/providers/aws/r/network_acl.html">aws_network_acl</a> | ||
</li> | ||
|
||
<li<%= sidebar_current("docs-aws-resource-network-acl-assoc") %>> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep the full |
||
<a href="/docs/providers/aws/r/network_acl_assoc.html">aws_network_acl_association</a> | ||
</li> | ||
|
||
<li<%= sidebar_current("docs-aws-resource-network-acl-rule") %>> | ||
<a href="/docs/providers/aws/r/network_acl_rule.html">aws_network_acl_rule</a> | ||
</li> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_network_acl_association" | ||
sidebar_current: "docs-aws-resource-network-acl-association" | ||
description: |- | ||
Provides an network ACL association resource. | ||
--- | ||
|
||
# aws\_network\_acl\_association | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. antislashes are not needed anymore, it is safe to remove them here |
||
|
||
Provides an network ACL association resource. You might set up network ACLs associate to your subnet. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "aws_network_acl_association" "main" { | ||
network_acl_id = "${aws_network_acl.main.id}" | ||
subnet_id = "${aws_subnet.main.id}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `network_acl_id` - (Required) The ID of the network acl . | ||
* `subnet_id` - (Required) The ID of the associated Subnet. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `id` - The ID of the network ACL | ||
* `network_acl_id` - The ID of the network ACL | ||
* `subnet_id` - The ID of the subnet id | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small nitpick but this last line is not needed here 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Go 1.7+, redundant type declaration in composite literal can be safely removed, so:
can become:
"subnet_id": {