Skip to content
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

service/batch: Support resource tagging and prevent differences with new secrets support in container properties #15470

Merged
merged 2 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions aws/data_source_aws_batch_compute_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/batch"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsBatchComputeEnvironment() *schema.Resource {
Expand Down Expand Up @@ -34,6 +35,8 @@ func dataSourceAwsBatchComputeEnvironment() *schema.Resource {
Computed: true,
},

"tags": tagsSchemaComputed(),

"type": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -59,6 +62,7 @@ func dataSourceAwsBatchComputeEnvironment() *schema.Resource {

func dataSourceAwsBatchComputeEnvironmentRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).batchconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

params := &batch.DescribeComputeEnvironmentsInput{
ComputeEnvironments: []*string{aws.String(d.Get("compute_environment_name").(string))},
Expand Down Expand Up @@ -88,5 +92,10 @@ func dataSourceAwsBatchComputeEnvironmentRead(d *schema.ResourceData, meta inter
d.Set("status", computeEnvironment.Status)
d.Set("status_reason", computeEnvironment.StatusReason)
d.Set("state", computeEnvironment.State)

if err := d.Set("tags", keyvaluetags.BatchKeyValueTags(computeEnvironment.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
bflad marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}
51 changes: 13 additions & 38 deletions aws/data_source_aws_batch_compute_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccDataSourceAwsBatchComputeEnvironment_basic(t *testing.T) {
Expand All @@ -21,47 +20,23 @@ func TestAccDataSourceAwsBatchComputeEnvironment_basic(t *testing.T) {
{
Config: testAccDataSourceAwsBatchComputeEnvironmentConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsBatchComputeEnvironmentCheck(datasourceName, resourceName),
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "compute_environment_name", resourceName, "compute_environment_name"),
resource.TestCheckResourceAttrPair(datasourceName, "ecs_cluster_arn", resourceName, "ecs_cluster_arn"),
resource.TestCheckResourceAttrPair(datasourceName, "service_role", resourceName, "service_role"),
resource.TestCheckResourceAttrPair(datasourceName, "state", resourceName, "state"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(datasourceName, "type", resourceName, "type"),
Comment on lines -24 to +29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

),
},
},
})
}

func testAccDataSourceAwsBatchComputeEnvironmentCheck(datasourceName, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ds, ok := s.RootModule().Resources[datasourceName]
if !ok {
return fmt.Errorf("root module has no data source called %s", datasourceName)
}

batchCeRs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("root module has no resource called %s", resourceName)
}

attrNames := []string{
"arn",
"compute_environment_name",
}

for _, attrName := range attrNames {
if ds.Primary.Attributes[attrName] != batchCeRs.Primary.Attributes[attrName] {
return fmt.Errorf(
"%s is %s; want %s",
attrName,
ds.Primary.Attributes[attrName],
batchCeRs.Primary.Attributes[attrName],
)
}
}

return nil
}
}

func testAccDataSourceAwsBatchComputeEnvironmentConfig(rName string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}

resource "aws_iam_role" "ecs_instance_role" {
name = "ecs_%[1]s"

Expand All @@ -73,7 +48,7 @@ resource "aws_iam_role" "ecs_instance_role" {
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
"Service": "ec2.${data.aws_partition.current.dns_suffix}"
}
}
]
Expand All @@ -83,7 +58,7 @@ EOF

resource "aws_iam_role_policy_attachment" "ecs_instance_role" {
role = aws_iam_role.ecs_instance_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}

resource "aws_iam_instance_profile" "ecs_instance_role" {
Expand All @@ -102,7 +77,7 @@ resource "aws_iam_role" "aws_batch_service_role" {
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "batch.amazonaws.com"
"Service": "batch.${data.aws_partition.current.dns_suffix}"
}
}
]
Expand All @@ -112,7 +87,7 @@ EOF

resource "aws_iam_role_policy_attachment" "aws_batch_service_role" {
role = aws_iam_role.aws_batch_service_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole"
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSBatchServiceRole"
}

