Merge pull request #1398 from hashicorp/f-aws-upstream-subnet
provider/aws: Convert AWS Subnet to mainstream aws-sdk-go
This commit is contained in:
commit
281825db76
|
@ -5,8 +5,8 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/aws-sdk-go/aws"
|
"github.com/awslabs/aws-sdk-go/aws"
|
||||||
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
)
|
)
|
||||||
|
@ -51,15 +51,15 @@ func resourceAwsSubnet() *schema.Resource {
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceAwsSubnetCreate(d *schema.ResourceData, meta interface{}) error {
|
func resourceAwsSubnetCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
ec2conn := meta.(*AWSClient).ec2conn
|
conn := meta.(*AWSClient).ec2SDKconn
|
||||||
|
|
||||||
createOpts := &ec2.CreateSubnetRequest{
|
createOpts := &ec2.CreateSubnetInput{
|
||||||
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
|
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
|
||||||
CIDRBlock: aws.String(d.Get("cidr_block").(string)),
|
CIDRBlock: aws.String(d.Get("cidr_block").(string)),
|
||||||
VPCID: aws.String(d.Get("vpc_id").(string)),
|
VPCID: aws.String(d.Get("vpc_id").(string)),
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := ec2conn.CreateSubnet(createOpts)
|
resp, err := conn.CreateSubnet(createOpts)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error creating subnet: %s", err)
|
return fmt.Errorf("Error creating subnet: %s", err)
|
||||||
|
@ -75,7 +75,7 @@ func resourceAwsSubnetCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
stateConf := &resource.StateChangeConf{
|
stateConf := &resource.StateChangeConf{
|
||||||
Pending: []string{"pending"},
|
Pending: []string{"pending"},
|
||||||
Target: "available",
|
Target: "available",
|
||||||
Refresh: SubnetStateRefreshFunc(ec2conn, *subnet.SubnetID),
|
Refresh: SubnetStateRefreshFunc(conn, *subnet.SubnetID),
|
||||||
Timeout: 10 * time.Minute,
|
Timeout: 10 * time.Minute,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,10 +91,10 @@ func resourceAwsSubnetCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error {
|
func resourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
ec2conn := meta.(*AWSClient).ec2conn
|
conn := meta.(*AWSClient).ec2SDKconn
|
||||||
|
|
||||||
resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsRequest{
|
resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsInput{
|
||||||
SubnetIDs: []string{d.Id()},
|
SubnetIDs: []*string{aws.String(d.Id())},
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -109,39 +109,39 @@ func resourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
subnet := &resp.Subnets[0]
|
subnet := resp.Subnets[0]
|
||||||
|
|
||||||
d.Set("vpc_id", subnet.VPCID)
|
d.Set("vpc_id", subnet.VPCID)
|
||||||
d.Set("availability_zone", subnet.AvailabilityZone)
|
d.Set("availability_zone", subnet.AvailabilityZone)
|
||||||
d.Set("cidr_block", subnet.CIDRBlock)
|
d.Set("cidr_block", subnet.CIDRBlock)
|
||||||
d.Set("map_public_ip_on_launch", subnet.MapPublicIPOnLaunch)
|
d.Set("map_public_ip_on_launch", subnet.MapPublicIPOnLaunch)
|
||||||
d.Set("tags", tagsToMap(subnet.Tags))
|
d.Set("tags", tagsToMapSDK(subnet.Tags))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceAwsSubnetUpdate(d *schema.ResourceData, meta interface{}) error {
|
func resourceAwsSubnetUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||||
ec2conn := meta.(*AWSClient).ec2conn
|
conn := meta.(*AWSClient).ec2SDKconn
|
||||||
|
|
||||||
d.Partial(true)
|
d.Partial(true)
|
||||||
|
|
||||||
if err := setTags(ec2conn, d); err != nil {
|
if err := setTagsSDK(conn, d); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
d.SetPartial("tags")
|
d.SetPartial("tags")
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.HasChange("map_public_ip_on_launch") {
|
if d.HasChange("map_public_ip_on_launch") {
|
||||||
modifyOpts := &ec2.ModifySubnetAttributeRequest{
|
modifyOpts := &ec2.ModifySubnetAttributeInput{
|
||||||
SubnetID: aws.String(d.Id()),
|
SubnetID: aws.String(d.Id()),
|
||||||
MapPublicIPOnLaunch: &ec2.AttributeBooleanValue{
|
MapPublicIPOnLaunch: &ec2.AttributeBooleanValue{
|
||||||
aws.Boolean(d.Get("map_public_ip_on_launch").(bool)),
|
Value: aws.Boolean(d.Get("map_public_ip_on_launch").(bool)),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("[DEBUG] Subnet modify attributes: %#v", modifyOpts)
|
log.Printf("[DEBUG] Subnet modify attributes: %#v", modifyOpts)
|
||||||
|
|
||||||
err := ec2conn.ModifySubnetAttribute(modifyOpts)
|
_, err := conn.ModifySubnetAttribute(modifyOpts)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -156,10 +156,10 @@ func resourceAwsSubnetUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceAwsSubnetDelete(d *schema.ResourceData, meta interface{}) error {
|
func resourceAwsSubnetDelete(d *schema.ResourceData, meta interface{}) error {
|
||||||
ec2conn := meta.(*AWSClient).ec2conn
|
conn := meta.(*AWSClient).ec2SDKconn
|
||||||
|
|
||||||
log.Printf("[INFO] Deleting subnet: %s", d.Id())
|
log.Printf("[INFO] Deleting subnet: %s", d.Id())
|
||||||
req := &ec2.DeleteSubnetRequest{
|
req := &ec2.DeleteSubnetInput{
|
||||||
SubnetID: aws.String(d.Id()),
|
SubnetID: aws.String(d.Id()),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,7 +169,7 @@ func resourceAwsSubnetDelete(d *schema.ResourceData, meta interface{}) error {
|
||||||
Timeout: 5 * time.Minute,
|
Timeout: 5 * time.Minute,
|
||||||
MinTimeout: 1 * time.Second,
|
MinTimeout: 1 * time.Second,
|
||||||
Refresh: func() (interface{}, string, error) {
|
Refresh: func() (interface{}, string, error) {
|
||||||
err := ec2conn.DeleteSubnet(req)
|
_, err := conn.DeleteSubnet(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if apiErr, ok := err.(aws.APIError); ok {
|
if apiErr, ok := err.(aws.APIError); ok {
|
||||||
if apiErr.Code == "DependencyViolation" {
|
if apiErr.Code == "DependencyViolation" {
|
||||||
|
@ -200,8 +200,8 @@ func resourceAwsSubnetDelete(d *schema.ResourceData, meta interface{}) error {
|
||||||
// SubnetStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch a Subnet.
|
// SubnetStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch a Subnet.
|
||||||
func SubnetStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
func SubnetStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
||||||
return func() (interface{}, string, error) {
|
return func() (interface{}, string, error) {
|
||||||
resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsRequest{
|
resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsInput{
|
||||||
SubnetIDs: []string{id},
|
SubnetIDs: []*string{aws.String(id)},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidSubnetID.NotFound" {
|
if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidSubnetID.NotFound" {
|
||||||
|
@ -218,7 +218,7 @@ func SubnetStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc
|
||||||
return nil, "", nil
|
return nil, "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
subnet := &resp.Subnets[0]
|
subnet := resp.Subnets[0]
|
||||||
return subnet, *subnet.State, nil
|
return subnet, *subnet.State, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/aws-sdk-go/aws"
|
"github.com/awslabs/aws-sdk-go/aws"
|
||||||
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
|
@ -43,7 +43,7 @@ func TestAccAWSSubnet(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAccCheckSubnetDestroy(s *terraform.State) error {
|
func testAccCheckSubnetDestroy(s *terraform.State) error {
|
||||||
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
conn := testAccProvider.Meta().(*AWSClient).ec2SDKconn
|
||||||
|
|
||||||
for _, rs := range s.RootModule().Resources {
|
for _, rs := range s.RootModule().Resources {
|
||||||
if rs.Type != "aws_subnet" {
|
if rs.Type != "aws_subnet" {
|
||||||
|
@ -51,8 +51,8 @@ func testAccCheckSubnetDestroy(s *terraform.State) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to find the resource
|
// Try to find the resource
|
||||||
resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsRequest{
|
resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsInput{
|
||||||
SubnetIDs: []string{rs.Primary.ID},
|
SubnetIDs: []*string{aws.String(rs.Primary.ID)},
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if len(resp.Subnets) > 0 {
|
if len(resp.Subnets) > 0 {
|
||||||
|
@ -86,9 +86,9 @@ func testAccCheckSubnetExists(n string, v *ec2.Subnet) resource.TestCheckFunc {
|
||||||
return fmt.Errorf("No ID is set")
|
return fmt.Errorf("No ID is set")
|
||||||
}
|
}
|
||||||
|
|
||||||
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
conn := testAccProvider.Meta().(*AWSClient).ec2SDKconn
|
||||||
resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsRequest{
|
resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsInput{
|
||||||
SubnetIDs: []string{rs.Primary.ID},
|
SubnetIDs: []*string{aws.String(rs.Primary.ID)},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -97,7 +97,7 @@ func testAccCheckSubnetExists(n string, v *ec2.Subnet) resource.TestCheckFunc {
|
||||||
return fmt.Errorf("Subnet not found")
|
return fmt.Errorf("Subnet not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
*v = resp.Subnets[0]
|
*v = *resp.Subnets[0]
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -112,5 +112,8 @@ resource "aws_subnet" "foo" {
|
||||||
cidr_block = "10.1.1.0/24"
|
cidr_block = "10.1.1.0/24"
|
||||||
vpc_id = "${aws_vpc.foo.id}"
|
vpc_id = "${aws_vpc.foo.id}"
|
||||||
map_public_ip_on_launch = true
|
map_public_ip_on_launch = true
|
||||||
|
tags {
|
||||||
|
Name = "tf-subnet-acc-test"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
|
@ -0,0 +1,99 @@
|
||||||
|
package aws
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/awslabs/aws-sdk-go/aws"
|
||||||
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
||||||
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
// tagsSchema returns the schema to use for tags.
|
||||||
|
//
|
||||||
|
// func tagsSchema() *schema.Schema {
|
||||||
|
// return &schema.Schema{
|
||||||
|
// Type: schema.TypeMap,
|
||||||
|
// Optional: true,
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// setTags is a helper to set the tags for a resource. It expects the
|
||||||
|
// tags field to be named "tags"
|
||||||
|
func setTagsSDK(conn *ec2.EC2, d *schema.ResourceData) error {
|
||||||
|
if d.HasChange("tags") {
|
||||||
|
oraw, nraw := d.GetChange("tags")
|
||||||
|
o := oraw.(map[string]interface{})
|
||||||
|
n := nraw.(map[string]interface{})
|
||||||
|
create, remove := diffTagsSDK(tagsFromMapSDK(o), tagsFromMapSDK(n))
|
||||||
|
|
||||||
|
// Set tags
|
||||||
|
if len(remove) > 0 {
|
||||||
|
log.Printf("[DEBUG] Removing tags: %#v", remove)
|
||||||
|
_, err := conn.DeleteTags(&ec2.DeleteTagsInput{
|
||||||
|
Resources: []*string{aws.String(d.Id())},
|
||||||
|
Tags: remove,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(create) > 0 {
|
||||||
|
log.Printf("[DEBUG] Creating tags: %#v", create)
|
||||||
|
_, err := conn.CreateTags(&ec2.CreateTagsInput{
|
||||||
|
Resources: []*string{aws.String(d.Id())},
|
||||||
|
Tags: create,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// diffTags takes our tags locally and the ones remotely and returns
|
||||||
|
// the set of tags that must be created, and the set of tags that must
|
||||||
|
// be destroyed.
|
||||||
|
func diffTagsSDK(oldTags, newTags []*ec2.Tag) ([]*ec2.Tag, []*ec2.Tag) {
|
||||||
|
// First, we're creating everything we have
|
||||||
|
create := make(map[string]interface{})
|
||||||
|
for _, t := range newTags {
|
||||||
|
create[*t.Key] = *t.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build the list of what to remove
|
||||||
|
var remove []*ec2.Tag
|
||||||
|
for _, t := range oldTags {
|
||||||
|
old, ok := create[*t.Key]
|
||||||
|
if !ok || old != *t.Value {
|
||||||
|
// Delete it!
|
||||||
|
remove = append(remove, t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tagsFromMapSDK(create), remove
|
||||||
|
}
|
||||||
|
|
||||||
|
// tagsFromMap returns the tags for the given map of data.
|
||||||
|
func tagsFromMapSDK(m map[string]interface{}) []*ec2.Tag {
|
||||||
|
result := make([]*ec2.Tag, 0, len(m))
|
||||||
|
for k, v := range m {
|
||||||
|
result = append(result, &ec2.Tag{
|
||||||
|
Key: aws.String(k),
|
||||||
|
Value: aws.String(v.(string)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// tagsToMap turns the list of tags into a map.
|
||||||
|
func tagsToMapSDK(ts []*ec2.Tag) map[string]string {
|
||||||
|
result := make(map[string]string)
|
||||||
|
for _, t := range ts {
|
||||||
|
result[*t.Key] = *t.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
|
@ -0,0 +1,85 @@
|
||||||
|
package aws
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
||||||
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
|
"github.com/hashicorp/terraform/terraform"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDiffTagsSDK(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Old, New map[string]interface{}
|
||||||
|
Create, Remove map[string]string
|
||||||
|
}{
|
||||||
|
// Basic add/remove
|
||||||
|
{
|
||||||
|
Old: map[string]interface{}{
|
||||||
|
"foo": "bar",
|
||||||
|
},
|
||||||
|
New: map[string]interface{}{
|
||||||
|
"bar": "baz",
|
||||||
|
},
|
||||||
|
Create: map[string]string{
|
||||||
|
"bar": "baz",
|
||||||
|
},
|
||||||
|
Remove: map[string]string{
|
||||||
|
"foo": "bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// Modify
|
||||||
|
{
|
||||||
|
Old: map[string]interface{}{
|
||||||
|
"foo": "bar",
|
||||||
|
},
|
||||||
|
New: map[string]interface{}{
|
||||||
|
"foo": "baz",
|
||||||
|
},
|
||||||
|
Create: map[string]string{
|
||||||
|
"foo": "baz",
|
||||||
|
},
|
||||||
|
Remove: map[string]string{
|
||||||
|
"foo": "bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tc := range cases {
|
||||||
|
c, r := diffTagsSDK(tagsFromMapSDK(tc.Old), tagsFromMapSDK(tc.New))
|
||||||
|
cm := tagsToMapSDK(c)
|
||||||
|
rm := tagsToMapSDK(r)
|
||||||
|
if !reflect.DeepEqual(cm, tc.Create) {
|
||||||
|
t.Fatalf("%d: bad create: %#v", i, cm)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(rm, tc.Remove) {
|
||||||
|
t.Fatalf("%d: bad remove: %#v", i, rm)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// testAccCheckTags can be used to check the tags on a resource.
|
||||||
|
func testAccCheckTagsSDK(
|
||||||
|
ts []*ec2.Tag, key string, value string) resource.TestCheckFunc {
|
||||||
|
return func(s *terraform.State) error {
|
||||||
|
m := tagsToMapSDK(ts)
|
||||||
|
v, ok := m[key]
|
||||||
|
if value != "" && !ok {
|
||||||
|
return fmt.Errorf("Missing tag: %s", key)
|
||||||
|
} else if value == "" && ok {
|
||||||
|
return fmt.Errorf("Extra tag: %s", key)
|
||||||
|
}
|
||||||
|
if value == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if v != value {
|
||||||
|
return fmt.Errorf("%s: bad value: %s", key, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue