Skip to content

Commit

Permalink
Add google_project to v6 cai2hcl (#12713)
Browse files Browse the repository at this point in the history
Co-authored-by: Cameron Thornton <[email protected]>
  • Loading branch information
zli82016 and c2thorn authored Jan 8, 2025
1 parent 6d54b51 commit e49e760
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mmv1/templates/tgc_v6/cai2hcl/resource_converters.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions mmv1/third_party/tgc_v6/cai2hcl/converters/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e49e760

Please sign in to comment.