Skip to content

Commit

Permalink
Add remaining fields to TPU VM resource (#9308) (#1629)
Browse files Browse the repository at this point in the history
* Revert "Skip generated tests for google_tpu_v2_vm (#9315)"

This reverts commit 5dc9f2ea88610c0d2a2a94d04548d60e90e1cc54.

* Add remaining fields to TPU VM resource

* Change update test to use preemptible vm

* Retain disk in test so that it isn't removed prematurely

* Add networking fields

* Respond to PR comments

* More tweaks from PR comments

* Tweak test names based on feedback
[upstream:b48fc2d24968dc972360eb82c704b4eaeb8b40ba]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Oct 23, 2023
1 parent 7cb6147 commit d7fad0e
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/hashicorp/hcl/v2 v2.14.1
github.com/hashicorp/terraform-json v0.14.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.0
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20231020233921-7172a0397fdd
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20231023131605-b8aecd8e835f
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ github.com/hashicorp/terraform-plugin-log v0.7.0/go.mod h1:p4R1jWBXRTvL4odmEkFfD
github.com/hashicorp/terraform-plugin-mux v0.8.0 h1:WCTP66mZ+iIaIrCNJnjPEYnVjawTshnDJu12BcXK1EI=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.0 h1:FtCLTiTcykdsURXPt/ku7fYXm3y19nbzbZcUxHx9RbI=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.0/go.mod h1:80wf5oad1tW+oLnbXS4UTYmDCrl7BuN1Q+IA91X1a4Y=
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20231020233921-7172a0397fdd h1:i9vMmXH4jT1kgVGlFimMaQgL0yX8b2dZtqDkBUAG36s=
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20231020233921-7172a0397fdd/go.mod h1:siHCRIedgArrFUUzSvIsIj2tWJT6YMtJJg7fBB8H5LY=
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20231023131605-b8aecd8e835f h1:+UekJtQte0QcEcsd9RreJqwrPZOXGS79ndBEgHHUQ2I=
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20231023131605-b8aecd8e835f/go.mod h1:siHCRIedgArrFUUzSvIsIj2tWJT6YMtJJg7fBB8H5LY=
github.com/hashicorp/terraform-registry-address v0.1.0 h1:W6JkV9wbum+m516rCl5/NjKxCyTVaaUBbzYcMzBDO3U=
github.com/hashicorp/terraform-registry-address v0.1.0/go.mod h1:EnyO2jYO6j29DTHbJcm00E5nQTFeTtyZH3H5ycydQ5A=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
Expand Down
302 changes: 302 additions & 0 deletions tfplan2cai/converters/google/resources/services/tpuv2/tpuv2_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,46 @@ package tpuv2

import (
"reflect"
"strings"

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

"github.com/GoogleCloudPlatform/terraform-google-conversion/v2/tfplan2cai/converters/google/resources/cai"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
)

// Suppress unremovable default scope values from GCP.
func tpuServiceAccountAddedScopesSuppress(k, old, new string, d *schema.ResourceData) bool {
if strings.Contains(k, "scope.#") && (new == "0" || new == "") && old != new {
return false
}

// Get changes for service_account.xx.scope
b := strings.Split(k, ".")
o, n := d.GetChange(strings.TrimSuffix(k, "."+b[len(b)-1]))
if o == nil || n == nil {
return false
}

oList := normalizeScopes(tpgresource.ConvertStringArr(o.([]interface{})))
nList := normalizeScopes(tpgresource.ConvertStringArr(n.([]interface{})))

return reflect.DeepEqual(oList, nList)
}

// Normalize the scopes by filtering out the `https://www.googleapis.com/auth/pubsub` scope during
// comparison
func normalizeScopes(scopes []string) []string {
var result []string
for _, s := range scopes {
if s != "https://www.googleapis.com/auth/pubsub" {
result = append(result, s)
}
}
return result
}

const TpuV2VmAssetType string = "tpu.googleapis.com/Vm"

func ResourceConverterTpuV2Vm() cai.ResourceConverter {
Expand Down Expand Up @@ -78,6 +112,60 @@ func GetTpuV2VmApiObject(d tpgresource.TerraformResourceData, config *transport_
} else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
cidrBlockProp, err := expandTpuV2VmCidrBlock(d.Get("cidr_block"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("cidr_block"); !tpgresource.IsEmptyValue(reflect.ValueOf(cidrBlockProp)) && (ok || !reflect.DeepEqual(v, cidrBlockProp)) {
obj["cidrBlock"] = cidrBlockProp
}
networkConfigProp, err := expandTpuV2VmNetworkConfig(d.Get("network_config"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("network_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(networkConfigProp)) && (ok || !reflect.DeepEqual(v, networkConfigProp)) {
obj["networkConfig"] = networkConfigProp
}
serviceAccountProp, err := expandTpuV2VmServiceAccount(d.Get("service_account"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("service_account"); !tpgresource.IsEmptyValue(reflect.ValueOf(serviceAccountProp)) && (ok || !reflect.DeepEqual(v, serviceAccountProp)) {
obj["serviceAccount"] = serviceAccountProp
}
schedulingConfigProp, err := expandTpuV2VmSchedulingConfig(d.Get("scheduling_config"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("scheduling_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(schedulingConfigProp)) && (ok || !reflect.DeepEqual(v, schedulingConfigProp)) {
obj["schedulingConfig"] = schedulingConfigProp
}
dataDisksProp, err := expandTpuV2VmDataDisks(d.Get("data_disks"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("data_disks"); !tpgresource.IsEmptyValue(reflect.ValueOf(dataDisksProp)) && (ok || !reflect.DeepEqual(v, dataDisksProp)) {
obj["dataDisks"] = dataDisksProp
}
shieldedInstanceConfigProp, err := expandTpuV2VmShieldedInstanceConfig(d.Get("shielded_instance_config"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("shielded_instance_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(shieldedInstanceConfigProp)) && (ok || !reflect.DeepEqual(v, shieldedInstanceConfigProp)) {
obj["shieldedInstanceConfig"] = shieldedInstanceConfigProp
}
metadataProp, err := expandTpuV2VmMetadata(d.Get("metadata"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("metadata"); !tpgresource.IsEmptyValue(reflect.ValueOf(metadataProp)) && (ok || !reflect.DeepEqual(v, metadataProp)) {
obj["metadata"] = metadataProp
}
tagsProp, err := expandTpuV2VmTags(d.Get("tags"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("tags"); !tpgresource.IsEmptyValue(reflect.ValueOf(tagsProp)) && (ok || !reflect.DeepEqual(v, tagsProp)) {
obj["tags"] = tagsProp
}
labelsProp, err := expandTpuV2VmEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}

return obj, nil
}
Expand All @@ -97,3 +185,217 @@ func expandTpuV2VmAcceleratorType(v interface{}, d tpgresource.TerraformResource
func expandTpuV2VmDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

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

func expandTpuV2VmNetworkConfig(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

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

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

transformedEnableExternalIps, err := expandTpuV2VmNetworkConfigEnableExternalIps(original["enable_external_ips"], d, config)
if err != nil {
return nil, err
} else {
transformed["enableExternalIps"] = transformedEnableExternalIps
}

transformedCanIpForward, err := expandTpuV2VmNetworkConfigCanIpForward(original["can_ip_forward"], d, config)
if err != nil {
return nil, err
} else {
transformed["canIpForward"] = transformedCanIpForward
}

return transformed, nil
}

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

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

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

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

func expandTpuV2VmServiceAccount(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

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

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

return transformed, nil
}

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

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

func expandTpuV2VmSchedulingConfig(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedPreemptible, err := expandTpuV2VmSchedulingConfigPreemptible(original["preemptible"], d, config)
if err != nil {
return nil, err
} else {
transformed["preemptible"] = transformedPreemptible
}

transformedReserved, err := expandTpuV2VmSchedulingConfigReserved(original["reserved"], d, config)
if err != nil {
return nil, err
} else {
transformed["reserved"] = transformedReserved
}

return transformed, nil
}

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

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

func expandTpuV2VmDataDisks(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{})

transformedSourceDisk, err := expandTpuV2VmDataDisksSourceDisk(original["source_disk"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedSourceDisk); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["sourceDisk"] = transformedSourceDisk
}

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

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

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

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

func expandTpuV2VmShieldedInstanceConfig(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedEnableSecureBoot, err := expandTpuV2VmShieldedInstanceConfigEnableSecureBoot(original["enable_secure_boot"], d, config)
if err != nil {
return nil, err
} else {
transformed["enableSecureBoot"] = transformedEnableSecureBoot
}

return transformed, nil
}

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

func expandTpuV2VmMetadata(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
return m, nil
}

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

func expandTpuV2VmEffectiveLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
return m, nil
}

0 comments on commit d7fad0e

Please sign in to comment.