-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add resource and data sources for node pools
- Loading branch information
Showing
13 changed files
with
1,180 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.