2016-10-16 13:18:06 +02:00
|
|
|
package pagerduty
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
pagerduty "github.com/PagerDuty/go-pagerduty"
|
2016-10-16 13:18:06 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourcePagerDutyServiceIntegration() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourcePagerDutyServiceIntegrationCreate,
|
|
|
|
Read: resourcePagerDutyServiceIntegrationRead,
|
|
|
|
Update: resourcePagerDutyServiceIntegrationUpdate,
|
|
|
|
// NOTE: It's currently not possible to delete integrations via the API.
|
|
|
|
// Therefore it needs to be manually removed from the Web UI.
|
|
|
|
Delete: resourcePagerDutyServiceIntegrationDelete,
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-10-24 16:43:53 +02:00
|
|
|
"name": {
|
2016-10-16 13:18:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"service": {
|
2016-10-16 13:18:06 +02:00
|
|
|
Type: schema.TypeString,
|
2016-10-16 16:48:20 +02:00
|
|
|
Optional: true,
|
2016-10-16 13:18:06 +02:00
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"type": {
|
2016-10-16 13:18:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2016-10-20 06:53:51 +02:00
|
|
|
ForceNew: true,
|
2016-10-17 23:20:03 +02:00
|
|
|
ValidateFunc: validateValueFunc([]string{
|
|
|
|
"aws_cloudwatch_inbound_integration",
|
|
|
|
"cloudkick_inbound_integration",
|
|
|
|
"event_transformer_api_inbound_integration",
|
|
|
|
"generic_email_inbound_integration",
|
|
|
|
"generic_events_api_inbound_integration",
|
|
|
|
"keynote_inbound_integration",
|
|
|
|
"nagios_inbound_integration",
|
|
|
|
"pingdom_inbound_integration",
|
|
|
|
"sql_monitor_inbound_integration",
|
|
|
|
}),
|
2016-10-16 13:18:06 +02:00
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"vendor": {
|
2016-10-20 06:53:51 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
ForceNew: true,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"integration_key": {
|
2016-10-16 13:18:06 +02:00
|
|
|
Type: schema.TypeString,
|
2016-10-20 06:53:51 +02:00
|
|
|
Optional: true,
|
2016-10-16 13:18:06 +02:00
|
|
|
Computed: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"integration_email": {
|
2016-10-16 13:18:06 +02:00
|
|
|
Type: schema.TypeString,
|
2016-10-20 06:53:51 +02:00
|
|
|
Optional: true,
|
2016-10-16 13:18:06 +02:00
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildServiceIntegrationStruct(d *schema.ResourceData) *pagerduty.Integration {
|
2016-10-20 06:53:51 +02:00
|
|
|
serviceIntegration := pagerduty.Integration{
|
2016-10-16 13:18:06 +02:00
|
|
|
Name: d.Get("name").(string),
|
2016-10-20 06:53:51 +02:00
|
|
|
Type: d.Get("type").(string),
|
2016-10-16 13:18:06 +02:00
|
|
|
Service: &pagerduty.APIObject{
|
2016-10-20 06:53:51 +02:00
|
|
|
Type: "service_reference",
|
2016-10-16 13:18:06 +02:00
|
|
|
ID: d.Get("service").(string),
|
|
|
|
},
|
2016-10-18 14:54:33 +02:00
|
|
|
APIObject: pagerduty.APIObject{
|
2016-10-20 06:53:51 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
Type: "service_integration",
|
2016-10-18 14:54:33 +02:00
|
|
|
},
|
2016-10-16 13:18:06 +02:00
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
if attr, ok := d.GetOk("integration_key"); ok {
|
|
|
|
serviceIntegration.IntegrationKey = attr.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.GetOk("integration_email"); ok {
|
|
|
|
serviceIntegration.IntegrationEmail = attr.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.GetOk("vendor"); ok {
|
|
|
|
serviceIntegration.Vendor = &pagerduty.APIObject{
|
|
|
|
ID: attr.(string),
|
|
|
|
Type: "vendor_reference",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &serviceIntegration
|
2016-10-16 13:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyServiceIntegrationCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
serviceIntegration := buildServiceIntegrationStruct(d)
|
2016-10-16 13:18:06 +02:00
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
log.Printf("[INFO] Creating PagerDuty service integration %s", serviceIntegration.Name)
|
2016-10-16 13:18:06 +02:00
|
|
|
|
|
|
|
service := d.Get("service").(string)
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
serviceIntegration, err := client.CreateIntegration(service, *serviceIntegration)
|
2016-10-16 13:18:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
d.SetId(serviceIntegration.ID)
|
2016-10-16 13:18:06 +02:00
|
|
|
|
|
|
|
return resourcePagerDutyServiceIntegrationRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Reading PagerDuty service integration %s", d.Id())
|
|
|
|
|
|
|
|
service := d.Get("service").(string)
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
o := &pagerduty.GetIntegrationOptions{}
|
|
|
|
|
|
|
|
serviceIntegration, err := client.GetIntegration(service, d.Id(), *o)
|
2016-10-16 13:18:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
d.Set("name", serviceIntegration.Name)
|
|
|
|
d.Set("type", serviceIntegration.Type)
|
|
|
|
d.Set("service", serviceIntegration.Service)
|
|
|
|
d.Set("vendor", serviceIntegration.Vendor)
|
|
|
|
d.Set("integration_key", serviceIntegration.IntegrationKey)
|
|
|
|
d.Set("integration_email", serviceIntegration.IntegrationEmail)
|
2016-10-16 13:18:06 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyServiceIntegrationUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
serviceIntegration := buildServiceIntegrationStruct(d)
|
2016-10-16 13:18:06 +02:00
|
|
|
|
|
|
|
service := d.Get("service").(string)
|
|
|
|
|
2016-10-16 16:48:20 +02:00
|
|
|
log.Printf("[INFO] Updating PagerDuty service integration %s", d.Id())
|
2016-10-16 13:18:06 +02:00
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
if _, err := client.UpdateIntegration(service, *serviceIntegration); err != nil {
|
2016-10-16 13:18:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyServiceIntegrationDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
log.Printf("[INFO] Removing PagerDuty service integration %s", d.Id())
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|