github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/pagerduty/resource_pagerduty_team.go (about)

     1  package pagerduty
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/PagerDuty/go-pagerduty"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourcePagerDutyTeam() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourcePagerDutyTeamCreate,
    13  		Read:   resourcePagerDutyTeamRead,
    14  		Update: resourcePagerDutyTeamUpdate,
    15  		Delete: resourcePagerDutyTeamDelete,
    16  		Importer: &schema.ResourceImporter{
    17  			State: schema.ImportStatePassthrough,
    18  		},
    19  		Schema: map[string]*schema.Schema{
    20  			"name": {
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  			},
    24  			"description": {
    25  				Type:     schema.TypeString,
    26  				Optional: true,
    27  				Default:  "Managed by Terraform",
    28  			},
    29  		},
    30  	}
    31  }
    32  
    33  func buildTeamStruct(d *schema.ResourceData) *pagerduty.Team {
    34  	team := pagerduty.Team{
    35  		Name: d.Get("name").(string),
    36  	}
    37  
    38  	if attr, ok := d.GetOk("description"); ok {
    39  		team.Description = attr.(string)
    40  	}
    41  
    42  	return &team
    43  }
    44  
    45  func resourcePagerDutyTeamCreate(d *schema.ResourceData, meta interface{}) error {
    46  	client := meta.(*pagerduty.Client)
    47  
    48  	team := buildTeamStruct(d)
    49  
    50  	log.Printf("[INFO] Creating PagerDuty team %s", team.Name)
    51  
    52  	team, err := client.CreateTeam(team)
    53  
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	d.SetId(team.ID)
    59  
    60  	return nil
    61  
    62  }
    63  
    64  func resourcePagerDutyTeamRead(d *schema.ResourceData, meta interface{}) error {
    65  	client := meta.(*pagerduty.Client)
    66  
    67  	log.Printf("[INFO] Reading PagerDuty team %s", d.Id())
    68  
    69  	team, err := client.GetTeam(d.Id())
    70  
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	d.Set("name", team.Name)
    76  	d.Set("description", team.Description)
    77  
    78  	return nil
    79  }
    80  
    81  func resourcePagerDutyTeamUpdate(d *schema.ResourceData, meta interface{}) error {
    82  	client := meta.(*pagerduty.Client)
    83  
    84  	team := buildTeamStruct(d)
    85  
    86  	log.Printf("[INFO] Updating PagerDuty team %s", d.Id())
    87  
    88  	if _, err := client.UpdateTeam(d.Id(), team); err != nil {
    89  		return err
    90  	}
    91  
    92  	return nil
    93  }
    94  
    95  func resourcePagerDutyTeamDelete(d *schema.ResourceData, meta interface{}) error {
    96  	client := meta.(*pagerduty.Client)
    97  
    98  	log.Printf("[INFO] Deleting PagerDuty team %s", d.Id())
    99  
   100  	if err := client.DeleteTeam(d.Id()); err != nil {
   101  		return err
   102  	}
   103  
   104  	d.SetId("")
   105  
   106  	return nil
   107  }