github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/pagerduty/resource_pagerduty_schedule.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 resourcePagerDutySchedule() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourcePagerDutyScheduleCreate,
    13  		Read:   resourcePagerDutyScheduleRead,
    14  		Update: resourcePagerDutyScheduleUpdate,
    15  		Delete: resourcePagerDutyScheduleDelete,
    16  		Importer: &schema.ResourceImporter{
    17  			State: schema.ImportStatePassthrough,
    18  		},
    19  		Schema: map[string]*schema.Schema{
    20  			"name": {
    21  				Type:     schema.TypeString,
    22  				Optional: true,
    23  			},
    24  			"time_zone": {
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  			},
    28  			"description": {
    29  				Type:     schema.TypeString,
    30  				Optional: true,
    31  				Default:  "Managed by Terraform",
    32  			},
    33  			"layer": {
    34  				Type:     schema.TypeList,
    35  				Required: true,
    36  				ForceNew: true,
    37  				Elem: &schema.Resource{
    38  					Schema: map[string]*schema.Schema{
    39  						"id": {
    40  							Type:     schema.TypeString,
    41  							Computed: true,
    42  						},
    43  						"name": {
    44  							Type:     schema.TypeString,
    45  							Optional: true,
    46  							Computed: true,
    47  						},
    48  						"start": {
    49  							Type:     schema.TypeString,
    50  							Optional: true,
    51  							Computed: true,
    52  							DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
    53  								if old == "" {
    54  									return false
    55  								}
    56  								return true
    57  							},
    58  						},
    59  						"end": {
    60  							Type:     schema.TypeString,
    61  							Optional: true,
    62  						},
    63  						"rotation_virtual_start": {
    64  							Type:     schema.TypeString,
    65  							Optional: true,
    66  							Computed: true,
    67  							DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
    68  								if old == "" {
    69  									return false
    70  								}
    71  								return true
    72  							},
    73  						},
    74  						"rotation_turn_length_seconds": {
    75  							Type:     schema.TypeInt,
    76  							Required: true,
    77  						},
    78  						"users": {
    79  							Type:     schema.TypeList,
    80  							Required: true,
    81  							Elem: &schema.Schema{
    82  								Type: schema.TypeString,
    83  							},
    84  						},
    85  						"restriction": {
    86  							Optional: true,
    87  							Type:     schema.TypeList,
    88  							Elem: &schema.Resource{
    89  								Schema: map[string]*schema.Schema{
    90  									"type": {
    91  										Type:     schema.TypeString,
    92  										Required: true,
    93  									},
    94  									"start_time_of_day": {
    95  										Type:     schema.TypeString,
    96  										Required: true,
    97  									},
    98  									"start_day_of_week": {
    99  										Type:     schema.TypeInt,
   100  										Optional: true,
   101  									},
   102  									"duration_seconds": {
   103  										Type:     schema.TypeInt,
   104  										Required: true,
   105  									},
   106  								},
   107  							},
   108  						},
   109  					},
   110  				},
   111  			},
   112  		},
   113  	}
   114  }
   115  
   116  func buildScheduleStruct(d *schema.ResourceData) *pagerduty.Schedule {
   117  	scheduleLayers := d.Get("layer").([]interface{})
   118  
   119  	schedule := pagerduty.Schedule{
   120  		Name:           d.Get("name").(string),
   121  		TimeZone:       d.Get("time_zone").(string),
   122  		ScheduleLayers: expandScheduleLayers(scheduleLayers),
   123  	}
   124  
   125  	if attr, ok := d.GetOk("description"); ok {
   126  		schedule.Description = attr.(string)
   127  	}
   128  
   129  	return &schedule
   130  }
   131  
   132  func resourcePagerDutyScheduleCreate(d *schema.ResourceData, meta interface{}) error {
   133  	client := meta.(*pagerduty.Client)
   134  
   135  	schedule := buildScheduleStruct(d)
   136  
   137  	log.Printf("[INFO] Creating PagerDuty schedule: %s", schedule.Name)
   138  
   139  	schedule, err := client.CreateSchedule(*schedule)
   140  
   141  	if err != nil {
   142  		return err
   143  	}
   144  
   145  	d.SetId(schedule.ID)
   146  
   147  	return resourcePagerDutyScheduleRead(d, meta)
   148  }
   149  
   150  func resourcePagerDutyScheduleRead(d *schema.ResourceData, meta interface{}) error {
   151  	client := meta.(*pagerduty.Client)
   152  
   153  	log.Printf("[INFO] Reading PagerDuty schedule: %s", d.Id())
   154  
   155  	schedule, err := client.GetSchedule(d.Id(), pagerduty.GetScheduleOptions{})
   156  
   157  	if err != nil {
   158  		return err
   159  	}
   160  
   161  	d.Set("name", schedule.Name)
   162  	d.Set("time_zone", schedule.TimeZone)
   163  	d.Set("description", schedule.Description)
   164  
   165  	if err := d.Set("layer", flattenScheduleLayers(schedule.ScheduleLayers)); err != nil {
   166  		return err
   167  	}
   168  
   169  	return nil
   170  }
   171  
   172  func resourcePagerDutyScheduleUpdate(d *schema.ResourceData, meta interface{}) error {
   173  	client := meta.(*pagerduty.Client)
   174  
   175  	schedule := buildScheduleStruct(d)
   176  
   177  	log.Printf("[INFO] Updating PagerDuty schedule: %s", d.Id())
   178  
   179  	if _, err := client.UpdateSchedule(d.Id(), *schedule); err != nil {
   180  		return err
   181  	}
   182  
   183  	return nil
   184  }
   185  
   186  func resourcePagerDutyScheduleDelete(d *schema.ResourceData, meta interface{}) error {
   187  	client := meta.(*pagerduty.Client)
   188  
   189  	log.Printf("[INFO] Deleting PagerDuty schedule: %s", d.Id())
   190  
   191  	if err := client.DeleteSchedule(d.Id()); err != nil {
   192  		return err
   193  	}
   194  
   195  	d.SetId("")
   196  
   197  	return nil
   198  }