Skip to content

Commit 8ed024d

Browse files
toppercodesampagent
andcommitted
Add Terraform v2 dashboard resource and data source
Amp-Thread-ID: https://ampcode.com/threads/T-019c9bc7-a165-774f-b025-2e92d6e0b9d5 Co-authored-by: Amp <amp@ampcode.com>
1 parent 60598a8 commit 8ed024d

8 files changed

Lines changed: 830 additions & 83 deletions

File tree

axiom/data_source_dashboard.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package axiom
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/datasource"
8+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
11+
"github.com/axiomhq/axiom-go/axiom"
12+
)
13+
14+
var _ datasource.DataSource = &DashboardDataSource{}
15+
16+
func NewDashboardDataSource() datasource.DataSource {
17+
return &DashboardDataSource{}
18+
}
19+
20+
type DashboardDataSource struct {
21+
client *axiom.Client
22+
}
23+
24+
type DashboardDataSourceModel struct {
25+
ID types.String `tfsdk:"id"`
26+
UID types.String `tfsdk:"uid"`
27+
Dashboard types.String `tfsdk:"dashboard"`
28+
Version types.Int64 `tfsdk:"version"`
29+
InternalID types.String `tfsdk:"internal_id"`
30+
CreatedAt types.String `tfsdk:"created_at"`
31+
UpdatedAt types.String `tfsdk:"updated_at"`
32+
CreatedBy types.String `tfsdk:"created_by"`
33+
UpdatedBy types.String `tfsdk:"updated_by"`
34+
}
35+
36+
func (d *DashboardDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
37+
if req.ProviderData == nil {
38+
return
39+
}
40+
41+
client, ok := req.ProviderData.(*axiom.Client)
42+
if !ok {
43+
resp.Diagnostics.AddError(
44+
"Unexpected datasource Configure Type",
45+
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
46+
)
47+
return
48+
}
49+
50+
d.client = client
51+
}
52+
53+
func (d *DashboardDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
54+
resp.TypeName = req.ProviderTypeName + "_dashboard"
55+
}
56+
57+
func (d *DashboardDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
58+
resp.Schema = schema.Schema{
59+
Attributes: map[string]schema.Attribute{
60+
"uid": schema.StringAttribute{
61+
Required: true,
62+
MarkdownDescription: "Dashboard UID.",
63+
},
64+
"id": schema.StringAttribute{
65+
Computed: true,
66+
MarkdownDescription: "Dashboard identifier (same value as `uid`).",
67+
},
68+
"dashboard": schema.StringAttribute{
69+
Computed: true,
70+
MarkdownDescription: "Dashboard document as normalized JSON.",
71+
},
72+
"version": schema.Int64Attribute{
73+
Computed: true,
74+
MarkdownDescription: "Monotonic dashboard version.",
75+
},
76+
"internal_id": schema.StringAttribute{
77+
Computed: true,
78+
MarkdownDescription: "Internal dashboard identifier returned by the API.",
79+
},
80+
"created_at": schema.StringAttribute{
81+
Computed: true,
82+
MarkdownDescription: "Creation timestamp returned by the API.",
83+
},
84+
"updated_at": schema.StringAttribute{
85+
Computed: true,
86+
MarkdownDescription: "Last update timestamp returned by the API.",
87+
},
88+
"created_by": schema.StringAttribute{
89+
Computed: true,
90+
MarkdownDescription: "Creator returned by the API.",
91+
},
92+
"updated_by": schema.StringAttribute{
93+
Computed: true,
94+
MarkdownDescription: "Last updater returned by the API.",
95+
},
96+
},
97+
}
98+
}
99+
100+
func (d *DashboardDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
101+
var config DashboardDataSourceModel
102+
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
103+
if resp.Diagnostics.HasError() {
104+
return
105+
}
106+
107+
if d.client == nil {
108+
resp.Diagnostics.AddError("Client Error", "Client is not set")
109+
return
110+
}
111+
112+
raw, err := d.client.Dashboards.GetRaw(ctx, config.UID.ValueString())
113+
if err != nil {
114+
resp.Diagnostics.AddError("Failed to read dashboard", err.Error())
115+
return
116+
}
117+
118+
dashboard, err := decodeDashboardResource(raw)
119+
if err != nil {
120+
resp.Diagnostics.AddError("Failed to read dashboard", fmt.Sprintf("Unable to decode API response: %s", err))
121+
return
122+
}
123+
124+
dashboardJSON, err := normalizeDashboardRaw(dashboard.Dashboard)
125+
if err != nil {
126+
resp.Diagnostics.AddError("Failed to read dashboard", fmt.Sprintf("Unable to normalize dashboard payload: %s", err))
127+
return
128+
}
129+
130+
state := DashboardDataSourceModel{
131+
UID: types.StringValue(dashboard.UID),
132+
ID: types.StringValue(dashboard.UID),
133+
Dashboard: types.StringValue(dashboardJSON),
134+
Version: types.Int64Value(dashboard.Version),
135+
InternalID: types.StringValue(dashboard.ID),
136+
CreatedAt: types.StringValue(dashboard.CreatedAt),
137+
UpdatedAt: types.StringValue(dashboard.UpdatedAt),
138+
CreatedBy: types.StringValue(dashboard.CreatedBy),
139+
UpdatedBy: types.StringValue(dashboard.UpdatedBy),
140+
}
141+
142+
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
143+
}

axiom/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func (p *axiomProvider) Configure(ctx context.Context, req provider.ConfigureReq
113113
// DataSources defines the data sources implemented in the provider.
114114
func (p *axiomProvider) DataSources(_ context.Context) []func() datasource.DataSource {
115115
return []func() datasource.DataSource{
116+
NewDashboardDataSource,
116117
NewDatasetDataSource,
117118
NewMonitorDataSource,
118119
NewNotifierDataSource,
@@ -125,6 +126,7 @@ func (p *axiomProvider) DataSources(_ context.Context) []func() datasource.DataS
125126
// Resources defines the resources implemented in the provider.
126127
func (p *axiomProvider) Resources(_ context.Context) []func() resource.Resource {
127128
return []func() resource.Resource{
129+
NewDashboardResource,
128130
NewDatasetResource,
129131
NewMonitorResource,
130132
NewNotifierResource,

0 commit comments

Comments
 (0)