github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  
    78  	input := &codecommit.CreateRepositoryInput{
    79  		RepositoryName:        aws.String(d.Get("repository_name").(string)),
    80  		RepositoryDescription: aws.String(d.Get("description").(string)),
    81  	}
    82  
    83  	out, err := conn.CreateRepository(input)
    84  	if err != nil {
    85  		return fmt.Errorf("Error creating CodeCommit Repository: %s", err)
    86  	}
    87  
    88  	d.SetId(d.Get("repository_name").(string))
    89  	d.Set("repository_id", out.RepositoryMetadata.RepositoryId)
    90  	d.Set("arn", out.RepositoryMetadata.Arn)
    91  	d.Set("clone_url_http", out.RepositoryMetadata.CloneUrlHttp)
    92  	d.Set("clone_url_ssh", out.RepositoryMetadata.CloneUrlSsh)
    93  
    94  	return resourceAwsCodeCommitRepositoryUpdate(d, meta)
    95  }
    96  
    97  func resourceAwsCodeCommitRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {
    98  	conn := meta.(*AWSClient).codecommitconn
    99  
   100  	if _, ok := d.GetOk("default_branch"); ok {
   101  		if d.HasChange("default_branch") {
   102  			if err := resourceAwsCodeCommitUpdateDefaultBranch(conn, d); err != nil {
   103  				return err
   104  			}
   105  		}
   106  	}
   107  
   108  	if d.HasChange("description") {
   109  		if err := resourceAwsCodeCommitUpdateDescription(conn, d); err != nil {
   110  			return err
   111  		}
   112  	}
   113  
   114  	return resourceAwsCodeCommitRepositoryRead(d, meta)
   115  }
   116  
   117  func resourceAwsCodeCommitRepositoryRead(d *schema.ResourceData, meta interface{}) error {
   118  	conn := meta.(*AWSClient).codecommitconn
   119  
   120  	input := &codecommit.GetRepositoryInput{
   121  		RepositoryName: aws.String(d.Id()),
   122  	}
   123  
   124  	out, err := conn.GetRepository(input)
   125  	if err != nil {
   126  		return fmt.Errorf("Error reading CodeCommit Repository: %s", err.Error())
   127  	}
   128  
   129  	d.Set("repository_id", out.RepositoryMetadata.RepositoryId)
   130  	d.Set("arn", out.RepositoryMetadata.Arn)
   131  	d.Set("clone_url_http", out.RepositoryMetadata.CloneUrlHttp)
   132  	d.Set("clone_url_ssh", out.RepositoryMetadata.CloneUrlSsh)
   133  
   134  	if _, ok := d.GetOk("default_branch"); ok {
   135  		if out.RepositoryMetadata.DefaultBranch != nil {
   136  			d.Set("default_branch", out.RepositoryMetadata.DefaultBranch)
   137  		}
   138  	}
   139  
   140  	return nil
   141  }
   142  
   143  func resourceAwsCodeCommitRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
   144  	conn := meta.(*AWSClient).codecommitconn
   145  
   146  	log.Printf("[DEBUG] CodeCommit Delete Repository: %s", d.Id())
   147  	_, err := conn.DeleteRepository(&codecommit.DeleteRepositoryInput{
   148  		RepositoryName: aws.String(d.Id()),
   149  	})
   150  	if err != nil {
   151  		return fmt.Errorf("Error deleting CodeCommit Repository: %s", err.Error())
   152  	}
   153  
   154  	return nil
   155  }
   156  
   157  func resourceAwsCodeCommitUpdateDescription(conn *codecommit.CodeCommit, d *schema.ResourceData) error {
   158  	branchInput := &codecommit.UpdateRepositoryDescriptionInput{
   159  		RepositoryName:        aws.String(d.Id()),
   160  		RepositoryDescription: aws.String(d.Get("description").(string)),
   161  	}
   162  
   163  	_, err := conn.UpdateRepositoryDescription(branchInput)
   164  	if err != nil {
   165  		return fmt.Errorf("Error Updating Repository Description for CodeCommit Repository: %s", err.Error())
   166  	}
   167  
   168  	return nil
   169  }
   170  
   171  func resourceAwsCodeCommitUpdateDefaultBranch(conn *codecommit.CodeCommit, d *schema.ResourceData) error {
   172  	input := &codecommit.ListBranchesInput{
   173  		RepositoryName: aws.String(d.Id()),
   174  	}
   175  
   176  	out, err := conn.ListBranches(input)
   177  	if err != nil {
   178  		return fmt.Errorf("Error reading CodeCommit Repository branches: %s", err.Error())
   179  	}
   180  
   181  	if len(out.Branches) == 0 {
   182  		log.Printf("[WARN] Not setting Default Branch CodeCommit Repository that has no branches: %s", d.Id())
   183  		return nil
   184  	}
   185  
   186  	branchInput := &codecommit.UpdateDefaultBranchInput{
   187  		RepositoryName:    aws.String(d.Id()),
   188  		DefaultBranchName: aws.String(d.Get("default_branch").(string)),
   189  	}
   190  
   191  	_, err = conn.UpdateDefaultBranch(branchInput)
   192  	if err != nil {
   193  		return fmt.Errorf("Error Updating Default Branch for CodeCommit Repository: %s", err.Error())
   194  	}
   195  
   196  	return nil
   197  }