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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/ecr_repository_policy: Support import #7974

Merged
merged 1 commit into from
Mar 17, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the intention here is that ECR may eventually support more than one registry per AWS account, but that is not currently supported nor does the aws_ecr_repository resource import require the leading registry ID (e.g. AWS account ID). So this seems okay.

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" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ thanks for fixing this

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
```