Add support for service integration

This commit is contained in:
Alexander Hellbom 2016-10-16 13:18:06 +02:00
parent 5c99f1317a
commit 9e81677354
2 changed files with 140 additions and 5 deletions

View File

@ -19,11 +19,12 @@ func Provider() terraform.ResourceProvider {
},
ResourcesMap: map[string]*schema.Resource{
"pagerduty_user": resourcePagerDutyUser(),
"pagerduty_team": resourcePagerDutyTeam(),
"pagerduty_service": resourcePagerDutyService(),
"pagerduty_schedule": resourcePagerDutySchedule(),
"pagerduty_escalation_policy": resourcePagerDutyEscalationPolicy(),
"pagerduty_user": resourcePagerDutyUser(),
"pagerduty_team": resourcePagerDutyTeam(),
"pagerduty_service": resourcePagerDutyService(),
"pagerduty_service_integration": resourcePagerDutyServiceIntegration(),
"pagerduty_schedule": resourcePagerDutySchedule(),
"pagerduty_escalation_policy": resourcePagerDutyEscalationPolicy(),
},
ConfigureFunc: providerConfigure,

View File

@ -0,0 +1,134 @@
package pagerduty
import (
"log"
"github.com/PagerDuty/go-pagerduty"
"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,
Importer: &schema.ResourceImporter{
State: resourcePagerDutyServiceIntegrationImport,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"service": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"integration_key": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"integration_email": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func buildServiceIntegrationStruct(d *schema.ResourceData) *pagerduty.Integration {
service := pagerduty.Integration{
Type: d.Get("type").(string),
Name: d.Get("name").(string),
Service: &pagerduty.APIObject{
Type: "service",
ID: d.Get("service").(string),
},
}
return &service
}
func resourcePagerDutyServiceIntegrationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
i := buildServiceIntegrationStruct(d)
log.Printf("[INFO] Creating PagerDuty service integration %s", i.Name)
service := d.Get("service").(string)
s, err := client.CreateIntegration(service, *i)
if err != nil {
return err
}
d.SetId(s.ID)
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)
i, err := client.GetIntegration(service, d.Id(), pagerduty.GetIntegrationOptions{})
if err != nil {
return err
}
d.Set("name", i.Name)
d.Set("type", i.Type)
d.Set("service", i.Service)
d.Set("integration_key", i.IntegrationKey)
d.Set("integration_email", i.IntegrationEmail)
return nil
}
func resourcePagerDutyServiceIntegrationUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
s := buildServiceIntegrationStruct(d)
s.ID = d.Id()
service := d.Get("service").(string)
log.Printf("[INFO] Updating PagerDuty service %s", d.Id())
s, err := client.UpdateIntegration(service, *s)
if err != nil {
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
}
func resourcePagerDutyServiceIntegrationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if err := resourcePagerDutyServiceIntegrationRead(d, meta); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}