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

Cai2Hcl: refactor of converter_map and split of utils.go #9378

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 0 additions & 21 deletions mmv1/third_party/cai2hcl/common/converter_factory.go

This file was deleted.

44 changes: 5 additions & 39 deletions mmv1/third_party/cai2hcl/common/converter_map.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,10 @@
package common

import (
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/caiasset"
)

// Configuration object to map CAI Assets to Converters.
// ConverterMap is a configuration object to map assets to converters.
type ConverterMap struct {
// Mapping from CAI Asset Type to converter name.
AssetTypeToConverter map[string]string

// Mapping from converter name and converter.
Converters map[string]Converter
}

// ConvertMap implements Converter interface to be able to convert any Asset type.
func (c ConverterMap) Convert(assets []*caiasset.Asset) ([]*HCLResourceBlock, error) {
// Group resources from the same tf resource type for convert.
// tf -> cai has 1:N mappings occasionally
groups := make(map[string][]*caiasset.Asset)
for _, asset := range assets {

name, _ := c.AssetTypeToConverter[asset.Type]
if name != "" {
groups[name] = append(groups[name], asset)
}
}

allBlocks := []*HCLResourceBlock{}
for name, assets := range groups {
converter, ok := c.Converters[name]
if !ok {
continue
}
newBlocks, err := converter.Convert(assets)
if err != nil {
return nil, err
}

allBlocks = append(allBlocks, newBlocks...)
}
// AssetTypeToConverterName maps asset type to converter name.
AssetTypeToConverterName map[string]string

return allBlocks, nil
// ConverterNameToConverter maps converter name to converter instance.
ConverterNameToConverter map[string]Converter
}
1 change: 1 addition & 0 deletions mmv1/third_party/cai2hcl/common/hcl_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/zclconf/go-cty/cty"
)

// HclWriteBlocks prints HCLResourceBlock objects as string.
func HclWriteBlocks(blocks []*HCLResourceBlock) ([]byte, error) {
melinath marked this conversation as resolved.
Show resolved Hide resolved
f := hclwrite.NewFile()
rootBody := f.Body()
Expand Down
6 changes: 3 additions & 3 deletions mmv1/third_party/cai2hcl/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
ctyjson "github.com/zclconf/go-cty/cty/json"
)

