Skip to content

Commit

Permalink
Fix using internal "ID" field in schema and update space tests (#9)
Browse files Browse the repository at this point in the history
* Avoid using reserved field ID as a schema field key

* Update test for Space to use official AO client and rename file

* Remove superfluous service_id field and set ID during create for service

* Fix function name
  • Loading branch information
solarchad committed Sep 11, 2019
1 parent a9ee0d7 commit f87973e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 deletions.
8 changes: 1 addition & 7 deletions appoptics/resource_appoptics_space.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ func resourceAppOpticsSpace() *schema.Resource {
Required: true,
ForceNew: false,
},
"id": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
Expand All @@ -53,6 +49,7 @@ func resourceAppOpticsSpaceCreate(d *schema.ResourceData, meta interface{}) erro
return nil
})

d.SetId(strconv.Itoa(space.ID))
return resourceAppOpticsSpaceReadResult(d, space)
}

Expand All @@ -77,9 +74,6 @@ func resourceAppOpticsSpaceRead(d *schema.ResourceData, meta interface{}) error
}

func resourceAppOpticsSpaceReadResult(d *schema.ResourceData, space *appoptics.Space) error {
if err := d.Set("id", space.ID); err != nil {
return err
}
if err := d.Set("name", space.Name); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ import (
"strconv"
"testing"

"github.com/akahn/go-librato/librato"
"github.com/appoptics/appoptics-api-go"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccLibratoSpace_Basic(t *testing.T) {
var space librato.Space
func TestAccAppOpticsSpaceBasic(t *testing.T) {
var space appoptics.Space
name := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibratoSpaceDestroy,
CheckDestroy: testAccCheckAppOpticsSpaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckLibratoSpaceConfig_basic(name),
Config: testAccCheckAppOpticsSpaceConfigBasic(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoSpaceExists("appoptics_space.foobar", &space),
testAccCheckLibratoSpaceAttributes(&space, name),
testAccCheckAppOpticsSpaceExists("appoptics_space.foobar", &space),
testAccCheckAppOpticsSpaceAttributes(&space, name),
resource.TestCheckResourceAttr(
"appoptics_space.foobar", "name", name),
),
Expand All @@ -33,21 +33,20 @@ func TestAccLibratoSpace_Basic(t *testing.T) {
})
}

func testAccCheckLibratoSpaceDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*librato.Client)
func testAccCheckAppOpticsSpaceDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*appoptics.Client)

for _, rs := range s.RootModule().Resources {
if rs.Type != "appoptics_space" {
continue
}

id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
id, err := strconv.Atoi(rs.Primary.ID)
if err != nil {
return fmt.Errorf("ID not a number")
}

_, _, err = client.Spaces.Get(uint(id))

_, err = client.SpacesService().Retrieve(id)
if err == nil {
return fmt.Errorf("Space still exists")
}
Expand All @@ -56,18 +55,18 @@ func testAccCheckLibratoSpaceDestroy(s *terraform.State) error {
return nil
}

func testAccCheckLibratoSpaceAttributes(space *librato.Space, name string) resource.TestCheckFunc {
func testAccCheckAppOpticsSpaceAttributes(space *appoptics.Space, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {

if space.Name == nil || *space.Name != name {
return fmt.Errorf("Bad name: %s", *space.Name)
if space.Name == "" || space.Name != name {
return fmt.Errorf("Bad name: %s", space.Name)
}

return nil
}
}

func testAccCheckLibratoSpaceExists(n string, space *librato.Space) resource.TestCheckFunc {
func testAccCheckAppOpticsSpaceExists(n string, space *appoptics.Space) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]

Expand All @@ -79,30 +78,28 @@ func testAccCheckLibratoSpaceExists(n string, space *librato.Space) resource.Tes
return fmt.Errorf("No Space ID is set")
}

client := testAccProvider.Meta().(*librato.Client)
client := testAccProvider.Meta().(*appoptics.Client)

id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
id, err := strconv.Atoi(rs.Primary.ID)
if err != nil {
return fmt.Errorf("ID not a number")
}

foundSpace, _, err := client.Spaces.Get(uint(id))
foundSpace, err := client.SpacesService().Retrieve(id)

if err != nil {
return err
}

if foundSpace.ID == nil || *foundSpace.ID != uint(id) {
if foundSpace.ID != id {
return fmt.Errorf("Space not found")
}

*space = *foundSpace

return nil
}
}

func testAccCheckLibratoSpaceConfig_basic(name string) string {
func testAccCheckAppOpticsSpaceConfigBasic(name string) string {
return fmt.Sprintf(`
resource "appoptics_space" "foobar" {
name = "%s"
Expand Down
6 changes: 1 addition & 5 deletions appoptics/resource_librato_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ func resourceAppOpticsService() *schema.Resource {
Delete: resourceAppOpticsServiceDelete,

Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"type": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -115,6 +111,7 @@ func resourceAppOpticsServiceCreate(d *schema.ResourceData, meta interface{}) er
return nil
})

d.SetId(strconv.Itoa(int(*serviceResult.ID)))
return resourceAppOpticsServiceReadResult(d, serviceResult)
}

Expand All @@ -141,7 +138,6 @@ func resourceAppOpticsServiceRead(d *schema.ResourceData, meta interface{}) erro

func resourceAppOpticsServiceReadResult(d *schema.ResourceData, service *librato.Service) error {
d.SetId(strconv.FormatUint(uint64(*service.ID), 10))
d.Set("id", *service.ID)
d.Set("type", *service.Type)
d.Set("title", *service.Title)
settings, _ := resourceAppOpticsServicesFlatten(service.Settings)
Expand Down

0 comments on commit f87973e

Please sign in to comment.