From dc434d8a1f77666fd845c430fc46d72c2b882f7a Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Thu, 12 Oct 2017 11:59:43 +0900 Subject: [PATCH 1/3] Make file --- aws/provider.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/provider.go b/aws/provider.go index 5b033c65fb3..c0ad59f1747 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -310,6 +310,7 @@ func Provider() terraform.ResourceProvider { "aws_dynamodb_table": resourceAwsDynamoDbTable(), "aws_ebs_snapshot": resourceAwsEbsSnapshot(), "aws_ebs_volume": resourceAwsEbsVolume(), + "aws_ecr_lifecycle_policy": resourceAwsEcrLifecyclePolicy(), "aws_ecr_repository": resourceAwsEcrRepository(), "aws_ecr_repository_policy": resourceAwsEcrRepositoryPolicy(), "aws_ecs_cluster": resourceAwsEcsCluster(), From 3f8ab71bc1405fe0b31c402915c61f6ae3893403 Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Sat, 14 Oct 2017 17:44:29 +0900 Subject: [PATCH 2/3] Add aws_ecr_lifecycle_policy --- aws/resource_aws_ecr_lifecycle_policy.go | 100 ++++++++++++++++++ aws/resource_aws_ecr_lifecycle_policy_test.go | 97 +++++++++++++++++ website/aws.erb | 4 + .../docs/r/ecr_lifecycle_policy.html.markdown | 57 ++++++++++ 4 files changed, 258 insertions(+) create mode 100644 aws/resource_aws_ecr_lifecycle_policy.go create mode 100644 aws/resource_aws_ecr_lifecycle_policy_test.go create mode 100644 website/docs/r/ecr_lifecycle_policy.html.markdown diff --git a/aws/resource_aws_ecr_lifecycle_policy.go b/aws/resource_aws_ecr_lifecycle_policy.go new file mode 100644 index 00000000000..4610bf1a48d --- /dev/null +++ b/aws/resource_aws_ecr_lifecycle_policy.go @@ -0,0 +1,100 @@ +package aws + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/service/ecr" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsEcrLifecyclePolicy() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsEcrLifecyclePolicyCreate, + Read: resourceAwsEcrLifecyclePolicyRead, + Delete: resourceAwsEcrLifecyclePolicyDelete, + + Schema: map[string]*schema.Schema{ + "repository": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "policy": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "registry_id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceAwsEcrLifecyclePolicyCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ecrconn + + input := &ecr.PutLifecyclePolicyInput{ + RepositoryName: aws.String(d.Get("repository").(string)), + LifecyclePolicyText: aws.String(d.Get("policy").(string)), + } + + resp, err := conn.PutLifecyclePolicy(input) + if err != nil { + return err + } + d.SetId(*resp.RepositoryName) + d.Set("registry_id", resp.RegistryId) + return resourceAwsEcrLifecyclePolicyRead(d, meta) +} + +func resourceAwsEcrLifecyclePolicyRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ecrconn + + input := &ecr.GetLifecyclePolicyInput{ + RegistryId: aws.String(d.Get("registry_id").(string)), + RepositoryName: aws.String(d.Get("repository").(string)), + } + + _, err := conn.GetLifecyclePolicy(input) + if err != nil { + if aerr, ok := err.(awserr.Error); ok { + switch aerr.Code() { + case ecr.ErrCodeRepositoryNotFoundException, ecr.ErrCodeLifecyclePolicyNotFoundException: + d.SetId("") + return nil + default: + return err + } + } + return err + } + + return nil +} + +func resourceAwsEcrLifecyclePolicyDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ecrconn + + input := &ecr.DeleteLifecyclePolicyInput{ + RegistryId: aws.String(d.Get("registry_id").(string)), + RepositoryName: aws.String(d.Get("repository").(string)), + } + + _, err := conn.DeleteLifecyclePolicy(input) + if err != nil { + if aerr, ok := err.(awserr.Error); ok { + switch aerr.Code() { + case ecr.ErrCodeRepositoryNotFoundException, ecr.ErrCodeLifecyclePolicyNotFoundException: + d.SetId("") + return nil + default: + return err + } + } + return err + } + + return nil +} diff --git a/aws/resource_aws_ecr_lifecycle_policy_test.go b/aws/resource_aws_ecr_lifecycle_policy_test.go new file mode 100644 index 00000000000..84719f52189 --- /dev/null +++ b/aws/resource_aws_ecr_lifecycle_policy_test.go @@ -0,0 +1,97 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/service/ecr" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSEcrLifecyclePolicy_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSEcrLifecyclePolicyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccEcrLifecyclePolicyConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSEcrLifecyclePolicyExists("aws_ecr_lifecycle_policy.foo"), + ), + }, + }, + }) +} + +func testAccCheckAWSEcrLifecyclePolicyDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).ecrconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_ecr_lifecycle_policy" { + continue + } + + input := &ecr.GetLifecyclePolicyInput{ + RegistryId: aws.String(rs.Primary.Attributes["registry_id"]), + RepositoryName: aws.String(rs.Primary.Attributes["repository"]), + } + + _, err := conn.GetLifecyclePolicy(input) + if err != nil { + if aerr, ok := err.(awserr.Error); ok { + switch aerr.Code() { + case ecr.ErrCodeRepositoryNotFoundException: + return nil + default: + return err + } + } + return err + } + } + + return nil +} + +func testAccCheckAWSEcrLifecyclePolicyExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + _, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + return nil + } +} + +const testAccEcrLifecyclePolicyConfig = ` +resource "aws_ecr_repository" "foo" { + name = "bar" +} +resource "aws_ecr_lifecycle_policy" "foo" { + repository = "${aws_ecr_repository.foo.name}" + policy = <ECS Resources