terraform/builtin/providers/bitbucket/resource_hook.go

209 lines
4.4 KiB
Go
Raw Normal View History

2016-06-29 04:28:49 +02:00
package bitbucket
import (
"bytes"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
)
type Hook struct {
Uuid string `json:"uuid,omitempty"`
Url string `json:"url,omitempty"`
Description string `json:"description,omitempty"`
Active bool `json:"active,omitempty"`
Events []string `json:"events,omitempty"`
}
func resourceHook() *schema.Resource {
return &schema.Resource{
Create: resourceHookCreate,
Read: resourceHookRead,
Update: resourceHookUpdate,
Delete: resourceHookDelete,
Exists: resourceHookExists,
Schema: map[string]*schema.Schema{
2016-09-08 21:57:12 +02:00
"owner": &schema.Schema{
2016-06-29 04:28:49 +02:00
Type: schema.TypeString,
Required: true,
2016-08-09 18:54:38 +02:00
ForceNew: true,
2016-06-29 04:28:49 +02:00
},
"repository": &schema.Schema{
Type: schema.TypeString,
Required: true,
2016-08-09 18:54:38 +02:00
ForceNew: true,
2016-06-29 04:28:49 +02:00
},
"active": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"url": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"uuid": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"events": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}
func createHook(d *schema.ResourceData) *Hook {
events := make([]string, 0, len(d.Get("events").(*schema.Set).List()))
for _, item := range d.Get("events").(*schema.Set).List() {
events = append(events, item.(string))
}
return &Hook{
Url: d.Get("url").(string),
Description: d.Get("description").(string),
Active: d.Get("active").(bool),
Events: events,
}
}
func resourceHookCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*BitbucketClient)
hook := createHook(d)
var jsonbuffer []byte
jsonpayload := bytes.NewBuffer(jsonbuffer)
enc := json.NewEncoder(jsonpayload)
enc.Encode(hook)
hook_req, err := client.Post(fmt.Sprintf("2.0/repositories/%s/%s/hooks",
2016-09-08 21:57:12 +02:00
d.Get("owner").(string),
2016-06-29 04:28:49 +02:00
d.Get("repository").(string),
), jsonpayload)
decoder := json.NewDecoder(hook_req.Body)
err = decoder.Decode(&hook)
if err != nil {
return err
}
2016-08-09 18:54:38 +02:00
d.SetId(hook.Uuid)
d.Set("uuid", hook.Uuid)
2016-06-29 04:28:49 +02:00
return resourceHookRead(d, m)
}
func resourceHookRead(d *schema.ResourceData, m interface{}) error {
client := m.(*BitbucketClient)
hook_req, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s",
2016-09-08 21:57:12 +02:00
d.Get("owner").(string),
2016-06-29 04:28:49 +02:00
d.Get("repository").(string),
d.Get("uuid").(string),
))
if err != nil {
return err
}
var hook Hook
decoder := json.NewDecoder(hook_req.Body)
err = decoder.Decode(&hook)
if err != nil {
return err
}
d.Set("uuid", string(hook.Uuid))
d.Set("description", string(hook.Description))
d.Set("active", bool(hook.Active))
d.Set("url", string(hook.Url))
eventsList := make([]string, 0, len(hook.Events))
for _, event := range hook.Events {
eventsList = append(eventsList, string(event))
}
d.Set("events", eventsList)
return nil
}
func resourceHookUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*BitbucketClient)
hook := createHook(d)
var jsonbuffer []byte
jsonpayload := bytes.NewBuffer(jsonbuffer)
enc := json.NewEncoder(jsonpayload)
enc.Encode(hook)
hook_req, err := client.Put(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s",
2016-09-08 21:57:12 +02:00
d.Get("owner").(string),
2016-06-29 04:28:49 +02:00
d.Get("repository").(string),
d.Get("uuid").(string),
), jsonpayload)
if err != nil {
return err
}
decoder := json.NewDecoder(hook_req.Body)
err = decoder.Decode(&hook)
if err != nil {
return err
}
return resourceHookRead(d, m)
}
func resourceHookExists(d *schema.ResourceData, m interface{}) (bool, error) {
client := m.(*BitbucketClient)
if _, okay := d.GetOk("uuid"); okay {
hook_req, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s",
2016-09-08 21:57:12 +02:00
d.Get("owner").(string),
2016-06-29 04:28:49 +02:00
d.Get("repository").(string),
d.Get("uuid").(string),
))
if err != nil {
panic(err)
}
if hook_req.StatusCode != 200 {
d.SetId("")
return false, nil
}
return true, nil
} else {
return false, nil
}
}
func resourceHookDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*BitbucketClient)
_, err := client.Delete(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s",
2016-09-08 21:57:12 +02:00
d.Get("owner").(string),
2016-06-29 04:28:49 +02:00
d.Get("repository").(string),
d.Get("uuid").(string),
))
if err != nil {
return err
}
return nil
}