From 7cbab8f0b3117696d940e710ef3eeb1dec6d0897 Mon Sep 17 00:00:00 2001 From: Zhenhua Li Date: Wed, 8 Jan 2025 14:11:20 -0800 Subject: [PATCH] Add google_project to v6 cai2hcl (#12713) Co-authored-by: Cameron Thornton --- .../cai2hcl/resource_converters.go.tmpl | 7 ++ .../services/resourcemanager/project.go | 76 +++++++++++++++++++ .../tgc_v6/cai2hcl/converters/utils/utils.go | 10 +++ 3 files changed, 93 insertions(+) create mode 100644 mmv1/third_party/tgc_v6/cai2hcl/converters/services/resourcemanager/project.go diff --git a/mmv1/templates/tgc_v6/cai2hcl/resource_converters.go.tmpl b/mmv1/templates/tgc_v6/cai2hcl/resource_converters.go.tmpl index b7f5e92cc2eb..51b0ef39230a 100644 --- a/mmv1/templates/tgc_v6/cai2hcl/resource_converters.go.tmpl +++ b/mmv1/templates/tgc_v6/cai2hcl/resource_converters.go.tmpl @@ -29,8 +29,15 @@ package converters import ( "github.com/GoogleCloudPlatform/terraform-google-conversion/v6/pkg/cai2hcl/models" + "github.com/GoogleCloudPlatform/terraform-google-conversion/v6/pkg/cai2hcl/converters/services/resourcemanager" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + tpg_provider "github.com/hashicorp/terraform-provider-google-beta/google-beta/provider" ) +var provider *schema.Provider = tpg_provider.Provider() + // ConverterMap is a collection of converters instances, indexed by cai asset type. var ConverterMap = map[string]models.Converter{ + resourcemanager.ProjectAssetType: resourcemanager.NewProjectConverter(provider), } diff --git a/mmv1/third_party/tgc_v6/cai2hcl/converters/services/resourcemanager/project.go b/mmv1/third_party/tgc_v6/cai2hcl/converters/services/resourcemanager/project.go new file mode 100644 index 000000000000..b921a89b9dc1 --- /dev/null +++ b/mmv1/third_party/tgc_v6/cai2hcl/converters/services/resourcemanager/project.go @@ -0,0 +1,76 @@ +package resourcemanager + +import ( + "fmt" + "strings" + + "github.com/GoogleCloudPlatform/terraform-google-conversion/v6/pkg/cai2hcl/converters/utils" + "github.com/GoogleCloudPlatform/terraform-google-conversion/v6/pkg/cai2hcl/models" + "github.com/GoogleCloudPlatform/terraform-google-conversion/v6/pkg/caiasset" + + tfschema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +// ProjectAssetType is the CAI asset type name for project. +const ProjectAssetType string = "cloudresourcemanager.googleapis.com/Project" + +// ProjectSchemaName is the TF resource schema name for resourcemanager project. +const ProjectSchemaName string = "google_project" + +// ProjectConverter for compute project resource. +type ProjectConverter struct { + name string + schema map[string]*tfschema.Schema +} + +// NewProjectConverter returns an HCL converter for compute project. +func NewProjectConverter(provider *tfschema.Provider) models.Converter { + schema := provider.ResourcesMap[ProjectSchemaName].Schema + + return &ProjectConverter{ + name: ProjectSchemaName, + schema: schema, + } +} + +// Convert converts asset resource data. +func (c *ProjectConverter) Convert(asset *caiasset.Asset) ([]*models.TerraformResourceBlock, error) { + if asset == nil || asset.Resource == nil && asset.Resource.Data == nil { + return nil, nil + } + + var blocks []*models.TerraformResourceBlock + block, err := c.convertResourceData(asset) + if err != nil { + return nil, err + } + blocks = append(blocks, block) + return blocks, nil +} + +func (c *ProjectConverter) convertResourceData(asset *caiasset.Asset) (*models.TerraformResourceBlock, error) { + if asset == nil || asset.Resource == nil || asset.Resource.Data == nil { + return nil, fmt.Errorf("asset resource data is nil") + } + + assetResourceData := asset.Resource.Data + + hclData := make(map[string]interface{}) + hclData["name"] = assetResourceData["name"] + hclData["project_id"] = assetResourceData["projectId"] + hclData["labels"] = utils.RemoveTerraformAttributionLabel(assetResourceData["labels"]) + if strings.Contains(asset.Resource.Parent, "folders/") { + hclData["folder_id"] = utils.ParseFieldValue(asset.Resource.Parent, "folders") + } else if strings.Contains(asset.Resource.Parent, "organizations/") { + hclData["org_id"] = utils.ParseFieldValue(asset.Resource.Parent, "organizations") + } + + ctyVal, err := utils.MapToCtyValWithSchema(hclData, c.schema) + if err != nil { + return nil, err + } + return &models.TerraformResourceBlock{ + Labels: []string{c.name, assetResourceData["projectId"].(string)}, + Value: ctyVal, + }, nil +} diff --git a/mmv1/third_party/tgc_v6/cai2hcl/converters/utils/utils.go b/mmv1/third_party/tgc_v6/cai2hcl/converters/utils/utils.go index 8a5e7029d94b..15a1d0379663 100644 --- a/mmv1/third_party/tgc_v6/cai2hcl/converters/utils/utils.go +++ b/mmv1/third_party/tgc_v6/cai2hcl/converters/utils/utils.go @@ -23,6 +23,16 @@ func ParseFieldValue(url string, name string) string { return "" } +// Remove the Terraform attribution label "goog-terraform-provisioned" from labels +func RemoveTerraformAttributionLabel(raw interface{}) map[string]interface{} { + if raw == nil { + return nil + } + labels := raw.(map[string]interface{}) + delete(labels, "goog-terraform-provisioned") + return labels +} + // DecodeJSON decodes the map object into the target struct. func DecodeJSON(data map[string]interface{}, v interface{}) error { b, err := json.Marshal(data)