Skip to content

Conversation

@Duologic
Copy link
Contributor

This PR refactors the generator to use https://github.com/hashicorp/terraform-exec/ to execute the Terraform binary. This removes the need for the user to provide the Terraform binary as part of their runtime environment, allowing the generator to be used as a library with less friction.

@github-actions
Copy link
Contributor

In order to lower resource usage and have a faster runtime, PRs will not run Cloud tests automatically.
To do so, a Grafana Labs employee must trigger the cloud acceptance tests workflow manually.

gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/hashicorp/terraform-exec v0.21.0 => github.com/hrmsk66/terraform-exec v0.21.0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using a fork awaiting the merge of this PR: hashicorp/terraform-exec#446

func setupTerraform(cfg *Config) (*tfexec.Terraform, error) {
installer := &releases.ExactVersion{
Product: product.Terraform,
Version: version.Must(version.NewVersion("1.8.4")),
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've pinned the version here for now, should probably become configurable.

Comment on lines -30 to +10
state, err := runTerraformWithOutput(dir, "show", "-json")
func getState(ctx context.Context, cfg *Config) (*tfjson.State, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

tfexec implements tfjson to read the state, hence the big changeset in this file.

Comment on lines 85 to 79
stack := stack{
name: stackName,
managementKey: cfg.Grafana.Auth,
url: cfg.Grafana.URL,
smToken: "",
smURL: "",
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Synthesizing a stack here to reduce the signatures of the generate functions.

if err != nil {
return fmt.Errorf("failed to run terraform init: %w", err)
}
cfg.Terraform = tf
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 sure if this is the best place to put it, probably not but, eh 🤷

@Duologic Duologic force-pushed the duologic/gonative_tfexec branch from dbab7b6 to ed45869 Compare May 29, 2024 13:35
@Duologic Duologic marked this pull request as ready for review May 29, 2024 13:39
@Duologic Duologic requested a review from a team as a code owner May 29, 2024 13:39
Copy link
Contributor

@julienduchesne julienduchesne left a comment

Choose a reason for hiding this comment

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

Nice!

go.mod Outdated
golang.org/x/text v0.15.0
)

require (
Copy link
Contributor

Choose a reason for hiding this comment

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

Merge this with the upper block IMO

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've just used go mod edit -replace and go mod tidy, stupid tools doing stupid things :P

@julienduchesne julienduchesne merged commit d58abb4 into main May 29, 2024
@julienduchesne julienduchesne deleted the duologic/gonative_tfexec branch May 29, 2024 16:51
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.

2 participants