From cc5d940c65f7fe666c5ae0c7c999d9b3f2e308a5 Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Sun, 17 Mar 2019 10:40:28 +0900 Subject: [PATCH] resource/ecr_repository_policy: Support import --- aws/resource_aws_ecr_repository_policy.go | 5 +++- ...resource_aws_ecr_repository_policy_test.go | 25 +++++++++++-------- .../r/ecr_repository_policy.html.markdown | 8 ++++++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/aws/resource_aws_ecr_repository_policy.go b/aws/resource_aws_ecr_repository_policy.go index ecbe9a5cc63..c661ed1140c 100644 --- a/aws/resource_aws_ecr_repository_policy.go +++ b/aws/resource_aws_ecr_repository_policy.go @@ -17,6 +17,9 @@ func resourceAwsEcrRepositoryPolicy() *schema.Resource { Read: resourceAwsEcrRepositoryPolicyRead, Update: resourceAwsEcrRepositoryPolicyUpdate, Delete: resourceAwsEcrRepositoryPolicyDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "repository": { @@ -78,7 +81,6 @@ func resourceAwsEcrRepositoryPolicyRead(d *schema.ResourceData, meta interface{} log.Printf("[DEBUG] Reading repository policy %s", d.Id()) out, err := conn.GetRepositoryPolicy(&ecr.GetRepositoryPolicyInput{ - RegistryId: aws.String(d.Get("registry_id").(string)), RepositoryName: aws.String(d.Id()), }) if err != nil { @@ -99,6 +101,7 @@ func resourceAwsEcrRepositoryPolicyRead(d *schema.ResourceData, meta interface{} repositoryPolicy := out d.SetId(*repositoryPolicy.RepositoryName) + d.Set("repository", repositoryPolicy.RepositoryName) d.Set("registry_id", repositoryPolicy.RegistryId) d.Set("policy", repositoryPolicy.PolicyText) diff --git a/aws/resource_aws_ecr_repository_policy_test.go b/aws/resource_aws_ecr_repository_policy_test.go index 8b3313a7070..240cc985b40 100644 --- a/aws/resource_aws_ecr_repository_policy_test.go +++ b/aws/resource_aws_ecr_repository_policy_test.go @@ -14,6 +14,7 @@ import ( func TestAccAWSEcrRepositoryPolicy_basic(t *testing.T) { randString := acctest.RandString(10) + resourceName := "aws_ecr_repository_policy.default" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -23,15 +24,21 @@ func TestAccAWSEcrRepositoryPolicy_basic(t *testing.T) { { Config: testAccAWSEcrRepositoryPolicy(randString), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSEcrRepositoryPolicyExists("aws_ecr_repository_policy.default"), + testAccCheckAWSEcrRepositoryPolicyExists(resourceName), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } func TestAccAWSEcrRepositoryPolicy_iam(t *testing.T) { randString := acctest.RandString(10) + resourceName := "aws_ecr_repository_policy.default" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -41,9 +48,14 @@ func TestAccAWSEcrRepositoryPolicy_iam(t *testing.T) { { Config: testAccAWSEcrRepositoryPolicyWithIAMRole(randString), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSEcrRepositoryPolicyExists("aws_ecr_repository_policy.default"), + testAccCheckAWSEcrRepositoryPolicyExists(resourceName), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -84,11 +96,6 @@ func testAccCheckAWSEcrRepositoryPolicyExists(name string) resource.TestCheckFun func testAccAWSEcrRepositoryPolicy(randString string) string { return fmt.Sprintf(` -# ECR initially only available in us-east-1 -# https://aws.amazon.com/blogs/aws/ec2-container-registry-now-generally-available/ -provider "aws" { - region = "us-east-1" -} resource "aws_ecr_repository" "foo" { name = "tf-acc-test-ecr-%s" } @@ -120,10 +127,6 @@ EOF // exercise our retry logic, since we try to use the new resource instantly. func testAccAWSEcrRepositoryPolicyWithIAMRole(randString string) string { return fmt.Sprintf(` -provider "aws" { - region = "us-east-1" -} - resource "aws_ecr_repository" "foo" { name = "tf-acc-test-ecr-%s" } diff --git a/website/docs/r/ecr_repository_policy.html.markdown b/website/docs/r/ecr_repository_policy.html.markdown index de269865682..7e197d5c92d 100644 --- a/website/docs/r/ecr_repository_policy.html.markdown +++ b/website/docs/r/ecr_repository_policy.html.markdown @@ -70,3 +70,11 @@ In addition to all arguments above, the following attributes are exported: * `repository` - The name of the repository. * `registry_id` - The registry ID where the repository was created. + +## Import + +ECR Repository Policy can be imported using the repository name, e.g. + +``` +$ terraform import aws_ecr_repository_policy.example example +```