Skip to content

Commit

Permalink
✨ new tfgen go package to generate hcl code
Browse files Browse the repository at this point in the history
This is the first step to start writing automation code to onboard integration into Mondoo. `tfgen` is a primitive that will help us write HCL code in plain Go programming language.

For example, here is the translation of this code that integrates a Google project into the Mondoo platform.

> Code: https://registry.terraform.io/providers/mondoohq/mondoo/latest/docs/resources/integration_gcp

```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
afiune committed Oct 24, 2024
1 parent 8f55ac3 commit d1ec5c6
Show file tree
Hide file tree
Showing 3 changed files with 1,281 additions and 0 deletions.
Loading

0 comments on commit d1ec5c6

Please sign in to comment.