github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_ecr_repository.go (about)

     1  package aws
     2  
     3  import (
     4  	"log"
     5  	"time"
     6  
     7  	"fmt"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/aws/awserr"
    11  	"github.com/aws/aws-sdk-go/service/ecr"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/helper/schema"
    14  )
    15  
    16  func resourceAwsEcrRepository() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsEcrRepositoryCreate,
    19  		Read:   resourceAwsEcrRepositoryRead,
    20  		Delete: resourceAwsEcrRepositoryDelete,
    21  		Importer: &schema.ResourceImporter{
    22  			State: schema.ImportStatePassthrough,
    23  		},
    24  
    25  		Schema: map[string]*schema.Schema{
    26  			"name": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  				ForceNew: true,
    30  			},
    31  			"arn": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Computed: true,
    34  			},
    35  			"registry_id": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Computed: true,
    38  			},
    39  			"repository_url": &schema.Schema{
    40  				Type:     schema.TypeString,
    41  				Computed: true,
    42  			},
    43  		},
    44  	}
    45  }
    46  
    47  func resourceAwsEcrRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
    48  	conn := meta.(*AWSClient).ecrconn
    49  
    50  	input := ecr.CreateRepositoryInput{
    51  		RepositoryName: aws.String(d.Get("name").(string)),
    52  	}
    53  
    54  	log.Printf("[DEBUG] Creating ECR resository: %s", input)
    55  	out, err := conn.CreateRepository(&input)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	repository := *out.Repository
    61  
    62  	log.Printf("[DEBUG] ECR repository created: %q", *repository.RepositoryArn)
    63  
    64  	d.SetId(*repository.RepositoryName)
    65  	d.Set("arn", repository.RepositoryArn)
    66  	d.Set("registry_id", repository.RegistryId)
    67  
    68  	return resourceAwsEcrRepositoryRead(d, meta)
    69  }
    70  
    71  func resourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) error {
    72  	conn := meta.(*AWSClient).ecrconn
    73  
    74  	log.Printf("[DEBUG] Reading repository %s", d.Id())
    75  	out, err := conn.DescribeRepositories(&ecr.DescribeRepositoriesInput{
    76  		RepositoryNames: []*string{aws.String(d.Id())},
    77  	})
    78  	if err != nil {
    79  		if ecrerr, ok := err.(awserr.Error); ok && ecrerr.Code() == "RepositoryNotFoundException" {
    80  			d.SetId("")
    81  			return nil
    82  		}
    83  		return err
    84  	}
    85  
    86  	repository := out.Repositories[0]
    87  
    88  	log.Printf("[DEBUG] Received repository %s", out)
    89  
    90  	d.SetId(*repository.RepositoryName)
    91  	d.Set("arn", repository.RepositoryArn)
    92  	d.Set("registry_id", repository.RegistryId)
    93  	d.Set("name", repository.RepositoryName)
    94  
    95  	repositoryUrl := buildRepositoryUrl(repository, meta.(*AWSClient).region)
    96  	log.Printf("[INFO] Setting the repository url to be %s", repositoryUrl)
    97  	d.Set("repository_url", repositoryUrl)
    98  
    99  	return nil
   100  }
   101  
   102  func buildRepositoryUrl(repo *ecr.Repository, region string) string {
   103  	return fmt.Sprintf("https://%s.dkr.ecr.%s.amazonaws.com/%s", *repo.RegistryId, region, *repo.RepositoryName)
   104  }
   105  
   106  func resourceAwsEcrRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
   107  	conn := meta.(*AWSClient).ecrconn
   108  
   109  	_, err := conn.DeleteRepository(&ecr.DeleteRepositoryInput{
   110  		RepositoryName: aws.String(d.Id()),
   111  		RegistryId:     aws.String(d.Get("registry_id").(string)),
   112  		Force:          aws.Bool(true),
   113  	})
   114  	if err != nil {
   115  		if ecrerr, ok := err.(awserr.Error); ok && ecrerr.Code() == "RepositoryNotFoundException" {
   116  			d.SetId("")
   117  			return nil
   118  		}
   119  		return err
   120  	}
   121  
   122  	log.Printf("[DEBUG] Waiting for ECR Repository %q to be deleted", d.Id())
   123  	err = resource.Retry(20*time.Minute, func() *resource.RetryError {
   124  		_, err := conn.DescribeRepositories(&ecr.DescribeRepositoriesInput{
   125  			RepositoryNames: []*string{aws.String(d.Id())},
   126  		})
   127  
   128  		if err != nil {
   129  			awsErr, ok := err.(awserr.Error)
   130  			if !ok {
   131  				return resource.NonRetryableError(err)
   132  			}
   133  
   134  			if awsErr.Code() == "RepositoryNotFoundException" {
   135  				return nil
   136  			}
   137  
   138  			return resource.NonRetryableError(err)
   139  		}
   140  
   141  		return resource.RetryableError(
   142  			fmt.Errorf("%q: Timeout while waiting for the ECR Repository to be deleted", d.Id()))
   143  	})
   144  	if err != nil {
   145  		return err
   146  	}
   147  
   148  	d.SetId("")
   149  	log.Printf("[DEBUG] repository %q deleted.", d.Get("name").(string))
   150  
   151  	return nil
   152  }