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

✨ new tfgen go package to generate hcl code #1457

Merged
merged 1 commit into from
Oct 25, 2024
Merged

✨ new tfgen go package to generate hcl code #1457

merged 1 commit into from
Oct 25, 2024

Conversation

afiune
Copy link
Contributor

@afiune afiune commented Oct 24, 2024

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:

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:

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"
}

Copy link
Contributor

github-actions bot commented Oct 24, 2024

Test Results

  1 files  ± 0   25 suites  +1   17s ⏱️ ±0s
438 tests +35  437 ✅ +35  1 💤 ±0  0 ❌ ±0 
439 runs  +35  438 ✅ +35  1 💤 ±0  0 ❌ ±0 

Results for commit 3c68f16. ± Comparison against base commit 8f55ac3.

♻️ This comment has been updated with latest results.

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 <afiune@mondoo.com>
@afiune afiune merged commit 557afe6 into main Oct 25, 2024
14 checks passed
@afiune afiune deleted the afiune/tfgen branch October 25, 2024 10:16
@github-actions github-actions bot locked and limited conversation to collaborators Oct 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants