github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_codecommit_repository.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/codecommit"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceAwsCodeCommitRepository() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAwsCodeCommitRepositoryCreate,
    15  		Update: resourceAwsCodeCommitRepositoryUpdate,
    16  		Read:   resourceAwsCodeCommitRepositoryRead,
    17  		Delete: resourceAwsCodeCommitRepositoryDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"repository_name": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    25  					value := v.(string)
    26  					if len(value) > 100 {
    27  						errors = append(errors, fmt.Errorf(
    28  							"%q cannot be longer than 100 characters", k))
    29  					}
    30  					return
    31  				},
    32  			},
    33  
    34  			"description": &schema.Schema{
    35  				Type:     schema.TypeString,
    36  				Optional: true,
    37  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    38  					value := v.(string)
    39  					if len(value) > 1000 {
    40  						errors = append(errors, fmt.Errorf(
    41  							"%q cannot be longer than 1000 characters", k))
    42  					}
    43  					return
    44  				},
    45  			},
    46  
    47  			"arn": &schema.Schema{
    48  				Type:     schema.TypeString,
    49  				Computed: true,
    50  			},
    51  
    52  			"repository_id": &schema.Schema{
    53  				Type:     schema.TypeString,
    54  				Computed: true,
    55  			},
    56  
    57  			"clone_url_http": &schema.Schema{
    58  				Type:     schema.TypeString,
    59  				Computed: true,
    60  			},
    61  
    62  			"clone_url_ssh": &schema.Schema{
    63  				Type:     schema.TypeString,
    64  				Computed: true,
    65  			},
    66  
    67  			"default_branch": &schema.Schema{
    68  				Type:     schema.TypeString,
    69  				Optional: true,
    70  			},
    71  		},
    72  	}
    73  }
    74  
    75  func resourceAwsCodeCommitRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
    76  	conn := meta.(*AWSClient).codecommitconn
    77  	region := meta.(*AWSClient).region
    78  
    79  	//	This is a temporary thing - we need to ensure that CodeCommit is only being run against us-east-1
    80  	//	As this is the only place that AWS currently supports it
    81  	if region != "us-east-1" {
    82  		return fmt.Errorf("CodeCommit can only be used with us-east-1. You are trying to use it on %s", region)
    83  	}
    84  
    85  	input := &codecommit.CreateRepositoryInput{
    86  		RepositoryName:        aws.String(d.Get("repository_name").(string)),
    87  		RepositoryDescription: aws.String(d.Get("description").(string)),
    88  	}
    89  
    90  	out, err := conn.CreateRepository(input)
    91  	if err != nil {
    92  		return fmt.Errorf("Error creating CodeCommit Repository: %s", err)
    93  	}
    94  
    95  	d.SetId(d.Get("repository_name").(string))
    96  	d.Set("repository_id", *out.RepositoryMetadata.RepositoryId)
    97  	d.Set("arn", *out.RepositoryMetadata.Arn)
    98  	d.Set("clone_url_http", *out.RepositoryMetadata.CloneUrlHttp)
    99  	d.Set("clone_url_ssh", *out.RepositoryMetadata.CloneUrlSsh)
   100  
   101  	return resourceAwsCodeCommitRepositoryUpdate(d, meta)
   102  }
   103  
   104  func resourceAwsCodeCommitRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {
   105  	conn := meta.(*AWSClient).codecommitconn
   106  
   107  	if d.HasChange("default_branch") {
   108  		if err := resourceAwsCodeCommitUpdateDefaultBranch(conn, d); err != nil {
   109  			return err
   110  		}
   111  	}
   112  
   113  	if d.HasChange("description") {
   114  		if err := resourceAwsCodeCommitUpdateDescription(conn, d); err != nil {
   115  			return err
   116  		}
   117  	}
   118  
   119  	return resourceAwsCodeCommitRepositoryRead(d, meta)
   120  }
   121  
   122  func resourceAwsCodeCommitRepositoryRead(d *schema.ResourceData, meta interface{}) error {
   123  	conn := meta.(*AWSClient).codecommitconn
   124  
   125  	input := &codecommit.GetRepositoryInput{
   126  		RepositoryName: aws.String(d.Id()),
   127  	}
   128  
   129  	out, err := conn.GetRepository(input)
   130  	if err != nil {
   131  		return fmt.Errorf("Error reading CodeCommit Repository: %s", err.Error())
   132  	}
   133  
   134  	d.Set("repository_id", *out.RepositoryMetadata.RepositoryId)
   135  	d.Set("arn", *out.RepositoryMetadata.Arn)
   136  	d.Set("clone_url_http", *out.RepositoryMetadata.CloneUrlHttp)
   137  	d.Set("clone_url_ssh", *out.RepositoryMetadata.CloneUrlSsh)
   138  	if out.RepositoryMetadata.DefaultBranch != nil {
   139  		d.Set("default_branch", *out.RepositoryMetadata.DefaultBranch)
   140  	}
   141  
   142  	return nil
   143  }
   144  
   145  func resourceAwsCodeCommitRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
   146  	conn := meta.(*AWSClient).codecommitconn
   147  
   148  	log.Printf("[DEBUG] CodeCommit Delete Repository: %s", d.Id())
   149  	_, err := conn.DeleteRepository(&codecommit.DeleteRepositoryInput{
   150  		RepositoryName: aws.String(d.Id()),
   151  	})
   152  	if err != nil {
   153  		return fmt.Errorf("Error deleting CodeCommit Repository: %s", err.Error())
   154  	}
   155  
   156  	return nil
   157  }
   158  
   159  func resourceAwsCodeCommitUpdateDescription(conn *codecommit.CodeCommit, d *schema.ResourceData) error {
   160  	branchInput := &codecommit.UpdateRepositoryDescriptionInput{
   161  		RepositoryName:        aws.String(d.Id()),
   162  		RepositoryDescription: aws.String(d.Get("description").(string)),
   163  	}
   164  
   165  	_, err := conn.UpdateRepositoryDescription(branchInput)
   166  	if err != nil {
   167  		return fmt.Errorf("Error Updating Repository Description for CodeCommit Repository: %s", err.Error())
   168  	}
   169  
   170  	return nil
   171  }
   172  
   173  func resourceAwsCodeCommitUpdateDefaultBranch(conn *codecommit.CodeCommit, d *schema.ResourceData) error {
   174  	branchInput := &codecommit.UpdateDefaultBranchInput{
   175  		RepositoryName:    aws.String(d.Id()),
   176  		DefaultBranchName: aws.String(d.Get("default_branch").(string)),
   177  	}
   178  
   179  	_, err := conn.UpdateDefaultBranch(branchInput)
   180  	if err != nil {
   181  		return fmt.Errorf("Error Updating Default Branch for CodeCommit Repository: %s", err.Error())
   182  	}
   183  
   184  	return nil
   185  }