-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add google_project to v6 cai2hcl (#12713)
Co-authored-by: Cameron Thornton <[email protected]>
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
mmv1/third_party/tgc_v6/cai2hcl/converters/services/resourcemanager/project.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters