From f21e7724ade9937e5084b3f8999fdd507ca72948 Mon Sep 17 00:00:00 2001 From: dfdossantos Date: Tue, 21 May 2024 17:14:51 +0000 Subject: [PATCH] Add support for `google_compute_target_pool` to TGC (#10727) --- mmv1/provider/terraform_tgc.rb | 4 +- mmv1/templates/tgc/resource_converters.go.erb | 1 + mmv1/third_party/tgc/compute_target_pool.go | 157 ++++++++++++++++++ .../data/example_google_compute_autoscaler.tf | 6 - .../example_google_compute_target_pool.json | 23 +++ .../example_google_compute_target_pool.tf | 21 +++ 6 files changed, 205 insertions(+), 7 deletions(-) create mode 100644 mmv1/third_party/tgc/compute_target_pool.go create mode 100644 mmv1/third_party/tgc/tests/data/example_google_compute_target_pool.json create mode 100644 mmv1/third_party/tgc/tests/data/example_google_compute_target_pool.tf diff --git a/mmv1/provider/terraform_tgc.rb b/mmv1/provider/terraform_tgc.rb index af92d4086476..6533e86ecef5 100644 --- a/mmv1/provider/terraform_tgc.rb +++ b/mmv1/provider/terraform_tgc.rb @@ -307,7 +307,9 @@ def copy_common_files(output_folder, generate_code, _generate_docs) ['converters/google/convert_test.go', 'third_party/tgc/convert_test.go'], ['converters/google/resources/service_account_key.go', - 'third_party/tgc/service_account_key.go'] + 'third_party/tgc/service_account_key.go'], + ['converters/google/resources/compute_target_pool.go', + 'third_party/tgc/compute_target_pool.go'] ]) end diff --git a/mmv1/templates/tgc/resource_converters.go.erb b/mmv1/templates/tgc/resource_converters.go.erb index 64015eeb959c..cfb3d7e2a344 100644 --- a/mmv1/templates/tgc/resource_converters.go.erb +++ b/mmv1/templates/tgc/resource_converters.go.erb @@ -61,6 +61,7 @@ func ResourceConverters() map[string][]cai.ResourceConverter { "google_compute_target_http_proxy": {compute.ResourceConverterComputeTargetHttpProxy()}, "google_compute_target_https_proxy": {compute.ResourceConverterComputeTargetHttpsProxy()}, "google_compute_target_ssl_proxy": {compute.ResourceConverterComputeTargetSslProxy()}, + "google_compute_target_pool": {resourceConverterComputeTargetPool()}, "google_dataproc_autoscaling_policy": {dataproc.ResourceConverterDataprocAutoscalingPolicy()}, "google_dns_managed_zone": {dns.ResourceConverterDNSManagedZone()}, "google_dns_policy": {dns.ResourceConverterDNSPolicy()}, diff --git a/mmv1/third_party/tgc/compute_target_pool.go b/mmv1/third_party/tgc/compute_target_pool.go new file mode 100644 index 000000000000..60a5e28b8a8a --- /dev/null +++ b/mmv1/third_party/tgc/compute_target_pool.go @@ -0,0 +1,157 @@ +package google + +import ( + "reflect" + + "github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/cai" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" +) + +const ComputeTargetPoolAssetType string = "compute.googleapis.com/TargetPool" + +func resourceConverterComputeTargetPool() cai.ResourceConverter { + return cai.ResourceConverter{ + AssetType: ComputeTargetPoolAssetType, + Convert: GetComputeTargetPoolCaiObject, + } +} + +func GetComputeTargetPoolCaiObject(d tpgresource.TerraformResourceData, config *transport_tpg.Config) ([]cai.Asset, error) { + name, err := cai.AssetName(d, config, "//compute.googleapis.com/projects/{{project}}/regions/{{region}}/targetPools/{{name}}") + if err != nil { + return []cai.Asset{}, err + } + if obj, err := GetComputeTargetPoolApiObject(d, config); err == nil { + return []cai.Asset{{ + Name: name, + Type: ComputeTargetPoolAssetType, + Resource: &cai.AssetResource{ + Version: "v1", + DiscoveryDocumentURI: "https://www.googleapis.com/discovery/v1/apis/compute/v1/rest", + DiscoveryName: "TargetPool", + Data: obj, + }, + }}, nil + } else { + return []cai.Asset{}, err + } +} + +func GetComputeTargetPoolApiObject(d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]interface{}, error) { + obj := make(map[string]interface{}) + + nameProp, err := expandComputeTargetPoolName(d.Get("name"), d, config) + if err != nil { + return nil, err + } else if v, ok := d.GetOkExists("name"); !tpgresource.IsEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { + obj["name"] = nameProp + } + + descriptionProp, err := expandComputeTargetPoolDescription(d.Get("description"), d, config) + if err != nil { + return nil, err + } else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { + obj["description"] = descriptionProp + } + + regionProp, err := expandComputeTargetPoolRegion(d.Get("region"), d, config) + if err != nil { + return nil, err + } else if v, ok := d.GetOkExists("region"); !tpgresource.IsEmptyValue(reflect.ValueOf(regionProp)) && (ok || !reflect.DeepEqual(v, regionProp)) { + obj["region"] = regionProp + } + + healthChecksProp, err := expandComputeTargetPoolHealthChecks(d.Get("health_checks"), d, config) + if err != nil { + return nil, err + } else if v, ok := d.GetOkExists("health_checks"); !tpgresource.IsEmptyValue(reflect.ValueOf(healthChecksProp)) && (ok || !reflect.DeepEqual(v, healthChecksProp)) { + obj["healthChecks"] = healthChecksProp + } + + instancesProp, err := expandComputeTargetPoolInstances(d.Get("instances"), d, config) + if err != nil { + return nil, err + } else if v, ok := d.GetOkExists("instances"); !tpgresource.IsEmptyValue(reflect.ValueOf(instancesProp)) && (ok || !reflect.DeepEqual(v, instancesProp)) { + obj["instances"] = instancesProp + } + + sessionAffinityProp, err := expandComputeTargetPoolSessionAffinity(d.Get("session_affinity"), d, config) + if err != nil { + return nil, err + } else if v, ok := d.GetOkExists("session_affinity"); !tpgresource.IsEmptyValue(reflect.ValueOf(sessionAffinityProp)) && (ok || !reflect.DeepEqual(v, sessionAffinityProp)) { + obj["sessionAffinity"] = sessionAffinityProp + } + + failoverRatioProp, err := expandComputeTargetPoolFailoverRatio(d.Get("failover_ratio"), d, config) + if err != nil { + return nil, err + } else if v, ok := d.GetOkExists("failover_ratio"); !tpgresource.IsEmptyValue(reflect.ValueOf(failoverRatioProp)) && (ok || !reflect.DeepEqual(v, failoverRatioProp)) { + obj["failoverRatio"] = failoverRatioProp + } + + backupPoolProp, err := expandComputeTargetPoolBackupPool(d.Get("backup_pool"), d, config) + if err != nil { + return nil, err + } else if v, ok := d.GetOkExists("backup_pool"); !tpgresource.IsEmptyValue(reflect.ValueOf(backupPoolProp)) && (ok || !reflect.DeepEqual(v, backupPoolProp)) { + obj["backupPool"] = backupPoolProp + } + + selfLinkProp, err := expandComputeTargetPoolSelfLink(d.Get("self_link"), d, config) + if err != nil { + return nil, err + } else if v, ok := d.GetOkExists("self_link"); !tpgresource.IsEmptyValue(reflect.ValueOf(selfLinkProp)) && (ok || !reflect.DeepEqual(v, selfLinkProp)) { + obj["selfLink"] = selfLinkProp + } + + securityPolicyProp, err := expandComputeTargetPoolSecurityPolicy(d.Get("security_policy"), d, config) + if err != nil { + return nil, err + } else if v, ok := d.GetOkExists("security_policy"); !tpgresource.IsEmptyValue(reflect.ValueOf(securityPolicyProp)) && (ok || !reflect.DeepEqual(v, securityPolicyProp)) { + obj["securityPolicy"] = securityPolicyProp + } + + return obj, nil +} + +func expandComputeTargetPoolName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandComputeTargetPoolDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandComputeTargetPoolRegion(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandComputeTargetPoolHealthChecks(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandComputeTargetPoolInstances(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + v = v.(*schema.Set).List() + return v, nil +} + +func expandComputeTargetPoolSessionAffinity(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandComputeTargetPoolFailoverRatio(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandComputeTargetPoolBackupPool(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandComputeTargetPoolSelfLink(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandComputeTargetPoolSecurityPolicy(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} diff --git a/mmv1/third_party/tgc/tests/data/example_google_compute_autoscaler.tf b/mmv1/third_party/tgc/tests/data/example_google_compute_autoscaler.tf index 8db9694b0975..b1a54e83113c 100644 --- a/mmv1/third_party/tgc/tests/data/example_google_compute_autoscaler.tf +++ b/mmv1/third_party/tgc/tests/data/example_google_compute_autoscaler.tf @@ -67,11 +67,6 @@ resource "google_compute_instance_template" "foobar" { } } -resource "google_compute_target_pool" "foobar" { - name = "my-target-pool-2" - region = "us-central1" -} - resource "google_compute_instance_group_manager" "foobar" { name = "my-igm" zone = "us-east1-c" @@ -81,6 +76,5 @@ resource "google_compute_instance_group_manager" "foobar" { name = "primary" } - target_pools = [google_compute_target_pool.foobar.id] base_instance_name = "foobar" } \ No newline at end of file diff --git a/mmv1/third_party/tgc/tests/data/example_google_compute_target_pool.json b/mmv1/third_party/tgc/tests/data/example_google_compute_target_pool.json new file mode 100644 index 000000000000..8da914cbed7e --- /dev/null +++ b/mmv1/third_party/tgc/tests/data/example_google_compute_target_pool.json @@ -0,0 +1,23 @@ +[ + { + "name": "//compute.googleapis.com/projects/{{.Provider.project}}/regions/us-central1/targetPools/instance-target-pool", + "asset_type": "compute.googleapis.com/TargetPool", + "resource": { + "version": "v1", + "discovery_document_uri": "https://www.googleapis.com/discovery/v1/apis/compute/v1/rest", + "discovery_name": "TargetPool", + "parent": "//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}", + "data": { + "instances": [ + "us-central1-b/myinstance2", + "us-central1-a/myinstance1" + ], + "name": "instance-target-pool", + "region": "us-central1", + "sessionAffinity": "NONE" + } + }, + "ancestors": ["organizations/{{.OrgID}}"], + "ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}" + } +] \ No newline at end of file diff --git a/mmv1/third_party/tgc/tests/data/example_google_compute_target_pool.tf b/mmv1/third_party/tgc/tests/data/example_google_compute_target_pool.tf new file mode 100644 index 000000000000..c740d77ffd83 --- /dev/null +++ b/mmv1/third_party/tgc/tests/data/example_google_compute_target_pool.tf @@ -0,0 +1,21 @@ +terraform { + required_providers { + google = { + source = "hashicorp/google-beta" + version = "~> {{.Provider.version}}" + } + } +} + +provider "google" { + {{if .Provider.credentials }}credentials = "{{.Provider.credentials}}"{{end}} +} + +resource "google_compute_target_pool" "default" { + name = "instance-target-pool" + region="us-central1" + instances = [ + "us-central1-a/myinstance1", + "us-central1-b/myinstance2", + ] +} \ No newline at end of file