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

🤖 auto-generate resources for Mondoo integrations #195

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

afiune
Copy link
Contributor

@afiune afiune commented Jan 10, 2025

First iteration to a generator that create Terraform resources for Mondoo Integrations.

Most of our integrations are very similar and the only thing that changes are the ConfigurationOptions
per integration type. This generator iterates over all possible config options and generates the following
file:

  1. The terraform resource
  2. Tests for the new resource
  3. Basic example of the new resource

Once the resource is generated, we run our auto-generation of documentation, which created the docs
for this resource. Though, today I am writing the generated files to gen/generated/ and the idea is that
we can start copying the example from there and adapt quickly with the specific configuration options.

The Sentinel One integration was generated with this generator. #194

Next up, we need to code the full generation so that we write directly to internal/ and examples/ only.

This comment has been minimized.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Signed-off-by: Salim Afiune Maya <[email protected]>
@chris-rock
Copy link
Member

Nice step to auto-generate the integrations! If I understand the code right now, we would generate it only once. Do you think we can run this in the next step as a go generate task and replace all existing integrations with that approach as well? I would like to ensure integrations do not drift away over time.

// {{.ResourceClassName}} options
}

func (m integration{{.ResourceClassName}}ResourceModel) GetConfigurationOptions() *mondoov1.{{.ResourceClassName}}ConfigurationOptionsInput {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For best practices, we should also add generated comments here so that its easier to understand what the methods are doing

"strings"
"text/template"

"github.com/fatih/structs"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not include unmaintained go modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any other go module that does this. I we ok copying the code and NOT use the import?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the alternative solution not to use mapstructure? github.com/go-viper/mapstructure/v2. In case we need to change the tags for mondoo-go, we need touch the templates here https://github.com/mondoohq/mondoo-go/blob/main/gen/gen.go#L260

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! 🌟

}

// Ensure the output directory exists
outputDirPath := filepath.Join("gen", "generated")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we create this directly in the directory the other integrations are in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not yet. still need some more work but we will ultimately write it in the right directory.


for k := range m {
// TODO we know the type and the struct associated to the type, we need
// to look it (the struct) and use the same `structs.Map(v)` to list all
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something reads strange to me.
Perhaps:

Suggested change
// to look it (the struct) and use the same `structs.Map(v)` to list all
// to look at it (the struct) and use the same `structs.Map(v)` to list all

return err
}
// Create example main.tf
err = os.WriteFile(filepath.Join(examplesDirPath, "main.tf"), mainDotTFTestFile(), 0644)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: Could we generate this directly in the final directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not yet. still need some more work but we will ultimately write it in the right directory.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something for a follow-up or do you want to include it in this PR?

return nil
}

var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this works as expected. Wouldn't the . also match an upper case letter?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, I'm missing something here.
Could you please add a comment or a test case to show how this works?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would we need in this auto generate PR to also be able to generate the docs, like in https://github.com/mondoohq/terraform-provider-mondoo/pull/194/files#diff-f9f43e36f94b402f6ad590eef25f3da2adbf02034de08d64aeb834510e3159e9

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs will be auto generated too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants