terraform/builtin/providers/github/resource_github_organizatio...

138 lines
3.4 KiB
Go
Raw Normal View History

provider/github: add repository_webhook resource (#12924) * provider/github: add repository_webhook resource `repository_webhook` can be used to manage webhooks for repositories. It is currently limited to organization repositories only. The changeset includes both documentation and tests. The tests are green: ``` make testacc TEST=./builtin/providers/github TESTARGS='-run=TestAccGithubRepositoryWebhook_basic' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/21 16:20:07 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/github -v -run=TestAccGithubRepositoryWebhook_basic -timeout 120m === RUN TestAccGithubRepositoryWebhook_basic --- PASS: TestAccGithubRepositoryWebhook_basic (5.10s) PASS ok github.com/hashicorp/terraform/builtin/providers/github 5.113s ``` * provider/github: add github_organization_webhook the `github_organization_webhook` resource is similar to the `github_repository_webhook` resource, but it manages webhooks for an organization. the tests are green: ``` make testacc TEST=./builtin/providers/github TESTARGS='-run=TestAccGithubOrganizationWebhook' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/21 17:23:33 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/github -v -run=TestAccGithubOrganizationWebhook -timeout 120m === RUN TestAccGithubOrganizationWebhook_basic --- PASS: TestAccGithubOrganizationWebhook_basic (2.09s) PASS ok github.com/hashicorp/terraform/builtin/providers/github 2.109s ```
2017-03-23 10:21:45 +01:00
package github
import (
"context"
"fmt"
"strconv"
"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceGithubOrganizationWebhook() *schema.Resource {
return &schema.Resource{
Create: resourceGithubOrganizationWebhookCreate,
Read: resourceGithubOrganizationWebhookRead,
Update: resourceGithubOrganizationWebhookUpdate,
Delete: resourceGithubOrganizationWebhookDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateGithubOrganizationWebhookName,
},
"events": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"configuration": {
Type: schema.TypeMap,
Optional: true,
},
"url": {
Type: schema.TypeString,
Computed: true,
},
"active": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
},
}
}
func validateGithubOrganizationWebhookName(v interface{}, k string) (ws []string, errors []error) {
if v.(string) != "web" {
errors = append(errors, fmt.Errorf("Github: name can only be web"))
}
return
}
func resourceGithubOrganizationWebhookObject(d *schema.ResourceData) *github.Hook {
url := d.Get("url").(string)
active := d.Get("active").(bool)
events := []string{}
eventSet := d.Get("events").(*schema.Set)
for _, v := range eventSet.List() {
events = append(events, v.(string))
}
name := d.Get("name").(string)
hook := &github.Hook{
Name: &name,
URL: &url,
Events: events,
Active: &active,
Config: d.Get("configuration").(map[string]interface{}),
}
return hook
}
func resourceGithubOrganizationWebhookCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
hk := resourceGithubOrganizationWebhookObject(d)
hook, _, err := client.Organizations.CreateHook(context.TODO(), meta.(*Organization).name, hk)
if err != nil {
return err
}
d.SetId(strconv.Itoa(*hook.ID))
return resourceGithubOrganizationWebhookRead(d, meta)
}
func resourceGithubOrganizationWebhookRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
hookID, _ := strconv.Atoi(d.Id())
hook, resp, err := client.Organizations.GetHook(context.TODO(), meta.(*Organization).name, hookID)
if err != nil {
if resp.StatusCode == 404 {
d.SetId("")
return nil
}
return err
}
d.Set("name", hook.Name)
d.Set("url", hook.URL)
d.Set("active", hook.Active)
d.Set("events", hook.Events)
d.Set("configuration", hook.Config)
return nil
}
func resourceGithubOrganizationWebhookUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
hk := resourceGithubOrganizationWebhookObject(d)
hookID, err := strconv.Atoi(d.Id())
if err != nil {
return err
}
_, _, err = client.Organizations.EditHook(context.TODO(), meta.(*Organization).name, hookID, hk)
if err != nil {
return err
}
return resourceGithubOrganizationWebhookRead(d, meta)
}
func resourceGithubOrganizationWebhookDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
hookID, err := strconv.Atoi(d.Id())
if err != nil {
return err
}
_, err = client.Organizations.DeleteHook(context.TODO(), meta.(*Organization).name, hookID)
return err
}