Skip to content

Commit

Permalink
Add support for google_compute_target_pool to TGC (#10727)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfdossantos authored May 21, 2024
1 parent 49d55fc commit 9c8826e
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 7 deletions.
4 changes: 3 additions & 1 deletion mmv1/provider/terraform_tgc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions mmv1/templates/tgc/resource_converters.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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()},
Expand Down
157 changes: 157 additions & 0 deletions mmv1/third_party/tgc/compute_target_pool.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
Original file line number Diff line number Diff line change
@@ -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}}"
}
]
Original file line number Diff line number Diff line change
@@ -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",
]
}

0 comments on commit 9c8826e

Please sign in to comment.