Skip to content

Commit

Permalink
fix: tag should query MAAS machines once
Browse files Browse the repository at this point in the history
  • Loading branch information
skatsaounis committed Aug 20, 2024
1 parent cca4e47 commit 0782fe1
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 5 deletions.
25 changes: 20 additions & 5 deletions maas/resource_maas_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package maas
import (
"context"
"fmt"
"slices"

"github.com/canonical/gomaasclient/client"
"github.com/canonical/gomaasclient/entity"
Expand Down Expand Up @@ -192,14 +193,28 @@ func getTagTFMachinesSystemIDs(client *client.Client, d *schema.ResourceData) ([
if !ok {
return nil, nil
}
machines, err := client.Machines.Get(&entity.MachinesParams{})
if err != nil {
return nil, err
}
machinesSystemIDs := []string{}
for _, machineIdentifier := range convertToStringSlice(p.(*schema.Set).List()) {
machine, err := getMachine(client, machineIdentifier)
if err != nil {
return nil, err
for _, identifier := range convertToStringSlice(p.(*schema.Set).List()) {
found := false
for _, m := range machines {
if slices.Contains([]string{m.SystemID, m.Hostname, m.FQDN}, identifier) {
if slices.Contains(machinesSystemIDs, m.SystemID) {
return nil, fmt.Errorf("machine (%s) is referenced more than once", m.SystemID)
}
machinesSystemIDs = append(machinesSystemIDs, m.SystemID)
found = true
break
}
}
if !found {
return nil, fmt.Errorf("machine (%s) not found", identifier)
}
machinesSystemIDs = append(machinesSystemIDs, machine.SystemID)
}

return machinesSystemIDs, nil
}

Expand Down
126 changes: 126 additions & 0 deletions maas/resource_maas_tag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package maas_test

import (
"fmt"
"os"
"strconv"
"strings"
"terraform-provider-maas/maas/testutils"
"testing"

"github.com/canonical/gomaasclient/client"
"github.com/canonical/gomaasclient/entity"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/assert"
)

func TestAccResourceMaasTag_basic(t *testing.T) {

var tag entity.Tag
comment := "Test comment"
name := acctest.RandomWithPrefix("tf-tag-")
machines := os.Getenv("TF_ACC_TAG_MACHINES")

checks := []resource.TestCheckFunc{
testAccMaasTagCheckExists("maas_tag.test", &tag),
resource.TestCheckResourceAttr("maas_tag.test", "name", name),
resource.TestCheckResourceAttr("maas_tag.test", "comment", comment),
resource.TestCheckResourceAttr("maas_tag.test", "machines.#", strconv.Itoa(len(strings.Split(machines, ",")))),
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, []string{"TF_ACC_TAG_MACHINES"}) },
Providers: testutils.TestAccProviders,
CheckDestroy: testAccCheckMaasTagDestroy,
ErrorCheck: func(err error) error { return err },
Steps: []resource.TestStep{
{
Config: testAccMaasTag(name, comment, machines),
Check: resource.ComposeTestCheckFunc(checks...),
},
// Test import using name
{
ResourceName: "maas_tag.test",
ImportState: true,
ImportStateCheck: func(is []*terraform.InstanceState) error {
var tag *terraform.InstanceState
if len(is) != 1 {
return fmt.Errorf("expected 1 state: %#v", t)
}
tag = is[0]
assert.Equal(t, tag.Attributes["name"], name)
assert.Equal(t, tag.Attributes["comment"], comment)
assert.Equal(t, tag.Attributes["machines.#"], strconv.Itoa(len(strings.Split(machines, ","))))
return nil
},
},
},
})
}

func testAccMaasTagCheckExists(rn string, tag *entity.Tag) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rn]
if !ok {
return fmt.Errorf("resource not found: %s\n %#v", rn, s.RootModule().Resources)
}

if rs.Primary.ID == "" {
return fmt.Errorf("resource id not set")
}

conn := testutils.TestAccProvider.Meta().(*client.Client)
gotTag, err := conn.Tag.Get(rs.Primary.ID)
if err != nil {
return fmt.Errorf("error getting tag: %s", err)
}

*tag = *gotTag

return nil
}
}

func testAccMaasTag(name string, comment string, machines string) string {
return fmt.Sprintf(`
resource "maas_tag" "test" {
name = "%s"
kernel_opts = "console=tty1 console=ttyS0"
machines = split(",", "%s")
comment = "%s"
}
`, name, machines, comment)
}

func testAccCheckMaasTagDestroy(s *terraform.State) error {
// retrieve the connection established in Provider configuration
conn := testutils.TestAccProvider.Meta().(*client.Client)

// loop through the resources in state, verifying each maas_tag
// is destroyed
for _, rs := range s.RootModule().Resources {
if rs.Type != "maas_tag" {
continue
}

// Retrieve our maas_tag by referencing it's state ID for API lookup
response, err := conn.Tag.Get(rs.Primary.ID)
if err == nil {
if response != nil && response.Name == rs.Primary.ID {
return fmt.Errorf("MAAS Tag (%s) still exists.", rs.Primary.ID)
}

return nil
}

// If the error is equivalent to 404 not found, the maas_tag is destroyed.
// Otherwise return the error
if !strings.Contains(err.Error(), "404 Not Found") {
return err
}
}

return nil
}

0 comments on commit 0782fe1

Please sign in to comment.