Skip to content

Commit 219216f

Browse files
committed
feat: add labels field to kibana synthetics monitor resource
- Add labels field to SyntheticsMonitorConfig and SyntheticsMonitor structs - Add labels field to terraform schema as MapAttribute - Add conversion functions for labels between API and terraform types - Update documentation with labels field description and example - Labels support key-value pairs for filtering and grouping monitors
1 parent 89b2281 commit 219216f

File tree

5 files changed

+281
-4
lines changed

5 files changed

+281
-4
lines changed

docs/resources/kibana_synthetics_monitor.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ resource "elasticstack_kibana_synthetics_monitor" "my_monitor" {
4141
locations = ["us_west"]
4242
enabled = false
4343
tags = ["tag"]
44+
labels = {
45+
environment = "production"
46+
team = "platform"
47+
service = "web-app"
48+
}
4449
alert = {
4550
status = {
4651
enabled = true
@@ -77,6 +82,7 @@ resource "elasticstack_kibana_synthetics_monitor" "my_monitor" {
7782
- `enabled` (Boolean) Whether the monitor is enabled. Default: `true`
7883
- `http` (Attributes) HTTP Monitor specific fields (see [below for nested schema](#nestedatt--http))
7984
- `icmp` (Attributes) ICMP Monitor specific fields (see [below for nested schema](#nestedatt--icmp))
85+
- `labels` (Map of String) Key-value pairs of labels to associate with the monitor. Labels can be used for filtering and grouping monitors.
8086
- `locations` (List of String) Where to deploy the monitor. Monitors can be deployed in multiple locations so that you can detect differences in availability and response times across those locations.
8187
- `namespace` (String) The data stream namespace. Note: if you change its value, kibana creates new datastream. A user needs permissions for new/old datastream in update case to be able to see full monitor history. The `namespace` field should be lowercase and not contain spaces. The namespace must not include any of the following characters: *, \, /, ?, ", <, >, |, whitespace, ,, #, :, or -. Default: `default`
8288
- `params` (String) Monitor parameters. Raw JSON object, use `jsonencode` function to represent JSON

examples/resources/elasticstack_kibana_synthetics_monitor/resource.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ resource "elasticstack_kibana_synthetics_monitor" "my_monitor" {
99
locations = ["us_west"]
1010
enabled = false
1111
tags = ["tag"]
12+
labels = {
13+
environment = "production"
14+
team = "platform"
15+
service = "web-app"
16+
}
1217
alert = {
1318
status = {
1419
enabled = true

internal/kibana/synthetics/schema.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ type tfModelV0 struct {
107107
PrivateLocations []types.String `tfsdk:"private_locations"`
108108
Enabled types.Bool `tfsdk:"enabled"`
109109
Tags []types.String `tfsdk:"tags"`
110+
Labels types.Map `tfsdk:"labels"`
110111
Alert types.Object `tfsdk:"alert"` //tfAlertConfigV0
111112
APMServiceName types.String `tfsdk:"service_name"`
112113
TimeoutSeconds types.Int64 `tfsdk:"timeout"`
@@ -217,6 +218,11 @@ func monitorConfigSchema() schema.Schema {
217218
Optional: true,
218219
MarkdownDescription: "An array of tags.",
219220
},
221+
"labels": schema.MapAttribute{
222+
ElementType: types.StringType,
223+
Optional: true,
224+
MarkdownDescription: "Key-value pairs of labels to associate with the monitor. Labels can be used for filtering and grouping monitors.",
225+
},
220226
"alert": monitorAlertConfigSchema(),
221227
"service_name": schema.StringAttribute{
222228
Optional: true,
@@ -557,6 +563,31 @@ func StringSliceValue(v []string) []types.String {
557563
return res
558564
}
559565

566+
func MapStringValue(v map[string]string) types.Map {
567+
if len(v) == 0 {
568+
return types.MapNull(types.StringType)
569+
}
570+
elements := make(map[string]attr.Value)
571+
for k, val := range v {
572+
elements[k] = types.StringValue(val)
573+
}
574+
mapValue, _ := types.MapValue(types.StringType, elements)
575+
return mapValue
576+
}
577+
578+
func ValueStringMap(v types.Map) map[string]string {
579+
if v.IsNull() || v.IsUnknown() {
580+
return make(map[string]string)
581+
}
582+
result := make(map[string]string)
583+
for k, val := range v.Elements() {
584+
if strVal, ok := val.(types.String); ok {
585+
result[k] = strVal.ValueString()
586+
}
587+
}
588+
return result
589+
}
590+
560591
func toNormalizedValue(jsObj kbapi.JsonObject) (jsontypes.Normalized, error) {
561592
res, err := json.Marshal(jsObj)
562593
if err != nil {
@@ -679,6 +710,7 @@ func (v *tfModelV0) toModelV0(ctx context.Context, api *kbapi.SyntheticsMonitor,
679710
PrivateLocations: StringSliceValue(privateLocLabels),
680711
Enabled: types.BoolPointerValue(api.Enabled),
681712
Tags: StringSliceValue(api.Tags),
713+
Labels: MapStringValue(api.Labels),
682714
Alert: alertV0,
683715
APMServiceName: types.StringValue(api.APMServiceName),
684716
TimeoutSeconds: types.Int64Value(timeout),
@@ -901,6 +933,7 @@ func (v *tfModelV0) toSyntheticsMonitorConfig(ctx context.Context) (*kbapi.Synth
901933
PrivateLocations: ValueStringSlice(v.PrivateLocations),
902934
Enabled: v.Enabled.ValueBoolPointer(),
903935
Tags: ValueStringSlice(v.Tags),
936+
Labels: ValueStringMap(v.Labels),
904937
Alert: toTFAlertConfig(ctx, v.Alert),
905938
APMServiceName: v.APMServiceName.ValueString(),
906939
TimeoutSeconds: int(v.TimeoutSeconds.ValueInt64()),

0 commit comments

Comments
 (0)