2016-09-23 21:43:06 +02:00
|
|
|
package pagerduty
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/PagerDuty/go-pagerduty"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourcePagerDutyTeam() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourcePagerDutyTeamCreate,
|
|
|
|
Read: resourcePagerDutyTeamRead,
|
|
|
|
Update: resourcePagerDutyTeamUpdate,
|
|
|
|
Delete: resourcePagerDutyTeamDelete,
|
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: resourcePagerDutyTeamImport,
|
|
|
|
},
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-10-24 16:43:53 +02:00
|
|
|
"name": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"description": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "Managed by Terraform",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildTeamStruct(d *schema.ResourceData) *pagerduty.Team {
|
|
|
|
team := pagerduty.Team{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.GetOk("description"); ok {
|
|
|
|
team.Description = attr.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &team
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyTeamCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
team := buildTeamStruct(d)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
log.Printf("[INFO] Creating PagerDuty team %s", team.Name)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
team, err := client.CreateTeam(team)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
d.SetId(team.ID)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyTeamRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Reading PagerDuty team %s", d.Id())
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
team, err := client.GetTeam(d.Id())
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
d.Set("name", team.Name)
|
|
|
|
d.Set("description", team.Description)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyTeamUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
team := buildTeamStruct(d)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
log.Printf("[INFO] Updating PagerDuty team %s", d.Id())
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
if _, err := client.UpdateTeam(d.Id(), team); err != nil {
|
2016-09-23 21:43:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyTeamDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting PagerDuty team %s", d.Id())
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
if err := client.DeleteTeam(d.Id()); err != nil {
|
2016-09-23 21:43:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyTeamImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
|
|
|
if err := resourcePagerDutyTeamRead(d, meta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []*schema.ResourceData{d}, nil
|
|
|
|
}
|