forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created resource files. Copied test file from another resource
- Loading branch information
1 parent
20e33f1
commit 49408fa
Showing
2 changed files
with
223 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceAwsSubnetNaclAssociation() *schema.Resource { | ||
//lintignore:R011 | ||
return &schema.Resource{ | ||
Create: resourceAwsSubnetNaclAssociationCreate, | ||
Read: resourceAwsSubnetNaclAssociationRead, | ||
Update: resourceAwsSubnetNaclAssociationUpdate, | ||
Delete: resourceAwsSubnetNaclAssociationDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(10 * time.Minute), | ||
Delete: schema.DefaultTimeout(20 * time.Minute), | ||
}, | ||
|
||
SchemaVersion: 1, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"subnet_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: false, | ||
}, | ||
|
||
"network_acl_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: false, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsSubnetNaclAssociationCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
// find all current nacls with subnet id | ||
subnetId := d.Get("subnet_id").(string) | ||
existingAssociation := findNetworkAclAssociation(subnetId, conn) | ||
|
||
log.Printf(existingAssociation.(string)) | ||
createOpts := &ec2.ReplaceNetworkAclAssociationInput{ | ||
AssociationId: aws.String(existingAssociation.NetworkAclAssociationId.(string)), | ||
NetworkAclId: aws.String(d.Get("network_acl_id").(string)), | ||
} | ||
|
||
var err error | ||
resp, err := conn.ReplaceNetworkAclAssociation(createOpts) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error replacing subnet network acl association: %w", err) | ||
} | ||
|
||
// Get the ID and store it | ||
associationId := aws.StringValue(resp.NewAssociationId) | ||
d.SetId(associationId.(string)) | ||
log.Printf("[INFO] New Association ID: %s", associationId) | ||
|
||
_, err = stateConf.WaitForState() | ||
|
||
if err != nil { | ||
return fmt.Errorf("error waiting for subnet (%s) to become ready: %w", d.Id(), err) | ||
} | ||
|
||
return resourceAwsSubnetNaclAssociationRead(d, meta) | ||
} | ||
|
||
func resourceAwsSubnetNaclAssociationRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
subnetId := d.Get("subnet_id").(string) | ||
existingAssociation, err := findNetworkAclAssociation(subnetId, conn) | ||
|
||
if err != nil { | ||
if isAWSErr(err, "InvalidNetworkAclSubnetAssociation.NotFound", "") { | ||
log.Printf("[WARN] Network Acl Association for Subnet Id (%s) not found, removing from state", d.Get("network_acl_id"), d.Get("subnet_id")) | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
if d.Get("subnet_id") == aws.StringValue(existingAssociation.SubnetId) { | ||
d.SetId(aws.StringValue(existingAssociation.NetworkAclAssociationId)) | ||
d.Set("network_acl_id", aws.StringValue(existingAssociation.NetworkAclId)) | ||
return nil | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsSubnetNaclAssociationUpdate(d *schema.ResourceData, meta interface{}) error { | ||
return resourceAwsSubnetNaclAssociationCreate(d, meta) | ||
} | ||
|
||
func resourceAwsSubnetNaclAssociationDelete(d *schema.ResourceData, meta interface{}) error { | ||
log.Printf("[WARN] Cannot destroy Network ACL Association. Terraform will remove this resource from the state file, however resources may remain.") | ||
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,111 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccAWSNetworkInterfaceAttachment_basic(t *testing.T) { | ||
var conf ec2.NetworkInterface | ||
rInt := acctest.RandInt() | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
IDRefreshName: "aws_network_interface.bar", | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSENIDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSNetworkInterfaceAttachmentConfig_basic(rInt), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSENIExists("aws_network_interface.bar", &conf), | ||
resource.TestCheckResourceAttr( | ||
"aws_network_interface_attachment.test", "device_index", "1"), | ||
resource.TestCheckResourceAttrSet( | ||
"aws_network_interface_attachment.test", "instance_id"), | ||
resource.TestCheckResourceAttrSet( | ||
"aws_network_interface_attachment.test", "network_interface_id"), | ||
resource.TestCheckResourceAttrSet( | ||
"aws_network_interface_attachment.test", "attachment_id"), | ||
resource.TestCheckResourceAttrSet( | ||
"aws_network_interface_attachment.test", "status"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccAWSNetworkInterfaceAttachmentConfig_basic(rInt int) string { | ||
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` | ||
resource "aws_vpc" "foo" { | ||
cidr_block = "172.16.0.0/16" | ||
tags = { | ||
Name = "terraform-testacc-network-iface-attachment-basic" | ||
} | ||
} | ||
data "aws_availability_zones" "available" { | ||
state = "available" | ||
filter { | ||
name = "opt-in-status" | ||
values = ["opt-in-not-required"] | ||
} | ||
} | ||
resource "aws_subnet" "foo" { | ||
vpc_id = aws_vpc.foo.id | ||
cidr_block = "172.16.10.0/24" | ||
availability_zone = data.aws_availability_zones.available.names[0] | ||
tags = { | ||
Name = "tf-acc-network-iface-attachment-basic" | ||
} | ||
} | ||
resource "aws_security_group" "foo" { | ||
vpc_id = aws_vpc.foo.id | ||
description = "foo" | ||
name = "foo-%d" | ||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "tcp" | ||
cidr_blocks = ["10.0.0.0/16"] | ||
} | ||
} | ||
resource "aws_network_interface" "bar" { | ||
subnet_id = aws_subnet.foo.id | ||
private_ips = ["172.16.10.100"] | ||
security_groups = [aws_security_group.foo.id] | ||
description = "Managed by Terraform" | ||
tags = { | ||
Name = "bar_interface" | ||
} | ||
} | ||
resource "aws_instance" "foo" { | ||
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id | ||
instance_type = "t2.micro" | ||
subnet_id = aws_subnet.foo.id | ||
tags = { | ||
Name = "foo-%d" | ||
} | ||
} | ||
resource "aws_network_interface_attachment" "test" { | ||
device_index = 1 | ||
instance_id = aws_instance.foo.id | ||
network_interface_id = aws_network_interface.bar.id | ||
} | ||
`, rInt, rInt) | ||
} |