github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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: resourcePagerDutyScheduleImport, 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 "duration_seconds": { 99 Type: schema.TypeInt, 100 Required: true, 101 }, 102 }, 103 }, 104 }, 105 }, 106 }, 107 }, 108 }, 109 } 110 } 111 112 func buildScheduleStruct(d *schema.ResourceData) *pagerduty.Schedule { 113 scheduleLayers := d.Get("layer").([]interface{}) 114 115 schedule := pagerduty.Schedule{ 116 Name: d.Get("name").(string), 117 TimeZone: d.Get("time_zone").(string), 118 ScheduleLayers: expandScheduleLayers(scheduleLayers), 119 } 120 121 if attr, ok := d.GetOk("description"); ok { 122 schedule.Description = attr.(string) 123 } 124 125 return &schedule 126 } 127 128 func resourcePagerDutyScheduleCreate(d *schema.ResourceData, meta interface{}) error { 129 client := meta.(*pagerduty.Client) 130 131 schedule := buildScheduleStruct(d) 132 133 log.Printf("[INFO] Creating PagerDuty schedule: %s", schedule.Name) 134 135 schedule, err := client.CreateSchedule(*schedule) 136 137 if err != nil { 138 return err 139 } 140 141 d.SetId(schedule.ID) 142 143 return resourcePagerDutyScheduleRead(d, meta) 144 } 145 146 func resourcePagerDutyScheduleRead(d *schema.ResourceData, meta interface{}) error { 147 client := meta.(*pagerduty.Client) 148 149 log.Printf("[INFO] Reading PagerDuty schedule: %s", d.Id()) 150 151 schedule, err := client.GetSchedule(d.Id(), pagerduty.GetScheduleOptions{}) 152 153 if err != nil { 154 return err 155 } 156 157 d.Set("name", schedule.Name) 158 d.Set("time_zone", schedule.TimeZone) 159 d.Set("description", schedule.Description) 160 161 if err := d.Set("layer", flattenScheduleLayers(schedule.ScheduleLayers)); err != nil { 162 return err 163 } 164 165 return nil 166 } 167 168 func resourcePagerDutyScheduleUpdate(d *schema.ResourceData, meta interface{}) error { 169 client := meta.(*pagerduty.Client) 170 171 schedule := buildScheduleStruct(d) 172 173 log.Printf("[INFO] Updating PagerDuty schedule: %s", d.Id()) 174 175 if _, err := client.UpdateSchedule(d.Id(), *schedule); err != nil { 176 return err 177 } 178 179 return nil 180 } 181 182 func resourcePagerDutyScheduleDelete(d *schema.ResourceData, meta interface{}) error { 183 client := meta.(*pagerduty.Client) 184 185 log.Printf("[INFO] Deleting PagerDuty schedule: %s", d.Id()) 186 187 if err := client.DeleteSchedule(d.Id()); err != nil { 188 return err 189 } 190 191 d.SetId("") 192 193 return nil 194 } 195 196 func resourcePagerDutyScheduleImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { 197 if err := resourcePagerDutyScheduleRead(d, meta); err != nil { 198 return nil, err 199 } 200 return []*schema.ResourceData{d}, nil 201 }