github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/pagerduty/resource_pagerduty_service_integration.go (about) 1 package pagerduty 2 3 import ( 4 "log" 5 6 pagerduty "github.com/PagerDuty/go-pagerduty" 7 "github.com/hashicorp/terraform/helper/schema" 8 ) 9 10 func resourcePagerDutyServiceIntegration() *schema.Resource { 11 return &schema.Resource{ 12 Create: resourcePagerDutyServiceIntegrationCreate, 13 Read: resourcePagerDutyServiceIntegrationRead, 14 Update: resourcePagerDutyServiceIntegrationUpdate, 15 // NOTE: It's currently not possible to delete integrations via the API. 16 // Therefore it needs to be manually removed from the Web UI. 17 Delete: resourcePagerDutyServiceIntegrationDelete, 18 Schema: map[string]*schema.Schema{ 19 "name": { 20 Type: schema.TypeString, 21 Optional: true, 22 }, 23 "service": { 24 Type: schema.TypeString, 25 Optional: true, 26 }, 27 "type": { 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 ValidateFunc: validateValueFunc([]string{ 32 "aws_cloudwatch_inbound_integration", 33 "cloudkick_inbound_integration", 34 "event_transformer_api_inbound_integration", 35 "generic_email_inbound_integration", 36 "generic_events_api_inbound_integration", 37 "keynote_inbound_integration", 38 "nagios_inbound_integration", 39 "pingdom_inbound_integration", 40 "sql_monitor_inbound_integration", 41 }), 42 }, 43 "vendor": { 44 Type: schema.TypeString, 45 ForceNew: true, 46 Optional: true, 47 }, 48 "integration_key": { 49 Type: schema.TypeString, 50 Optional: true, 51 Computed: true, 52 }, 53 "integration_email": { 54 Type: schema.TypeString, 55 Optional: true, 56 Computed: true, 57 }, 58 }, 59 } 60 } 61 62 func buildServiceIntegrationStruct(d *schema.ResourceData) *pagerduty.Integration { 63 serviceIntegration := pagerduty.Integration{ 64 Name: d.Get("name").(string), 65 Type: d.Get("type").(string), 66 Service: &pagerduty.APIObject{ 67 Type: "service_reference", 68 ID: d.Get("service").(string), 69 }, 70 APIObject: pagerduty.APIObject{ 71 ID: d.Id(), 72 Type: "service_integration", 73 }, 74 } 75 76 if attr, ok := d.GetOk("integration_key"); ok { 77 serviceIntegration.IntegrationKey = attr.(string) 78 } 79 80 if attr, ok := d.GetOk("integration_email"); ok { 81 serviceIntegration.IntegrationEmail = attr.(string) 82 } 83 84 if attr, ok := d.GetOk("vendor"); ok { 85 serviceIntegration.Vendor = &pagerduty.APIObject{ 86 ID: attr.(string), 87 Type: "vendor_reference", 88 } 89 } 90 91 return &serviceIntegration 92 } 93 94 func resourcePagerDutyServiceIntegrationCreate(d *schema.ResourceData, meta interface{}) error { 95 client := meta.(*pagerduty.Client) 96 97 serviceIntegration := buildServiceIntegrationStruct(d) 98 99 log.Printf("[INFO] Creating PagerDuty service integration %s", serviceIntegration.Name) 100 101 service := d.Get("service").(string) 102 103 serviceIntegration, err := client.CreateIntegration(service, *serviceIntegration) 104 105 if err != nil { 106 return err 107 } 108 109 d.SetId(serviceIntegration.ID) 110 111 return resourcePagerDutyServiceIntegrationRead(d, meta) 112 } 113 114 func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interface{}) error { 115 client := meta.(*pagerduty.Client) 116 117 log.Printf("[INFO] Reading PagerDuty service integration %s", d.Id()) 118 119 service := d.Get("service").(string) 120 121 o := &pagerduty.GetIntegrationOptions{} 122 123 serviceIntegration, err := client.GetIntegration(service, d.Id(), *o) 124 125 if err != nil { 126 return err 127 } 128 129 d.Set("name", serviceIntegration.Name) 130 d.Set("type", serviceIntegration.Type) 131 d.Set("service", serviceIntegration.Service) 132 d.Set("vendor", serviceIntegration.Vendor) 133 d.Set("integration_key", serviceIntegration.IntegrationKey) 134 d.Set("integration_email", serviceIntegration.IntegrationEmail) 135 136 return nil 137 } 138 139 func resourcePagerDutyServiceIntegrationUpdate(d *schema.ResourceData, meta interface{}) error { 140 client := meta.(*pagerduty.Client) 141 142 serviceIntegration := buildServiceIntegrationStruct(d) 143 144 service := d.Get("service").(string) 145 146 log.Printf("[INFO] Updating PagerDuty service integration %s", d.Id()) 147 148 if _, err := client.UpdateIntegration(service, *serviceIntegration); err != nil { 149 return err 150 } 151 152 return nil 153 } 154 155 func resourcePagerDutyServiceIntegrationDelete(d *schema.ResourceData, meta interface{}) error { 156 log.Printf("[INFO] Removing PagerDuty service integration %s", d.Id()) 157 158 d.SetId("") 159 160 return nil 161 }