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

Add support for google_compute_target_pool to TGC #2287

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
157 changes: 157 additions & 0 deletions tfplan2cai/converters/google/resources/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 @@ -101,6 +101,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
1 change: 1 addition & 0 deletions tfplan2cai/test/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestReadPlannedAssetsCoverage(t *testing.T) {
{name: "example_google_compute_router"},
{name: "example_google_compute_ssl_certificate"},
{name: "example_google_compute_target_http_proxy"},
{name: "example_google_compute_target_pool"},
{name: "example_google_dataproc_autoscaling_policy"},
{name: "example_google_datastream_connection_profile"},
{name: "example_google_datastream_private_connection"},
Expand Down
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",
]
}