github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/pagerduty/resource_pagerduty_service.go (about) 1 package pagerduty 2 3 import ( 4 "log" 5 6 pagerduty "github.com/PagerDuty/go-pagerduty" 7 "github.com/hashicorp/terraform/helper/schema" 8 ) 9 10 func resourcePagerDutyService() *schema.Resource { 11 return &schema.Resource{ 12 Create: resourcePagerDutyServiceCreate, 13 Read: resourcePagerDutyServiceRead, 14 Update: resourcePagerDutyServiceUpdate, 15 Delete: resourcePagerDutyServiceDelete, 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 "description": { 25 Type: schema.TypeString, 26 Optional: true, 27 Default: "Managed by Terraform", 28 }, 29 "auto_resolve_timeout": { 30 Type: schema.TypeInt, 31 Optional: true, 32 }, 33 "last_incident_timestamp": { 34 Type: schema.TypeString, 35 Computed: true, 36 }, 37 "created_at": { 38 Type: schema.TypeString, 39 Computed: true, 40 }, 41 "status": { 42 Type: schema.TypeString, 43 Computed: true, 44 }, 45 "acknowledgement_timeout": { 46 Type: schema.TypeInt, 47 Optional: true, 48 }, 49 "escalation_policy": { 50 Type: schema.TypeString, 51 Required: true, 52 }, 53 "incident_urgency_rule": &schema.Schema{ 54 Type: schema.TypeList, 55 Optional: true, 56 Elem: &schema.Resource{ 57 Schema: map[string]*schema.Schema{ 58 "type": { 59 Type: schema.TypeString, 60 Required: true, 61 }, 62 "urgency": { 63 Type: schema.TypeString, 64 Optional: true, 65 }, 66 "during_support_hours": { 67 Type: schema.TypeList, 68 MaxItems: 1, 69 MinItems: 1, 70 Optional: true, 71 Elem: &schema.Resource{ 72 Schema: map[string]*schema.Schema{ 73 "type": { 74 Type: schema.TypeString, 75 Optional: true, 76 }, 77 "urgency": { 78 Type: schema.TypeString, 79 Optional: true, 80 }, 81 }, 82 }, 83 }, 84 "outside_support_hours": { 85 Type: schema.TypeList, 86 MaxItems: 1, 87 MinItems: 1, 88 Optional: true, 89 Elem: &schema.Resource{ 90 Schema: map[string]*schema.Schema{ 91 "type": { 92 Type: schema.TypeString, 93 Optional: true, 94 }, 95 "urgency": { 96 Type: schema.TypeString, 97 Optional: true, 98 }, 99 }, 100 }, 101 }, 102 }, 103 }, 104 }, 105 "support_hours": &schema.Schema{ 106 Type: schema.TypeList, 107 Optional: true, 108 MaxItems: 1, 109 MinItems: 1, 110 ForceNew: true, 111 Elem: &schema.Resource{ 112 Schema: map[string]*schema.Schema{ 113 "type": { 114 Type: schema.TypeString, 115 Optional: true, 116 }, 117 "time_zone": { 118 Type: schema.TypeString, 119 Optional: true, 120 }, 121 "start_time": { 122 Type: schema.TypeString, 123 Optional: true, 124 }, 125 "end_time": { 126 Type: schema.TypeString, 127 Optional: true, 128 }, 129 "days_of_week": { 130 Type: schema.TypeList, 131 Optional: true, 132 MaxItems: 7, 133 Elem: &schema.Schema{Type: schema.TypeInt}, 134 }, 135 }, 136 }, 137 }, 138 "scheduled_actions": &schema.Schema{ 139 Type: schema.TypeList, 140 Optional: true, 141 ForceNew: true, 142 Elem: &schema.Resource{ 143 Schema: map[string]*schema.Schema{ 144 "type": { 145 Type: schema.TypeString, 146 Optional: true, 147 }, 148 "to_urgency": { 149 Type: schema.TypeString, 150 Optional: true, 151 }, 152 "at": &schema.Schema{ 153 Type: schema.TypeList, 154 Optional: true, 155 Elem: &schema.Resource{ 156 Schema: map[string]*schema.Schema{ 157 "type": { 158 Type: schema.TypeString, 159 Optional: true, 160 }, 161 "name": { 162 Type: schema.TypeString, 163 Optional: true, 164 }, 165 }, 166 }, 167 }, 168 }, 169 }, 170 }, 171 }, 172 } 173 } 174 175 func buildServiceStruct(d *schema.ResourceData) *pagerduty.Service { 176 service := pagerduty.Service{ 177 Name: d.Get("name").(string), 178 Status: d.Get("status").(string), 179 APIObject: pagerduty.APIObject{ 180 ID: d.Id(), 181 }, 182 } 183 184 if attr, ok := d.GetOk("description"); ok { 185 service.Description = attr.(string) 186 } 187 188 if attr, ok := d.GetOk("auto_resolve_timeout"); ok { 189 autoResolveTimeout := uint(attr.(int)) 190 service.AutoResolveTimeout = &autoResolveTimeout 191 } 192 193 if attr, ok := d.GetOk("acknowledgement_timeout"); ok { 194 acknowledgementTimeout := uint(attr.(int)) 195 service.AcknowledgementTimeout = &acknowledgementTimeout 196 } 197 198 escalationPolicy := &pagerduty.EscalationPolicy{ 199 APIObject: pagerduty.APIObject{ 200 ID: d.Get("escalation_policy").(string), 201 Type: "escalation_policy_reference", 202 }, 203 } 204 205 service.EscalationPolicy = *escalationPolicy 206 207 if attr, ok := d.GetOk("incident_urgency_rule"); ok { 208 if iur, ok := expandIncidentUrgencyRule(attr); ok { 209 service.IncidentUrgencyRule = iur 210 } 211 } 212 if attr, ok := d.GetOk("support_hours"); ok { 213 service.SupportHours = expandSupportHours(attr) 214 } 215 if attr, ok := d.GetOk("scheduled_actions"); ok { 216 service.ScheduledActions = expandScheduledActions(attr) 217 } 218 219 return &service 220 } 221 222 func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) error { 223 client := meta.(*pagerduty.Client) 224 225 service := buildServiceStruct(d) 226 227 log.Printf("[INFO] Creating PagerDuty service %s", service.Name) 228 229 service, err := client.CreateService(*service) 230 231 if err != nil { 232 return err 233 } 234 235 d.SetId(service.ID) 236 237 return nil 238 } 239 240 func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) error { 241 client := meta.(*pagerduty.Client) 242 243 log.Printf("[INFO] Reading PagerDuty service %s", d.Id()) 244 245 o := &pagerduty.GetServiceOptions{} 246 247 service, err := client.GetService(d.Id(), o) 248 249 if err != nil { 250 if isNotFound(err) { 251 d.SetId("") 252 return nil 253 } 254 return err 255 } 256 257 d.Set("name", service.Name) 258 d.Set("status", service.Status) 259 d.Set("created_at", service.CreateAt) 260 d.Set("escalation_policy", service.EscalationPolicy.ID) 261 d.Set("description", service.Description) 262 d.Set("auto_resolve_timeout", service.AutoResolveTimeout) 263 d.Set("last_incident_timestamp", service.LastIncidentTimestamp) 264 d.Set("acknowledgement_timeout", service.AcknowledgementTimeout) 265 266 if incidentUrgencyRule, ok := flattenIncidentUrgencyRule(service); ok { 267 d.Set("incident_urgency_rule", incidentUrgencyRule) 268 } 269 270 supportHours := flattenSupportHours(service) 271 d.Set("support_hours", supportHours) 272 273 scheduledActions := flattenScheduledActions(service) 274 d.Set("scheduled_actions", scheduledActions) 275 276 return nil 277 } 278 279 func resourcePagerDutyServiceUpdate(d *schema.ResourceData, meta interface{}) error { 280 client := meta.(*pagerduty.Client) 281 282 service := buildServiceStruct(d) 283 284 log.Printf("[INFO] Updating PagerDuty service %s", d.Id()) 285 286 if _, err := client.UpdateService(*service); err != nil { 287 return err 288 } 289 290 return nil 291 } 292 293 func resourcePagerDutyServiceDelete(d *schema.ResourceData, meta interface{}) error { 294 client := meta.(*pagerduty.Client) 295 296 log.Printf("[INFO] Deleting PagerDuty service %s", d.Id()) 297 298 if err := client.DeleteService(d.Id()); err != nil { 299 return err 300 } 301 302 d.SetId("") 303 304 return nil 305 }