Skip to content

Commit

Permalink
service/ecr: Refactor to use keyvaluetags library
Browse files Browse the repository at this point in the history
Reference: #7926

Output from acceptance testing:

```
--- PASS: TestAccAWSEcrDataSource_ecrImage (13.77s)
--- PASS: TestAccAWSEcrRepository_basic (14.42s)
--- PASS: TestAccAWSEcrRepository_immutability (14.53s)
--- PASS: TestAccAWSEcrDataSource_ecrRepository (16.21s)
--- PASS: TestAccAWSEcrRepository_tags (20.90s)
```
  • Loading branch information
bflad committed Oct 1, 2019
1 parent 688c305 commit b5256fc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 257 deletions.
16 changes: 11 additions & 5 deletions aws/data_source_aws_ecr_repository.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/ecr"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsEcrRepository() *schema.Resource {
Expand Down Expand Up @@ -54,17 +55,22 @@ func dataSourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) er
}

repository := out.Repositories[0]

log.Printf("[DEBUG] Received ECR repository %s", out)
arn := aws.StringValue(repository.RepositoryArn)

d.SetId(aws.StringValue(repository.RepositoryName))
d.Set("arn", repository.RepositoryArn)
d.Set("arn", arn)
d.Set("registry_id", repository.RegistryId)
d.Set("name", repository.RepositoryName)
d.Set("repository_url", repository.RepositoryUri)

if err := getTagsECR(conn, d); err != nil {
return fmt.Errorf("error getting ECR repository tags: %s", err)
tags, err := keyvaluetags.EcrListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for ECR Repository (%s): %s", arn, err)
}

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

return nil
Expand Down
25 changes: 19 additions & 6 deletions aws/resource_aws_ecr_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsEcrRepository() *schema.Resource {
Expand Down Expand Up @@ -65,7 +66,7 @@ func resourceAwsEcrRepositoryCreate(d *schema.ResourceData, meta interface{}) er
input := ecr.CreateRepositoryInput{
ImageTagMutability: aws.String(d.Get("image_tag_mutability").(string)),
RepositoryName: aws.String(d.Get("name").(string)),
Tags: tagsFromMapECR(d.Get("tags").(map[string]interface{})),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().EcrTags(),
}

log.Printf("[DEBUG] Creating ECR repository: %#v", input)
Expand Down Expand Up @@ -119,21 +120,29 @@ func resourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) erro
}

repository := out.Repositories[0]
arn := aws.StringValue(repository.RepositoryArn)

d.Set("arn", repository.RepositoryArn)
d.Set("arn", arn)
d.Set("name", repository.RepositoryName)
d.Set("registry_id", repository.RegistryId)
d.Set("repository_url", repository.RepositoryUri)
d.Set("image_tag_mutability", repository.ImageTagMutability)

if err := getTagsECR(conn, d); err != nil {
return fmt.Errorf("error getting ECR repository tags: %s", err)
tags, err := keyvaluetags.EcrListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for ECR Repository (%s): %s", arn, err)
}

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

return nil
}

func resourceAwsEcrRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {
arn := d.Get("arn").(string)
conn := meta.(*AWSClient).ecrconn

if d.HasChange("image_tag_mutability") {
Expand All @@ -142,8 +151,12 @@ func resourceAwsEcrRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
}
}

if err := setTagsECR(conn, d); err != nil {
return fmt.Errorf("error setting ECR repository tags: %s", err)
if d.HasChange("tags") {
o, n := d.GetChange("tags")

if err := keyvaluetags.EcrUpdateTags(conn, arn, o, n); err != nil {
return fmt.Errorf("error updating ECR Repository (%s) tags: %s", arn, err)
}
}

return resourceAwsEcrRepositoryRead(d, meta)
Expand Down
135 changes: 0 additions & 135 deletions aws/tagsECR.go

This file was deleted.

111 changes: 0 additions & 111 deletions aws/tagsECR_test.go

This file was deleted.

0 comments on commit b5256fc

Please sign in to comment.