github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/bitbucket/resource_hook.go (about)

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