github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/pagerduty/resource_pagerduty_addon.go (about) 1 package pagerduty 2 3 import ( 4 "log" 5 6 "github.com/PagerDuty/go-pagerduty" 7 "github.com/hashicorp/terraform/helper/schema" 8 ) 9 10 func resourcePagerDutyAddon() *schema.Resource { 11 return &schema.Resource{ 12 Create: resourcePagerDutyAddonCreate, 13 Read: resourcePagerDutyAddonRead, 14 Update: resourcePagerDutyAddonUpdate, 15 Delete: resourcePagerDutyAddonDelete, 16 Importer: &schema.ResourceImporter{ 17 State: schema.ImportStatePassthrough, 18 }, 19 Schema: map[string]*schema.Schema{ 20 "name": { 21 Type: schema.TypeString, 22 Required: true, 23 }, 24 "src": { 25 Type: schema.TypeString, 26 Required: true, 27 }, 28 }, 29 } 30 } 31 32 func buildAddonStruct(d *schema.ResourceData) *pagerduty.Addon { 33 addon := pagerduty.Addon{ 34 Name: d.Get("name").(string), 35 Src: d.Get("src").(string), 36 APIObject: pagerduty.APIObject{ 37 Type: "full_page_addon", 38 }, 39 } 40 41 return &addon 42 } 43 44 func resourcePagerDutyAddonCreate(d *schema.ResourceData, meta interface{}) error { 45 client := meta.(*pagerduty.Client) 46 47 addon := buildAddonStruct(d) 48 49 log.Printf("[INFO] Creating PagerDuty add-on %s", addon.Name) 50 51 addon, err := client.InstallAddon(*addon) 52 if err != nil { 53 return err 54 } 55 56 d.SetId(addon.ID) 57 58 return resourcePagerDutyAddonRead(d, meta) 59 } 60 61 func resourcePagerDutyAddonRead(d *schema.ResourceData, meta interface{}) error { 62 client := meta.(*pagerduty.Client) 63 64 log.Printf("[INFO] Reading PagerDuty add-on %s", d.Id()) 65 66 addon, err := client.GetAddon(d.Id()) 67 if err != nil { 68 if isNotFound(err) { 69 d.SetId("") 70 return nil 71 } 72 return err 73 } 74 75 d.Set("name", addon.Name) 76 d.Set("src", addon.Src) 77 78 return nil 79 } 80 81 func resourcePagerDutyAddonUpdate(d *schema.ResourceData, meta interface{}) error { 82 client := meta.(*pagerduty.Client) 83 84 addon := buildAddonStruct(d) 85 86 log.Printf("[INFO] Updating PagerDuty add-on %s", d.Id()) 87 88 if _, err := client.UpdateAddon(d.Id(), *addon); err != nil { 89 return err 90 } 91 92 return nil 93 } 94 95 func resourcePagerDutyAddonDelete(d *schema.ResourceData, meta interface{}) error { 96 client := meta.(*pagerduty.Client) 97 98 log.Printf("[INFO] Deleting PagerDuty add-on %s", d.Id()) 99 100 if err := client.DeleteAddon(d.Id()); err != nil { 101 return err 102 } 103 104 d.SetId("") 105 106 return nil 107 }