2015-12-22 16:31:30 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2016-09-26 13:51:11 +02:00
|
|
|
"time"
|
2015-12-22 16:31:30 +01:00
|
|
|
|
2016-03-09 00:51:30 +01:00
|
|
|
"fmt"
|
|
|
|
|
2015-12-22 16:31:30 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ecr"
|
2016-09-26 13:51:11 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-12-22 16:31:30 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsEcrRepository() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsEcrRepositoryCreate,
|
|
|
|
Read: resourceAwsEcrRepositoryRead,
|
|
|
|
Delete: resourceAwsEcrRepositoryDelete,
|
2016-06-23 01:58:59 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-12-22 16:31:30 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"registry_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-03-09 00:51:30 +01:00
|
|
|
"repository_url": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-12-22 16:31:30 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsEcrRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ecrconn
|
|
|
|
|
|
|
|
input := ecr.CreateRepositoryInput{
|
|
|
|
RepositoryName: aws.String(d.Get("name").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Creating ECR resository: %s", input)
|
|
|
|
out, err := conn.CreateRepository(&input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
repository := *out.Repository
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] ECR repository created: %q", *repository.RepositoryArn)
|
|
|
|
|
|
|
|
d.SetId(*repository.RepositoryName)
|
2016-08-28 18:15:45 +02:00
|
|
|
d.Set("arn", repository.RepositoryArn)
|
|
|
|
d.Set("registry_id", repository.RegistryId)
|
2015-12-22 16:31:30 +01:00
|
|
|
|
|
|
|
return resourceAwsEcrRepositoryRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ecrconn
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Reading repository %s", d.Id())
|
|
|
|
out, err := conn.DescribeRepositories(&ecr.DescribeRepositoriesInput{
|
|
|
|
RepositoryNames: []*string{aws.String(d.Id())},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if ecrerr, ok := err.(awserr.Error); ok && ecrerr.Code() == "RepositoryNotFoundException" {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
repository := out.Repositories[0]
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Received repository %s", out)
|
|
|
|
|
|
|
|
d.SetId(*repository.RepositoryName)
|
2016-08-28 18:15:45 +02:00
|
|
|
d.Set("arn", repository.RepositoryArn)
|
|
|
|
d.Set("registry_id", repository.RegistryId)
|
2016-06-23 01:58:59 +02:00
|
|
|
d.Set("name", repository.RepositoryName)
|
2015-12-22 16:31:30 +01:00
|
|
|
|
2016-03-09 00:51:30 +01:00
|
|
|
repositoryUrl := buildRepositoryUrl(repository, meta.(*AWSClient).region)
|
|
|
|
log.Printf("[INFO] Setting the repository url to be %s", repositoryUrl)
|
|
|
|
d.Set("repository_url", repositoryUrl)
|
|
|
|
|
2015-12-22 16:31:30 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-09 00:51:30 +01:00
|
|
|
func buildRepositoryUrl(repo *ecr.Repository, region string) string {
|
|
|
|
return fmt.Sprintf("https://%s.dkr.ecr.%s.amazonaws.com/%s", *repo.RegistryId, region, *repo.RepositoryName)
|
|
|
|
}
|
|
|
|
|
2015-12-22 16:31:30 +01:00
|
|
|
func resourceAwsEcrRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ecrconn
|
|
|
|
|
|
|
|
_, err := conn.DeleteRepository(&ecr.DeleteRepositoryInput{
|
|
|
|
RepositoryName: aws.String(d.Id()),
|
|
|
|
RegistryId: aws.String(d.Get("registry_id").(string)),
|
|
|
|
Force: aws.Bool(true),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if ecrerr, ok := err.(awserr.Error); ok && ecrerr.Code() == "RepositoryNotFoundException" {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-26 13:51:11 +02:00
|
|
|
log.Printf("[DEBUG] Waiting for ECR Repository %q to be deleted", d.Id())
|
|
|
|
err = resource.Retry(20*time.Minute, func() *resource.RetryError {
|
|
|
|
_, err := conn.DescribeRepositories(&ecr.DescribeRepositoriesInput{
|
|
|
|
RepositoryNames: []*string{aws.String(d.Id())},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
awsErr, ok := err.(awserr.Error)
|
|
|
|
if !ok {
|
|
|
|
return resource.NonRetryableError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if awsErr.Code() == "RepositoryNotFoundException" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource.NonRetryableError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource.RetryableError(
|
|
|
|
fmt.Errorf("%q: Timeout while waiting for the ECR Repository to be deleted", d.Id()))
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
log.Printf("[DEBUG] repository %q deleted.", d.Get("name").(string))
|
2015-12-22 16:31:30 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|