Skip to content

Commit

Permalink
add resource and data sources for node pools
Browse files Browse the repository at this point in the history
  • Loading branch information
lgfa29 committed Jul 7, 2023
1 parent e33898b commit 382deed
Show file tree
Hide file tree
Showing 13 changed files with 1,180 additions and 8 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/nomad v1.5.2
github.com/hashicorp/nomad/api v0.0.0-20230321225438-9a2fdb5f53dc
github.com/hashicorp/nomad/api v0.0.0-20230627160105-1d1e606846fa // v1.6.0-beta.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/hashicorp/vault v0.10.4
github.com/stretchr/testify v1.8.1
Expand Down Expand Up @@ -78,7 +78,6 @@ require (
github.com/oklog/run v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/shoenig/test v0.6.3 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
Expand Down Expand Up @@ -238,8 +239,8 @@ github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/nomad v1.5.2 h1:4dHbKF2UZEYXoXPUfG1VdSDaOaGpuqZUiCnuKuna83o=
github.com/hashicorp/nomad v1.5.2/go.mod h1:ywYvhApBI9mRlmS8qMhoaWUbzeVGiJHKMjwy8l7fHRY=
github.com/hashicorp/nomad/api v0.0.0-20230321225438-9a2fdb5f53dc h1:XhUHxwOMmGbSun7xsszFLsBqGz3WbWhi5zWVAEo+c8M=
github.com/hashicorp/nomad/api v0.0.0-20230321225438-9a2fdb5f53dc/go.mod h1:bKUb1ytds5KwUioHdvdq9jmrDqCThv95si0Ub7iNeBg=
github.com/hashicorp/nomad/api v0.0.0-20230627160105-1d1e606846fa h1:RzT6JRS4Ehe12Iy2kmsCrjivgsHuGngCXEwxoirNz9U=
github.com/hashicorp/nomad/api v0.0.0-20230627160105-1d1e606846fa/go.mod h1:Xjd3OXUTfsWbCCBsQd3EdfPTz5evDi+fxqdvpN+WqQg=
github.com/hashicorp/serf v0.10.1 h1:Z1H2J60yRKvfDYAOZLd2MU0ND4AH/WDz7xYHDWQsIPY=
github.com/hashicorp/terraform-exec v0.15.0 h1:cqjh4d8HYNQrDoEmlSGelHmg2DYDh5yayckvJ5bV18E=
github.com/hashicorp/terraform-exec v0.15.0/go.mod h1:H4IG8ZxanU+NW0ZpDRNsvh9f0ul7C0nHP+rUR/CHs7I=
Expand Down Expand Up @@ -356,8 +357,7 @@ github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAm
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shoenig/test v0.6.3 h1:GVXWJFk9PiOjN0KoJ7VrJGH6uLPnqxR7/fe3HUPfE0c=
github.com/shoenig/test v0.6.3/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
github.com/shoenig/test v0.6.6 h1:Oe8TPH9wAbv++YPNDKJWUnI8Q4PPWCx3UbOfH+FxiMU=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
Expand Down
88 changes: 88 additions & 0 deletions nomad/data_source_node_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package nomad

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-nomad/nomad/helper"
)

func dataSourceNodePool() *schema.Resource {
return &schema.Resource{
Read: dataSourceNodePoolRead,

Schema: map[string]*schema.Schema{
"name": {
Description: "Unique name for this node pool.",
Type: schema.TypeString,
Required: true,
},
"description": {
Description: "Description for this node pool.",
Type: schema.TypeString,
Computed: true,
},
"meta": {
Description: "Metadata associated with the node pool",
Type: schema.TypeMap,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
},
"scheduler_config": {
Description: "Scheduler configuration for the node pool.",
Type: schema.TypeSet,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"scheduler_algorithm": {
Description: "The scheduler algorithm to use in the node pool.",
Type: schema.TypeString,
Computed: true,
},

// This field must be a string instead of a bool (and differ from
// Nomad) in order to represent a tristate.
// - "enabled": memory oversubscription is enabled in the pool.
// - "disabled": memory oversubscription is disabled in the pool.
// - "": the global memory oversubscription value is used.
"memory_oversubscription": {
Description: "If true, the node pool will have memory oversubscription enabled.",
Type: schema.TypeString,
Computed: true,
},
},
},
Computed: true,
},
},
}
}

