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

Code Generation: Do not generate org_id field when set to default #1584

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/generate/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func generateCloudResources(ctx context.Context, cfg *Config) ([]stack, error) {
}

log.Println("Post-processing for cloud")
if err := stripDefaults(filepath.Join(cfg.OutputDir, "cloud-resources.tf"), map[string]string{}); err != nil {
if err := stripDefaults(filepath.Join(cfg.OutputDir, "cloud-resources.tf"), nil); err != nil {
return nil, err
}
if err := wrapJSONFieldsInFunction(filepath.Join(cfg.OutputDir, "cloud-resources.tf")); err != nil {
Expand Down
11 changes: 8 additions & 3 deletions pkg/generate/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ func generateGrafanaResources(ctx context.Context, auth, url, stackName string,
}

log.Printf("Post-processing for %s\n", stackName)
if err := stripDefaults(filepath.Join(outPath, stackName+"-resources.tf"), map[string]string{
"org_id": " \"1\"",
}); err != nil {
stripDefaultsExtraFields := map[string]any{}
if singleOrg {
stripDefaultsExtraFields["org_id"] = true // Always remove org_id if single org
} else {
stripDefaultsExtraFields["org_id"] = `"1"` // Remove org_id if it's the default
}

if err := stripDefaults(filepath.Join(outPath, stackName+"-resources.tf"), stripDefaultsExtraFields); err != nil {
return err
}
if err := abstractDashboards(filepath.Join(outPath, stackName+"-resources.tf")); err != nil {
Expand Down
37 changes: 31 additions & 6 deletions pkg/generate/postprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/zclconf/go-cty/cty"
)

func stripDefaults(fpath string, extraFieldsToRemove map[string]string) error {
func stripDefaults(fpath string, extraFieldsToRemove map[string]any) error {
file, err := readHCLFile(fpath)
if err != nil {
return err
Expand Down Expand Up @@ -183,7 +183,7 @@ func readHCLFile(fpath string) (*hclwrite.File, error) {
return file, nil
}

func stripDefaultsFromBlock(block *hclwrite.Block, extraFieldsToRemove map[string]string) bool {
func stripDefaultsFromBlock(block *hclwrite.Block, extraFieldsToRemove map[string]any) bool {
hasChanges := false
for _, innblock := range block.Body().Blocks() {
if s := stripDefaultsFromBlock(innblock, extraFieldsToRemove); s {
Expand Down Expand Up @@ -211,10 +211,24 @@ func stripDefaultsFromBlock(block *hclwrite.Block, extraFieldsToRemove map[strin
hasChanges = true
}
}
for key, value := range extraFieldsToRemove {
if name == key && string(attribute.Expr().BuildTokens(nil).Bytes()) == value {
if rm := block.Body().RemoveAttribute(name); rm != nil {
hasChanges = true
for key, valueToRemove := range extraFieldsToRemove {
if name == key {
toRemove := false
fieldValue := strings.TrimSpace(string(attribute.Expr().BuildTokens(nil).Bytes()))
fieldValue, err := extractJSONEncode(fieldValue)
if err != nil {
continue
}

if v, ok := valueToRemove.(bool); ok && v {
toRemove = true
} else if v, ok := valueToRemove.(string); ok && v == fieldValue {
toRemove = true
}
if toRemove {
if rm := block.Body().RemoveAttribute(name); rm != nil {
hasChanges = true
}
}
}
}
Expand Down Expand Up @@ -268,3 +282,14 @@ func HCL2ValueFromConfigValue(v interface{}) cty.Value {
panic(fmt.Errorf("can't convert %#v to cty.Value", v))
}
}

func extractJSONEncode(value string) (string, error) {
if !strings.HasPrefix(value, "jsonencode(") {
return "", nil
}
value = strings.TrimPrefix(value, "jsonencode(")
value = strings.TrimSuffix(value, ")")

b, err := json.MarshalIndent(value, "", " ")
return string(b), err
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ resource "grafana_dashboard" "localhost_1_my-dashboard-uid" {
uid = "my-dashboard-uid"
})
folder = "my-folder-uid"
org_id = jsonencode(1)
}

# __generated__ by Terraform from "1:my-folder-uid"
resource "grafana_folder" "localhost_1_my-folder-uid" {
provider = grafana.localhost
org_id = jsonencode(1)
title = "My Folder"
uid = "my-folder-uid"
}
Expand All @@ -26,5 +24,4 @@ resource "grafana_notification_policy" "localhost_1_policy" {
contact_point = "grafana-default-email"
disable_provenance = true
group_by = ["grafana_folder", "alertname"]
org_id = jsonencode(1)
}
Loading