Skip to content

Commit

Permalink
resource/ecr_repository_policy: Support import
Browse files Browse the repository at this point in the history
  • Loading branch information
atsushi-ishibashi committed Mar 17, 2019
1 parent 97d7f4a commit cc5d940
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
5 changes: 4 additions & 1 deletion aws/resource_aws_ecr_repository_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)

Expand Down
25 changes: 14 additions & 11 deletions aws/resource_aws_ecr_repository_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand All @@ -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) },
Expand All @@ -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,
},
},
})
}
Expand Down Expand Up @@ -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"
}
Expand Down Expand Up @@ -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"
}
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/ecr_repository_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit cc5d940

Please sign in to comment.