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

New Resource: aws_ecrpublic_repository_policy #16901

Merged
merged 23 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
63c9561
update after repository dependency merge
breathingdust Mar 18, 2021
37da50c
remove update from basic
breathingdust Jun 19, 2021
0e61283
Merge branch 'main' into f-ecrpublic-repository-policy
breathingdust Jun 19, 2021
2e7fad7
use repository_name to match AWS API
breathingdust Jun 23, 2021
42cd1b7
add changelog
breathingdust Jun 23, 2021
8d50c73
changes from code review
breathingdust Jun 23, 2021
14ec149
use value
breathingdust Jun 23, 2021
3e508aa
Merge branch 'f-ecrpublic-repository-policy' of github.com:terraform-…
breathingdust Jun 23, 2021
9acc511
remove foos from docs
breathingdust Jun 23, 2021
8334a97
remove d.SetId() from update
breathingdust Jun 23, 2021
d4441b9
correct code block
breathingdust Jun 23, 2021
8191ce9
fix example section
breathingdust Jun 24, 2021
bd681cb
add error check
breathingdust Jun 24, 2021
a03d5de
merge main
breathingdust Nov 22, 2021
bdbbc88
fix for semgrep conversion rule
breathingdust Dec 8, 2021
faf71d1
Alphabetize attributes.
ewbankkit Dec 16, 2021
70caa19
r/aws_ecrpublic_repository_policy: Consolidate 'resourceRepositoryPol…
ewbankkit Dec 16, 2021
56a684c
Rename acceptance test functions.
ewbankkit Dec 16, 2021
25a0208
r/aws_ecrpublic_repository_policy: Add 'TestAccECRPublicRepositoryPol…
ewbankkit Dec 16, 2021
8974582
Standard policy diff handling.
ewbankkit Dec 16, 2021
ea5f7b9
Merge branch 'main' into f-ecrpublic-repository-policy
ewbankkit Dec 16, 2021
c96be40
r/aws_ecrpublic_repository_policy: Ensure that state import tests pass!
ewbankkit Dec 16, 2021
84f405f
Add us-east-1-only note to documentation.
ewbankkit Dec 16, 2021
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
3 changes: 3 additions & 0 deletions .changelog/16901.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_ecrpublic_repository_policy
```
3 changes: 2 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,8 @@ func Provider() *schema.Provider {
"aws_ecr_repository": ecr.ResourceRepository(),
"aws_ecr_repository_policy": ecr.ResourceRepositoryPolicy(),

"aws_ecrpublic_repository": ecrpublic.ResourceRepository(),
"aws_ecrpublic_repository": ecrpublic.ResourceRepository(),
"aws_ecrpublic_repository_policy": ecrpublic.ResourceRepositoryPolicy(),

"aws_ecs_account_setting_default": ecs.ResourceAccountSettingDefault(),
"aws_ecs_capacity_provider": ecs.ResourceCapacityProvider(),
Expand Down
170 changes: 170 additions & 0 deletions internal/service/ecrpublic/repository_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package ecrpublic

import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecrpublic"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func ResourceRepositoryPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceRepositoryPolicyPut,
Read: resourceRepositoryPolicyRead,
Update: resourceRepositoryPolicyPut,
Delete: resourceRepositoryPolicyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"policy": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: verify.SuppressEquivalentPolicyDiffs,
ValidateFunc: validation.StringIsJSON,
},
"registry_id": {
Type: schema.TypeString,
Computed: true,
},
"repository_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

const (
policyPutTimeout = 2 * time.Minute
)

func resourceRepositoryPolicyPut(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).ECRPublicConn

policy, err := structure.NormalizeJsonString(d.Get("policy").(string))

if err != nil {
return fmt.Errorf("policy (%s) is invalid JSON: %w", policy, err)
}

repositoryName := d.Get("repository_name").(string)
input := &ecrpublic.SetRepositoryPolicyInput{
PolicyText: aws.String(policy),
RepositoryName: aws.String(repositoryName),
}

log.Printf("[DEBUG] Setting ECR Public Repository Policy: %s", input)
outputRaw, err := tfresource.RetryWhen(policyPutTimeout,
func() (interface{}, error) {
return conn.SetRepositoryPolicy(input)
},
func(err error) (bool, error) {
if tfawserr.ErrMessageContains(err, ecrpublic.ErrCodeInvalidParameterException, "Invalid repository policy provided") {
return true, err
}

return false, err
},
)

if err != nil {
return fmt.Errorf("error setting ECR Public Repository (%s) Policy: %w", repositoryName, err)
}

if d.IsNewResource() {
d.SetId(aws.StringValue(outputRaw.(*ecrpublic.SetRepositoryPolicyOutput).RepositoryName))
}

return resourceRepositoryPolicyRead(d, meta)
}

func resourceRepositoryPolicyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).ECRPublicConn

output, err := FindRepositoryPolicyByName(conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] ECR Public Repository Policy (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading ECR Public Repository Policy (%s): %w", d.Id(), err)
}

policyToSet, err := verify.SecondJSONUnlessEquivalent(d.Get("policy").(string), aws.StringValue(output.PolicyText))

if err != nil {
return fmt.Errorf("while setting policy (%s), encountered: %w", policyToSet, err)
}

policyToSet, err = structure.NormalizeJsonString(policyToSet)

if err != nil {
return fmt.Errorf("policy (%s) is an invalid JSON: %w", policyToSet, err)
}

d.Set("policy", policyToSet)
d.Set("registry_id", output.RegistryId)
d.Set("repository_name", output.RepositoryName)

return nil
}

func resourceRepositoryPolicyDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).ECRPublicConn

_, err := conn.DeleteRepositoryPolicy(&ecrpublic.DeleteRepositoryPolicyInput{
RegistryId: aws.String(d.Get("registry_id").(string)),
RepositoryName: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, ecrpublic.ErrCodeRepositoryNotFoundException, ecrpublic.ErrCodeRepositoryPolicyNotFoundException) {
return nil
}

if err != nil {
return fmt.Errorf("error deleting ECR Public Repository Policy (%s): %w", d.Id(), err)
}

return nil
}

func FindRepositoryPolicyByName(conn *ecrpublic.ECRPublic, name string) (*ecrpublic.GetRepositoryPolicyOutput, error) {
input := &ecrpublic.GetRepositoryPolicyInput{
RepositoryName: aws.String(name),
}

output, err := conn.GetRepositoryPolicy(input)

if tfawserr.ErrCodeEquals(err, ecrpublic.ErrCodeRepositoryNotFoundException, ecrpublic.ErrCodeRepositoryPolicyNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output, nil
}
Loading