// Extracts named part from resource url.
// ParseFieldValue extracts named part from resource url.
func ParseFieldValue(url string, name string) string {
fragments := strings.Split(url, "/")
for ix, item := range fragments {
Expand All @@ -22,7 +22,7 @@ func ParseFieldValue(url string, name string) string {
return ""
}

// Decodes the map object into the target struct.
// DecodeJSON decodes the map object into the target struct.
func DecodeJSON(data map[string]interface{}, v interface{}) error {
b, err := json.Marshal(data)
if err != nil {
Expand All @@ -34,7 +34,7 @@ func DecodeJSON(data map[string]interface{}, v interface{}) error {
return nil
}

// Converts resource from untyped map format to TF JSON.
// MapToCtyValWithSchema converts resource from untyped map format to TF JSON.
func MapToCtyValWithSchema(m map[string]interface{}, s map[string]*schema.Schema) (cty.Value, error) {
b, err := json.Marshal(&m)
if err != nil {
Expand Down
28 changes: 23 additions & 5 deletions mmv1/third_party/cai2hcl/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,32 @@ func Convert(assets []*caiasset.Asset, options *Options) ([]byte, error) {
return nil, fmt.Errorf("logger is not initialized")
}

converter := ConverterMap
// Group resources from the same tf resource type for convert.
// tf -> cai has 1:N mappings occasionally
groups := make(map[string][]*caiasset.Asset)
for _, asset := range assets {

name, _ := ConverterMap.AssetTypeToConverterName[asset.Type]
if name != "" {
groups[name] = append(groups[name], asset)
}
}
amirkaromashkin marked this conversation as resolved.
Show resolved Hide resolved

blocks, err := converter.Convert(assets)
if err != nil {
return nil, err
allBlocks := []*common.HCLResourceBlock{}
for name, assets := range groups {
converter, ok := ConverterMap.ConverterNameToConverter[name]
if !ok {
continue
}
newBlocks, err := converter.Convert(assets)
if err != nil {
return nil, err
}

allBlocks = append(allBlocks, newBlocks...)
}

t, err := common.HclWriteBlocks(blocks)
t, err := common.HclWriteBlocks(allBlocks)

options.ErrorLogger.Debug(string(t))

Expand Down
4 changes: 1 addition & 3 deletions mmv1/third_party/cai2hcl/convert_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cai2hcl
package cai2hcl_test

import (
"testing"
Expand All @@ -9,7 +9,6 @@ import (
func TestConvertCompute(t *testing.T) {
cai2hclTesting.AssertTestFiles(
t,
ConverterMap,
"./services/compute/testdata",
[]string{
"full_compute_instance",
Expand All @@ -19,7 +18,6 @@ func TestConvertCompute(t *testing.T) {
func TestConvertResourcemanager(t *testing.T) {
cai2hclTesting.AssertTestFiles(
t,
ConverterMap,
"./services/resourcemanager/testdata",
[]string{
"project_create",
Expand Down
29 changes: 15 additions & 14 deletions mmv1/third_party/cai2hcl/converter_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import (
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/cai2hcl/common"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/cai2hcl/services/compute"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/cai2hcl/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()

var ConverterMap = common.ConverterMap{
AssetTypeToConverter: map[string]string{
// Compute
compute.ComputeInstanceAssetType: "google_compute_instance",
compute.ComputeForwardingRuleAssetType: "google_compute_forwarding_rule",
AssetTypeToConverterName: map[string]string{
compute.ComputeInstanceAssetType: compute.ComputeInstanceSchemaName,
compute.ComputeForwardingRuleAssetType: compute.ComputeForwardingRuleSchemaName,

// ResourceManager
resourcemanager.ProjectAssetType: "google_project",
resourcemanager.ProjectBillingAssetType: "google_project",
resourcemanager.ProjectAssetType: resourcemanager.ProjectSchemaName,
resourcemanager.ProjectBillingAssetType: resourcemanager.ProjectSchemaName,
},
Converters: common.CreateConverters(map[string]common.ConverterFactory{
// Compute
"google_compute_instance": compute.NewComputeInstanceConverter,
"google_compute_forwarding_rule": compute.NewComputeForwardingRuleConverter,

// ResourceManager
"google_project": resourcemanager.NewProjectConverter,
}),
ConverterNameToConverter: map[string]common.Converter{
compute.ComputeInstanceSchemaName: compute.NewComputeInstanceConverter(provider),
compute.ComputeForwardingRuleSchemaName: compute.NewComputeForwardingRuleConverter(provider),

resourcemanager.ProjectSchemaName: resourcemanager.NewProjectConverter(provider),
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ import (
computeV1 "google.golang.org/api/compute/v1"
)

// ComputeForwardingRuleAssetType is the CAI asset type name for compute instance.
// ComputeForwardingRuleAssetType is a CAI asset type name.
const ComputeForwardingRuleAssetType string = "compute.googleapis.com/ForwardingRule"

// ComputeForwardingRuleSchemaName is a TF resource schema name.
const ComputeForwardingRuleSchemaName string = "google_compute_forwarding_rule"

// ComputeForwardingRuleConverter for regional forwarding rule.
type ComputeForwardingRuleConverter struct {
name string
schema map[string]*tfschema.Schema
}

// NewComputeForwardingRuleConverter returns an HCL converter for compute instance.
func NewComputeForwardingRuleConverter(name string, schema map[string]*tfschema.Schema) common.Converter {
func NewComputeForwardingRuleConverter(provider *tfschema.Provider) common.Converter {
schema := provider.ResourcesMap[ComputeForwardingRuleSchemaName].Schema

return &ComputeForwardingRuleConverter{
name: name,
name: ComputeForwardingRuleSchemaName,
schema: schema,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package compute_test
import (
"testing"

"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/cai2hcl"
cai2hcl_testing "github.com/GoogleCloudPlatform/terraform-google-conversion/v5/cai2hcl/testing"
)

func TestComputeForwardingRule(t *testing.T) {
cai2hcl_testing.AssertTestFiles(
t,
cai2hcl.ConverterMap,
"./testdata",
[]string{"compute_forwarding_rule"})
}
13 changes: 9 additions & 4 deletions mmv1/third_party/cai2hcl/services/compute/compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@ import (
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/caiasset"

"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/cai2hcl/common"
tfschema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/zclconf/go-cty/cty"
"google.golang.org/api/compute/v1"
)

// ComputeInstanceAssetType is the CAI asset type name for compute instance.
const ComputeInstanceAssetType string = "compute.googleapis.com/Instance"

// ComputeInstanceSchemaName is the TF resource schema name for compute instance.
const ComputeInstanceSchemaName string = "google_compute_instance"

// ComputeInstanceConverter for compute instance resource.
type ComputeInstanceConverter struct {
name string
schema map[string]*tfschema.Schema
schema map[string]*schema.Schema
}

// NewComputeInstanceConverter returns an HCL converter for compute instance.
func NewComputeInstanceConverter(name string, schema map[string]*tfschema.Schema) common.Converter {
func NewComputeInstanceConverter(provider *schema.Provider) common.Converter {
schema := provider.ResourcesMap[ComputeInstanceSchemaName].Schema

return &ComputeInstanceConverter{
name: name,
name: ComputeInstanceSchemaName,
schema: schema,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package compute_test
import (
"testing"

"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/cai2hcl"
cai2hclTesting "github.com/GoogleCloudPlatform/terraform-google-conversion/v5/cai2hcl/testing"
)

func TestComputeInstance(t *testing.T) {
cai2hclTesting.AssertTestFiles(
t,
cai2hcl.ConverterMap,
"./testdata",
[]string{
"full_compute_instance",
Expand Down
9 changes: 7 additions & 2 deletions mmv1/third_party/cai2hcl/services/resourcemanager/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const ProjectAssetType string = "cloudresourcemanager.googleapis.com/Project"
// ProjectAssetType is the CAI asset type name for project.
const ProjectBillingAssetType string = "cloudbilling.googleapis.com/ProjectBillingInfo"

// 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
Expand All @@ -27,9 +30,11 @@ type ProjectConverter struct {
}

// NewProjectConverter returns an HCL converter for compute project.
func NewProjectConverter(name string, schema map[string]*tfschema.Schema) common.Converter {
func NewProjectConverter(provider *tfschema.Provider) common.Converter {
schema := provider.ResourcesMap[ProjectSchemaName].Schema

return &ProjectConverter{
name: name,
name: ProjectSchemaName,
schema: schema,
billings: make(map[string]string),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package resourcemanager_test
import (
"testing"

"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/cai2hcl"
cai2hclTesting "github.com/GoogleCloudPlatform/terraform-google-conversion/v5/cai2hcl/testing"
)

func TestComputeInstance(t *testing.T) {
cai2hclTesting.AssertTestFiles(
t,
cai2hcl.ConverterMap,
"./testdata",
[]string{
"project_create",
Expand Down
17 changes: 7 additions & 10 deletions mmv1/third_party/cai2hcl/testing/assert_test_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"testing"

"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/cai2hcl/common"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/cai2hcl"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/caiasset"
"go.uber.org/zap"

Expand All @@ -18,7 +18,7 @@ type _TestCase struct {
sourceFolder string
}

func AssertTestFiles(t *testing.T, converter common.Converter, folder string, fileNames []string) {
func AssertTestFiles(t *testing.T, folder string, fileNames []string) {
cases := []_TestCase{}

for _, name := range fileNames {
Expand All @@ -31,15 +31,15 @@ func AssertTestFiles(t *testing.T, converter common.Converter, folder string, fi
t.Run(c.name, func(t *testing.T) {
t.Parallel()

err := assertTestData(c, converter)
err := assertTestData(c)
if err != nil {
t.Fatal(err)
}
})
}
}

func assertTestData(testCase _TestCase, converter common.Converter) (err error) {
func assertTestData(testCase _TestCase) (err error) {
fileName := testCase.name
folder := testCase.sourceFolder

Expand All @@ -64,12 +64,9 @@ func assertTestData(testCase _TestCase, converter common.Converter) (err error)
return err
}

blocks, err := converter.Convert(assets)
if err != nil {
return err
}

got, err := common.HclWriteBlocks(blocks)
got, err := cai2hcl.Convert(assets, &cai2hcl.Options{
ErrorLogger: logger,
})
if err != nil {
return err
}
Expand Down