resource "aws_security_group" "sample" {
Expand Down
8 changes: 8 additions & 0 deletions aws/data_source_aws_batch_job_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/batch"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsBatchJobQueue() *schema.Resource {
Expand Down Expand Up @@ -39,6 +40,8 @@ func dataSourceAwsBatchJobQueue() *schema.Resource {
Computed: true,
},

"tags": tagsSchemaComputed(),

"priority": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -66,6 +69,7 @@ func dataSourceAwsBatchJobQueue() *schema.Resource {

func dataSourceAwsBatchJobQueueRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).batchconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

params := &batch.DescribeJobQueuesInput{
JobQueues: []*string{aws.String(d.Get("name").(string))},
Expand Down Expand Up @@ -105,5 +109,9 @@ func dataSourceAwsBatchJobQueueRead(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf("error setting compute_environment_order: %s", err)
}

if err := d.Set("tags", keyvaluetags.BatchKeyValueTags(jobQueue.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}
52 changes: 12 additions & 40 deletions aws/data_source_aws_batch_job_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccDataSourceAwsBatchJobQueue_basic(t *testing.T) {
Expand All @@ -21,49 +20,22 @@ func TestAccDataSourceAwsBatchJobQueue_basic(t *testing.T) {
{
Config: testAccDataSourceAwsBatchJobQueueConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsBatchJobQueueCheck(datasourceName, resourceName),
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "compute_environment_order.#", resourceName, "compute_environments.#"),
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "priority", resourceName, "priority"),
resource.TestCheckResourceAttrPair(datasourceName, "state", resourceName, "state"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
Comment on lines -24 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

),
},
},
})
}

func testAccDataSourceAwsBatchJobQueueCheck(datasourceName, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ds, ok := s.RootModule().Resources[datasourceName]
if !ok {
return fmt.Errorf("root module has no data source called %s", datasourceName)
}

jobQueueRs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("root module has no resource called %s", resourceName)
}

attrNames := []string{
"arn",
"name",
"state",
"priority",
}

for _, attrName := range attrNames {
if ds.Primary.Attributes[attrName] != jobQueueRs.Primary.Attributes[attrName] {
return fmt.Errorf(
"%s is %s; want %s",
attrName,
ds.Primary.Attributes[attrName],
jobQueueRs.Primary.Attributes[attrName],
)
}
}

return nil
}
}

func testAccDataSourceAwsBatchJobQueueConfig(rName string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}

resource "aws_iam_role" "ecs_instance_role" {
name = "ecs_%[1]s"

Expand All @@ -75,7 +47,7 @@ resource "aws_iam_role" "ecs_instance_role" {
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
"Service": "ec2.${data.aws_partition.current.dns_suffix}"
}
}
]
Expand All @@ -85,7 +57,7 @@ EOF

resource "aws_iam_role_policy_attachment" "ecs_instance_role" {
role = aws_iam_role.ecs_instance_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}

resource "aws_iam_instance_profile" "ecs_instance_role" {
Expand All @@ -104,7 +76,7 @@ resource "aws_iam_role" "aws_batch_service_role" {
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "batch.amazonaws.com"
"Service": "batch.${data.aws_partition.current.dns_suffix}"
}
}
]
Expand All @@ -114,7 +86,7 @@ EOF

resource "aws_iam_role_policy_attachment" "aws_batch_service_role" {
role = aws_iam_role.aws_batch_service_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole"
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSBatchServiceRole"
}

resource "aws_security_group" "sample" {
Expand Down
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/gettag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const filename = `get_tag_gen.go`

var serviceNames = []string{
"autoscaling",
"batch",
"dynamodb",
"ec2",
"ecs",
Expand Down
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/listtags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var serviceNames = []string{
"athena",
"autoscaling",
"backup",
"batch",
"cloud9",
"cloudfront",
"cloudhsmv2",
Expand Down
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/updatetags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var serviceNames = []string{
"athena",
"autoscaling",
"backup",
"batch",
"cloud9",
"cloudfront",
"cloudhsmv2",
Expand Down
16 changes: 16 additions & 0 deletions aws/internal/keyvaluetags/get_tag_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions aws/internal/keyvaluetags/list_tags_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/aws/aws-sdk-go/service/athena"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/backup"
"github.com/aws/aws-sdk-go/service/batch"
"github.com/aws/aws-sdk-go/service/cloud9"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/aws/aws-sdk-go/service/cloudhsmv2"
Expand Down Expand Up @@ -147,6 +148,8 @@ func ServiceClientType(serviceName string) string {
funcType = reflect.TypeOf(autoscaling.New)
case "backup":
funcType = reflect.TypeOf(backup.New)
case "batch":
funcType = reflect.TypeOf(batch.New)
case "cloud9":
funcType = reflect.TypeOf(cloud9.New)
case "cloudfront":
Expand Down
Loading