Skip to content

Commit

Permalink
Merge pull request #1706 from Ninir/b-datasources-tags
Browse files Browse the repository at this point in the history
Updated the Data Source Tags structure
  • Loading branch information
Ninir authored Oct 11, 2017
2 parents 999d600 + 2b47ed6 commit d825908
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 379 deletions.
4 changes: 2 additions & 2 deletions aws/data_source_aws_ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func dataSourceAwsAmi() *schema.Resource {
Type: schema.TypeMap,
Computed: true,
},
"tags": dataSourceTagsSchema(),
"tags": tagsSchemaComputed(),
},
}
}
Expand Down Expand Up @@ -304,7 +304,7 @@ func amiDescriptionAttributes(d *schema.ResourceData, image *ec2.Image) error {
if err := d.Set("state_reason", amiStateReason(image.StateReason)); err != nil {
return err
}
if err := d.Set("tags", dataSourceTags(image.Tags)); err != nil {
if err := d.Set("tags", tagsToMap(image.Tags)); err != nil {
return err
}
return nil
Expand Down
1 change: 0 additions & 1 deletion aws/data_source_aws_ami_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func dataSourceAwsAmiIds() *schema.Resource {
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": dataSourceTagsSchema(),
"ids": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Expand Down
146 changes: 73 additions & 73 deletions aws/data_source_aws_ami_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestAccAWSAmiDataSource_natInstance(t *testing.T) {
resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "state", "available"),
resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "state_reason.code", "UNSET"),
resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "state_reason.message", "UNSET"),
resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "tags.#", "0"),
resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "tags.%", "0"),
resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "virtualization_type", "hvm"),
),
},
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestAccAWSAmiDataSource_windowsInstance(t *testing.T) {
resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "state", "available"),
resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "state_reason.code", "UNSET"),
resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "state_reason.message", "UNSET"),
resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "tags.#", "0"),
resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "tags.%", "0"),
resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "virtualization_type", "hvm"),
),
},
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestAccAWSAmiDataSource_instanceStore(t *testing.T) {
resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "state", "available"),
resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "state_reason.code", "UNSET"),
resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "state_reason.message", "UNSET"),
resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "tags.#", "0"),
resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "tags.%", "0"),
resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "virtualization_type", "hvm"),
),
},
Expand Down Expand Up @@ -250,104 +250,104 @@ func testAccCheckAwsAmiDataSourceID(n string) resource.TestCheckFunc {
// Amazon Linux AMIs.
const testAccCheckAwsAmiDataSourceConfig = `
data "aws_ami" "nat_ami" {
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn-ami-vpc-nat*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "block-device-mapping.volume-type"
values = ["standard"]
}
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn-ami-vpc-nat*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "block-device-mapping.volume-type"
values = ["standard"]
}
}
`

// Windows image test.
const testAccCheckAwsAmiDataSourceWindowsConfig = `
data "aws_ami" "windows_ami" {
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["Windows_Server-2012-R2*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "block-device-mapping.volume-type"
values = ["gp2"]
}
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["Windows_Server-2012-R2*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "block-device-mapping.volume-type"
values = ["gp2"]
}
}
`

// Instance store test - using Ubuntu images
const testAccCheckAwsAmiDataSourceInstanceStoreConfig = `
data "aws_ami" "instance_store_ami" {
most_recent = true
filter {
name = "owner-id"
values = ["099720109477"]
}
filter {
name = "name"
values = ["ubuntu/images/hvm-instance/ubuntu-trusty-14.04-amd64-server*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["instance-store"]
}
most_recent = true
filter {
name = "owner-id"
values = ["099720109477"]
}
filter {
name = "name"
values = ["ubuntu/images/hvm-instance/ubuntu-trusty-14.04-amd64-server*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["instance-store"]
}
}
`

// Testing owner parameter
const testAccCheckAwsAmiDataSourceOwnersConfig = `
data "aws_ami" "amazon_ami" {
most_recent = true
owners = ["amazon"]
most_recent = true
owners = ["amazon"]
}
`

const testAccCheckAwsAmiDataSourceEmptyOwnersConfig = `
data "aws_ami" "amazon_ami" {
most_recent = true
owners = [""]
most_recent = true
owners = [""]
}
`

// Testing name_regex parameter
const testAccCheckAwsAmiDataSourceNameRegexConfig = `
data "aws_ami" "name_regex_filtered_ami" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn-ami-*"]
}
name_regex = "^amzn-ami-\\d{3}[5].*-ecs-optimized"
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn-ami-*"]
}
name_regex = "^amzn-ami-\\d{3}[5].*-ecs-optimized"
}
`
46 changes: 0 additions & 46 deletions aws/data_source_aws_common_schema.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,11 @@
package aws

import (
"bytes"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceTagsHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["key"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["value"].(string)))
return hashcode.String(buf.String())
}

func dataSourceTags(m []*ec2.Tag) *schema.Set {
s := &schema.Set{
F: dataSourceTagsHash,
}
for _, v := range m {
tag := map[string]interface{}{
"key": *v.Key,
"value": *v.Value,
}
s.Add(tag)
}
return s
}

func buildAwsDataSourceFilters(set *schema.Set) []*ec2.Filter {
var filters []*ec2.Filter
for _, v := range set.List() {
Expand Down Expand Up @@ -69,23 +43,3 @@ func dataSourceFiltersSchema() *schema.Schema {
},
}
}

func dataSourceTagsSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Computed: true,
Set: dataSourceTagsHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
},
},
},
}
}
4 changes: 2 additions & 2 deletions aws/data_source_aws_ebs_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func dataSourceAwsEbsSnapshot() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": dataSourceTagsSchema(),
"tags": tagsSchemaComputed(),
},
}
}
Expand Down Expand Up @@ -154,7 +154,7 @@ func snapshotDescriptionAttributes(d *schema.ResourceData, snapshot *ec2.Snapsho
d.Set("owner_id", snapshot.OwnerId)
d.Set("owner_alias", snapshot.OwnerAlias)

if err := d.Set("tags", dataSourceTags(snapshot.Tags)); err != nil {
if err := d.Set("tags", tagsToMap(snapshot.Tags)); err != nil {
return err
}

Expand Down
1 change: 0 additions & 1 deletion aws/data_source_aws_ebs_snapshot_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func dataSourceAwsEbsSnapshotIds() *schema.Resource {
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": dataSourceTagsSchema(),
"ids": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Expand Down
Loading

0 comments on commit d825908

Please sign in to comment.