github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/bitbucket/resource_repository.go (about) 1 package bitbucket 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "github.com/hashicorp/terraform/helper/schema" 8 "log" 9 ) 10 11 type CloneUrl struct { 12 Href string `json:"href,omitempty"` 13 Name string `json:"name,omitempty"` 14 } 15 16 type Repository struct { 17 SCM string `json:"scm,omitempty"` 18 HasWiki bool `json:"has_wiki,omitempty"` 19 HasIssues bool `json:"has_issues,omitempty"` 20 Website string `json:"website,omitempty"` 21 IsPrivate bool `json:"is_private,omitempty"` 22 ForkPolicy string `json:"fork_policy,omitempty"` 23 Language string `json:"language,omitempty"` 24 Description string `json:"description,omitempty"` 25 Name string `json:"name,omitempty"` 26 UUID string `json:"uuid,omitempty"` 27 Project struct { 28 Key string `json:"key,omitempty"` 29 } `json:"project,omitempty"` 30 Links struct { 31 Clone []CloneUrl `json:"clone,omitempty"` 32 } `json:"links,omitempty"` 33 } 34 35 func resourceRepository() *schema.Resource { 36 return &schema.Resource{ 37 Create: resourceRepositoryCreate, 38 Update: resourceRepositoryUpdate, 39 Read: resourceRepositoryRead, 40 Delete: resourceRepositoryDelete, 41 Importer: &schema.ResourceImporter{ 42 State: schema.ImportStatePassthrough, 43 }, 44 45 Schema: map[string]*schema.Schema{ 46 "scm": &schema.Schema{ 47 Type: schema.TypeString, 48 Optional: true, 49 Default: "git", 50 }, 51 "has_wiki": &schema.Schema{ 52 Type: schema.TypeBool, 53 Optional: true, 54 Default: false, 55 }, 56 "has_issues": &schema.Schema{ 57 Type: schema.TypeBool, 58 Optional: true, 59 Default: false, 60 }, 61 "website": &schema.Schema{ 62 Type: schema.TypeString, 63 Optional: true, 64 }, 65 "clone_ssh": &schema.Schema{ 66 Type: schema.TypeString, 67 Computed: true, 68 }, 69 "clone_https": &schema.Schema{ 70 Type: schema.TypeString, 71 Computed: true, 72 }, 73 "project_key": &schema.Schema{ 74 Type: schema.TypeString, 75 Optional: true, 76 }, 77 "is_private": &schema.Schema{ 78 Type: schema.TypeBool, 79 Optional: true, 80 Default: true, 81 }, 82 "fork_policy": &schema.Schema{ 83 Type: schema.TypeString, 84 Optional: true, 85 Default: "allow_forks", 86 }, 87 "language": &schema.Schema{ 88 Type: schema.TypeString, 89 Optional: true, 90 }, 91 "description": &schema.Schema{ 92 Type: schema.TypeString, 93 Optional: true, 94 }, 95 "owner": &schema.Schema{ 96 Type: schema.TypeString, 97 Required: true, 98 }, 99 "name": &schema.Schema{ 100 Type: schema.TypeString, 101 Required: true, 102 }, 103 }, 104 } 105 } 106 107 func newRepositoryFromResource(d *schema.ResourceData) *Repository { 108 repo := &Repository{ 109 Name: d.Get("name").(string), 110 Language: d.Get("language").(string), 111 IsPrivate: d.Get("is_private").(bool), 112 Description: d.Get("description").(string), 113 ForkPolicy: d.Get("fork_policy").(string), 114 HasWiki: d.Get("has_wiki").(bool), 115 HasIssues: d.Get("has_issues").(bool), 116 SCM: d.Get("scm").(string), 117 Website: d.Get("website").(string), 118 } 119 120 repo.Project.Key = d.Get("project_key").(string) 121 return repo 122 } 123 124 func resourceRepositoryUpdate(d *schema.ResourceData, m interface{}) error { 125 client := m.(*BitbucketClient) 126 repository := newRepositoryFromResource(d) 127 128 var jsonbuffer []byte 129 130 jsonpayload := bytes.NewBuffer(jsonbuffer) 131 enc := json.NewEncoder(jsonpayload) 132 enc.Encode(repository) 133 134 repository_response, err := client.Put(fmt.Sprintf("2.0/repositories/%s/%s", 135 d.Get("owner").(string), 136 d.Get("name").(string), 137 ), jsonpayload) 138 139 if err != nil { 140 return err 141 } 142 143 if repository_response.StatusCode == 200 { 144 decoder := json.NewDecoder(repository_response.Body) 145 err = decoder.Decode(&repository) 146 if err != nil { 147 return err 148 } 149 } else { 150 return fmt.Errorf("Failed to put: %d", repository_response.StatusCode) 151 } 152 153 return resourceRepositoryRead(d, m) 154 } 155 156 func resourceRepositoryCreate(d *schema.ResourceData, m interface{}) error { 157 client := m.(*BitbucketClient) 158 repo := newRepositoryFromResource(d) 159 160 var jsonbuffer []byte 161 162 jsonpayload := bytes.NewBuffer(jsonbuffer) 163 enc := json.NewEncoder(jsonpayload) 164 enc.Encode(repo) 165 166 log.Printf("Sending %s \n", jsonpayload) 167 168 repo_req, err := client.Post(fmt.Sprintf("2.0/repositories/%s/%s", 169 d.Get("owner").(string), 170 d.Get("name").(string), 171 ), jsonpayload) 172 173 decoder := json.NewDecoder(repo_req.Body) 174 err = decoder.Decode(&repo) 175 if err != nil { 176 return err 177 } 178 179 log.Printf("Received %s \n", repo_req.Body) 180 181 if repo_req.StatusCode != 200 { 182 return fmt.Errorf("Failed to create repository got status code %d", repo_req.StatusCode) 183 } 184 185 d.SetId(string(fmt.Sprintf("%s/%s", d.Get("owner").(string), d.Get("name").(string)))) 186 187 return resourceRepositoryRead(d, m) 188 } 189 func resourceRepositoryRead(d *schema.ResourceData, m interface{}) error { 190 191 client := m.(*BitbucketClient) 192 repo_req, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s", 193 d.Get("owner").(string), 194 d.Get("name").(string), 195 )) 196 197 if err != nil { 198 return err 199 } 200 201 var repo Repository 202 203 decoder := json.NewDecoder(repo_req.Body) 204 err = decoder.Decode(&repo) 205 if err != nil { 206 return err 207 } 208 209 d.Set("scm", repo.SCM) 210 d.Set("is_private", repo.IsPrivate) 211 d.Set("has_wiki", repo.HasWiki) 212 d.Set("has_issues", repo.HasIssues) 213 d.Set("name", repo.Name) 214 d.Set("language", repo.Language) 215 d.Set("fork_policy", repo.ForkPolicy) 216 d.Set("website", repo.Website) 217 d.Set("description", repo.Description) 218 d.Set("project_key", repo.Project.Key) 219 220 for _, clone_url := range repo.Links.Clone { 221 if clone_url.Name == "https" { 222 d.Set("clone_https", clone_url.Href) 223 } else { 224 d.Set("clone_ssh", clone_url.Href) 225 } 226 } 227 228 return nil 229 } 230 231 func resourceRepositoryDelete(d *schema.ResourceData, m interface{}) error { 232 client := m.(*BitbucketClient) 233 delete_response, err := client.Delete(fmt.Sprintf("2.0/repositories/%s/%s", 234 d.Get("owner").(string), 235 d.Get("name").(string), 236 )) 237 238 if err != nil { 239 return err 240 } 241 242 if delete_response.StatusCode != 204 { 243 return fmt.Errorf("Failed to delete the repository got status code %d", delete_response.StatusCode) 244 } 245 246 return nil 247 }