terraform/builtin/providers/pagerduty/resource_pagerduty_team.go

115 lines
2.3 KiB
Go

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{
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
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)
team := buildTeamStruct(d)
log.Printf("[INFO] Creating PagerDuty team %s", team.Name)
team, err := client.CreateTeam(team)
if err != nil {
return err
}
d.SetId(team.ID)
return nil
}
func resourcePagerDutyTeamRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
log.Printf("[INFO] Reading PagerDuty team %s", d.Id())
team, err := client.GetTeam(d.Id())
if err != nil {
return err
}
d.Set("name", team.Name)
d.Set("description", team.Description)
return nil
}
func resourcePagerDutyTeamUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
team := buildTeamStruct(d)
log.Printf("[INFO] Updating PagerDuty team %s", d.Id())
if _, err := client.UpdateTeam(d.Id(), team); err != nil {
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())
if err := client.DeleteTeam(d.Id()); err != nil {
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
}