Replace tags files with tags_sdk, rename, and update
This commit is contained in:
parent
96c41de946
commit
57556bba75
|
@ -481,7 +481,7 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
d.Set("subnet_id", instance.SubnetID)
|
d.Set("subnet_id", instance.SubnetID)
|
||||||
}
|
}
|
||||||
d.Set("ebs_optimized", instance.EBSOptimized)
|
d.Set("ebs_optimized", instance.EBSOptimized)
|
||||||
d.Set("tags", tagsToMapSDK(instance.Tags))
|
d.Set("tags", tagsToMap(instance.Tags))
|
||||||
d.Set("tenancy", instance.Placement.Tenancy)
|
d.Set("tenancy", instance.Placement.Tenancy)
|
||||||
|
|
||||||
// Determine whether we're referring to security groups with
|
// Determine whether we're referring to security groups with
|
||||||
|
@ -579,7 +579,7 @@ func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||||
// TODO(mitchellh): wait for the attributes we modified to
|
// TODO(mitchellh): wait for the attributes we modified to
|
||||||
// persist the change...
|
// persist the change...
|
||||||
|
|
||||||
if err := setTagsSDK(ec2conn, d); err != nil {
|
if err := setTags(ec2conn, d); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
d.SetPartial("tags")
|
d.SetPartial("tags")
|
||||||
|
|
|
@ -219,9 +219,9 @@ func TestAccAWSInstance_tags(t *testing.T) {
|
||||||
Config: testAccCheckInstanceConfigTags,
|
Config: testAccCheckInstanceConfigTags,
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckInstanceExists("aws_instance.foo", &v),
|
testAccCheckInstanceExists("aws_instance.foo", &v),
|
||||||
testAccCheckTagsSDK(&v.Tags, "foo", "bar"),
|
testAccCheckTags(&v.Tags, "foo", "bar"),
|
||||||
// Guard against regression of https://github.com/hashicorp/terraform/issues/914
|
// Guard against regression of https://github.com/hashicorp/terraform/issues/914
|
||||||
testAccCheckTagsSDK(&v.Tags, "#", ""),
|
testAccCheckTags(&v.Tags, "#", ""),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -229,8 +229,8 @@ func TestAccAWSInstance_tags(t *testing.T) {
|
||||||
Config: testAccCheckInstanceConfigTagsUpdate,
|
Config: testAccCheckInstanceConfigTagsUpdate,
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckInstanceExists("aws_instance.foo", &v),
|
testAccCheckInstanceExists("aws_instance.foo", &v),
|
||||||
testAccCheckTagsSDK(&v.Tags, "foo", ""),
|
testAccCheckTags(&v.Tags, "foo", ""),
|
||||||
testAccCheckTagsSDK(&v.Tags, "bar", "baz"),
|
testAccCheckTags(&v.Tags, "bar", "baz"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -43,7 +43,7 @@ func resourceAwsInternetGatewayCreate(d *schema.ResourceData, meta interface{})
|
||||||
d.SetId(*ig.InternetGatewayID)
|
d.SetId(*ig.InternetGatewayID)
|
||||||
log.Printf("[INFO] InternetGateway ID: %s", d.Id())
|
log.Printf("[INFO] InternetGateway ID: %s", d.Id())
|
||||||
|
|
||||||
err = setTagsSDK(ec2conn, d)
|
err = setTags(ec2conn, d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ func resourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) er
|
||||||
d.Set("vpc_id", ig.Attachments[0].VPCID)
|
d.Set("vpc_id", ig.Attachments[0].VPCID)
|
||||||
}
|
}
|
||||||
|
|
||||||
d.Set("tags", tagsToMapSDK(ig.Tags))
|
d.Set("tags", tagsToMap(ig.Tags))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ func resourceAwsInternetGatewayUpdate(d *schema.ResourceData, meta interface{})
|
||||||
|
|
||||||
ec2conn := meta.(*AWSClient).awsEC2conn
|
ec2conn := meta.(*AWSClient).awsEC2conn
|
||||||
|
|
||||||
if err := setTagsSDK(ec2conn, d); err != nil {
|
if err := setTags(ec2conn, d); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -98,7 +98,7 @@ func TestAccInternetGateway_tags(t *testing.T) {
|
||||||
Config: testAccCheckInternetGatewayConfigTags,
|
Config: testAccCheckInternetGatewayConfigTags,
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
|
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
|
||||||
testAccCheckTagsSDK(&v.Tags, "foo", "bar"),
|
testAccCheckTags(&v.Tags, "foo", "bar"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -106,8 +106,8 @@ func TestAccInternetGateway_tags(t *testing.T) {
|
||||||
Config: testAccCheckInternetGatewayConfigTagsUpdate,
|
Config: testAccCheckInternetGatewayConfigTagsUpdate,
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
|
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
|
||||||
testAccCheckTagsSDK(&v.Tags, "foo", ""),
|
testAccCheckTags(&v.Tags, "foo", ""),
|
||||||
testAccCheckTagsSDK(&v.Tags, "bar", "baz"),
|
testAccCheckTags(&v.Tags, "bar", "baz"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -161,7 +161,7 @@ func resourceAwsNetworkAclRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
d.Set("vpc_id", networkAcl.VPCID)
|
d.Set("vpc_id", networkAcl.VPCID)
|
||||||
d.Set("ingress", ingressEntries)
|
d.Set("ingress", ingressEntries)
|
||||||
d.Set("egress", egressEntries)
|
d.Set("egress", egressEntries)
|
||||||
d.Set("tags", tagsToMapSDK(networkAcl.Tags))
|
d.Set("tags", tagsToMap(networkAcl.Tags))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -202,7 +202,7 @@ func resourceAwsNetworkAclUpdate(d *schema.ResourceData, meta interface{}) error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := setTagsSDK(ec2conn, d); err != nil {
|
if err := setTags(ec2conn, d); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
d.SetPartial("tags")
|
d.SetPartial("tags")
|
||||||
|
|
|
@ -151,7 +151,7 @@ func TestAccAWSNetworkAclsOnlyEgressRules(t *testing.T) {
|
||||||
Config: testAccAWSNetworkAclEgressConfig,
|
Config: testAccAWSNetworkAclEgressConfig,
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckAWSNetworkAclExists("aws_network_acl.bond", &networkAcl),
|
testAccCheckAWSNetworkAclExists("aws_network_acl.bond", &networkAcl),
|
||||||
testAccCheckTagsSDK(&networkAcl.Tags, "foo", "bar"),
|
testAccCheckTags(&networkAcl.Tags, "foo", "bar"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -146,7 +146,7 @@ func resourceAwsRouteTableRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
d.Set("route", route)
|
d.Set("route", route)
|
||||||
|
|
||||||
// Tags
|
// Tags
|
||||||
d.Set("tags", tagsToMapSDK(rt.Tags))
|
d.Set("tags", tagsToMap(rt.Tags))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -203,7 +203,7 @@ func resourceAwsRouteTableUpdate(d *schema.ResourceData, meta interface{}) error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := setTagsSDK(ec2conn, d); err != nil {
|
if err := setTags(ec2conn, d); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
d.SetPartial("tags")
|
d.SetPartial("tags")
|
||||||
|
|
|
@ -134,7 +134,7 @@ func TestAccAWSRouteTable_tags(t *testing.T) {
|
||||||
Config: testAccRouteTableConfigTags,
|
Config: testAccRouteTableConfigTags,
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckRouteTableExists("aws_route_table.foo", &route_table),
|
testAccCheckRouteTableExists("aws_route_table.foo", &route_table),
|
||||||
testAccCheckTagsSDK(&route_table.Tags, "foo", "bar"),
|
testAccCheckTags(&route_table.Tags, "foo", "bar"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -142,8 +142,8 @@ func TestAccAWSRouteTable_tags(t *testing.T) {
|
||||||
Config: testAccRouteTableConfigTagsUpdate,
|
Config: testAccRouteTableConfigTagsUpdate,
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckRouteTableExists("aws_route_table.foo", &route_table),
|
testAccCheckRouteTableExists("aws_route_table.foo", &route_table),
|
||||||
testAccCheckTagsSDK(&route_table.Tags, "foo", ""),
|
testAccCheckTags(&route_table.Tags, "foo", ""),
|
||||||
testAccCheckTagsSDK(&route_table.Tags, "bar", "baz"),
|
testAccCheckTags(&route_table.Tags, "bar", "baz"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -209,7 +209,7 @@ func resourceAwsSecurityGroupRead(d *schema.ResourceData, meta interface{}) erro
|
||||||
d.Set("owner_id", sg.OwnerID)
|
d.Set("owner_id", sg.OwnerID)
|
||||||
d.Set("ingress", ingressRules)
|
d.Set("ingress", ingressRules)
|
||||||
d.Set("egress", egressRules)
|
d.Set("egress", egressRules)
|
||||||
d.Set("tags", tagsToMapSDK(sg.Tags))
|
d.Set("tags", tagsToMap(sg.Tags))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,7 +239,7 @@ func resourceAwsSecurityGroupUpdate(d *schema.ResourceData, meta interface{}) er
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := setTagsSDK(ec2conn, d); err != nil {
|
if err := setTags(ec2conn, d); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -296,7 +296,7 @@ func TestAccAWSSecurityGroup_tags(t *testing.T) {
|
||||||
Config: testAccAWSSecurityGroupConfigTags,
|
Config: testAccAWSSecurityGroupConfigTags,
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckAWSSecurityGroupExists("aws_security_group.foo", &group),
|
testAccCheckAWSSecurityGroupExists("aws_security_group.foo", &group),
|
||||||
testAccCheckTagsSDK(&group.Tags, "foo", "bar"),
|
testAccCheckTags(&group.Tags, "foo", "bar"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -304,8 +304,8 @@ func TestAccAWSSecurityGroup_tags(t *testing.T) {
|
||||||
Config: testAccAWSSecurityGroupConfigTagsUpdate,
|
Config: testAccAWSSecurityGroupConfigTagsUpdate,
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckAWSSecurityGroupExists("aws_security_group.foo", &group),
|
testAccCheckAWSSecurityGroupExists("aws_security_group.foo", &group),
|
||||||
testAccCheckTagsSDK(&group.Tags, "foo", ""),
|
testAccCheckTags(&group.Tags, "foo", ""),
|
||||||
testAccCheckTagsSDK(&group.Tags, "bar", "baz"),
|
testAccCheckTags(&group.Tags, "bar", "baz"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -115,7 +115,7 @@ func resourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
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", tagsToMapSDK(subnet.Tags))
|
d.Set("tags", tagsToMap(subnet.Tags))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ func resourceAwsSubnetUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||||
|
|
||||||
d.Partial(true)
|
d.Partial(true)
|
||||||
|
|
||||||
if err := setTagsSDK(ec2conn, d); err != nil {
|
if err := setTags(ec2conn, d); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
d.SetPartial("tags")
|
d.SetPartial("tags")
|
||||||
|
|
|
@ -128,7 +128,7 @@ func resourceAwsVpcRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
d.Set("cidr_block", vpc.CIDRBlock)
|
d.Set("cidr_block", vpc.CIDRBlock)
|
||||||
|
|
||||||
// Tags
|
// Tags
|
||||||
d.Set("tags", tagsToMapSDK(vpc.Tags))
|
d.Set("tags", tagsToMap(vpc.Tags))
|
||||||
|
|
||||||
// Attributes
|
// Attributes
|
||||||
attribute := "enableDnsSupport"
|
attribute := "enableDnsSupport"
|
||||||
|
@ -220,7 +220,7 @@ func resourceAwsVpcUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||||
d.SetPartial("enable_dns_support")
|
d.SetPartial("enable_dns_support")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := setTagsSDK(ec2conn, d); err != nil {
|
if err := setTags(ec2conn, d); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
d.SetPartial("tags")
|
d.SetPartial("tags")
|
||||||
|
|
|
@ -95,7 +95,7 @@ func resourceAwsVpcPeeringRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
d.Set("peer_owner_id", pc.AccepterVPCInfo.OwnerID)
|
d.Set("peer_owner_id", pc.AccepterVPCInfo.OwnerID)
|
||||||
d.Set("peer_vpc_id", pc.AccepterVPCInfo.VPCID)
|
d.Set("peer_vpc_id", pc.AccepterVPCInfo.VPCID)
|
||||||
d.Set("vpc_id", pc.RequesterVPCInfo.VPCID)
|
d.Set("vpc_id", pc.RequesterVPCInfo.VPCID)
|
||||||
d.Set("tags", tagsToMapSDK(pc.Tags))
|
d.Set("tags", tagsToMap(pc.Tags))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -103,7 +103,7 @@ func resourceAwsVpcPeeringRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
func resourceAwsVpcPeeringUpdate(d *schema.ResourceData, meta interface{}) error {
|
func resourceAwsVpcPeeringUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||||
ec2conn := meta.(*AWSClient).awsEC2conn
|
ec2conn := meta.(*AWSClient).awsEC2conn
|
||||||
|
|
||||||
if err := setTagsSDK(ec2conn, d); err != nil {
|
if err := setTags(ec2conn, d); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
d.SetPartial("tags")
|
d.SetPartial("tags")
|
||||||
|
|
|
@ -65,7 +65,7 @@ func TestAccVpc_tags(t *testing.T) {
|
||||||
testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
|
testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
|
"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
|
||||||
testAccCheckTagsSDK(&vpc.Tags, "foo", "bar"),
|
testAccCheckTags(&vpc.Tags, "foo", "bar"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -73,8 +73,8 @@ func TestAccVpc_tags(t *testing.T) {
|
||||||
Config: testAccVpcConfigTagsUpdate,
|
Config: testAccVpcConfigTagsUpdate,
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckVpcExists("aws_vpc.foo", &vpc),
|
testAccCheckVpcExists("aws_vpc.foo", &vpc),
|
||||||
testAccCheckTagsSDK(&vpc.Tags, "foo", ""),
|
testAccCheckTags(&vpc.Tags, "foo", ""),
|
||||||
testAccCheckTagsSDK(&vpc.Tags, "bar", "baz"),
|
testAccCheckTags(&vpc.Tags, "bar", "baz"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -3,11 +3,13 @@ package aws
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/hashicorp/aws-sdk-go/aws"
|
||||||
|
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/mitchellh/goamz/ec2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// tagsSchema returns the schema to use for tags.
|
// tagsSchema returns the schema to use for tags.
|
||||||
|
//
|
||||||
func tagsSchema() *schema.Schema {
|
func tagsSchema() *schema.Schema {
|
||||||
return &schema.Schema{
|
return &schema.Schema{
|
||||||
Type: schema.TypeMap,
|
Type: schema.TypeMap,
|
||||||
|
@ -27,13 +29,21 @@ func setTags(conn *ec2.EC2, d *schema.ResourceData) error {
|
||||||
// Set tags
|
// Set tags
|
||||||
if len(remove) > 0 {
|
if len(remove) > 0 {
|
||||||
log.Printf("[DEBUG] Removing tags: %#v", remove)
|
log.Printf("[DEBUG] Removing tags: %#v", remove)
|
||||||
if _, err := conn.DeleteTags([]string{d.Id()}, remove); err != nil {
|
err := conn.DeleteTags(&ec2.DeleteTagsRequest{
|
||||||
|
Resources: []string{d.Id()},
|
||||||
|
Tags: remove,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(create) > 0 {
|
if len(create) > 0 {
|
||||||
log.Printf("[DEBUG] Creating tags: %#v", create)
|
log.Printf("[DEBUG] Creating tags: %#v", create)
|
||||||
if _, err := conn.CreateTags([]string{d.Id()}, create); err != nil {
|
err := conn.CreateTags(&ec2.CreateTagsRequest{
|
||||||
|
Resources: []string{d.Id()},
|
||||||
|
Tags: create,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,14 +59,14 @@ func diffTags(oldTags, newTags []ec2.Tag) ([]ec2.Tag, []ec2.Tag) {
|
||||||
// First, we're creating everything we have
|
// First, we're creating everything we have
|
||||||
create := make(map[string]interface{})
|
create := make(map[string]interface{})
|
||||||
for _, t := range newTags {
|
for _, t := range newTags {
|
||||||
create[t.Key] = t.Value
|
create[*t.Key] = *t.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the list of what to remove
|
// Build the list of what to remove
|
||||||
var remove []ec2.Tag
|
var remove []ec2.Tag
|
||||||
for _, t := range oldTags {
|
for _, t := range oldTags {
|
||||||
old, ok := create[t.Key]
|
old, ok := create[*t.Key]
|
||||||
if !ok || old != t.Value {
|
if !ok || old != *t.Value {
|
||||||
// Delete it!
|
// Delete it!
|
||||||
remove = append(remove, t)
|
remove = append(remove, t)
|
||||||
}
|
}
|
||||||
|
@ -70,8 +80,8 @@ func tagsFromMap(m map[string]interface{}) []ec2.Tag {
|
||||||
result := make([]ec2.Tag, 0, len(m))
|
result := make([]ec2.Tag, 0, len(m))
|
||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
result = append(result, ec2.Tag{
|
result = append(result, ec2.Tag{
|
||||||
Key: k,
|
Key: aws.String(k),
|
||||||
Value: v.(string),
|
Value: aws.String(v.(string)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +92,7 @@ func tagsFromMap(m map[string]interface{}) []ec2.Tag {
|
||||||
func tagsToMap(ts []ec2.Tag) map[string]string {
|
func tagsToMap(ts []ec2.Tag) map[string]string {
|
||||||
result := make(map[string]string)
|
result := make(map[string]string)
|
||||||
for _, t := range ts {
|
for _, t := range ts {
|
||||||
result[t.Key] = t.Value
|
result[*t.Key] = *t.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -1,106 +0,0 @@
|
||||||
package aws
|
|
||||||
|
|
||||||
// TODO: Clint: consolidate tags and tags_sdk
|
|
||||||
// tags_sdk and tags_sdk_test are used only for transition to aws-sdk-go
|
|
||||||
// and will replace tags and tags_test when the transition to aws-sdk-go/ec2 is
|
|
||||||
// complete
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/hashicorp/aws-sdk-go/aws"
|
|
||||||
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
|
||||||
)
|
|
||||||
|
|
||||||
// tagsSchema returns the schema to use for tags.
|
|
||||||
//
|
|
||||||
// TODO: uncomment this when we replace the original tags.go
|
|
||||||
//
|
|
||||||
// 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.DeleteTagsRequest{
|
|
||||||
Resources: []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.CreateTagsRequest{
|
|
||||||
Resources: []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
|
|
||||||
}
|
|
|
@ -1,85 +0,0 @@
|
||||||
package aws
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/hashicorp/aws-sdk-go/gen/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
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
"github.com/mitchellh/goamz/ec2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDiffTags(t *testing.T) {
|
func TestDiffTags(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue