github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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: resourcePagerDutyServiceImport, 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 }, 54 } 55 } 56 57 func buildServiceStruct(d *schema.ResourceData) *pagerduty.Service { 58 service := pagerduty.Service{ 59 Name: d.Get("name").(string), 60 Status: d.Get("status").(string), 61 APIObject: pagerduty.APIObject{ 62 ID: d.Id(), 63 }, 64 } 65 66 if attr, ok := d.GetOk("description"); ok { 67 service.Description = attr.(string) 68 } 69 70 if attr, ok := d.GetOk("auto_resolve_timeout"); ok { 71 autoResolveTimeout := uint(attr.(int)) 72 service.AutoResolveTimeout = &autoResolveTimeout 73 } 74 75 if attr, ok := d.GetOk("acknowledgement_timeout"); ok { 76 acknowledgementTimeout := uint(attr.(int)) 77 service.AcknowledgementTimeout = &acknowledgementTimeout 78 } 79 80 escalationPolicy := &pagerduty.EscalationPolicy{ 81 APIObject: pagerduty.APIObject{ 82 ID: d.Get("escalation_policy").(string), 83 Type: "escalation_policy_reference", 84 }, 85 } 86 87 service.EscalationPolicy = *escalationPolicy 88 89 return &service 90 } 91 92 func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) error { 93 client := meta.(*pagerduty.Client) 94 95 service := buildServiceStruct(d) 96 97 log.Printf("[INFO] Creating PagerDuty service %s", service.Name) 98 99 service, err := client.CreateService(*service) 100 101 if err != nil { 102 return err 103 } 104 105 d.SetId(service.ID) 106 107 return nil 108 } 109 110 func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) error { 111 client := meta.(*pagerduty.Client) 112 113 log.Printf("[INFO] Reading PagerDuty service %s", d.Id()) 114 115 o := &pagerduty.GetServiceOptions{} 116 117 service, err := client.GetService(d.Id(), o) 118 119 if err != nil { 120 return err 121 } 122 123 d.Set("name", service.Name) 124 d.Set("status", service.Status) 125 d.Set("created_at", service.CreateAt) 126 d.Set("escalation_policy", service.EscalationPolicy.ID) 127 d.Set("description", service.Description) 128 d.Set("auto_resolve_timeout", service.AutoResolveTimeout) 129 d.Set("last_incident_timestamp", service.LastIncidentTimestamp) 130 d.Set("acknowledgement_timeout", service.AcknowledgementTimeout) 131 132 return nil 133 } 134 135 func resourcePagerDutyServiceUpdate(d *schema.ResourceData, meta interface{}) error { 136 client := meta.(*pagerduty.Client) 137 138 service := buildServiceStruct(d) 139 140 log.Printf("[INFO] Updating PagerDuty service %s", d.Id()) 141 142 if _, err := client.UpdateService(*service); err != nil { 143 return err 144 } 145 146 return nil 147 } 148 149 func resourcePagerDutyServiceDelete(d *schema.ResourceData, meta interface{}) error { 150 client := meta.(*pagerduty.Client) 151 152 log.Printf("[INFO] Deleting PagerDuty service %s", d.Id()) 153 154 if err := client.DeleteService(d.Id()); err != nil { 155 return err 156 } 157 158 d.SetId("") 159 160 return nil 161 } 162 163 func resourcePagerDutyServiceImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { 164 if err := resourcePagerDutyServiceRead(d, meta); err != nil { 165 return nil, err 166 } 167 return []*schema.ResourceData{d}, nil 168 }