-
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
Closed
takahiro9
wants to merge
10
commits into
hashicorp:master
from
takahiro9:add_resource_aws_network_acl_association
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f467708
add network acl association resource
e4ee9fb
add document
657a1b0
add html document
f749c98
Corresponded to point out
01ed6d2
fix and add resource delete function
64014fb
Fixed based on indication
de507c5
Merge branch 'master' into add_resource_aws_network_acl_association
d2529da
update test
d8bae10
update html
14cc390
import not use fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
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": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"network_acl_id": { | ||
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) | ||
|
||
association, errAssociation := findNetworkAclAssociation(subnetId, conn) | ||
if errAssociation != nil { | ||
return fmt.Errorf("Failed to find association for subnet %s: %s", subnetId, errAssociation) | ||
} | ||
|
||
associationOpts := ec2.ReplaceNetworkAclAssociationInput{ | ||
AssociationId: association.NetworkAclAssociationId, | ||
NetworkAclId: aws.String(naclId), | ||
} | ||
|
||
log.Printf("[DEBUG] Creating Network ACL association: %#v", associationOpts) | ||
|
||
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 | ||
} | ||
|
||
return resourceAwsNetworkAclAssociationRead(d, meta) | ||
} | ||
|
||
func resourceAwsNetworkAclAssociationRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
// Inspect that the association exists | ||
subnetId := d.Get("subnet_id").(string) | ||
_, errAssociation := findNetworkAclAssociation(subnetId, conn) | ||
if errAssociation != nil { | ||
log.Printf("[WARN] Association for subnet %s was not found, removing from state", subnetId) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
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) | ||
|
||
association, errAssociation := findNetworkAclAssociation(subnetId, conn) | ||
if errAssociation != nil { | ||
return fmt.Errorf("Failed to find association for subnet %s: %s", subnetId, errAssociation) | ||
} | ||
|
||
associationOpts := ec2.ReplaceNetworkAclAssociationInput{ | ||
AssociationId: association.NetworkAclAssociationId, | ||
NetworkAclId: aws.String(naclId), | ||
} | ||
|
||
_, err := conn.ReplaceNetworkAclAssociation(&associationOpts) | ||
|
||
log.Printf("[DEBUG] Updating Network ACL association: %#v", associationOpts) | ||
|
||
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 | ||
} | ||
|
||
return resourceAwsNetworkAclAssociationRead(d, meta) | ||
} | ||
|
||
func resourceAwsNetworkAclAssociationDelete(d *schema.ResourceData, meta interface{}) error { | ||
|
||
conn := meta.(*AWSClient).ec2conn | ||
|
||
subnetId := d.Get("subnet_id").(string) | ||
|
||
association, errAssociation := findNetworkAclAssociation(subnetId, conn) | ||
if errAssociation != nil { | ||
return fmt.Errorf("Failed to find association for subnet %s: %s", subnetId, errAssociation) | ||
} | ||
|
||
defaultAcl, err := getDefaultNetworkAcl(d.Get("vpc_id").(string), conn) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Failed to get networkAcl : %s", err) | ||
} | ||
|
||
associationOpts := ec2.ReplaceNetworkAclAssociationInput{ | ||
AssociationId: association.NetworkAclAssociationId, | ||
NetworkAclId: defaultAcl.NetworkAclId, | ||
} | ||
|
||
log.Printf("[DEBUG] Replacing Network ACL association: %#v", associationOpts) | ||
|
||
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 | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAWSNetworkAclAssociation(t *testing.T) { | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
IDRefreshName: "aws_network_acl.acl_a", | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSNetworkAclDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSNetworkAclAssoc, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
testAccCheckSubnetIsAssociatedWithAcl("aws_network_acl.acl_a", "aws_subnet.sunet_a"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
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}" | ||
} | ||
}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_network_acl_association" | ||
sidebar_current: "docs-aws-resource-network-acl-association" | ||
description: |- | ||
Provides an network ACL association resource. | ||
--- | ||
|
||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 :)