github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/github/resource_github_organization_webhook.go (about)

     1  package github
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"github.com/google/go-github/github"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceGithubOrganizationWebhook() *schema.Resource {
    13  
    14  	return &schema.Resource{
    15  		Create: resourceGithubOrganizationWebhookCreate,
    16  		Read:   resourceGithubOrganizationWebhookRead,
    17  		Update: resourceGithubOrganizationWebhookUpdate,
    18  		Delete: resourceGithubOrganizationWebhookDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"name": {
    22  				Type:         schema.TypeString,
    23  				Required:     true,
    24  				ForceNew:     true,
    25  				ValidateFunc: validateGithubOrganizationWebhookName,
    26  			},
    27  			"events": &schema.Schema{
    28  				Type:     schema.TypeSet,
    29  				Required: true,
    30  				Elem:     &schema.Schema{Type: schema.TypeString},
    31  				Set:      schema.HashString,
    32  			},
    33  			"configuration": {
    34  				Type:     schema.TypeMap,
    35  				Optional: true,
    36  			},
    37  			"url": {
    38  				Type:     schema.TypeString,
    39  				Computed: true,
    40  			},
    41  			"active": {
    42  				Type:     schema.TypeBool,
    43  				Optional: true,
    44  				Default:  true,
    45  			},
    46  		},
    47  	}
    48  }
    49  
    50  func validateGithubOrganizationWebhookName(v interface{}, k string) (ws []string, errors []error) {
    51  	if v.(string) != "web" {
    52  		errors = append(errors, fmt.Errorf("Github: name can only be web"))
    53  	}
    54  	return
    55  }
    56  
    57  func resourceGithubOrganizationWebhookObject(d *schema.ResourceData) *github.Hook {
    58  	url := d.Get("url").(string)
    59  	active := d.Get("active").(bool)
    60  	events := []string{}
    61  	eventSet := d.Get("events").(*schema.Set)
    62  	for _, v := range eventSet.List() {
    63  		events = append(events, v.(string))
    64  	}
    65  	name := d.Get("name").(string)
    66  
    67  	hook := &github.Hook{
    68  		Name:   &name,
    69  		URL:    &url,
    70  		Events: events,
    71  		Active: &active,
    72  		Config: d.Get("configuration").(map[string]interface{}),
    73  	}
    74  
    75  	return hook
    76  }
    77  
    78  func resourceGithubOrganizationWebhookCreate(d *schema.ResourceData, meta interface{}) error {
    79  	client := meta.(*Organization).client
    80  	hk := resourceGithubOrganizationWebhookObject(d)
    81  
    82  	hook, _, err := client.Organizations.CreateHook(context.TODO(), meta.(*Organization).name, hk)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	d.SetId(strconv.Itoa(*hook.ID))
    87  
    88  	return resourceGithubOrganizationWebhookRead(d, meta)
    89  }
    90  
    91  func resourceGithubOrganizationWebhookRead(d *schema.ResourceData, meta interface{}) error {
    92  	client := meta.(*Organization).client
    93  	hookID, _ := strconv.Atoi(d.Id())
    94  
    95  	hook, resp, err := client.Organizations.GetHook(context.TODO(), meta.(*Organization).name, hookID)
    96  	if err != nil {
    97  		if resp.StatusCode == 404 {
    98  			d.SetId("")
    99  			return nil
   100  		}
   101  		return err
   102  	}
   103  	d.Set("name", hook.Name)
   104  	d.Set("url", hook.URL)
   105  	d.Set("active", hook.Active)
   106  	d.Set("events", hook.Events)
   107  	d.Set("configuration", hook.Config)
   108  
   109  	return nil
   110  }
   111  
   112  func resourceGithubOrganizationWebhookUpdate(d *schema.ResourceData, meta interface{}) error {
   113  	client := meta.(*Organization).client
   114  	hk := resourceGithubOrganizationWebhookObject(d)
   115  	hookID, err := strconv.Atoi(d.Id())
   116  	if err != nil {
   117  		return err
   118  	}
   119  
   120  	_, _, err = client.Organizations.EditHook(context.TODO(), meta.(*Organization).name, hookID, hk)
   121  	if err != nil {
   122  		return err
   123  	}
   124  
   125  	return resourceGithubOrganizationWebhookRead(d, meta)
   126  }
   127  
   128  func resourceGithubOrganizationWebhookDelete(d *schema.ResourceData, meta interface{}) error {
   129  	client := meta.(*Organization).client
   130  	hookID, err := strconv.Atoi(d.Id())
   131  	if err != nil {
   132  		return err
   133  	}
   134  
   135  	_, err = client.Organizations.DeleteHook(context.TODO(), meta.(*Organization).name, hookID)
   136  	return err
   137  }