Skip to content

Commit

Permalink
Convert service to new SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
solarchad committed Sep 24, 2019
1 parent 5650064 commit 1659c1e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 45 deletions.
62 changes: 31 additions & 31 deletions appoptics/resource_librato_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)))
Expand All @@ -94,79 +94,79 @@ 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)
}
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)

return nil
}

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))
fullService, err := client.ServicesService().Retrieve(int(serviceID))
if err != nil {
return err
}

service := new(librato.Service)
service := new(appoptics.Service)
if d.HasChange("type") {
service.Type = librato.String(d.Get("type").(string))
service.Type = d.Get("type").(string)
fullService.Type = service.Type
}
if d.HasChange("title") {
service.Title = librato.String(d.Get("title").(string))
service.Title = d.Get("title").(string)
fullService.Title = service.Title
}
if d.HasChange("settings") {
Expand All @@ -178,8 +178,8 @@ func resourceAppOpticsServiceUpdate(d *schema.ResourceData, meta interface{}) er
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)
}
Expand All @@ -194,7 +194,7 @@ 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
}
Expand All @@ -213,22 +213,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)
Expand Down
26 changes: 12 additions & 14 deletions appoptics/resource_librato_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand All @@ -32,7 +32,7 @@ func TestAccAppOpticsServiceBasic(t *testing.T) {
}

func TestAccAppOpticsService_Updated(t *testing.T) {
var service librato.Service
var service appoptics.Service

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -62,7 +62,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" {
Expand All @@ -74,7 +74,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")
Expand All @@ -84,18 +84,18 @@ func testAccCheckAppOpticsServiceDestroy(s *terraform.State) error {
return nil
}

func testAccCheckAppOpticsServiceTitle(service *librato.Service, title string) resource.TestCheckFunc {
func testAccCheckAppOpticsServiceTitle(service *appoptics.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)
if 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]

Expand All @@ -107,25 +107,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
}
}
Expand Down

0 comments on commit 1659c1e

Please sign in to comment.