github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_ecr_repository_policy.go (about)

     1  package aws
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/aws/aws-sdk-go/aws/awserr"
     8  	"github.com/aws/aws-sdk-go/service/ecr"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceAwsEcrRepositoryPolicy() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAwsEcrRepositoryPolicyCreate,
    15  		Read:   resourceAwsEcrRepositoryPolicyRead,
    16  		Update: resourceAwsEcrRepositoryPolicyUpdate,
    17  		Delete: resourceAwsEcrRepositoryPolicyDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"repository": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  			"policy": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  			},
    29  			"registry_id": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Computed: true,
    32  			},
    33  		},
    34  	}
    35  }
    36  
    37  func resourceAwsEcrRepositoryPolicyCreate(d *schema.ResourceData, meta interface{}) error {
    38  	conn := meta.(*AWSClient).ecrconn
    39  
    40  	input := ecr.SetRepositoryPolicyInput{
    41  		RepositoryName: aws.String(d.Get("repository").(string)),
    42  		PolicyText:     aws.String(d.Get("policy").(string)),
    43  	}
    44  
    45  	log.Printf("[DEBUG] Creating ECR resository policy: %s", input)
    46  	out, err := conn.SetRepositoryPolicy(&input)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	repositoryPolicy := *out
    52  
    53  	log.Printf("[DEBUG] ECR repository policy created: %s", *repositoryPolicy.RepositoryName)
    54  
    55  	d.SetId(*repositoryPolicy.RepositoryName)
    56  	d.Set("registry_id", repositoryPolicy.RegistryId)
    57  
    58  	return resourceAwsEcrRepositoryPolicyRead(d, meta)
    59  }
    60  
    61  func resourceAwsEcrRepositoryPolicyRead(d *schema.ResourceData, meta interface{}) error {
    62  	conn := meta.(*AWSClient).ecrconn
    63  
    64  	log.Printf("[DEBUG] Reading repository policy %s", d.Id())
    65  	out, err := conn.GetRepositoryPolicy(&ecr.GetRepositoryPolicyInput{
    66  		RegistryId:     aws.String(d.Get("registry_id").(string)),
    67  		RepositoryName: aws.String(d.Id()),
    68  	})
    69  	if err != nil {
    70  		if ecrerr, ok := err.(awserr.Error); ok {
    71  			switch ecrerr.Code() {
    72  			case "RepositoryNotFoundException", "RepositoryPolicyNotFoundException":
    73  				d.SetId("")
    74  				return nil
    75  			default:
    76  				return err
    77  			}
    78  		}
    79  		return err
    80  	}
    81  
    82  	log.Printf("[DEBUG] Received repository policy %s", out)
    83  
    84  	repositoryPolicy := out
    85  
    86  	d.SetId(*repositoryPolicy.RepositoryName)
    87  	d.Set("registry_id", repositoryPolicy.RegistryId)
    88  
    89  	return nil
    90  }
    91  
    92  func resourceAwsEcrRepositoryPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
    93  	conn := meta.(*AWSClient).ecrconn
    94  
    95  	if !d.HasChange("policy") {
    96  		return nil
    97  	}
    98  
    99  	input := ecr.SetRepositoryPolicyInput{
   100  		RepositoryName: aws.String(d.Get("repository").(string)),
   101  		RegistryId:     aws.String(d.Get("registry_id").(string)),
   102  		PolicyText:     aws.String(d.Get("policy").(string)),
   103  	}
   104  
   105  	out, err := conn.SetRepositoryPolicy(&input)
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	repositoryPolicy := *out
   111  
   112  	d.SetId(*repositoryPolicy.RepositoryName)
   113  	d.Set("registry_id", repositoryPolicy.RegistryId)
   114  
   115  	return nil
   116  }
   117  
   118  func resourceAwsEcrRepositoryPolicyDelete(d *schema.ResourceData, meta interface{}) error {
   119  	conn := meta.(*AWSClient).ecrconn
   120  
   121  	_, err := conn.DeleteRepositoryPolicy(&ecr.DeleteRepositoryPolicyInput{
   122  		RepositoryName: aws.String(d.Id()),
   123  		RegistryId:     aws.String(d.Get("registry_id").(string)),
   124  	})
   125  	if err != nil {
   126  		if ecrerr, ok := err.(awserr.Error); ok {
   127  			switch ecrerr.Code() {
   128  			case "RepositoryNotFoundException", "RepositoryPolicyNotFoundException":
   129  				d.SetId("")
   130  				return nil
   131  			default:
   132  				return err
   133  			}
   134  		}
   135  		return err
   136  	}
   137  
   138  	log.Printf("[DEBUG] repository policy %s deleted.", d.Id())
   139  
   140  	return nil
   141  }