Skip to content

Commit

Permalink
make initial size or autoscaling policy required on creation time, no…
Browse files Browse the repository at this point in the history
…degroup mutable, update tests
  • Loading branch information
NickElliot committed Sep 21, 2023
1 parent 14abe60 commit c390e14
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 32 deletions.
25 changes: 7 additions & 18 deletions mmv1/products/compute/NodeGroup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ name: 'NodeGroup'
kind: 'compute#NodeGroup'
base_url: projects/{{project}}/zones/{{zone}}/nodeGroups
create_url: projects/{{project}}/zones/{{zone}}/nodeGroups?initialNodeCount=PRE_CREATE_REPLACE_ME
update_verb: :PATCH
update_mask: true
has_self_link: true
description: |
Represents a NodeGroup resource to manage a group of sole-tenant nodes.
immutable: true
references: !ruby/object:Api::Resource::ReferenceLinks
guides:
'Sole-Tenant Nodes': 'https://cloud.google.com/compute/docs/nodes/'
Expand All @@ -43,12 +44,6 @@ async: !ruby/object:Api::OpAsync
error: !ruby/object:Api::OpAsync::Error
path: 'error/errors'
message: 'message'
docs: !ruby/object:Provider::Terraform::Docs
warning: |
Due to limitations of the API, Terraform cannot update the
number of nodes in a node group and changes to node group size either
through Terraform config or through external changes will cause
Terraform to delete and recreate the node group.
examples:
- !ruby/object:Provider::Terraform::Examples
name: 'node_group_basic'
Expand Down Expand Up @@ -112,21 +107,13 @@ properties:
- !ruby/object:Api::Type::Integer
name: 'size'
description: |
The total number of nodes in the node group. One of `initial_size` or `size` must be specified.
immutable: true
send_empty_value: true
default_from_api: true
exactly_one_of:
- size
- initial_size
The total number of nodes in the node group.
output: true
- !ruby/object:Api::Type::Integer
name: 'initialSize'
description: |
The initial number of nodes in the node group. One of `initial_size` or `size` must be specified.
The initial number of nodes in the node group. One of `initial_size` or `autoscaling_policy` must be configured on resource creation.
url_param_only: true
exactly_one_of:
- size
- initial_size
- !ruby/object:Api::Type::String
name: 'maintenancePolicy'
description: |
Expand All @@ -147,6 +134,8 @@ properties:
description: |
If you use sole-tenant nodes for your workloads, you can use the node
group autoscaler to automatically manage the sizes of your node groups.
One of `initial_size` or `autoscaling_policy` must be configured on resource creation.
default_from_api: true
properties:
- !ruby/object:Api::Type::Enum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var sizeParam string
if v, ok := d.GetOkExists("size"); ok {
sizeParam = fmt.Sprintf("%v", v)
} else if v, ok := d.GetOkExists("initial_size"); ok {
if v, ok := d.GetOkExists("initial_size"); ok {
sizeParam = fmt.Sprintf("%v", v)
}else{
if _, ok := d.GetOkExists("autoscaling_policy"); ok{
sizeParam = fmt.Sprintf("%v", d.Get("autoscaling_policy.min_nodes"))
}else{
return errors.New("An initial_size or autoscaling_policy must be configured on node group creation.")
}
}

url = regexp.MustCompile("PRE_CREATE_REPLACE_ME").ReplaceAllLiteralString(url, sizeParam)
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"strings"
"time"

"regexp"

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

func TestAccComputeNodeGroup_updateNodeTemplate(t *testing.T) {
func TestAccComputeNodeGroup_update(t *testing.T) {
t.Parallel()

groupName := fmt.Sprintf("group--%d", acctest.RandInt(t))
Expand All @@ -26,31 +28,54 @@ func TestAccComputeNodeGroup_updateNodeTemplate(t *testing.T) {
CheckDestroy: testAccCheckComputeNodeGroupDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeNodeGroup_updateNodeTemplate(groupName, tmplPrefix, "tmpl1"),
Config: testAccComputeNodeGroup_update(groupName, tmplPrefix, "tmpl1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeNodeGroupCreationTimeBefore(&timeCreated),
),
},
{
ResourceName: "google_compute_node_group.nodes",
ImportState: true,
ImportStateVerify: true,
ResourceName: "google_compute_node_group.nodes",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"initial_size"},
},
{
Config: testAccComputeNodeGroup_updateNodeTemplate(groupName, tmplPrefix, "tmpl2"),
Config: testAccComputeNodeGroup_update2(groupName, tmplPrefix, "tmpl2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeNodeGroupCreationTimeBefore(&timeCreated),
),
},
{
ResourceName: "google_compute_node_group.nodes",
ImportState: true,
ImportStateVerify: true,
ResourceName: "google_compute_node_group.nodes",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"initial_size"},
},
},
})
}

