github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/bitbucket/resource_default_reviewers.go (about) 1 package bitbucket 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/hashicorp/terraform/helper/schema" 7 ) 8 9 type Reviewer struct { 10 DisplayName string `json:"display_name,omitempty"` 11 UUID string `json:"uuid,omitempty"` 12 Username string `json:"username,omitempty"` 13 Type string `json:"type,omitempty"` 14 } 15 16 type PaginatedReviewers struct { 17 Values []Reviewer `json:"values,omitempty"` 18 } 19 20 func resourceDefaultReviewers() *schema.Resource { 21 return &schema.Resource{ 22 Create: resourceDefaultReviewersCreate, 23 Read: resourceDefaultReviewersRead, 24 Delete: resourceDefaultReviewersDelete, 25 26 Schema: map[string]*schema.Schema{ 27 "owner": { 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 "repository": { 33 Type: schema.TypeString, 34 Required: true, 35 ForceNew: true, 36 }, 37 "reviewers": { 38 Type: schema.TypeSet, 39 Elem: &schema.Schema{Type: schema.TypeString}, 40 Required: true, 41 Set: schema.HashString, 42 ForceNew: true, 43 }, 44 }, 45 } 46 } 47 48 func resourceDefaultReviewersCreate(d *schema.ResourceData, m interface{}) error { 49 client := m.(*BitbucketClient) 50 51 for _, user := range d.Get("reviewers").(*schema.Set).List() { 52 reviewer_resp, err := client.PutOnly(fmt.Sprintf("2.0/repositories/%s/%s/default-reviewers/%s", 53 d.Get("owner").(string), 54 d.Get("repository").(string), 55 user, 56 )) 57 58 if err != nil { 59 return err 60 } 61 62 if reviewer_resp.StatusCode != 200 { 63 return fmt.Errorf("Failed to create reviewer %s got code %d", user.(string), reviewer_resp.StatusCode) 64 } 65 66 defer reviewer_resp.Body.Close() 67 } 68 69 d.SetId(fmt.Sprintf("%s/%s/reviewers", d.Get("owner").(string), d.Get("repository").(string))) 70 return resourceDefaultReviewersRead(d, m) 71 } 72 func resourceDefaultReviewersRead(d *schema.ResourceData, m interface{}) error { 73 client := m.(*BitbucketClient) 74 75 reviewers_response, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/default-reviewers", 76 d.Get("owner").(string), 77 d.Get("repository").(string), 78 )) 79 80 var reviewers PaginatedReviewers 81 82 decoder := json.NewDecoder(reviewers_response.Body) 83 err = decoder.Decode(&reviewers) 84 if err != nil { 85 return err 86 } 87 88 terraform_reviewers := make([]string, 0, len(reviewers.Values)) 89 90 for _, reviewer := range reviewers.Values { 91 terraform_reviewers = append(terraform_reviewers, reviewer.Username) 92 } 93 94 d.Set("reviewers", terraform_reviewers) 95 96 return nil 97 } 98 func resourceDefaultReviewersDelete(d *schema.ResourceData, m interface{}) error { 99 client := m.(*BitbucketClient) 100 101 for _, user := range d.Get("reviewers").(*schema.Set).List() { 102 resp, err := client.Delete(fmt.Sprintf("2.0/repositories/%s/%s/default-reviewers/%s", 103 d.Get("owner").(string), 104 d.Get("repository").(string), 105 user.(string), 106 )) 107 108 if err != nil { 109 return err 110 } 111 112 if resp.StatusCode != 204 { 113 return fmt.Errorf("[%d] Could not delete %s from default reviewer", 114 resp.StatusCode, 115 user.(string), 116 ) 117 } 118 defer resp.Body.Close() 119 } 120 return nil 121 }