github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/pagerduty/resource_pagerduty_service_integration.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 resourcePagerDutyServiceIntegration() *schema.Resource { 11 return &schema.Resource{ 12 Create: resourcePagerDutyServiceIntegrationCreate, 13 Read: resourcePagerDutyServiceIntegrationRead, 14 Update: resourcePagerDutyServiceIntegrationUpdate, 15 Delete: resourcePagerDutyServiceIntegrationDelete, 16 Schema: map[string]*schema.Schema{ 17 "name": { 18 Type: schema.TypeString, 19 Optional: true, 20 }, 21 "service": { 22 Type: schema.TypeString, 23 Optional: true, 24 }, 25 "type": { 26 Type: schema.TypeString, 27 Required: true, 28 ForceNew: true, 29 ValidateFunc: validateValueFunc([]string{ 30 "aws_cloudwatch_inbound_integration", 31 "cloudkick_inbound_integration", 32 "event_transformer_api_inbound_integration", 33 "generic_email_inbound_integration", 34 "generic_events_api_inbound_integration", 35 "keynote_inbound_integration", 36 "nagios_inbound_integration", 37 "pingdom_inbound_integration", 38 "sql_monitor_inbound_integration", 39 }), 40 }, 41 "vendor": { 42 Type: schema.TypeString, 43 ForceNew: true, 44 Optional: true, 45 }, 46 "integration_key": { 47 Type: schema.TypeString, 48 Optional: true, 49 Computed: true, 50 }, 51 "integration_email": { 52 Type: schema.TypeString, 53 Optional: true, 54 Computed: true, 55 }, 56 }, 57 } 58 } 59 60 func buildServiceIntegrationStruct(d *schema.ResourceData) *pagerduty.Integration { 61 serviceIntegration := pagerduty.Integration{ 62 Name: d.Get("name").(string), 63 Type: d.Get("type").(string), 64 Service: &pagerduty.APIObject{ 65 Type: "service_reference", 66 ID: d.Get("service").(string), 67 }, 68 APIObject: pagerduty.APIObject{ 69 ID: d.Id(), 70 Type: "service_integration", 71 }, 72 } 73 74 if attr, ok := d.GetOk("integration_key"); ok { 75 serviceIntegration.IntegrationKey = attr.(string) 76 } 77 78 if attr, ok := d.GetOk("integration_email"); ok { 79 serviceIntegration.IntegrationEmail = attr.(string) 80 } 81 82 if attr, ok := d.GetOk("vendor"); ok { 83 serviceIntegration.Vendor = &pagerduty.APIObject{ 84 ID: attr.(string), 85 Type: "vendor_reference", 86 } 87 } 88 89 return &serviceIntegration 90 } 91 92 func resourcePagerDutyServiceIntegrationCreate(d *schema.ResourceData, meta interface{}) error { 93 client := meta.(*pagerduty.Client) 94 95 serviceIntegration := buildServiceIntegrationStruct(d) 96 97 log.Printf("[INFO] Creating PagerDuty service integration %s", serviceIntegration.Name) 98 99 service := d.Get("service").(string) 100 101 serviceIntegration, err := client.CreateIntegration(service, *serviceIntegration) 102 103 if err != nil { 104 return err 105 } 106 107 d.SetId(serviceIntegration.ID) 108 109 return resourcePagerDutyServiceIntegrationRead(d, meta) 110 } 111 112 func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interface{}) error { 113 client := meta.(*pagerduty.Client) 114 115 log.Printf("[INFO] Reading PagerDuty service integration %s", d.Id()) 116 117 service := d.Get("service").(string) 118 119 o := &pagerduty.GetIntegrationOptions{} 120 121 serviceIntegration, err := client.GetIntegration(service, d.Id(), *o) 122 123 if err != nil { 124 if isNotFound(err) { 125 d.SetId("") 126 return nil 127 } 128 return err 129 } 130 131 d.Set("name", serviceIntegration.Name) 132 d.Set("type", serviceIntegration.Type) 133 d.Set("service", serviceIntegration.Service) 134 d.Set("vendor", serviceIntegration.Vendor) 135 d.Set("integration_key", serviceIntegration.IntegrationKey) 136 d.Set("integration_email", serviceIntegration.IntegrationEmail) 137 138 return nil 139 } 140 141 func resourcePagerDutyServiceIntegrationUpdate(d *schema.ResourceData, meta interface{}) error { 142 client := meta.(*pagerduty.Client) 143 144 serviceIntegration := buildServiceIntegrationStruct(d) 145 146 service := d.Get("service").(string) 147 148 log.Printf("[INFO] Updating PagerDuty service integration %s", d.Id()) 149 150 if _, err := client.UpdateIntegration(service, *serviceIntegration); err != nil { 151 return err 152 } 153 154 return nil 155 } 156 157 func resourcePagerDutyServiceIntegrationDelete(d *schema.ResourceData, meta interface{}) error { 158 client := meta.(*pagerduty.Client) 159 160 service := d.Get("service").(string) 161 162 log.Printf("[INFO] Removing PagerDuty service integration %s", d.Id()) 163 164 if err := client.DeleteIntegration(service, d.Id()); err != nil { 165 if isNotFound(err) { 166 d.SetId("") 167 return nil 168 } 169 return err 170 } 171 172 d.SetId("") 173 174 return nil 175 }