Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add disks support to compute_node_template #8620

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/12187.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added `disks` field to `google_compute_node_template` resource
```
144 changes: 144 additions & 0 deletions google-beta/services/compute/resource_compute_node_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,35 @@ to this node template.`,
ForceNew: true,
Description: `An optional textual description of the resource.`,
},
"disks": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `List of the type, size and count of disks attached to the
node template`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"disk_count": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Description: `Specifies the number of such disks.`,
},
"disk_size_gb": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Description: `Specifies the size of the disk in base-2 GB.`,
},
"disk_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Specifies the desired disk type on the node. This disk type must be a local storage type (e.g.: local-ssd). Note that for nodeTemplates, this should be the name of the disk type and not its URL.`,
},
},
},
},
"name": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -263,6 +292,12 @@ func resourceComputeNodeTemplateCreate(d *schema.ResourceData, meta interface{})
} else if v, ok := d.GetOkExists("cpu_overcommit_type"); !tpgresource.IsEmptyValue(reflect.ValueOf(cpuOvercommitTypeProp)) && (ok || !reflect.DeepEqual(v, cpuOvercommitTypeProp)) {
obj["cpuOvercommitType"] = cpuOvercommitTypeProp
}
disksProp, err := expandComputeNodeTemplateDisks(d.Get("disks"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("disks"); !tpgresource.IsEmptyValue(reflect.ValueOf(disksProp)) && (ok || !reflect.DeepEqual(v, disksProp)) {
obj["disks"] = disksProp
}
regionProp, err := expandComputeNodeTemplateRegion(d.Get("region"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -395,6 +430,9 @@ func resourceComputeNodeTemplateRead(d *schema.ResourceData, meta interface{}) e
if err := d.Set("cpu_overcommit_type", flattenComputeNodeTemplateCpuOvercommitType(res["cpuOvercommitType"], d, config)); err != nil {
return fmt.Errorf("Error reading NodeTemplate: %s", err)
}
if err := d.Set("disks", flattenComputeNodeTemplateDisks(res["disks"], d, config)); err != nil {
return fmt.Errorf("Error reading NodeTemplate: %s", err)
}
if err := d.Set("region", flattenComputeNodeTemplateRegion(res["region"], d, config)); err != nil {
return fmt.Errorf("Error reading NodeTemplate: %s", err)
}
Expand Down Expand Up @@ -592,6 +630,64 @@ func flattenComputeNodeTemplateCpuOvercommitType(v interface{}, d *schema.Resour
return v
}

func flattenComputeNodeTemplateDisks(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
}
l := v.([]interface{})
transformed := make([]interface{}, 0, len(l))
for _, raw := range l {
original := raw.(map[string]interface{})
if len(original) < 1 {
// Do not include empty json objects coming back from the api
continue
}
transformed = append(transformed, map[string]interface{}{
"disk_count": flattenComputeNodeTemplateDisksDiskCount(original["diskCount"], d, config),
"disk_type": flattenComputeNodeTemplateDisksDiskType(original["diskType"], d, config),
"disk_size_gb": flattenComputeNodeTemplateDisksDiskSizeGb(original["diskSizeGb"], d, config),
})
}
return transformed
}
func flattenComputeNodeTemplateDisksDiskCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := tpgresource.StringToFixed64(strVal); err == nil {
return intVal
}
}

// number values are represented as float64
if floatVal, ok := v.(float64); ok {
intVal := int(floatVal)
return intVal
}

return v // let terraform core handle it otherwise
}

func flattenComputeNodeTemplateDisksDiskType(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenComputeNodeTemplateDisksDiskSizeGb(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := tpgresource.StringToFixed64(strVal); err == nil {
return intVal
}
}

// number values are represented as float64
if floatVal, ok := v.(float64); ok {
intVal := int(floatVal)
return intVal
}

return v // let terraform core handle it otherwise
}

func flattenComputeNodeTemplateRegion(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -731,6 +827,54 @@ func expandComputeNodeTemplateCpuOvercommitType(v interface{}, d tpgresource.Ter
return v, nil
}

func expandComputeNodeTemplateDisks(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
for _, raw := range l {
if raw == nil {
continue
}
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedDiskCount, err := expandComputeNodeTemplateDisksDiskCount(original["disk_count"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedDiskCount); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["diskCount"] = transformedDiskCount
}

transformedDiskType, err := expandComputeNodeTemplateDisksDiskType(original["disk_type"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedDiskType); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["diskType"] = transformedDiskType
}

transformedDiskSizeGb, err := expandComputeNodeTemplateDisksDiskSizeGb(original["disk_size_gb"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedDiskSizeGb); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["diskSizeGb"] = transformedDiskSizeGb
}

req = append(req, transformed)
}
return req, nil
}

func expandComputeNodeTemplateDisksDiskCount(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandComputeNodeTemplateDisksDiskType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandComputeNodeTemplateDisksDiskSizeGb(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandComputeNodeTemplateRegion(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
f, err := tpgresource.ParseGlobalFieldValue("regions", v.(string), "project", d, config, true)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,51 @@ resource "google_compute_node_template" "template" {
`, context)
}

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

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeNodeTemplateDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeNodeTemplate_nodeTemplateDisksExample(context),
},
{
ResourceName: "google_compute_node_template.template",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"region"},
},
},
})
}

func testAccComputeNodeTemplate_nodeTemplateDisksExample(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_compute_node_types" "central1a" {
zone = "us-central1-a"
}

resource "google_compute_node_template" "template" {
name = "tf-test-soletenant-with-disks%{random_suffix}"
region = "us-central1"
node_type = "n2-node-80-640"

disks {
disk_count = 16
disk_size_gb = 375
disk_type = "local-ssd"
}
}
`, context)
}

func testAccCheckComputeNodeTemplateDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
45 changes: 45 additions & 0 deletions website/docs/r/compute_node_template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,31 @@ resource "google_compute_node_template" "template" {
}
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=node_template_disks&open_in_editor=main.tf" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Node Template Disks


```hcl
data "google_compute_node_types" "central1a" {
zone = "us-central1-a"
}

resource "google_compute_node_template" "template" {
name = "soletenant-with-disks"
region = "us-central1"
node_type = "n2-node-80-640"

disks {
disk_count = 16
disk_size_gb = 375
disk_type = "local-ssd"
}
}
```

## Argument Reference

Expand Down Expand Up @@ -150,6 +175,12 @@ The following arguments are supported:
Default value is `NONE`.
Possible values are: `ENABLED`, `NONE`.

* `disks` -
(Optional)
List of the type, size and count of disks attached to the
node template
Structure is [documented below](#nested_disks).

* `region` -
(Optional)
Region where nodes using the node template will be created.
Expand Down Expand Up @@ -202,6 +233,20 @@ The following arguments are supported:
Full or partial URL of the accelerator type resource to expose
to this node template.

<a name="nested_disks"></a>The `disks` block supports:

* `disk_count` -
(Optional)
Specifies the number of such disks.

* `disk_type` -
(Optional)
Specifies the desired disk type on the node. This disk type must be a local storage type (e.g.: local-ssd). Note that for nodeTemplates, this should be the name of the disk type and not its URL.

* `disk_size_gb` -
(Optional)
Specifies the size of the disk in base-2 GB.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:
Expand Down