Skip to content

Commit e467ae1

Browse files
4665: Setup for Log v2 config
1 parent 585a0bd commit e467ae1

File tree

9 files changed

+383
-4
lines changed

9 files changed

+383
-4
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "grafana_asserts_log_drilldown_config Resource - terraform-provider-grafana"
4+
subcategory: "Asserts"
5+
description: |-
6+
Manages Asserts Log Drilldown Environment Configuration through the Grafana API.
7+
---
8+
9+
# grafana_asserts_log_drilldown_config (Resource)
10+
11+
Manages Asserts Log Drilldown Environment Configuration through the Grafana API.
12+
13+
## Example Usage
14+
15+
```terraform
16+
resource "grafana_asserts_log_drilldown_config" "example" {
17+
name = "default"
18+
config = <<-EOT
19+
# Example log drilldown configuration
20+
# Structure depends on API schema; this is illustrative
21+
providers:
22+
- name: "loki"
23+
url: "https://logs.example.com"
24+
default: true
25+
mappings:
26+
- name: "kubernetes"
27+
matchers:
28+
- label: "job"
29+
value: "kubelet"
30+
query: "{job=\"kubelet\", namespace=\"default\"} |= \"error\""
31+
EOT
32+
}
33+
```
34+
35+
<!-- schema generated by tfplugindocs -->
36+
## Schema
37+
38+
### Required
39+
40+
- `config` (String) The environment configuration (EnvironmentDto), in YAML format.
41+
- `name` (String) The environment name for the log drilldown configuration.
42+
43+
### Read-Only
44+
45+
- `id` (String) The ID of this resource.
46+
47+
## Import
48+
49+
Import is supported using the following syntax:
50+
51+
```shell
52+
terraform import grafana_asserts_log_drilldown_config.name "{{ name }}"
53+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
terraform import grafana_asserts_log_drilldown_config.name "{{ name }}"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resource "grafana_asserts_log_drilldown_config" "example" {
2+
name = "default"
3+
config = <<-EOT
4+
# Example log drilldown configuration
5+
# Structure depends on API schema; this is illustrative
6+
providers:
7+
- name: "loki"
8+
url: "https://logs.example.com"
9+
default: true
10+
mappings:
11+
- name: "kubernetes"
12+
matchers:
13+
- label: "job"
14+
value: "kubelet"
15+
query: "{job=\"kubelet\", namespace=\"default\"} |= \"error\""
16+
EOT
17+
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/grafana/grafana/apps/dashboard v0.0.0-20250424064802-2fbb2d6f5d27
1818
github.com/grafana/grafana/apps/playlist v0.0.0-20250424064802-2fbb2d6f5d27
1919
github.com/grafana/grafana/pkg/apimachinery v0.0.0-20250424064802-2fbb2d6f5d27
20-
github.com/grafana/k6-cloud-openapi-client-go v0.0.0-20250416134020-d958828152cd
20+
github.com/grafana/k6-cloud-openapi-client-go v0.0.0-20250715154343-32edc34ec1db
2121
github.com/grafana/machine-learning-go-client v0.8.2
2222
github.com/grafana/river v0.3.0
2323
github.com/grafana/slo-openapi-client/go/slo v0.0.0-20250218172929-ab9cae090da6
@@ -52,7 +52,7 @@ require (
5252
k8s.io/client-go v0.32.3
5353
)
5454

55-
require github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20250805165836-14e16b51b910
55+
require github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20250811221725-7aa50fc5d960
5656

5757
require (
5858
cuelang.org/go v0.11.1 // indirect

go.sum

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ github.com/grafana/grafana-app-sdk/logging v0.35.1 h1:taVpl+RoixTYl0JBJGhH+fPVmw
182182
github.com/grafana/grafana-app-sdk/logging v0.35.1/go.mod h1:Y/bvbDhBiV/tkIle9RW49pgfSPIPSON8Q4qjx3pyqDk=
183183
github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20250805165836-14e16b51b910 h1:2OfDIhMtXWWVQcDp9cq/VMSBOJJfDek9450rcsV+qLg=
184184
github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20250805165836-14e16b51b910/go.mod h1:EL/5hluCvj6EDjkUfoClLKSKDoCoDowZUety28jhxQI=
185+
github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20250811125322-247815da58ca h1:GVzyCTi3rqvjK42b++lFjabG2zsrLvyAbbR43dWP6s0=
186+
github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20250811125322-247815da58ca/go.mod h1:EL/5hluCvj6EDjkUfoClLKSKDoCoDowZUety28jhxQI=
187+
github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20250811221725-7aa50fc5d960 h1:KZ1Hp0Crqja1D8iX3IekIH+z57ThRvOaMqi+kVhsM0g=
188+
github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20250811221725-7aa50fc5d960/go.mod h1:EL/5hluCvj6EDjkUfoClLKSKDoCoDowZUety28jhxQI=
185189
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20250526074454-7ec66e02e4bb h1:rmYEnCXHNQbRsuzc5jCX5qkBqFF37c5RCHlyqAAPJZo=
186190
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20250526074454-7ec66e02e4bb/go.mod h1:sYWkB3NhyirQJfy3wtNQ29UYjoHbRlJlYhqN1jNsC5g=
187191
github.com/grafana/grafana-openapi-client-go v0.0.0-20250617151817-c0f8cbb88d5c h1:jox7J0BnJmcZJp8lp631u4gjDEoIfpi6O3yrpiXNTtg=
@@ -194,8 +198,8 @@ github.com/grafana/grafana/apps/playlist v0.0.0-20250424064802-2fbb2d6f5d27 h1:a
194198
github.com/grafana/grafana/apps/playlist v0.0.0-20250424064802-2fbb2d6f5d27/go.mod h1:9U44mptAJW8bkvgPgCxsnki58/nz3wKPgDayeyeFWJs=
195199
github.com/grafana/grafana/pkg/apimachinery v0.0.0-20250424064802-2fbb2d6f5d27 h1:2Q158xAoyGgPVw7BIWF5cPYTkjjdsn9c8TpNA38gKzM=
196200
github.com/grafana/grafana/pkg/apimachinery v0.0.0-20250424064802-2fbb2d6f5d27/go.mod h1:kzjpaBODMbCSS2kvAnV43Pwxoq4lOxrgw/TGKqq8oTA=
197-
github.com/grafana/k6-cloud-openapi-client-go v0.0.0-20250416134020-d958828152cd h1:UOHbve+keUInhIBSqA4m7GtF2wEiKyW8f/kUt9S0+zM=
198-
github.com/grafana/k6-cloud-openapi-client-go v0.0.0-20250416134020-d958828152cd/go.mod h1:nBtSyJ0NTfJvEPSrRSKc1u7Vn0mB/kttEZdClHwTqi0=
201+
github.com/grafana/k6-cloud-openapi-client-go v0.0.0-20250715154343-32edc34ec1db h1:GZGkcFrQF09j8rZfxcIRql7liBf1U7pdmqSAseGintg=
202+
github.com/grafana/k6-cloud-openapi-client-go v0.0.0-20250715154343-32edc34ec1db/go.mod h1:RBPBP7qIR/K6qzQEQYESVhp/XJspiBTOyBEBCbPXrvI=
199203
github.com/grafana/machine-learning-go-client v0.8.2 h1:TvU4e+Kgg4GhwBNYTMjBUNq4tbhcxe0L8w1eo/UfV2M=
200204
github.com/grafana/machine-learning-go-client v0.8.2/go.mod h1:GQKDn10CZqG11l1Qtc6BZ5V6e54fSv5Vi8wskWn3BWs=
201205
github.com/grafana/otel-profiling-go v0.5.1 h1:stVPKAFZSa7eGiqbYuG25VcqYksR6iWvF3YH66t4qL8=

internal/resources/asserts/catalog-resource.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ metadata:
1919
title: grafana_asserts_disabled_alert_config (resource)
2020
description: |
2121
resource `grafana_asserts_disabled_alert_config` in Grafana Labs' Terraform Provider
22+
spec:
23+
subcomponentOf: component:default/terraform-provider-grafana
24+
type: terraform-resource
25+
owner: group:default/asserts
26+
lifecycle: production
27+
---
28+
apiVersion: backstage.io/v1alpha1
29+
kind: Component
30+
metadata:
31+
name: resource-grafana_asserts_log_drilldown_config
32+
title: grafana_asserts_log_drilldown_config (resource)
33+
description: |
34+
resource `grafana_asserts_log_drilldown_config` in Grafana Labs' Terraform Provider
2235
spec:
2336
subcomponentOf: component:default/terraform-provider-grafana
2437
type: terraform-resource
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package asserts
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"gopkg.in/yaml.v2"
11+
12+
assertsapi "github.com/grafana/grafana-asserts-public-clients/go/gcom"
13+
"github.com/grafana/terraform-provider-grafana/v4/internal/common"
14+
)
15+
16+
func makeResourceLogDrilldownConfig() *common.Resource {
17+
sch := &schema.Resource{
18+
Description: "Manages Asserts Log Drilldown Environment Configuration through the Grafana API.",
19+
20+
CreateContext: resourceLogDrilldownConfigCreate,
21+
ReadContext: resourceLogDrilldownConfigRead,
22+
UpdateContext: resourceLogDrilldownConfigUpdate,
23+
DeleteContext: resourceLogDrilldownConfigDelete,
24+
25+
Importer: &schema.ResourceImporter{StateContext: schema.ImportStatePassthroughContext},
26+
27+
Schema: map[string]*schema.Schema{
28+
"name": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
ForceNew: true,
32+
Description: "The environment name for the log drilldown configuration.",
33+
},
34+
"config": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
Description: "The environment configuration (EnvironmentDto), in YAML format.",
38+
},
39+
},
40+
}
41+
42+
return common.NewLegacySDKResource(
43+
common.CategoryAsserts,
44+
"grafana_asserts_log_drilldown_config",
45+
common.NewResourceID(common.StringIDField("name")),
46+
sch,
47+
)
48+
}
49+
50+
func resourceLogDrilldownConfigCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
51+
client, stackID, diags := validateAssertsClient(meta)
52+
if diags.HasError() {
53+
return diags
54+
}
55+
56+
name := d.Get("name").(string)
57+
cfgYAML := d.Get("config").(string)
58+
59+
var env assertsapi.EnvironmentDto
60+
if err := yaml.Unmarshal([]byte(cfgYAML), &env); err != nil {
61+
return diag.FromErr(fmt.Errorf("failed to unmarshal environment YAML: %w", err))
62+
}
63+
// Ensure name is set from resource name
64+
env.SetName(name)
65+
66+
req := client.LogConfigControllerAPI.UpsertEnvironmentConfig(ctx).
67+
EnvironmentDto(env).
68+
XScopeOrgID(fmt.Sprintf("%d", stackID))
69+
_, err := req.Execute()
70+
if err != nil {
71+
return diag.FromErr(fmt.Errorf("failed to upsert log environment config: %w", err))
72+
}
73+
74+
d.SetId(name)
75+
return resourceLogDrilldownConfigRead(ctx, d, meta)
76+
}
77+
78+
func resourceLogDrilldownConfigRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
79+
client, stackID, diags := validateAssertsClient(meta)
80+
if diags.HasError() {
81+
return diags
82+
}
83+
name := d.Id()
84+
85+
var tenantCfg *assertsapi.TenantEnvConfigResponseDto
86+
err := withRetryRead(ctx, func(retryCount, maxRetries int) *retry.RetryError {
87+
resp, _, err := client.LogConfigControllerAPI.GetTenantEnvConfig(ctx).
88+
XScopeOrgID(fmt.Sprintf("%d", stackID)).
89+
Execute()
90+
if err != nil {
91+
return createAPIError("get tenant environment log config", retryCount, maxRetries, err)
92+
}
93+
tenantCfg = resp
94+
return nil
95+
})
96+
if err != nil {
97+
return diag.FromErr(err)
98+
}
99+
100+
if tenantCfg == nil {
101+
d.SetId("")
102+
return nil
103+
}
104+
105+
found := false
106+
for _, env := range tenantCfg.GetEnvironments() {
107+
if env.GetName() == name {
108+
found = true
109+
break
110+
}
111+
}
112+
if !found {
113+
d.SetId("")
114+
return nil
115+
}
116+
117+
_ = d.Set("name", name)
118+
if v := d.Get("config").(string); v != "" {
119+
_ = d.Set("config", v)
120+
}
121+
return nil
122+
}
123+
124+
func resourceLogDrilldownConfigUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
125+
client, stackID, diags := validateAssertsClient(meta)
126+
if diags.HasError() {
127+
return diags
128+
}
129+
130+
name := d.Get("name").(string)
131+
cfgYAML := d.Get("config").(string)
132+
133+
var env assertsapi.EnvironmentDto
134+
if err := yaml.Unmarshal([]byte(cfgYAML), &env); err != nil {
135+
return diag.FromErr(fmt.Errorf("failed to unmarshal environment YAML: %w", err))
136+
}
137+
env.SetName(name)
138+
139+
req := client.LogConfigControllerAPI.UpsertEnvironmentConfig(ctx).
140+
EnvironmentDto(env).
141+
XScopeOrgID(fmt.Sprintf("%d", stackID))
142+
_, err := req.Execute()
143+
if err != nil {
144+
return diag.FromErr(fmt.Errorf("failed to upsert log environment config: %w", err))
145+
}
146+
147+
return resourceLogDrilldownConfigRead(ctx, d, meta)
148+
}
149+
150+
func resourceLogDrilldownConfigDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
151+
client, stackID, diags := validateAssertsClient(meta)
152+
if diags.HasError() {
153+
return diags
154+
}
155+
name := d.Id()
156+
157+
req := client.LogConfigControllerAPI.DeleteConfig(ctx, name).
158+
XScopeOrgID(fmt.Sprintf("%d", stackID))
159+
_, err := req.Execute()
160+
if err != nil {
161+
return diag.FromErr(fmt.Errorf("failed to delete log environment config: %w", err))
162+
}
163+
return nil
164+
}

0 commit comments

Comments
 (0)