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 google_project to v6 cai2hcl #12713

Merged
merged 2 commits into from
Jan 8, 2025
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
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
Loading