github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 Importer: &schema.ResourceImporter{ 19 State: schema.ImportStatePassthrough, 20 }, 21 22 Schema: map[string]*schema.Schema{ 23 "repository_name": { 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { 28 value := v.(string) 29 if len(value) > 100 { 30 errors = append(errors, fmt.Errorf( 31 "%q cannot be longer than 100 characters", k)) 32 } 33 return 34 }, 35 }, 36 37 "description": { 38 Type: schema.TypeString, 39 Optional: true, 40 ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { 41 value := v.(string) 42 if len(value) > 1000 { 43 errors = append(errors, fmt.Errorf( 44 "%q cannot be longer than 1000 characters", k)) 45 } 46 return 47 }, 48 }, 49 50 "arn": { 51 Type: schema.TypeString, 52 Computed: true, 53 }, 54 55 "repository_id": { 56 Type: schema.TypeString, 57 Computed: true, 58 }, 59 60 "clone_url_http": { 61 Type: schema.TypeString, 62 Computed: true, 63 }, 64 65 "clone_url_ssh": { 66 Type: schema.TypeString, 67 Computed: true, 68 }, 69 70 "default_branch": { 71 Type: schema.TypeString, 72 Optional: true, 73 }, 74 }, 75 } 76 } 77 78 func resourceAwsCodeCommitRepositoryCreate(d *schema.ResourceData, meta interface{}) error { 79 conn := meta.(*AWSClient).codecommitconn 80 81 input := &codecommit.CreateRepositoryInput{ 82 RepositoryName: aws.String(d.Get("repository_name").(string)), 83 RepositoryDescription: aws.String(d.Get("description").(string)), 84 } 85 86 out, err := conn.CreateRepository(input) 87 if err != nil { 88 return fmt.Errorf("Error creating CodeCommit Repository: %s", err) 89 } 90 91 d.SetId(d.Get("repository_name").(string)) 92 d.Set("repository_id", out.RepositoryMetadata.RepositoryId) 93 d.Set("arn", out.RepositoryMetadata.Arn) 94 d.Set("clone_url_http", out.RepositoryMetadata.CloneUrlHttp) 95 d.Set("clone_url_ssh", out.RepositoryMetadata.CloneUrlSsh) 96 97 return resourceAwsCodeCommitRepositoryUpdate(d, meta) 98 } 99 100 func resourceAwsCodeCommitRepositoryUpdate(d *schema.ResourceData, meta interface{}) error { 101 conn := meta.(*AWSClient).codecommitconn 102 103 if _, ok := d.GetOk("default_branch"); ok { 104 if d.HasChange("default_branch") { 105 if err := resourceAwsCodeCommitUpdateDefaultBranch(conn, d); err != nil { 106 return err 107 } 108 } 109 } 110 111 if d.HasChange("description") { 112 if err := resourceAwsCodeCommitUpdateDescription(conn, d); err != nil { 113 return err 114 } 115 } 116 117 return resourceAwsCodeCommitRepositoryRead(d, meta) 118 } 119 120 func resourceAwsCodeCommitRepositoryRead(d *schema.ResourceData, meta interface{}) error { 121 conn := meta.(*AWSClient).codecommitconn 122 123 input := &codecommit.GetRepositoryInput{ 124 RepositoryName: aws.String(d.Id()), 125 } 126 127 out, err := conn.GetRepository(input) 128 if err != nil { 129 return fmt.Errorf("Error reading CodeCommit Repository: %s", err.Error()) 130 } 131 132 d.Set("repository_id", out.RepositoryMetadata.RepositoryId) 133 d.Set("arn", out.RepositoryMetadata.Arn) 134 d.Set("clone_url_http", out.RepositoryMetadata.CloneUrlHttp) 135 d.Set("clone_url_ssh", out.RepositoryMetadata.CloneUrlSsh) 136 d.Set("description", out.RepositoryMetadata.RepositoryDescription) 137 d.Set("repository_name", out.RepositoryMetadata.RepositoryName) 138 139 if _, ok := d.GetOk("default_branch"); ok { 140 if out.RepositoryMetadata.DefaultBranch != nil { 141 d.Set("default_branch", out.RepositoryMetadata.DefaultBranch) 142 } 143 } 144 145 return nil 146 } 147 148 func resourceAwsCodeCommitRepositoryDelete(d *schema.ResourceData, meta interface{}) error { 149 conn := meta.(*AWSClient).codecommitconn 150 151 log.Printf("[DEBUG] CodeCommit Delete Repository: %s", d.Id()) 152 _, err := conn.DeleteRepository(&codecommit.DeleteRepositoryInput{ 153 RepositoryName: aws.String(d.Id()), 154 }) 155 if err != nil { 156 return fmt.Errorf("Error deleting CodeCommit Repository: %s", err.Error()) 157 } 158 159 return nil 160 } 161 162 func resourceAwsCodeCommitUpdateDescription(conn *codecommit.CodeCommit, d *schema.ResourceData) error { 163 branchInput := &codecommit.UpdateRepositoryDescriptionInput{ 164 RepositoryName: aws.String(d.Id()), 165 RepositoryDescription: aws.String(d.Get("description").(string)), 166 } 167 168 _, err := conn.UpdateRepositoryDescription(branchInput) 169 if err != nil { 170 return fmt.Errorf("Error Updating Repository Description for CodeCommit Repository: %s", err.Error()) 171 } 172 173 return nil 174 } 175 176 func resourceAwsCodeCommitUpdateDefaultBranch(conn *codecommit.CodeCommit, d *schema.ResourceData) error { 177 input := &codecommit.ListBranchesInput{ 178 RepositoryName: aws.String(d.Id()), 179 } 180 181 out, err := conn.ListBranches(input) 182 if err != nil { 183 return fmt.Errorf("Error reading CodeCommit Repository branches: %s", err.Error()) 184 } 185 186 if len(out.Branches) == 0 { 187 log.Printf("[WARN] Not setting Default Branch CodeCommit Repository that has no branches: %s", d.Id()) 188 return nil 189 } 190 191 branchInput := &codecommit.UpdateDefaultBranchInput{ 192 RepositoryName: aws.String(d.Id()), 193 DefaultBranchName: aws.String(d.Get("default_branch").(string)), 194 } 195 196 _, err = conn.UpdateDefaultBranch(branchInput) 197 if err != nil { 198 return fmt.Errorf("Error Updating Default Branch for CodeCommit Repository: %s", err.Error()) 199 } 200 201 return nil 202 }