github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/bitbucket/resource_hook.go (about) 1 package bitbucket 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "github.com/hashicorp/terraform/helper/schema" 8 ) 9 10 type Hook struct { 11 Uuid string `json:"uuid,omitempty"` 12 Url string `json:"url,omitempty"` 13 Description string `json:"description,omitempty"` 14 Active bool `json:"active,omitempty"` 15 Events []string `json:"events,omitempty"` 16 } 17 18 func resourceHook() *schema.Resource { 19 return &schema.Resource{ 20 Create: resourceHookCreate, 21 Read: resourceHookRead, 22 Update: resourceHookUpdate, 23 Delete: resourceHookDelete, 24 Exists: resourceHookExists, 25 26 Schema: map[string]*schema.Schema{ 27 "owner": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 "repository": &schema.Schema{ 33 Type: schema.TypeString, 34 Required: true, 35 ForceNew: true, 36 }, 37 "active": &schema.Schema{ 38 Type: schema.TypeBool, 39 Optional: true, 40 Default: true, 41 }, 42 "url": &schema.Schema{ 43 Type: schema.TypeString, 44 Required: true, 45 }, 46 "uuid": &schema.Schema{ 47 Type: schema.TypeString, 48 Computed: true, 49 }, 50 "description": &schema.Schema{ 51 Type: schema.TypeString, 52 Required: true, 53 }, 54 "events": &schema.Schema{ 55 Type: schema.TypeSet, 56 Required: true, 57 Elem: &schema.Schema{Type: schema.TypeString}, 58 Set: schema.HashString, 59 }, 60 }, 61 } 62 } 63 64 func createHook(d *schema.ResourceData) *Hook { 65 66 events := make([]string, 0, len(d.Get("events").(*schema.Set).List())) 67 68 for _, item := range d.Get("events").(*schema.Set).List() { 69 events = append(events, item.(string)) 70 } 71 72 return &Hook{ 73 Url: d.Get("url").(string), 74 Description: d.Get("description").(string), 75 Active: d.Get("active").(bool), 76 Events: events, 77 } 78 } 79 80 func resourceHookCreate(d *schema.ResourceData, m interface{}) error { 81 client := m.(*BitbucketClient) 82 hook := createHook(d) 83 84 var jsonbuffer []byte 85 86 jsonpayload := bytes.NewBuffer(jsonbuffer) 87 enc := json.NewEncoder(jsonpayload) 88 enc.Encode(hook) 89 90 hook_req, err := client.Post(fmt.Sprintf("2.0/repositories/%s/%s/hooks", 91 d.Get("owner").(string), 92 d.Get("repository").(string), 93 ), jsonpayload) 94 95 decoder := json.NewDecoder(hook_req.Body) 96 err = decoder.Decode(&hook) 97 if err != nil { 98 return err 99 } 100 101 d.SetId(hook.Uuid) 102 d.Set("uuid", hook.Uuid) 103 104 return resourceHookRead(d, m) 105 } 106 func resourceHookRead(d *schema.ResourceData, m interface{}) error { 107 client := m.(*BitbucketClient) 108 hook_req, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s", 109 d.Get("owner").(string), 110 d.Get("repository").(string), 111 d.Get("uuid").(string), 112 )) 113 114 if err != nil { 115 return err 116 } 117 118 var hook Hook 119 120 decoder := json.NewDecoder(hook_req.Body) 121 err = decoder.Decode(&hook) 122 if err != nil { 123 return err 124 } 125 126 d.Set("uuid", hook.Uuid) 127 d.Set("description", hook.Description) 128 d.Set("active", hook.Active) 129 d.Set("url", hook.Url) 130 131 eventsList := make([]string, 0, len(hook.Events)) 132 133 for _, event := range hook.Events { 134 eventsList = append(eventsList, event) 135 } 136 137 d.Set("events", eventsList) 138 139 return nil 140 } 141 142 func resourceHookUpdate(d *schema.ResourceData, m interface{}) error { 143 client := m.(*BitbucketClient) 144 hook := createHook(d) 145 146 var jsonbuffer []byte 147 148 jsonpayload := bytes.NewBuffer(jsonbuffer) 149 enc := json.NewEncoder(jsonpayload) 150 enc.Encode(hook) 151 152 hook_req, err := client.Put(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s", 153 d.Get("owner").(string), 154 d.Get("repository").(string), 155 d.Get("uuid").(string), 156 ), jsonpayload) 157 158 if err != nil { 159 return err 160 } 161 162 decoder := json.NewDecoder(hook_req.Body) 163 err = decoder.Decode(&hook) 164 if err != nil { 165 return err 166 } 167 168 return resourceHookRead(d, m) 169 } 170 171 func resourceHookExists(d *schema.ResourceData, m interface{}) (bool, error) { 172 client := m.(*BitbucketClient) 173 if _, okay := d.GetOk("uuid"); okay { 174 hook_req, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s", 175 d.Get("owner").(string), 176 d.Get("repository").(string), 177 d.Get("uuid").(string), 178 )) 179 180 if err != nil { 181 panic(err) 182 } 183 184 if hook_req.StatusCode != 200 { 185 d.SetId("") 186 return false, nil 187 } 188 189 return true, nil 190 } else { 191 return false, nil 192 } 193 194 } 195 196 func resourceHookDelete(d *schema.ResourceData, m interface{}) error { 197 client := m.(*BitbucketClient) 198 _, err := client.Delete(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s", 199 d.Get("owner").(string), 200 d.Get("repository").(string), 201 d.Get("uuid").(string), 202 )) 203 204 if err != nil { 205 return err 206 } 207 return nil 208 }