github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/pagerduty/resource_pagerduty_service_integration.go (about) 1 package pagerduty 2 3 import ( 4 "fmt" 5 "log" 6 7 pagerduty "github.com/PagerDuty/go-pagerduty" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 func resourcePagerDutyServiceIntegration() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourcePagerDutyServiceIntegrationCreate, 14 Read: resourcePagerDutyServiceIntegrationRead, 15 Update: resourcePagerDutyServiceIntegrationUpdate, 16 Delete: resourcePagerDutyServiceIntegrationDelete, 17 Importer: &schema.ResourceImporter{ 18 State: resourcePagerDutyServiceIntegrationImport, 19 }, 20 Schema: map[string]*schema.Schema{ 21 "name": { 22 Type: schema.TypeString, 23 Optional: true, 24 }, 25 "service": { 26 Type: schema.TypeString, 27 Optional: true, 28 }, 29 "type": { 30 Type: schema.TypeString, 31 Optional: true, 32 ForceNew: true, 33 Computed: true, 34 ConflictsWith: []string{"vendor"}, 35 ValidateFunc: validateValueFunc([]string{ 36 "aws_cloudwatch_inbound_integration", 37 "cloudkick_inbound_integration", 38 "event_transformer_api_inbound_integration", 39 "generic_email_inbound_integration", 40 "generic_events_api_inbound_integration", 41 "keynote_inbound_integration", 42 "nagios_inbound_integration", 43 "pingdom_inbound_integration", 44 "sql_monitor_inbound_integration", 45 }), 46 }, 47 "vendor": { 48 Type: schema.TypeString, 49 ForceNew: true, 50 Optional: true, 51 ConflictsWith: []string{"type"}, 52 Computed: true, 53 }, 54 "integration_key": { 55 Type: schema.TypeString, 56 Optional: true, 57 Computed: true, 58 }, 59 "integration_email": { 60 Type: schema.TypeString, 61 Optional: true, 62 Computed: true, 63 }, 64 }, 65 } 66 } 67 68 func buildServiceIntegrationStruct(d *schema.ResourceData) *pagerduty.Integration { 69 serviceIntegration := pagerduty.Integration{ 70 Name: d.Get("name").(string), 71 Service: &pagerduty.APIObject{ 72 Type: "service", 73 ID: d.Get("service").(string), 74 }, 75 APIObject: pagerduty.APIObject{ 76 ID: d.Id(), 77 Type: "service_integration", 78 }, 79 } 80 81 if attr, ok := d.GetOk("integration_key"); ok { 82 serviceIntegration.IntegrationKey = attr.(string) 83 } 84 85 if attr, ok := d.GetOk("integration_email"); ok { 86 serviceIntegration.IntegrationEmail = attr.(string) 87 } 88 89 if attr, ok := d.GetOk("type"); ok { 90 serviceIntegration.Type = attr.(string) 91 } 92 93 if attr, ok := d.GetOk("vendor"); ok { 94 serviceIntegration.Vendor = &pagerduty.APIObject{ 95 ID: attr.(string), 96 Type: "vendor", 97 } 98 } 99 100 return &serviceIntegration 101 } 102 103 func resourcePagerDutyServiceIntegrationCreate(d *schema.ResourceData, meta interface{}) error { 104 client := meta.(*pagerduty.Client) 105 106 serviceIntegration := buildServiceIntegrationStruct(d) 107 108 log.Printf("[INFO] Creating PagerDuty service integration %s", serviceIntegration.Name) 109 110 service := d.Get("service").(string) 111 112 serviceIntegration, err := client.CreateIntegration(service, *serviceIntegration) 113 114 if err != nil { 115 return err 116 } 117 118 d.SetId(serviceIntegration.ID) 119 120 return resourcePagerDutyServiceIntegrationRead(d, meta) 121 } 122 123 func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interface{}) error { 124 client := meta.(*pagerduty.Client) 125 126 log.Printf("[INFO] Reading PagerDuty service integration %s", d.Id()) 127 128 service := d.Get("service").(string) 129 130 o := &pagerduty.GetIntegrationOptions{} 131 132 serviceIntegration, err := client.GetIntegration(service, d.Id(), *o) 133 134 if err != nil { 135 if isNotFound(err) { 136 d.SetId("") 137 return nil 138 } 139 return err 140 } 141 142 d.Set("name", serviceIntegration.Name) 143 d.Set("type", serviceIntegration.Type) 144 d.Set("service", serviceIntegration.Service.ID) 145 d.Set("vendor", serviceIntegration.Vendor.ID) 146 d.Set("integration_key", serviceIntegration.IntegrationKey) 147 d.Set("integration_email", serviceIntegration.IntegrationEmail) 148 149 return nil 150 } 151 152 func resourcePagerDutyServiceIntegrationUpdate(d *schema.ResourceData, meta interface{}) error { 153 client := meta.(*pagerduty.Client) 154 155 serviceIntegration := buildServiceIntegrationStruct(d) 156 157 service := d.Get("service").(string) 158 159 log.Printf("[INFO] Updating PagerDuty service integration %s", d.Id()) 160 161 if _, err := client.UpdateIntegration(service, *serviceIntegration); err != nil { 162 return err 163 } 164 165 return nil 166 } 167 168 func resourcePagerDutyServiceIntegrationDelete(d *schema.ResourceData, meta interface{}) error { 169 client := meta.(*pagerduty.Client) 170 171 service := d.Get("service").(string) 172 173 log.Printf("[INFO] Removing PagerDuty service integration %s", d.Id()) 174 175 if err := client.DeleteIntegration(service, d.Id()); err != nil { 176 if isNotFound(err) { 177 d.SetId("") 178 return nil 179 } 180 return err 181 } 182 183 d.SetId("") 184 185 return nil 186 } 187 188 func resourcePagerDutyServiceIntegrationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { 189 client := meta.(*pagerduty.Client) 190 191 resp, err := client.ListServices(pagerduty.ListServiceOptions{}) 192 if err != nil { 193 return []*schema.ResourceData{}, err 194 } 195 196 var serviceID string 197 198 for _, service := range resp.Services { 199 for _, integration := range service.Integrations { 200 if integration.ID == d.Id() { 201 serviceID = service.ID 202 } 203 } 204 } 205 206 if serviceID == "" { 207 return []*schema.ResourceData{}, fmt.Errorf("Error importing pagerduty_service_integration. Could not locate a service ID for the integration") 208 } 209 210 d.Set("service", serviceID) 211 212 return []*schema.ResourceData{d}, nil 213 }