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