func TestAccComputeNodeGroup_fail(t *testing.T) {
t.Parallel()

groupName := fmt.Sprintf("group--%d", acctest.RandInt(t))
tmplPrefix := fmt.Sprintf("tmpl--%d", acctest.RandInt(t))

var timeCreated time.Time
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeNodeGroupDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeNodeGroup_fail(groupName, tmplPrefix, "tmpl1"),
ExpectError: regexp.MustCompile("An initial_size or autoscaling_policy must be configured on node group creation."),
},
},
})
}


func testAccCheckComputeNodeGroupCreationTimeBefore(prevTimeCreated *time.Time) resource.TestCheckFunc {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down Expand Up @@ -86,7 +111,7 @@ func testAccCheckComputeNodeGroupCreationTimeBefore(prevTimeCreated *time.Time)
}
}

func testAccComputeNodeGroup_updateNodeTemplate(groupName, tmplPrefix, tmplToUse string) string {
func testAccComputeNodeGroup_update(groupName, tmplPrefix, tmplToUse string) string {
return fmt.Sprintf(`
resource "google_compute_node_template" "tmpl1" {
name = "%s-first"
Expand All @@ -105,8 +130,58 @@ resource "google_compute_node_group" "nodes" {
zone = "us-central1-a"
description = "example google_compute_node_group for Terraform Google Provider"

size = 0
initial_size = 1
node_template = google_compute_node_template.%s.self_link
}

`, tmplPrefix, tmplPrefix, groupName, tmplToUse)
}

func testAccComputeNodeGroup_update2(groupName, tmplPrefix, tmplToUse string) string {
return fmt.Sprintf(`
resource "google_compute_node_template" "tmpl1" {
name = "%s-first"
region = "us-central1"
node_type = "n1-node-96-624"
}

resource "google_compute_node_template" "tmpl2" {
name = "%s-second"
region = "us-central1"
node_type = "n1-node-96-624"
}

resource "google_compute_node_group" "nodes" {
name = "%s"
zone = "us-central1-a"
description = "example google_compute_node_group for Terraform Google Provider"

autoscaling_policy {
mode = "ONLY_SCALE_OUT"
min_nodes = 1
max_nodes = 10
}
node_template = google_compute_node_template.%s.self_link
}

`, tmplPrefix, tmplPrefix, groupName, tmplToUse)
}

func testAccComputeNodeGroup_update(groupName, tmplPrefix, tmplToUse string) string {
return fmt.Sprintf(`
resource "google_compute_node_template" "tmpl1" {
name = "%s-first"
region = "us-central1"
node_type = "n1-node-96-624"
}

resource "google_compute_node_group" "nodes" {
name = "%s"
zone = "us-central1-a"
description = "example google_compute_node_group for Terraform Google Provider"

node_template = google_compute_node_template.%s.self_link
}

`, tmplPrefix, tmplPrefix, groupName, tmplToUse)
}

0 comments on commit c390e14

Please sign in to comment.