terraform/builtin/providers/pagerduty/resource_pagerduty_service_...

162 lines
4.1 KiB
Go
Raw Normal View History

2016-10-16 13:18:06 +02:00
package pagerduty
import (
"log"
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{
"name": {
2016-10-16 13:18:06 +02:00
Type: schema.TypeString,
Optional: true,
},
"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
},
"type": {
2016-10-16 13:18:06 +02:00
Type: schema.TypeString,
Required: true,
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
},
"vendor": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
"integration_key": {
2016-10-16 13:18:06 +02:00
Type: schema.TypeString,
Optional: true,
2016-10-16 13:18:06 +02:00
Computed: true,
},
"integration_email": {
2016-10-16 13:18:06 +02:00
Type: schema.TypeString,
Optional: true,
2016-10-16 13:18:06 +02:00
Computed: true,
},
},
}
}
func buildServiceIntegrationStruct(d *schema.ResourceData) *pagerduty.Integration {
serviceIntegration := pagerduty.Integration{
2016-10-16 13:18:06 +02:00
Name: d.Get("name").(string),
Type: d.Get("type").(string),
2016-10-16 13:18:06 +02:00
Service: &pagerduty.APIObject{
Type: "service_reference",
2016-10-16 13:18:06 +02:00
ID: d.Get("service").(string),
},
APIObject: pagerduty.APIObject{
ID: d.Id(),
Type: "service_integration",
},
2016-10-16 13:18:06 +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)
serviceIntegration := buildServiceIntegrationStruct(d)
2016-10-16 13:18:06 +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)
serviceIntegration, err := client.CreateIntegration(service, *serviceIntegration)
2016-10-16 13:18:06 +02:00
if err != nil {
return err
}
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)
o := &pagerduty.GetIntegrationOptions{}
serviceIntegration, err := client.GetIntegration(service, d.Id(), *o)
2016-10-16 13:18:06 +02:00
if err != nil {
return err
}
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)
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
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
}