func dataSourceNodePoolRead(d *schema.ResourceData, meta any) error {
client := meta.(ProviderConfig).client

name := d.Get("name").(string)
log.Printf("[DEBUG] Reading node pool %q", name)
pool, _, err := client.NodePools().Info(name, nil)
if err != nil {
return fmt.Errorf("error reading node pool %q: %w", name, err)
}
log.Printf("[DEBUG] Read node pool %q", name)

sw := helper.NewStateWriter(d)
sw.Set("name", pool.Name)
sw.Set("description", pool.Description)
sw.Set("meta", pool.Meta)
sw.Set("scheduler_config", flattenNodePoolSchedulerConfiguration(pool.SchedulerConfiguration))
if err := sw.Error(); err != nil {
return err
}

d.SetId(name)
return nil
}
148 changes: 148 additions & 0 deletions nomad/data_source_node_pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package nomad

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestDataSourceNodePool(t *testing.T) {
name := acctest.RandomWithPrefix("tf-nomad-test")
resource.Test(t, resource.TestCase{
Providers: testProviders,
PreCheck: func() { testAccPreCheck(t); testCheckMinVersion(t, "1.6.0-beta.1") },
Steps: []resource.TestStep{
{
Config: testDataSourceNodePoolConfig_builtIn,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.nomad_node_pool.all", "name", "all"),
resource.TestCheckResourceAttr("data.nomad_node_pool.default", "name", "default"),
),
},
{
Config: testDataSourceNodePoolConfig_doesntExist,
ExpectError: regexp.MustCompile("node pool not found"),
},
{
Config: testDataSourceNodePoolConfig_basic(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.nomad_node_pool.test", "name", name),
resource.TestCheckResourceAttr("data.nomad_node_pool.test", "description", "Terraform test node pool"),
resource.TestCheckResourceAttr("data.nomad_node_pool.test", "meta.%", "1"),
resource.TestCheckResourceAttr("data.nomad_node_pool.test", "meta.test", "true"),
resource.TestCheckNoResourceAttr("data.nomad_node_pool.test", "scheduler_config"),
),
},
},
CheckDestroy: testResourceNodePool_checkDestroy(name),
})
}

func TestDataSourceNodePool_schedConfig(t *testing.T) {
name := acctest.RandomWithPrefix("tf-nomad-test")
resource.Test(t, resource.TestCase{
Providers: testProviders,
PreCheck: func() { testAccPreCheck(t); testCheckMinVersion(t, "1.6.0-beta.1"); testCheckEnt(t) },
Steps: []resource.TestStep{
{
Config: testDataSourceNodePoolConfig_schedConfig(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.nomad_node_pool.no_mem_oversub", "name", fmt.Sprintf("%s-no-mem-oversub", name)),
resource.TestCheckResourceAttr("data.nomad_node_pool.no_mem_oversub", "scheduler_config.0.scheduler_algorithm", "spread"),
resource.TestCheckResourceAttr("data.nomad_node_pool.no_mem_oversub", "scheduler_config.0.memory_oversubscription", ""),

resource.TestCheckResourceAttr("data.nomad_node_pool.mem_oversub_disabled", "name", fmt.Sprintf("%s-mem-oversub-disabled", name)),
resource.TestCheckResourceAttr("data.nomad_node_pool.mem_oversub_disabled", "scheduler_config.0.scheduler_algorithm", "binpack"),
resource.TestCheckResourceAttr("data.nomad_node_pool.mem_oversub_disabled", "scheduler_config.0.memory_oversubscription", "disabled"),

resource.TestCheckResourceAttr("data.nomad_node_pool.mem_oversub_enabled", "name", fmt.Sprintf("%s-mem-oversub-enabled", name)),
resource.TestCheckResourceAttr("data.nomad_node_pool.mem_oversub_enabled", "scheduler_config.0.scheduler_algorithm", "binpack"),
resource.TestCheckResourceAttr("data.nomad_node_pool.mem_oversub_enabled", "scheduler_config.0.memory_oversubscription", "enabled"),
),
},
},
CheckDestroy: testResourceNodePool_checkDestroy(name),
})
}

const testDataSourceNodePoolConfig_builtIn = `
data "nomad_node_pool" "all" {
name = "all"
}
data "nomad_node_pool" "default" {
name = "default"
}
`

const testDataSourceNodePoolConfig_doesntExist = `
data "nomad_node_pool" "doesnt_exist" {
name = "doesnt-exist"
}
`

func testDataSourceNodePoolConfig_basic(name string) string {
return fmt.Sprintf(`
resource "nomad_node_pool" "test" {
name = "%s"
description = "Terraform test node pool"
meta = {
test = "true"
}
}
data "nomad_node_pool" "test" {
name = nomad_node_pool.test.name
}
`, name)
}

func testDataSourceNodePoolConfig_schedConfig(prefix string) string {
return fmt.Sprintf(`
resource "nomad_node_pool" "no_mem_oversub" {
name = "%[1]s-no-mem-oversub"
scheduler_config {
scheduler_algorithm = "spread"
}
}
data "nomad_node_pool" "no_mem_oversub" {
name = nomad_node_pool.no_mem_oversub.name
}
resource "nomad_node_pool" "mem_oversub_disabled" {
name = "%[1]s-mem-oversub-disabled"
scheduler_config {
scheduler_algorithm = "binpack"
memory_oversubscription = "disabled"
}
}
data "nomad_node_pool" "mem_oversub_disabled" {
name = nomad_node_pool.mem_oversub_disabled.name
}
resource "nomad_node_pool" "mem_oversub_enabled" {
name = "%[1]s-mem-oversub-enabled"
scheduler_config {
scheduler_algorithm = "binpack"
memory_oversubscription = "enabled"
}
}
data "nomad_node_pool" "mem_oversub_enabled" {
name = nomad_node_pool.mem_oversub_enabled.name
}
`, prefix)
}
Loading

0 comments on commit 382deed

Please sign in to comment.