Skip to content

Commit

Permalink
Config Generation: Omit org ID from Grafana resources
Browse files Browse the repository at this point in the history
(If there's only one org being generated)
If all resources are in the default org, there's no reason to have the `1:` prefix to all resources
This makes resource names and IDs much nicer to look at
  • Loading branch information
julienduchesne committed Aug 9, 2024
1 parent cd47547 commit 502259e
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 58 deletions.
23 changes: 17 additions & 6 deletions internal/resources/grafana/common_lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package grafana
import (
"context"
"fmt"
"strings"
"sync"

goapi "github.com/grafana/grafana-openapi-client-go/client"
Expand All @@ -12,14 +13,16 @@ import (

// ListerData is used as the data arg in "ListIDs" functions. It allows getting data common to multiple resources.
type ListerData struct {
singleOrg bool
orgIDs []int64
orgsInit sync.Once
omitSingleOrgID bool
singleOrg bool
orgIDs []int64
orgsInit sync.Once
}

func NewListerData(singleOrg bool) *ListerData {
func NewListerData(singleOrg, omitSingleOrgID bool) *ListerData {
return &ListerData{
singleOrg: singleOrg,
singleOrg: singleOrg,
omitSingleOrgID: omitSingleOrgID,
}
}

Expand Down Expand Up @@ -84,7 +87,15 @@ func listerFunctionOrgResource(listerFunc grafanaOrgResourceListerFunc) common.R
if err != nil {
return nil, err
}
ids = append(ids, idsInOrg...)

// Trim org ID from IDs if there is only one org and it's the default org
if len(orgIDs) == 1 && (orgID <= 1) && data.omitSingleOrgID {
for _, id := range idsInOrg {
ids = append(ids, strings.TrimPrefix(id, fmt.Sprintf("%d:", orgID)))
}
} else {
ids = append(ids, idsInOrg...)
}
}

return ids, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/testutils/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func CheckLister(terraformResource string) resource.TestCheckFunc {

// Get the list of IDs from the lister function
ctx := context.Background()
var listerData any = grafana.NewListerData(false)
var listerData any = grafana.NewListerData(false, false)
if resource.Category == common.CategoryCloud {
listerData = cloud.NewListerData(os.Getenv("GRAFANA_CLOUD_ORG"))
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,12 @@ func generateImportBlocks(ctx context.Context, client *common.Client, listerData
var blocks []*hclwrite.Block
for _, id := range ids {
cleanedID := allowedTerraformChars.ReplaceAllString(id, "_")
if provider != "cloud" {
if provider != "cloud" && provider != "" {
cleanedID = strings.ReplaceAll(provider, "-", "_") + "_" + cleanedID
}
if cleanedID[0] >= '0' && cleanedID[0] <= '9' {
cleanedID = "_" + cleanedID
}

matched, err := filterResourceByName(resource.Name, cleanedID, cfg.IncludeResources)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestAccGenerate(t *testing.T) {
name: "dashboard-filter-strict",
config: testutils.TestAccExample(t, "resources/grafana_dashboard/resource.tf"),
generateConfig: func(cfg *generate.Config) {
cfg.IncludeResources = []string{"grafana_dashboard._1_my-dashboard-uid"}
cfg.IncludeResources = []string{"grafana_dashboard.my-dashboard-uid"}
},
check: func(t *testing.T, tempDir string) {
assertFiles(t, tempDir, "testdata/generate/dashboard-filtered", []string{
Expand All @@ -147,7 +147,7 @@ func TestAccGenerate(t *testing.T) {
name: "dashboard-filter-wildcard-on-resource-type",
config: testutils.TestAccExample(t, "resources/grafana_dashboard/resource.tf"),
generateConfig: func(cfg *generate.Config) {
cfg.IncludeResources = []string{"*._1_my-dashboard-uid"}
cfg.IncludeResources = []string{"*.my-dashboard-uid"}
},
check: func(t *testing.T, tempDir string) {
assertFiles(t, tempDir, "testdata/generate/dashboard-filtered", []string{
Expand Down
2 changes: 1 addition & 1 deletion pkg/generate/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func generateGrafanaResources(ctx context.Context, cfg *Config, stack stack, gen
}

singleOrg := !strings.Contains(stack.managementKey, ":")
listerData := grafana.NewListerData(singleOrg)
listerData := grafana.NewListerData(singleOrg, true)

// Generate resources
config := provider.ProviderConfig{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: alerting.grafana.crossplane.io/v1alpha1
kind: ContactPoint
metadata:
name: 1-email-receiver
name: email-receiver
annotations:
crossplane.io/external-name: 1:email receiver
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: oss.grafana.crossplane.io/v1alpha1
kind: Dashboard
metadata:
name: 1-my-dashboard-uid
name: my-dashboard-uid
annotations:
crossplane.io/external-name: 1:my-dashboard-uid
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: oss.grafana.crossplane.io/v1alpha1
kind: Folder
metadata:
name: 1-my-folder-uid
name: my-folder-uid
annotations:
crossplane.io/external-name: 1:my-folder-uid
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: alerting.grafana.crossplane.io/v1alpha1
kind: NotificationPolicy
metadata:
name: 1-policy
name: policy
annotations:
crossplane.io/external-name: 1:policy
spec:
Expand Down
4 changes: 2 additions & 2 deletions pkg/generate/testdata/generate/dashboard-filtered/imports.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
to = grafana_dashboard._1_my-dashboard-uid
id = "1:my-dashboard-uid"
to = grafana_dashboard.my-dashboard-uid
id = "my-dashboard-uid"
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# __generated__ by Terraform
# Please review these resources and move them into your main configuration files.

# __generated__ by Terraform from "1:my-dashboard-uid"
resource "grafana_dashboard" "_1_my-dashboard-uid" {
# __generated__ by Terraform from "my-dashboard-uid"
resource "grafana_dashboard" "my-dashboard-uid" {
config_json = jsonencode({
title = "My Dashboard"
uid = "my-dashboard-uid"
Expand Down
16 changes: 8 additions & 8 deletions pkg/generate/testdata/generate/dashboard-json/imports.tf.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"import": [
{
"id": "1:email receiver",
"to": "grafana_contact_point._1_email_receiver"
"id": "email receiver",
"to": "grafana_contact_point.email_receiver"
},
{
"id": "1:my-dashboard-uid",
"to": "grafana_dashboard._1_my-dashboard-uid"
"id": "my-dashboard-uid",
"to": "grafana_dashboard.my-dashboard-uid"
},
{
"id": "1:my-folder-uid",
"to": "grafana_folder._1_my-folder-uid"
"id": "my-folder-uid",
"to": "grafana_folder.my-folder-uid"
},
{
"id": "1:policy",
"to": "grafana_notification_policy._1_policy"
"id": "policy",
"to": "grafana_notification_policy.policy"
},
{
"id": "1",
Expand Down
10 changes: 5 additions & 5 deletions pkg/generate/testdata/generate/dashboard-json/resources.tf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"resource": {
"grafana_contact_point": {
"_1_email_receiver": [
"email_receiver": [
{
"disable_provenance": true,
"email": [
Expand All @@ -18,23 +18,23 @@
]
},
"grafana_dashboard": {
"_1_my-dashboard-uid": [
"my-dashboard-uid": [
{
"config_json": "${jsonencode({\n title = \"My Dashboard\"\n uid = \"my-dashboard-uid\"\n })}",
"folder": "${grafana_folder._1_my-folder-uid.uid}"
"folder": "${grafana_folder.my-folder-uid.uid}"
}
]
},
"grafana_folder": {
"_1_my-folder-uid": [
"my-folder-uid": [
{
"title": "My Folder",
"uid": "my-folder-uid"
}
]
},
"grafana_notification_policy": {
"_1_policy": [
"policy": [
{
"contact_point": "grafana-default-email",
"disable_provenance": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
to = grafana_dashboard._0_my-dashboard-uid
id = "0:my-dashboard-uid"
to = grafana_dashboard.my-dashboard-uid
id = "my-dashboard-uid"
}

import {
to = grafana_folder._0_my-folder-uid
id = "0:my-folder-uid"
to = grafana_folder.my-folder-uid
id = "my-folder-uid"
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# __generated__ by Terraform
# Please review these resources and move them into your main configuration files.

# __generated__ by Terraform from "0:my-dashboard-uid"
resource "grafana_dashboard" "_0_my-dashboard-uid" {
# __generated__ by Terraform from "my-dashboard-uid"
resource "grafana_dashboard" "my-dashboard-uid" {
config_json = jsonencode({
title = "My Dashboard"
uid = "my-dashboard-uid"
})
folder = grafana_folder._0_my-folder-uid.uid
folder = grafana_folder.my-folder-uid.uid
}

# __generated__ by Terraform from "0:my-folder-uid"
resource "grafana_folder" "_0_my-folder-uid" {
# __generated__ by Terraform from "my-folder-uid"
resource "grafana_folder" "my-folder-uid" {
title = "My Folder"
uid = "my-folder-uid"
}
16 changes: 8 additions & 8 deletions pkg/generate/testdata/generate/dashboard/imports.tf
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import {
to = grafana_contact_point._1_email_receiver
id = "1:email receiver"
to = grafana_contact_point.email_receiver
id = "email receiver"
}

import {
to = grafana_dashboard._1_my-dashboard-uid
id = "1:my-dashboard-uid"
to = grafana_dashboard.my-dashboard-uid
id = "my-dashboard-uid"
}

import {
to = grafana_folder._1_my-folder-uid
id = "1:my-folder-uid"
to = grafana_folder.my-folder-uid
id = "my-folder-uid"
}

import {
to = grafana_notification_policy._1_policy
id = "1:policy"
to = grafana_notification_policy.policy
id = "policy"
}

import {
Expand Down
18 changes: 9 additions & 9 deletions pkg/generate/testdata/generate/dashboard/resources.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# __generated__ by Terraform
# Please review these resources and move them into your main configuration files.

# __generated__ by Terraform from "1:email receiver"
resource "grafana_contact_point" "_1_email_receiver" {
# __generated__ by Terraform from "email receiver"
resource "grafana_contact_point" "email_receiver" {
disable_provenance = true
name = "email receiver"
email {
Expand All @@ -12,23 +12,23 @@ resource "grafana_contact_point" "_1_email_receiver" {
}
}

# __generated__ by Terraform from "1:my-dashboard-uid"
resource "grafana_dashboard" "_1_my-dashboard-uid" {
# __generated__ by Terraform from "my-dashboard-uid"
resource "grafana_dashboard" "my-dashboard-uid" {
config_json = jsonencode({
title = "My Dashboard"
uid = "my-dashboard-uid"
})
folder = grafana_folder._1_my-folder-uid.uid
folder = grafana_folder.my-folder-uid.uid
}

# __generated__ by Terraform from "1:my-folder-uid"
resource "grafana_folder" "_1_my-folder-uid" {
# __generated__ by Terraform from "my-folder-uid"
resource "grafana_folder" "my-folder-uid" {
title = "My Folder"
uid = "my-folder-uid"
}

# __generated__ by Terraform from "1:policy"
resource "grafana_notification_policy" "_1_policy" {
# __generated__ by Terraform from "policy"
resource "grafana_notification_policy" "policy" {
contact_point = "grafana-default-email"
disable_provenance = true
group_by = ["grafana_folder", "alertname"]
Expand Down

0 comments on commit 502259e

Please sign in to comment.