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