-
Notifications
You must be signed in to change notification settings - Fork 251
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
Config Generation: Go implementation for executing Terraform binary #1592
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import ( | |
|
||
"github.com/grafana/terraform-provider-grafana/v3/internal/common" | ||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
"github.com/hashicorp/terraform-exec/tfexec" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
|
@@ -48,10 +49,12 @@ func Generate(ctx context.Context, cfg *Config) error { | |
log.Fatal(err) | ||
} | ||
|
||
tf, err := setupTerraform(cfg) | ||
// Terraform init to download the provider | ||
if err := runTerraform(cfg.OutputDir, "init"); err != nil { | ||
if err != nil { | ||
return fmt.Errorf("failed to run terraform init: %w", err) | ||
} | ||
cfg.Terraform = tf | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 🤷 |
||
|
||
if cfg.Cloud != nil { | ||
stacks, err := generateCloudResources(ctx, cfg) | ||
|
@@ -60,14 +63,21 @@ func Generate(ctx context.Context, cfg *Config) error { | |
} | ||
|
||
for _, stack := range stacks { | ||
if err := generateGrafanaResources(ctx, stack.managementKey, stack.url, "stack-"+stack.slug, false, cfg.OutputDir, stack.smURL, stack.smToken, cfg.IncludeResources); err != nil { | ||
stack.name = "stack-" + stack.slug | ||
if err := generateGrafanaResources(ctx, cfg, stack, false); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
if cfg.Grafana != nil { | ||
if err := generateGrafanaResources(ctx, cfg.Grafana.Auth, cfg.Grafana.URL, "", true, cfg.OutputDir, "", "", cfg.IncludeResources); err != nil { | ||
stack := stack{ | ||
managementKey: cfg.Grafana.Auth, | ||
url: cfg.Grafana.URL, | ||
smToken: "", | ||
smURL: "", | ||
} | ||
if err := generateGrafanaResources(ctx, cfg, stack, true); err != nil { | ||
return err | ||
} | ||
} | ||
|
@@ -82,16 +92,16 @@ func Generate(ctx context.Context, cfg *Config) error { | |
return nil | ||
} | ||
|
||
func generateImportBlocks(ctx context.Context, client *common.Client, listerData any, resources []*common.Resource, outPath, provider string, includedResources []string) error { | ||
func generateImportBlocks(ctx context.Context, client *common.Client, listerData any, resources []*common.Resource, cfg *Config, provider string) error { | ||
generatedFilename := func(suffix string) string { | ||
if provider == "" { | ||
return filepath.Join(outPath, suffix) | ||
return filepath.Join(cfg.OutputDir, suffix) | ||
} | ||
|
||
return filepath.Join(outPath, provider+"-"+suffix) | ||
return filepath.Join(cfg.OutputDir, provider+"-"+suffix) | ||
} | ||
|
||
resources, err := filterResources(resources, includedResources) | ||
resources, err := filterResources(resources, cfg.IncludeResources) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -141,7 +151,7 @@ func generateImportBlocks(ctx context.Context, client *common.Client, listerData | |
cleanedID = strings.ReplaceAll(provider, "-", "_") + "_" + cleanedID | ||
} | ||
|
||
matched, err := filterResourceByName(resource.Name, cleanedID, includedResources) | ||
matched, err := filterResourceByName(resource.Name, cleanedID, cfg.IncludeResources) | ||
if err != nil { | ||
wg.Done() | ||
results <- result{ | ||
|
@@ -198,7 +208,8 @@ func generateImportBlocks(ctx context.Context, client *common.Client, listerData | |
return err | ||
} | ||
|
||
if err := runTerraform(outPath, "plan", "-generate-config-out="+generatedFilename("resources.tf")); err != nil { | ||
_, err = cfg.Terraform.Plan(ctx, tfexec.GenerateConfigOut(generatedFilename("resources.tf"))) | ||
if err != nil { | ||
return fmt.Errorf("failed to generate resources: %w", err) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,46 @@ | ||
package generate | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-version" | ||
"github.com/hashicorp/hc-install/product" | ||
"github.com/hashicorp/hc-install/releases" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclparse" | ||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
"github.com/hashicorp/terraform-exec/tfexec" | ||
"github.com/tmccombs/hcl2json/convert" | ||
) | ||
|
||
func runTerraformWithOutput(dir string, command ...string) ([]byte, error) { | ||
cmd := exec.Command("terraform", command...) | ||
cmd.Dir = dir | ||
cmd.Stderr = os.Stderr | ||
return cmd.Output() | ||
} | ||
func setupTerraform(cfg *Config) (*tfexec.Terraform, error) { | ||
installer := &releases.ExactVersion{ | ||
Product: product.Terraform, | ||
Version: version.Must(version.NewVersion("1.8.4")), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've pinned the version here for now, should probably become configurable. |
||
} | ||
|
||
execPath, err := installer.Install(context.Background()) | ||
if err != nil { | ||
return nil, fmt.Errorf("error installing Terraform: %s", err) | ||
} | ||
|
||
tf, err := tfexec.NewTerraform(cfg.OutputDir, execPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("error running NewTerraform: %s", err) | ||
} | ||
|
||
err = tf.Init(context.Background(), tfexec.Upgrade(true)) | ||
if err != nil { | ||
return nil, fmt.Errorf("error running Init: %s", err) | ||
} | ||
|
||
func runTerraform(dir string, command ...string) error { | ||
out, err := runTerraformWithOutput(dir, command...) | ||
fmt.Println(string(out)) | ||
return err | ||
return tf, nil | ||
} | ||
|
||
func writeBlocks(filepath string, blocks ...*hclwrite.Block) error { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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