github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/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 _, ok := d.GetOk("default_branch"); ok { 108 if d.HasChange("default_branch") { 109 if err := resourceAwsCodeCommitUpdateDefaultBranch(conn, d); err != nil { 110 return err 111 } 112 } 113 } 114 115 if d.HasChange("description") { 116 if err := resourceAwsCodeCommitUpdateDescription(conn, d); err != nil { 117 return err 118 } 119 } 120 121 return resourceAwsCodeCommitRepositoryRead(d, meta) 122 } 123 124 func resourceAwsCodeCommitRepositoryRead(d *schema.ResourceData, meta interface{}) error { 125 conn := meta.(*AWSClient).codecommitconn 126 127 input := &codecommit.GetRepositoryInput{ 128 RepositoryName: aws.String(d.Id()), 129 } 130 131 out, err := conn.GetRepository(input) 132 if err != nil { 133 return fmt.Errorf("Error reading CodeCommit Repository: %s", err.Error()) 134 } 135 136 d.Set("repository_id", *out.RepositoryMetadata.RepositoryId) 137 d.Set("arn", *out.RepositoryMetadata.Arn) 138 d.Set("clone_url_http", *out.RepositoryMetadata.CloneUrlHttp) 139 d.Set("clone_url_ssh", *out.RepositoryMetadata.CloneUrlSsh) 140 141 if _, ok := d.GetOk("default_branch"); ok { 142 if out.RepositoryMetadata.DefaultBranch != nil { 143 d.Set("default_branch", *out.RepositoryMetadata.DefaultBranch) 144 } 145 } 146 147 return nil 148 } 149 150 func resourceAwsCodeCommitRepositoryDelete(d *schema.ResourceData, meta interface{}) error { 151 conn := meta.(*AWSClient).codecommitconn 152 153 log.Printf("[DEBUG] CodeCommit Delete Repository: %s", d.Id()) 154 _, err := conn.DeleteRepository(&codecommit.DeleteRepositoryInput{ 155 RepositoryName: aws.String(d.Id()), 156 }) 157 if err != nil { 158 return fmt.Errorf("Error deleting CodeCommit Repository: %s", err.Error()) 159 } 160 161 return nil 162 } 163 164 func resourceAwsCodeCommitUpdateDescription(conn *codecommit.CodeCommit, d *schema.ResourceData) error { 165 branchInput := &codecommit.UpdateRepositoryDescriptionInput{ 166 RepositoryName: aws.String(d.Id()), 167 RepositoryDescription: aws.String(d.Get("description").(string)), 168 } 169 170 _, err := conn.UpdateRepositoryDescription(branchInput) 171 if err != nil { 172 return fmt.Errorf("Error Updating Repository Description for CodeCommit Repository: %s", err.Error()) 173 } 174 175 return nil 176 } 177 178 func resourceAwsCodeCommitUpdateDefaultBranch(conn *codecommit.CodeCommit, d *schema.ResourceData) error { 179 input := &codecommit.ListBranchesInput{ 180 RepositoryName: aws.String(d.Id()), 181 } 182 183 out, err := conn.ListBranches(input) 184 if err != nil { 185 return fmt.Errorf("Error reading CodeCommit Repository branches: %s", err.Error()) 186 } 187 188 if len(out.Branches) == 0 { 189 log.Printf("[WARN] Not setting Default Branch CodeCommit Repository that has no branches: %s", d.Id()) 190 return nil 191 } 192 193 branchInput := &codecommit.UpdateDefaultBranchInput{ 194 RepositoryName: aws.String(d.Id()), 195 DefaultBranchName: aws.String(d.Get("default_branch").(string)), 196 } 197 198 _, err = conn.UpdateDefaultBranch(branchInput) 199 if err != nil { 200 return fmt.Errorf("Error Updating Default Branch for CodeCommit Repository: %s", err.Error()) 201 } 202 203 return nil 204 }