/
logs_data_source.go
251 lines (214 loc) · 8.02 KB
/
logs_data_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package provider
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"strconv"
"github.com/gdashboard/terraform-provider-gdashboard/internal/provider/grafana"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure provider defined types fully satisfy framework interfaces
var _ datasource.DataSource = &LogsDataSource{}
func NewLogsDataSource() datasource.DataSource {
return &LogsDataSource{}
}
// LogsDataSource defines the data source implementation.
type LogsDataSource struct {
CompactJson bool
}
// LogsDataSourceModel describes the data source data model.
type LogsDataSourceModel struct {
Id types.String `tfsdk:"id"`
Json types.String `tfsdk:"json"`
CompactJson types.Bool `tfsdk:"compact_json"`
Title types.String `tfsdk:"title"`
Description types.String `tfsdk:"description"`
Queries []Query `tfsdk:"queries"`
Graph []LogsOptions `tfsdk:"graph"`
}
type LogsOptions struct {
ShowTime types.Bool `tfsdk:"show_time"`
ShowUniqueLabels types.Bool `tfsdk:"show_unique_labels"`
ShowCommonLabels types.Bool `tfsdk:"show_common_labels"`
WrapLines types.Bool `tfsdk:"wrap_lines"`
PrettifyJson types.Bool `tfsdk:"prettify_json"`
EnableLogDetails types.Bool `tfsdk:"enable_log_details"`
Deduplication types.String `tfsdk:"deduplication"`
Order types.String `tfsdk:"order"`
}
func (d *LogsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_logs"
}
func (d *LogsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
Description: "Logs panel data source.",
MarkdownDescription: "Logs panel data source. See Grafana [documentation](https://grafana.com/docs/grafana/latest/panels-visualizations/visualizations/logs/) for more details.",
Blocks: map[string]schema.Block{
"queries": queryBlock(),
"graph": schema.ListNestedBlock{
Description: "The visualization options.",
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"show_time": schema.BoolAttribute{
Optional: true,
Description: "Whether to show the time column. This is the timestamp associated with the log line as reported from the data source.",
},
"show_unique_labels": schema.BoolAttribute{
Optional: true,
Description: "Whether to show the unique labels column, which shows only non-common labels.",
},
"show_common_labels": schema.BoolAttribute{
Optional: true,
Description: "Whether to show the common labels.",
},
"wrap_lines": schema.BoolAttribute{
Optional: true,
Description: "Whether to wrap the lines.",
},
"prettify_json": schema.BoolAttribute{
Optional: true,
Description: "Set this to true to pretty print all JSON logs. This setting does not affect logs in any format other than JSON.",
},
"enable_log_details": schema.BoolAttribute{
Optional: true,
Description: "Whether to show log details view for each log row.",
},
"deduplication": schema.StringAttribute{
Optional: true,
Description: "The layout orientation. The choices are: none, exact, numbers, signature.",
MarkdownDescription: "The layout orientation. The choices are: `none`, `exact`, `numbers`, `signature`.",
Validators: []validator.String{
stringvalidator.OneOf("none", "exact", "numbers", "signature"),
},
},
"order": schema.StringAttribute{
Optional: true,
Description: "The order in which to show logs first. The choices are: newest_first, oldest_first.",
MarkdownDescription: "The order in which to show logs first. The choices are: `newest_first`, `oldest_first`.",
Validators: []validator.String{
stringvalidator.OneOf("newest_first", "oldest_first"),
},
},
},
},
Validators: []validator.List{
listvalidator.SizeAtMost(1),
},
},
},
Attributes: map[string]schema.Attribute{
"id": idAttribute(),
"json": jsonAttribute(),
"compact_json": compactJsonAttribute(),
"title": titleAttribute(),
"description": descriptionAttribute(),
},
}
}
func (d *LogsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
defaults, ok := req.ProviderData.(Defaults)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected Defaults, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
}
d.CompactJson = defaults.CompactJson
}
func (d *LogsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data LogsDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
targets, minInterval := createTargets(data.Queries)
falseVal := false
trueVal := true
dedupStrategy := "none"
sortOrderAscending := "Ascending"
sortOrderDescending := "Descending"
options := grafana.LogsOptions{
ShowTime: &falseVal,
ShowLabels: &falseVal,
ShowCommonLabels: &falseVal,
WrapLogMessage: &falseVal,
PrettifyLogMessage: &falseVal,
EnableLogDetails: &trueVal,
DedupStrategy: &dedupStrategy,
SortOrder: &sortOrderDescending,
}
for _, graph := range data.Graph {
if !graph.ShowTime.IsNull() {
options.ShowTime = graph.ShowTime.ValueBoolPointer()
}
if !graph.ShowUniqueLabels.IsNull() {
options.ShowLabels = graph.ShowUniqueLabels.ValueBoolPointer()
}
if !graph.ShowCommonLabels.IsNull() {
options.ShowCommonLabels = graph.ShowCommonLabels.ValueBoolPointer()
}
if !graph.WrapLines.IsNull() {
options.WrapLogMessage = graph.WrapLines.ValueBoolPointer()
}
if !graph.PrettifyJson.IsNull() {
options.PrettifyLogMessage = graph.PrettifyJson.ValueBoolPointer()
}
if !graph.EnableLogDetails.IsNull() {
options.EnableLogDetails = graph.EnableLogDetails.ValueBoolPointer()
}
if !graph.Deduplication.IsNull() {
options.DedupStrategy = graph.Deduplication.ValueStringPointer()
}
if !graph.Order.IsNull() {
if graph.Order.ValueString() == "oldest_first" {
options.SortOrder = &sortOrderAscending
} else {
options.SortOrder = &sortOrderDescending
}
}
}
panel := &grafana.Panel{
CommonPanel: grafana.CommonPanel{
OfType: grafana.LogsType,
Title: data.Title.ValueString(),
Type: "logs",
Span: 12,
IsNew: true,
Interval: minInterval,
},
LogsPanel: &grafana.LogsPanel{
Targets: targets,
Options: options,
},
}
if !data.Description.IsNull() {
panel.CommonPanel.Description = data.Description.ValueStringPointer()
}
var jsonData []byte
var err error
if data.CompactJson.ValueBool() || d.CompactJson {
jsonData, err = json.Marshal(panel)
} else {
jsonData, err = json.MarshalIndent(panel, "", " ")
}
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Could not marshal json: %s", err))
return
}
data.Json = types.StringValue(string(jsonData))
data.Id = types.StringValue(strconv.Itoa(hashcode(jsonData)))
//resp.Diagnostics.AddError("Client Error", fmt.Sprintf("%s", string(jsonData)))
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}