terraform/builtin/providers/bitbucket/resource_hook.go

213 lines
4.4 KiB
Go
Raw Normal View History

2016-06-29 04:28:49 +02:00
package bitbucket
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/url"
2016-06-29 04:28:49 +02:00
"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)
payload, err := json.Marshal(hook)
if err != nil {
return err
}
2016-06-29 04:28:49 +02:00
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),
), bytes.NewBuffer(payload))
2016-06-29 04:28:49 +02:00
if err != nil {
return err
}
body, readerr := ioutil.ReadAll(hook_req.Body)
if readerr != nil {
return readerr
}
decodeerr := json.Unmarshal(body, &hook)
if decodeerr != nil {
return decodeerr
}
2016-08-09 18:54:38 +02:00
d.SetId(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, _ := 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),
url.PathEscape(d.Id()),
2016-06-29 04:28:49 +02:00
))
log.Printf("ID: %s", url.PathEscape(d.Id()))
2016-06-29 04:28:49 +02:00
if hook_req.StatusCode == 200 {
var hook Hook
2016-06-29 04:28:49 +02:00
body, readerr := ioutil.ReadAll(hook_req.Body)
if readerr != nil {
return readerr
}
2016-06-29 04:28:49 +02:00
decodeerr := json.Unmarshal(body, &hook)
if decodeerr != nil {
return decodeerr
}
2016-06-29 04:28:49 +02:00
d.Set("uuid", hook.Uuid)
d.Set("description", hook.Description)
d.Set("active", hook.Active)
d.Set("url", hook.Url)
2016-06-29 04:28:49 +02:00
eventsList := make([]string, 0, len(hook.Events))
2016-06-29 04:28:49 +02:00
for _, event := range hook.Events {
eventsList = append(eventsList, event)
}
d.Set("events", eventsList)
}
2016-06-29 04:28:49 +02:00
return nil
}
func resourceHookUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*BitbucketClient)
hook := createHook(d)
payload, err := json.Marshal(hook)
2016-06-29 04:28:49 +02:00
if err != nil {
return err
}
_, err = client.Put(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s",
d.Get("owner").(string),
d.Get("repository").(string),
url.PathEscape(d.Id()),
), bytes.NewBuffer(payload))
2016-06-29 04:28:49 +02:00
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),
url.PathEscape(d.Id()),
2016-06-29 04:28:49 +02:00
))
if err != nil {
panic(err)
}
if hook_req.StatusCode != 200 {
return false, err
2016-06-29 04:28:49 +02:00
}
return true, nil
}
return false, nil
2016-06-29 04:28:49 +02:00
}
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),
url.PathEscape(d.Id()),
2016-06-29 04:28:49 +02:00
))
return err
2016-06-29 04:28:49 +02:00
}