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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ECR repository immutable image tags #9557

Merged
merged 3 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
36 changes: 34 additions & 2 deletions aws/resource_aws_ecr_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceAwsEcrRepository() *schema.Resource {
Expand All @@ -32,6 +33,15 @@ func resourceAwsEcrRepository() *schema.Resource {
Required: true,
ForceNew: true,
},
"image_tag_mutability": {
sunilkumarmohanty marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
Default: ecr.ImageTagMutabilityMutable,
ValidateFunc: validation.StringInSlice([]string{
ecr.ImageTagMutabilityMutable,
ecr.ImageTagMutabilityImmutable,
}, false),
},
"tags": tagsSchema(),
"arn": {
Type: schema.TypeString,
Expand All @@ -53,8 +63,9 @@ func resourceAwsEcrRepositoryCreate(d *schema.ResourceData, meta interface{}) er
conn := meta.(*AWSClient).ecrconn

input := ecr.CreateRepositoryInput{
RepositoryName: aws.String(d.Get("name").(string)),
Tags: tagsFromMapECR(d.Get("tags").(map[string]interface{})),
ImageTagMutability: aws.String(d.Get("image_tag_mutability").(string)),
RepositoryName: aws.String(d.Get("name").(string)),
Tags: tagsFromMapECR(d.Get("tags").(map[string]interface{})),
}

log.Printf("[DEBUG] Creating ECR repository: %#v", input)
Expand Down Expand Up @@ -124,6 +135,12 @@ func resourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) erro
func resourceAwsEcrRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ecrconn

if d.HasChange("image_tag_mutability") {
if err := resourceAwsEcrRepositoryUpdateImageTagMutability(conn, d); err != nil {
return err
}
}

if err := setTagsECR(conn, d); err != nil {
return fmt.Errorf("error setting ECR repository tags: %s", err)
}
Expand Down Expand Up @@ -177,3 +194,18 @@ func resourceAwsEcrRepositoryDelete(d *schema.ResourceData, meta interface{}) er

return nil
}

func resourceAwsEcrRepositoryUpdateImageTagMutability(conn *ecr.ECR, d *schema.ResourceData) error {
input := &ecr.PutImageTagMutabilityInput{
ImageTagMutability: aws.String(d.Get("image_tag_mutability").(string)),
RepositoryName: aws.String(d.Id()),
RegistryId: aws.String(d.Get("registry_id").(string)),
}

_, err := conn.PutImageTagMutability(input)
if err != nil {
return fmt.Errorf("Error setting image tag mutability: %s", err.Error())
}

return nil
}
30 changes: 30 additions & 0 deletions aws/resource_aws_ecr_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ func TestAccAWSEcrRepository_tags(t *testing.T) {
})
}

func TestAccAWSEcrRepository_immutability(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ecr_repository.default"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcrRepositoryDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEcrRepositoryConfig_immutability(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcrRepositoryExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "image_tag_mutability", "IMMUTABLE"),
),
},
sunilkumarmohanty marked this conversation as resolved.
Show resolved Hide resolved
},
})
}

func testAccCheckAWSEcrRepositoryDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ecrconn

Expand Down Expand Up @@ -159,3 +180,12 @@ resource "aws_ecr_repository" "default" {
}
`, rName)
}

func testAccAWSEcrRepositoryConfig_immutability(rName string) string {
return fmt.Sprintf(`
resource "aws_ecr_repository" "default" {
name = %q
image_tag_mutability = "IMMUTABLE"
}
`, rName)
}
1 change: 1 addition & 0 deletions website/docs/r/ecr_repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ resource "aws_ecr_repository" "foo" {
The following arguments are supported:

* `name` - (Required) Name of the repository.
* `image_tag_mutability` - (Optional) The tag mutability setting for the repository. Must be one of: `MUTABLE` or `IMMUTABLE`
sunilkumarmohanty marked this conversation as resolved.
Show resolved Hide resolved
* `tags` - (Optional) A mapping of tags to assign to the resource.

## Attributes Reference
Expand Down