Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ new
tfgen
go package to generate hcl code (#1457)
This is the first step to start writing automation code to onboard integrations into the Mondoo platform. `tfgen` is a primitive that will help us write HCL code in plain Go programming language. We will use it to generate automation code from a new command named `cnspec integrate ...`. Terraform code can be complex, for example, to integrate a Google project into the Mondoo platform, a user would write this HCL code: > https://registry.terraform.io/providers/mondoohq/mondoo/latest/docs/resources/integration_gcp With `tfgen`, we can write that code in Go like: ```go mondooProvider, err := tfgen.NewProvider("mondoo", tfgen.HclProviderWithAttributes( map[string]interface{}{ "space": "hungry-poet-123456", }, )).ToBlock() googleProvider, err := tfgen.NewProvider("google", tfgen.HclProviderWithAttributes( map[string]interface{}{ "project": "prod-project-123", "region": "us-central1", }, )).ToBlock() googleServiceAccountResource, err := tfgen.NewResource("google_service_account", "mondoo", tfgen.HclResourceWithAttributesAndProviderDetails( map[string]interface{}{ "account_id": "mondoo-integration", "display_name": "Mondoo service account", }, nil, )).ToResourceBlock() googleServiceAccountKey, err := tfgen.NewResource("google_service_account_key", "mondoo", tfgen.HclResourceWithAttributesAndProviderDetails( map[string]interface{}{ "service_account_id": tfgen.CreateSimpleTraversal("google_service_account", "mondoo", "name"), }, nil, )).ToResourceBlock() mondooIntegrationGCP, err := tfgen.NewResource("mondoo_integration_gcp", "production", tfgen.HclResourceWithAttributesAndProviderDetails( map[string]interface{}{ "name": "Production account", "project_id": "prod-project-123", "credentials": map[string]interface{}{ "private_key": tfgen.NewFuncCall( "base64decode", tfgen.CreateSimpleTraversal("google_service_account_key", "mondoo", "private_key")), }, }, nil, )).ToResourceBlock() blocksOutput := tfgen.CreateHclStringOutput( tfgen.CombineHclBlocks( mondooProvider, googleProvider, googleServiceAccountResource, googleServiceAccountKey, mondooIntegrationGCP, )..., ) ``` This will result in the following HCL code: ```hcl provider "mondoo" { space = "hungry-poet-123456" } provider "google" { project = "prod-project-123" region = "us-central1" } resource "google_service_account" "mondoo" { account_id = "mondoo-integration" display_name = "Mondoo service account" } resource "google_service_account_key" "mondoo" { service_account_id = google_service_account.mondoo.name } resource "mondoo_integration_gcp" "production" { credentials = { private_key = base64decode(google_service_account_key.mondoo.private_key) } name = "Production account" project_id = "prod-project-123" } ``` Signed-off-by: Salim Afiune Maya <[email protected]>
- Loading branch information