Skip to content

Commit 6cb8900

Browse files
Config Generation: No provider alias on single instance (#1589)
If we're only going to have one `provider` instance, there's no need to set an alias
1 parent b88a3d6 commit 6cb8900

File tree

8 files changed

+64
-56
lines changed

8 files changed

+64
-56
lines changed

pkg/generate/generate.go

+15-17
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"log"
8-
"net"
9-
"net/url"
108
"os"
119
"path/filepath"
1210
"regexp"
@@ -69,16 +67,7 @@ func Generate(ctx context.Context, cfg *Config) error {
6967
}
7068

7169
if cfg.Grafana != nil {
72-
grafanaURLParsed, err := url.Parse(cfg.Grafana.URL)
73-
if err != nil {
74-
return err
75-
}
76-
77-
stackName := grafanaURLParsed.Hostname()
78-
if net.ParseIP(stackName) != nil {
79-
stackName = "ip_" + strings.ReplaceAll(stackName, ".", "_")
80-
}
81-
if err := generateGrafanaResources(ctx, cfg.Grafana.Auth, cfg.Grafana.URL, stackName, true, cfg.OutputDir, "", ""); err != nil {
70+
if err := generateGrafanaResources(ctx, cfg.Grafana.Auth, cfg.Grafana.URL, "", true, cfg.OutputDir, "", ""); err != nil {
8271
return err
8372
}
8473
}
@@ -94,6 +83,14 @@ func Generate(ctx context.Context, cfg *Config) error {
9483
}
9584

9685
func generateImportBlocks(ctx context.Context, client *common.Client, listerData any, resources []*common.Resource, outPath, provider string) error {
86+
generatedFilename := func(suffix string) string {
87+
if provider == "" {
88+
return filepath.Join(outPath, suffix)
89+
}
90+
91+
return filepath.Join(outPath, provider+"-"+suffix)
92+
}
93+
9794
// Generate HCL blocks in parallel with a wait group
9895
wg := sync.WaitGroup{}
9996
wg.Add(len(resources))
@@ -140,9 +137,11 @@ func generateImportBlocks(ctx context.Context, client *common.Client, listerData
140137
}
141138

142139
b := hclwrite.NewBlock("import", nil)
143-
b.Body().SetAttributeTraversal("provider", traversal("grafana", provider))
144140
b.Body().SetAttributeTraversal("to", traversal(resource.Name, cleanedID))
145141
b.Body().SetAttributeValue("id", cty.StringVal(id))
142+
if provider != "" {
143+
b.Body().SetAttributeTraversal("provider", traversal("grafana", provider))
144+
}
146145

147146
blocks[i] = b
148147
// TODO: Match and update existing import blocks
@@ -178,14 +177,13 @@ func generateImportBlocks(ctx context.Context, client *common.Client, listerData
178177
allBlocks = append(allBlocks, r.blocks...)
179178
}
180179

181-
if err := writeBlocks(filepath.Join(outPath, provider+"-imports.tf"), allBlocks...); err != nil {
180+
if err := writeBlocks(generatedFilename("imports.tf"), allBlocks...); err != nil {
182181
return err
183182
}
184183

185-
generatedFilename := fmt.Sprintf("%s-resources.tf", provider)
186-
if err := runTerraform(outPath, "plan", "-generate-config-out="+generatedFilename); err != nil {
184+
if err := runTerraform(outPath, "plan", "-generate-config-out="+generatedFilename("resources.tf")); err != nil {
187185
return fmt.Errorf("failed to generate resources: %w", err)
188186
}
189187

190-
return sortResourcesFile(filepath.Join(outPath, generatedFilename))
188+
return sortResourcesFile(generatedFilename("resources.tf"))
191189
}

pkg/generate/grafana.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package generate
22

33
import (
44
"context"
5-
"log"
65
"path/filepath"
76
"strings"
87

@@ -17,12 +16,22 @@ import (
1716
)
1817

1918
func generateGrafanaResources(ctx context.Context, auth, url, stackName string, genProvider bool, outPath, smURL, smToken string) error {
19+
generatedFilename := func(suffix string) string {
20+
if stackName == "" {
21+
return filepath.Join(outPath, suffix)
22+
}
23+
24+
return filepath.Join(outPath, stackName+"-"+suffix)
25+
}
26+
2027
if genProvider {
2128
providerBlock := hclwrite.NewBlock("provider", []string{"grafana"})
22-
providerBlock.Body().SetAttributeValue("alias", cty.StringVal(stackName))
2329
providerBlock.Body().SetAttributeValue("url", cty.StringVal(url))
2430
providerBlock.Body().SetAttributeValue("auth", cty.StringVal(auth))
25-
if err := writeBlocks(filepath.Join(outPath, stackName+"-provider.tf"), providerBlock); err != nil {
31+
if stackName != "" {
32+
providerBlock.Body().SetAttributeValue("alias", cty.StringVal(stackName))
33+
}
34+
if err := writeBlocks(generatedFilename("provider.tf"), providerBlock); err != nil {
2635
return err
2736
}
2837
}
@@ -60,21 +69,20 @@ func generateGrafanaResources(ctx context.Context, auth, url, stackName string,
6069
return err
6170
}
6271

63-
log.Printf("Post-processing for %s\n", stackName)
6472
stripDefaultsExtraFields := map[string]any{}
6573
if singleOrg {
6674
stripDefaultsExtraFields["org_id"] = true // Always remove org_id if single org
6775
} else {
6876
stripDefaultsExtraFields["org_id"] = `"1"` // Remove org_id if it's the default
6977
}
7078

71-
if err := stripDefaults(filepath.Join(outPath, stackName+"-resources.tf"), stripDefaultsExtraFields); err != nil {
79+
if err := stripDefaults(generatedFilename("resources.tf"), stripDefaultsExtraFields); err != nil {
7280
return err
7381
}
74-
if err := abstractDashboards(filepath.Join(outPath, stackName+"-resources.tf")); err != nil {
82+
if err := abstractDashboards(generatedFilename("resources.tf")); err != nil {
7583
return err
7684
}
77-
if err := wrapJSONFieldsInFunction(filepath.Join(outPath, stackName+"-resources.tf")); err != nil {
85+
if err := wrapJSONFieldsInFunction(generatedFilename("resources.tf")); err != nil {
7886
return err
7987
}
8088

pkg/generate/terraform.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,16 @@ func runTerraform(dir string, command ...string) error {
3030

3131
func writeBlocks(filepath string, blocks ...*hclwrite.Block) error {
3232
contents := hclwrite.NewFile()
33-
for i, b := range blocks {
34-
if i > 0 {
33+
if fileBytes, err := os.ReadFile(filepath); err == nil {
34+
var diags hcl.Diagnostics
35+
contents, diags = hclwrite.ParseConfig(fileBytes, filepath, hcl.InitialPos)
36+
if diags.HasErrors() {
37+
return errors.Join(diags.Errs()...)
38+
}
39+
}
40+
41+
for _, b := range blocks {
42+
if len(contents.Body().Blocks()) > 0 {
3543
contents.Body().AppendNewline()
3644
}
3745
contents.Body().AppendBlock(b)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {
2+
to = grafana_dashboard._1_my-dashboard-uid
3+
id = "1:my-dashboard-uid"
4+
}
5+
6+
import {
7+
to = grafana_folder._1_my-folder-uid
8+
id = "1:my-folder-uid"
9+
}
10+
11+
import {
12+
to = grafana_notification_policy._1_policy
13+
id = "1:policy"
14+
}

pkg/generate/testdata/generate/dashboard-expected/localhost-imports.tf

-17
This file was deleted.

pkg/generate/testdata/generate/dashboard-expected/localhost-provider.tf

-5
This file was deleted.

pkg/generate/testdata/generate/dashboard-expected/provider.tf

+5
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ terraform {
66
}
77
}
88
}
9+
10+
provider "grafana" {
11+
url = "http://localhost:3000"
12+
auth = "admin:admin"
13+
}

pkg/generate/testdata/generate/dashboard-expected/localhost-resources.tf renamed to pkg/generate/testdata/generate/dashboard-expected/resources.tf

+5-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
# Please review these resources and move them into your main configuration files.
33

44
# __generated__ by Terraform from "1:my-dashboard-uid"
5-
resource "grafana_dashboard" "localhost_1_my-dashboard-uid" {
6-
provider = grafana.localhost
5+
resource "grafana_dashboard" "_1_my-dashboard-uid" {
76
config_json = jsonencode({
87
title = "My Dashboard"
98
uid = "my-dashboard-uid"
@@ -12,15 +11,13 @@ resource "grafana_dashboard" "localhost_1_my-dashboard-uid" {
1211
}
1312

1413
# __generated__ by Terraform from "1:my-folder-uid"
15-
resource "grafana_folder" "localhost_1_my-folder-uid" {
16-
provider = grafana.localhost
17-
title = "My Folder"
18-
uid = "my-folder-uid"
14+
resource "grafana_folder" "_1_my-folder-uid" {
15+
title = "My Folder"
16+
uid = "my-folder-uid"
1917
}
2018

2119
# __generated__ by Terraform from "1:policy"
22-
resource "grafana_notification_policy" "localhost_1_policy" {
23-
provider = grafana.localhost
20+
resource "grafana_notification_policy" "_1_policy" {
2421
contact_point = "grafana-default-email"
2522
disable_provenance = true
2623
group_by = ["grafana_folder", "alertname"]

0 commit comments

Comments
 (0)