Skip to content

Commit

Permalink
GS-485: Get Services working and fix Alerts tests (#18)
Browse files Browse the repository at this point in the history
* All alerts and services tests are working
* Services is working with new SDK/Client
* Alerts is passing an services as []int instead of []*appoptics.Service
  • Loading branch information
solarchad authored Sep 26, 2019
1 parent 8af8ffb commit efb612b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 76 deletions.
24 changes: 10 additions & 14 deletions appoptics/resource_appoptics_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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)

Expand All @@ -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
}
Expand Down
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,92 +94,87 @@ 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))
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)))
if getErr != nil {
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)
}
Expand All @@ -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
},
Expand All @@ -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)
Expand Down
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 @@ -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"),
),
Expand All @@ -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) },
Expand All @@ -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"),
),
Expand All @@ -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"),
),
Expand All @@ -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" {
Expand All @@ -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")
Expand All @@ -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]

Expand All @@ -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
}
}
Expand Down

0 comments on commit efb612b

Please sign in to comment.