diff --git a/appoptics/resource_appoptics_alert.go b/appoptics/resource_appoptics_alert.go index b3aa3ad..1bfd290 100644 --- a/appoptics/resource_appoptics_alert.go +++ b/appoptics/resource_appoptics_alert.go @@ -167,7 +167,7 @@ func tagsValuesHash(s []interface{}) int { func resourceAppOpticsAlertCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*appoptics.Client) - alert := appoptics.Alert{ + alert := appoptics.AlertRequest{ Name: d.Get("name").(string), } if v, ok := d.GetOk("description"); ok { @@ -178,17 +178,15 @@ func resourceAppOpticsAlertCreate(d *schema.ResourceData, meta interface{}) erro if v, ok := d.GetOk("rearm_seconds"); ok { alert.RearmSeconds = v.(int) } - if v, ok := d.GetOk("services"); ok { - vs := v.(*schema.Set) - services := make([]*appoptics.Service, vs.Len()) + if _, ok := d.GetOk("services"); ok { + vs := d.Get("services").(*schema.Set) + services := make([]int, vs.Len()) for i, serviceID := range vs.List() { - service := new(appoptics.Service) var err error - service.ID, err = strconv.Atoi(serviceID.(string)) + services[i], err = strconv.Atoi(serviceID.(string)) if err != nil { - return err + return fmt.Errorf("Error: %s", err) } - services[i] = service } alert.Services = services } @@ -386,7 +384,7 @@ func resourceAppOpticsAlertUpdate(d *schema.ResourceData, meta interface{}) erro return err } - alert := new(appoptics.Alert) + alert := new(appoptics.AlertRequest) alert.ID = int(id) alert.Name = d.Get("name").(string) @@ -401,15 +399,13 @@ func resourceAppOpticsAlertUpdate(d *schema.ResourceData, meta interface{}) erro } if d.HasChange("services") { vs := d.Get("services").(*schema.Set) - services := make([]*appoptics.Service, vs.Len()) + services := make([]int, vs.Len()) for i, serviceID := range vs.List() { - service := new(appoptics.Service) var err error - service.ID, err = strconv.Atoi(serviceID.(string)) + services[i], err = strconv.Atoi(serviceID.(string)) if err != nil { - return err + return fmt.Errorf("Error: %s", err) } - services[i] = service } alert.Services = services } diff --git a/appoptics/resource_librato_service.go b/appoptics/resource_appoptics_service.go similarity index 74% rename from appoptics/resource_librato_service.go rename to appoptics/resource_appoptics_service.go index f9755a0..0ef02cc 100644 --- a/appoptics/resource_librato_service.go +++ b/appoptics/resource_appoptics_service.go @@ -8,7 +8,7 @@ import ( "strconv" "time" - "github.com/akahn/go-librato/librato" + "github.com/appoptics/appoptics-api-go" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" ) @@ -77,14 +77,14 @@ func normalizeJSON(jsonString interface{}) string { } func resourceAppOpticsServiceCreate(d *schema.ResourceData, meta interface{}) error { - client := meta.(*librato.Client) + client := meta.(*appoptics.Client) - service := new(librato.Service) + service := new(appoptics.Service) if v, ok := d.GetOk("type"); ok { - service.Type = librato.String(v.(string)) + service.Type = v.(string) } if v, ok := d.GetOk("title"); ok { - service.Title = librato.String(v.(string)) + service.Title = v.(string) } if v, ok := d.GetOk("settings"); ok { res, expandErr := resourceAppOpticsServicesExpandSettings(normalizeJSON(v.(string))) @@ -94,16 +94,16 @@ func resourceAppOpticsServiceCreate(d *schema.ResourceData, meta interface{}) er service.Settings = res } - serviceResult, _, err := client.Services.Create(service) + serviceResult, err := client.ServicesService().Create(service) if err != nil { return fmt.Errorf("Error creating AppOptics service: %s", err) } resource.Retry(1*time.Minute, func() *resource.RetryError { - _, _, err := client.Services.Get(*serviceResult.ID) + _, err := client.ServicesService().Retrieve(serviceResult.ID) if err != nil { - if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 { + if errResp, ok := err.(*appoptics.ErrorResponse); ok && errResp.Response.StatusCode == 404 { return resource.RetryableError(err) } return resource.NonRetryableError(err) @@ -111,35 +111,35 @@ func resourceAppOpticsServiceCreate(d *schema.ResourceData, meta interface{}) er return nil }) - d.SetId(strconv.Itoa(int(*serviceResult.ID))) - return resourceAppOpticsServiceReadResult(d, serviceResult) + d.SetId(strconv.Itoa(serviceResult.ID)) + return resourceAppOpticsServiceReadResult(d, *serviceResult) } func resourceAppOpticsServiceRead(d *schema.ResourceData, meta interface{}) error { - client := meta.(*librato.Client) + client := meta.(*appoptics.Client) id, err := strconv.ParseUint(d.Id(), 10, 0) if err != nil { return err } log.Printf("[INFO] Reading AppOptics Service: %d", id) - service, _, err := client.Services.Get(uint(id)) + service, err := client.ServicesService().Retrieve(int(id)) if err != nil { - if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 { + if errResp, ok := err.(*appoptics.ErrorResponse); ok && errResp.Response.StatusCode == 404 { d.SetId("") return nil } return fmt.Errorf("Error reading AppOptics Service %s: %s", d.Id(), err) } - log.Printf("[INFO] Received AppOptics Service: %s", service) + log.Printf("[INFO] Received AppOptics Service: %s", service.Title) - return resourceAppOpticsServiceReadResult(d, service) + return resourceAppOpticsServiceReadResult(d, *service) } -func resourceAppOpticsServiceReadResult(d *schema.ResourceData, service *librato.Service) error { - d.SetId(strconv.FormatUint(uint64(*service.ID), 10)) - d.Set("type", *service.Type) - d.Set("title", *service.Title) +func resourceAppOpticsServiceReadResult(d *schema.ResourceData, service appoptics.Service) error { + d.SetId(strconv.FormatUint(uint64(service.ID), 10)) + d.Set("type", service.Type) + d.Set("title", service.Title) settings, _ := resourceAppOpticsServicesFlatten(service.Settings) d.Set("settings", settings) @@ -147,27 +147,23 @@ func resourceAppOpticsServiceReadResult(d *schema.ResourceData, service *librato } func resourceAppOpticsServiceUpdate(d *schema.ResourceData, meta interface{}) error { - client := meta.(*librato.Client) + client := meta.(*appoptics.Client) serviceID, err := strconv.ParseUint(d.Id(), 10, 0) if err != nil { return err } - // Just to have whole object for comparison before/after update - fullService, _, err := client.Services.Get(uint(serviceID)) + service, err := client.ServicesService().Retrieve(int(serviceID)) if err != nil { return err } - service := new(librato.Service) if d.HasChange("type") { - service.Type = librato.String(d.Get("type").(string)) - fullService.Type = service.Type + service.Type = d.Get("type").(string) } if d.HasChange("title") { - service.Title = librato.String(d.Get("title").(string)) - fullService.Title = service.Title + service.Title = d.Get("title").(string) } if d.HasChange("settings") { res, getErr := resourceAppOpticsServicesExpandSettings(normalizeJSON(d.Get("settings").(string))) @@ -175,11 +171,10 @@ func resourceAppOpticsServiceUpdate(d *schema.ResourceData, meta interface{}) er return fmt.Errorf("Error expanding AppOptics service settings: %s", getErr) } service.Settings = res - fullService.Settings = res } - log.Printf("[INFO] Updating AppOptics Service %d: %s", serviceID, service) - _, err = client.Services.Update(uint(serviceID), service) + log.Printf("[INFO] Updating AppOptics Service %d: %s", serviceID, service.Title) + err = client.ServicesService().Update(service) if err != nil { return fmt.Errorf("Error updating AppOptics service: %s", err) } @@ -194,11 +189,11 @@ func resourceAppOpticsServiceUpdate(d *schema.ResourceData, meta interface{}) er ContinuousTargetOccurence: 5, Refresh: func() (interface{}, string, error) { log.Printf("[DEBUG] Checking if AppOptics Service %d was updated yet", serviceID) - changedService, _, getErr := client.Services.Get(uint(serviceID)) + changedService, getErr := client.ServicesService().Retrieve(int(serviceID)) if getErr != nil { return changedService, "", getErr } - isEqual := reflect.DeepEqual(*fullService, *changedService) + isEqual := reflect.DeepEqual(*service, *changedService) log.Printf("[DEBUG] Updated AppOptics Service %d match: %t", serviceID, isEqual) return changedService, fmt.Sprintf("%t", isEqual), nil }, @@ -213,22 +208,22 @@ func resourceAppOpticsServiceUpdate(d *schema.ResourceData, meta interface{}) er } func resourceAppOpticsServiceDelete(d *schema.ResourceData, meta interface{}) error { - client := meta.(*librato.Client) + client := meta.(*appoptics.Client) id, err := strconv.ParseUint(d.Id(), 10, 0) if err != nil { return err } log.Printf("[INFO] Deleting Service: %d", id) - _, err = client.Services.Delete(uint(id)) + err = client.ServicesService().Delete(int(id)) if err != nil { return fmt.Errorf("Error deleting Service: %s", err) } resource.Retry(1*time.Minute, func() *resource.RetryError { - _, _, err := client.Services.Get(uint(id)) + _, err := client.ServicesService().Retrieve(int(id)) if err != nil { - if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 { + if errResp, ok := err.(*appoptics.ErrorResponse); ok && errResp.Response.StatusCode == 404 { return nil } return resource.NonRetryableError(err) diff --git a/appoptics/resource_librato_service_test.go b/appoptics/resource_appoptics_service_test.go similarity index 73% rename from appoptics/resource_librato_service_test.go rename to appoptics/resource_appoptics_service_test.go index 6e9c067..d96966f 100644 --- a/appoptics/resource_librato_service_test.go +++ b/appoptics/resource_appoptics_service_test.go @@ -5,13 +5,13 @@ import ( "strconv" "testing" - "github.com/akahn/go-librato/librato" + "github.com/appoptics/appoptics-api-go" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) func TestAccAppOpticsServiceBasic(t *testing.T) { - var service librato.Service + var service appoptics.Service resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -22,7 +22,6 @@ func TestAccAppOpticsServiceBasic(t *testing.T) { Config: testAccCheckAppOpticsServiceConfigBasic, Check: resource.ComposeTestCheckFunc( testAccCheckAppOpticsServiceExists("appoptics_service.foobar", &service), - testAccCheckAppOpticsServiceTitle(&service, "Foo Bar"), resource.TestCheckResourceAttr( "appoptics_service.foobar", "title", "Foo Bar"), ), @@ -31,8 +30,8 @@ func TestAccAppOpticsServiceBasic(t *testing.T) { }) } -func TestAccAppOpticsService_Updated(t *testing.T) { - var service librato.Service +func TestAccAppOpticsServiceUpdated(t *testing.T) { + var service appoptics.Service resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -43,7 +42,6 @@ func TestAccAppOpticsService_Updated(t *testing.T) { Config: testAccCheckAppOpticsServiceConfigBasic, Check: resource.ComposeTestCheckFunc( testAccCheckAppOpticsServiceExists("appoptics_service.foobar", &service), - testAccCheckAppOpticsServiceTitle(&service, "Foo Bar"), resource.TestCheckResourceAttr( "appoptics_service.foobar", "title", "Foo Bar"), ), @@ -52,7 +50,6 @@ func TestAccAppOpticsService_Updated(t *testing.T) { Config: testAccCheckAppOpticsServiceConfigNewValue, Check: resource.ComposeTestCheckFunc( testAccCheckAppOpticsServiceExists("appoptics_service.foobar", &service), - testAccCheckAppOpticsServiceTitle(&service, "Bar Baz"), resource.TestCheckResourceAttr( "appoptics_service.foobar", "title", "Bar Baz"), ), @@ -62,7 +59,7 @@ func TestAccAppOpticsService_Updated(t *testing.T) { } func testAccCheckAppOpticsServiceDestroy(s *terraform.State) error { - client := testAccProvider.Meta().(*librato.Client) + client := testAccProvider.Meta().(*appoptics.Client) for _, rs := range s.RootModule().Resources { if rs.Type != "appoptics_service" { @@ -74,7 +71,7 @@ func testAccCheckAppOpticsServiceDestroy(s *terraform.State) error { return fmt.Errorf("ID not a number") } - _, _, err = client.Services.Get(uint(id)) + _, err = client.ServicesService().Retrieve(int(id)) if err == nil { return fmt.Errorf("Service still exists") @@ -84,18 +81,7 @@ func testAccCheckAppOpticsServiceDestroy(s *terraform.State) error { return nil } -func testAccCheckAppOpticsServiceTitle(service *librato.Service, title string) resource.TestCheckFunc { - return func(s *terraform.State) error { - - if service.Title == nil || *service.Title != title { - return fmt.Errorf("Bad title: %s", *service.Title) - } - - return nil - } -} - -func testAccCheckAppOpticsServiceExists(n string, service *librato.Service) resource.TestCheckFunc { +func testAccCheckAppOpticsServiceExists(n string, service *appoptics.Service) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -107,25 +93,23 @@ func testAccCheckAppOpticsServiceExists(n string, service *librato.Service) reso return fmt.Errorf("No Service ID is set") } - client := testAccProvider.Meta().(*librato.Client) + client := testAccProvider.Meta().(*appoptics.Client) id, err := strconv.ParseUint(rs.Primary.ID, 10, 0) if err != nil { return fmt.Errorf("ID not a number") } - foundService, _, err := client.Services.Get(uint(id)) + foundService, err := client.ServicesService().Retrieve(int(id)) if err != nil { return err } - if foundService.ID == nil || *foundService.ID != uint(id) { + if foundService.ID != int(id) { return fmt.Errorf("Service not found") } - *service = *foundService - return nil } }