Skip to content

Commit d78118d

Browse files
committed
update schema
1 parent 5d0d6f6 commit d78118d

File tree

5 files changed

+52
-22
lines changed

5 files changed

+52
-22
lines changed

internal/kibana/synthetics/create.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package synthetics
33
import (
44
"context"
55
"fmt"
6+
67
"github.com/hashicorp/terraform-plugin-framework/resource"
78
)
89

@@ -26,14 +27,14 @@ func (r *Resource) Create(ctx context.Context, request resource.CreateRequest, r
2627
return
2728
}
2829

29-
namespace := plan.SpaceID.ValueString()
30-
result, err := kibanaClient.KibanaSynthetics.Monitor.Add(ctx, input.config, input.fields, namespace)
30+
spaceId := plan.SpaceID.ValueString()
31+
result, err := kibanaClient.KibanaSynthetics.Monitor.Add(ctx, input.config, input.fields, spaceId)
3132
if err != nil {
32-
response.Diagnostics.AddError(fmt.Sprintf("Failed to create Kibana monitor `%s`, namespace %s", input.config.Name, namespace), err.Error())
33+
response.Diagnostics.AddError(fmt.Sprintf("Failed to create Kibana monitor `%s`, space %s", input.config.Name, spaceId), err.Error())
3334
return
3435
}
3536

36-
plan, diags = plan.toModelV0(ctx, result)
37+
plan, diags = plan.toModelV0(ctx, result, spaceId)
3738
response.Diagnostics.Append(diags...)
3839
if response.Diagnostics.HasError() {
3940
return

internal/kibana/synthetics/read.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
78
"github.com/disaster37/go-kibana-rest/v8/kbapi"
89
"github.com/hashicorp/terraform-plugin-framework/resource"
910
)
@@ -28,21 +29,21 @@ func (r *Resource) Read(ctx context.Context, request resource.ReadRequest, respo
2829
return
2930
}
3031

31-
namespace := compositeId.ClusterId
32+
spaceId := compositeId.ClusterId
3233
monitorId := kbapi.MonitorID(compositeId.ResourceId)
33-
result, err := kibanaClient.KibanaSynthetics.Monitor.Get(ctx, monitorId, namespace)
34+
result, err := kibanaClient.KibanaSynthetics.Monitor.Get(ctx, monitorId, spaceId)
3435
if err != nil {
3536
var apiError *kbapi.APIError
3637
if errors.As(err, &apiError) && apiError.Code == 404 {
3738
response.State.RemoveResource(ctx)
3839
return
3940
}
4041

41-
response.Diagnostics.AddError(fmt.Sprintf("Failed to get monitor `%s`, namespace %s", monitorId, namespace), err.Error())
42+
response.Diagnostics.AddError(fmt.Sprintf("Failed to get monitor `%s`, space %s", monitorId, spaceId), err.Error())
4243
return
4344
}
4445

45-
state, diags = state.toModelV0(ctx, result)
46+
state, diags = state.toModelV0(ctx, result, spaceId)
4647
response.Diagnostics.Append(diags...)
4748
if response.Diagnostics.HasError() {
4849
return

internal/kibana/synthetics/schema.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ type tfModelV0 struct {
9999
ID types.String `tfsdk:"id"`
100100
Name types.String `tfsdk:"name"`
101101
SpaceID types.String `tfsdk:"space_id"`
102+
Namespace types.String `tfsdk:"namespace"`
102103
Schedule types.Int64 `tfsdk:"schedule"`
103104
Locations []types.String `tfsdk:"locations"`
104105
PrivateLocations []types.String `tfsdk:"private_locations"`
@@ -143,7 +144,16 @@ func monitorConfigSchema() schema.Schema {
143144
MarkdownDescription: "The monitor’s name.",
144145
},
145146
"space_id": schema.StringAttribute{
146-
MarkdownDescription: "The namespace field should be lowercase and not contain spaces. The namespace must not include any of the following characters: *, \\, /, ?, \", <, >, |, whitespace, ,, #, :, or -. Default: `default`",
147+
MarkdownDescription: "Kibana space. The space ID that is part of the Kibana URL when inside the space. Space IDs are limited to lowercase alphanumeric, underscore, and hyphen characters (a-z, 0-9, _, and -). You are cannot change the ID with the update operation.",
148+
Optional: true,
149+
PlanModifiers: []planmodifier.String{
150+
stringplanmodifier.UseStateForUnknown(),
151+
stringplanmodifier.RequiresReplace(),
152+
},
153+
Computed: true,
154+
},
155+
"namespace": schema.StringAttribute{
156+
MarkdownDescription: "Fleet namespace. The `namespace` field should be lowercase and not contain spaces. The namespace must not include any of the following characters: *, \\, /, ?, \", <, >, |, whitespace, ,, #, :, or -. Default: `default`",
147157
Optional: true,
148158
PlanModifiers: []planmodifier.String{
149159
stringplanmodifier.UseStateForUnknown(),
@@ -566,7 +576,7 @@ func stringToInt64(v string) (int64, error) {
566576
return res, err
567577
}
568578

569-
func (v *tfModelV0) toModelV0(ctx context.Context, api *kbapi.SyntheticsMonitor) (*tfModelV0, diag.Diagnostics) {
579+
func (v *tfModelV0) toModelV0(ctx context.Context, api *kbapi.SyntheticsMonitor, space string) (*tfModelV0, diag.Diagnostics) {
570580
var schedule int64
571581
var err error
572582
dg := diag.Diagnostics{}
@@ -640,7 +650,7 @@ func (v *tfModelV0) toModelV0(ctx context.Context, api *kbapi.SyntheticsMonitor)
640650
}
641651

642652
resourceID := clients.CompositeId{
643-
ClusterId: api.Namespace,
653+
ClusterId: space,
644654
ResourceId: string(api.Id),
645655
}
646656

@@ -652,7 +662,8 @@ func (v *tfModelV0) toModelV0(ctx context.Context, api *kbapi.SyntheticsMonitor)
652662
return &tfModelV0{
653663
ID: types.StringValue(resourceID.String()),
654664
Name: types.StringValue(api.Name),
655-
SpaceID: types.StringValue(api.Namespace),
665+
SpaceID: types.StringValue(space),
666+
Namespace: types.StringValue(api.Namespace),
656667
Schedule: types.Int64Value(schedule),
657668
Locations: v.Locations,
658669
PrivateLocations: StringSliceValue(privateLocLabels),
@@ -883,7 +894,7 @@ func (v *tfModelV0) toSyntheticsMonitorConfig(ctx context.Context) (*kbapi.Synth
883894
Alert: toTFAlertConfig(ctx, v.Alert),
884895
APMServiceName: v.APMServiceName.ValueString(),
885896
TimeoutSeconds: int(v.TimeoutSeconds.ValueInt64()),
886-
Namespace: v.SpaceID.ValueString(),
897+
Namespace: v.Namespace.ValueString(),
887898
Params: params,
888899
RetestOnFailure: v.RetestOnFailure.ValueBoolPointer(),
889900
}, diag.Diagnostics{} //dg

internal/kibana/synthetics/schema_test.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package synthetics
33
import (
44
"context"
55
"encoding/json"
6-
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
76
"testing"
87

8+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
9+
910
"github.com/disaster37/go-kibana-rest/v8/kbapi"
1011
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
1112
"github.com/hashicorp/terraform-plugin-framework/attr"
@@ -49,6 +50,7 @@ func TestToModelV0(t *testing.T) {
4950
ID: types.StringValue("/"),
5051
Name: types.StringValue(""),
5152
SpaceID: types.StringValue(""),
53+
Namespace: types.StringValue(""),
5254
Schedule: types.Int64Value(0),
5355
APMServiceName: types.StringValue(""),
5456
TimeoutSeconds: types.Int64Value(0),
@@ -83,6 +85,7 @@ func TestToModelV0(t *testing.T) {
8385
ID: types.StringValue("/"),
8486
Name: types.StringValue(""),
8587
SpaceID: types.StringValue(""),
88+
Namespace: types.StringValue(""),
8689
Schedule: types.Int64Value(0),
8790
APMServiceName: types.StringValue(""),
8891
TimeoutSeconds: types.Int64Value(0),
@@ -111,6 +114,7 @@ func TestToModelV0(t *testing.T) {
111114
ID: types.StringValue("/"),
112115
Name: types.StringValue(""),
113116
SpaceID: types.StringValue(""),
117+
Namespace: types.StringValue(""),
114118
Schedule: types.Int64Value(0),
115119
APMServiceName: types.StringValue(""),
116120
TimeoutSeconds: types.Int64Value(0),
@@ -130,6 +134,7 @@ func TestToModelV0(t *testing.T) {
130134
ID: types.StringValue("/"),
131135
Name: types.StringValue(""),
132136
SpaceID: types.StringValue(""),
137+
Namespace: types.StringValue(""),
133138
Schedule: types.Int64Value(0),
134139
APMServiceName: types.StringValue(""),
135140
TimeoutSeconds: types.Int64Value(0),
@@ -191,6 +196,7 @@ func TestToModelV0(t *testing.T) {
191196
ID: types.StringValue("default/test-id-http"),
192197
Name: types.StringValue("test-name-http"),
193198
SpaceID: types.StringValue("default"),
199+
Namespace: types.StringValue("default"),
194200
Schedule: types.Int64Value(5),
195201
Locations: []types.String{types.StringValue("us_east")},
196202
PrivateLocations: []types.String{types.StringValue("test private location")},
@@ -229,7 +235,7 @@ func TestToModelV0(t *testing.T) {
229235
input: kbapi.SyntheticsMonitor{
230236
Id: "test-id-tcp",
231237
Name: "test-name-tcp",
232-
Namespace: "default",
238+
Namespace: "default-2",
233239
Enabled: tBool,
234240
Alert: &kbapi.MonitorAlertConfig{Status: &kbapi.SyntheticsStatusConfig{Enabled: tBool}},
235241
Schedule: &kbapi.MonitorScheduleConfig{Number: "5", Unit: "m"},
@@ -261,6 +267,7 @@ func TestToModelV0(t *testing.T) {
261267
ID: types.StringValue("default/test-id-tcp"),
262268
Name: types.StringValue("test-name-tcp"),
263269
SpaceID: types.StringValue("default"),
270+
Namespace: types.StringValue("default-2"),
264271
Schedule: types.Int64Value(5),
265272
Locations: nil,
266273
PrivateLocations: []types.String{types.StringValue("test private location")},
@@ -320,6 +327,7 @@ func TestToModelV0(t *testing.T) {
320327
ID: types.StringValue("default/test-id-icmp"),
321328
Name: types.StringValue("test-name-icmp"),
322329
SpaceID: types.StringValue("default"),
330+
Namespace: types.StringValue("default"),
323331
Schedule: types.Int64Value(5),
324332
Locations: nil,
325333
PrivateLocations: []types.String{types.StringValue("test private location")},
@@ -375,6 +383,7 @@ func TestToModelV0(t *testing.T) {
375383
ID: types.StringValue("default/test-id-browser"),
376384
Name: types.StringValue("test-name-browser"),
377385
SpaceID: types.StringValue("default"),
386+
Namespace: types.StringValue("default"),
378387
Schedule: types.Int64Value(5),
379388
Locations: nil,
380389
PrivateLocations: []types.String{types.StringValue("test private location")},
@@ -398,7 +407,7 @@ func TestToModelV0(t *testing.T) {
398407
for _, tt := range testcases {
399408
t.Run(tt.name, func(t *testing.T) {
400409
ctx := context.Background()
401-
model, diag := tt.expected.toModelV0(ctx, &tt.input)
410+
model, diag := tt.expected.toModelV0(ctx, &tt.input, tt.expected.SpaceID.ValueString())
402411
assert.False(t, diag.HasError())
403412
assert.Equal(t, &tt.expected, model)
404413
})
@@ -457,6 +466,7 @@ func TestToKibanaAPIRequest(t *testing.T) {
457466
ID: types.StringValue("test-id-http"),
458467
Name: types.StringValue("test-name-http"),
459468
SpaceID: types.StringValue("default"),
469+
Namespace: types.StringValue("default-3"),
460470
Schedule: types.Int64Value(5),
461471
Locations: []types.String{types.StringValue("us_east")},
462472
PrivateLocations: []types.String{types.StringValue("test private location")},
@@ -500,7 +510,7 @@ func TestToKibanaAPIRequest(t *testing.T) {
500510
Tags: []string{"tag1", "tag2"},
501511
Alert: &kbapi.MonitorAlertConfig{Status: &kbapi.SyntheticsStatusConfig{Enabled: tBool}, Tls: &kbapi.SyntheticsStatusConfig{Enabled: fBool}},
502512
APMServiceName: "test-service-http",
503-
Namespace: "default",
513+
Namespace: "default-3",
504514
TimeoutSeconds: 30,
505515
Params: kbapi.JsonObject{"param1": "value1"},
506516
},
@@ -533,6 +543,7 @@ func TestToKibanaAPIRequest(t *testing.T) {
533543
ID: types.StringValue("test-id-tcp"),
534544
Name: types.StringValue("test-name-tcp"),
535545
SpaceID: types.StringValue("default"),
546+
Namespace: types.StringValue("default"),
536547
Schedule: types.Int64Value(5),
537548
Locations: []types.String{types.StringValue("us_east")},
538549
PrivateLocations: nil,
@@ -597,6 +608,7 @@ func TestToKibanaAPIRequest(t *testing.T) {
597608
ID: types.StringValue("test-id-icmp"),
598609
Name: types.StringValue("test-name-icmp"),
599610
SpaceID: types.StringValue("default"),
611+
Namespace: types.StringValue("default"),
600612
Schedule: types.Int64Value(5),
601613
Locations: []types.String{types.StringValue("us_east")},
602614
PrivateLocations: nil,
@@ -637,6 +649,7 @@ func TestToKibanaAPIRequest(t *testing.T) {
637649
ID: types.StringValue("test-id-browser"),
638650
Name: types.StringValue("test-name-browser"),
639651
SpaceID: types.StringValue("default"),
652+
Namespace: types.StringValue("default"),
640653
Schedule: types.Int64Value(5),
641654
Locations: []types.String{types.StringValue("us_east")},
642655
PrivateLocations: nil,
@@ -722,6 +735,7 @@ func TestToModelV0MergeAttributes(t *testing.T) {
722735
ID: types.StringValue("/"),
723736
Name: types.StringValue(""),
724737
SpaceID: types.StringValue(""),
738+
Namespace: types.StringValue(""),
725739
Schedule: types.Int64Value(0),
726740
APMServiceName: types.StringValue(""),
727741
TimeoutSeconds: types.Int64Value(0),
@@ -767,6 +781,7 @@ func TestToModelV0MergeAttributes(t *testing.T) {
767781
ID: types.StringValue("/"),
768782
Name: types.StringValue(""),
769783
SpaceID: types.StringValue(""),
784+
Namespace: types.StringValue(""),
770785
Schedule: types.Int64Value(0),
771786
APMServiceName: types.StringValue(""),
772787
TimeoutSeconds: types.Int64Value(0),
@@ -801,6 +816,7 @@ func TestToModelV0MergeAttributes(t *testing.T) {
801816
ID: types.StringValue("/"),
802817
Name: types.StringValue(""),
803818
SpaceID: types.StringValue(""),
819+
Namespace: types.StringValue(""),
804820
Schedule: types.Int64Value(0),
805821
APMServiceName: types.StringValue(""),
806822
TimeoutSeconds: types.Int64Value(0),
@@ -816,7 +832,7 @@ func TestToModelV0MergeAttributes(t *testing.T) {
816832
for _, tt := range testcases {
817833
t.Run(tt.name, func(t *testing.T) {
818834
ctx := context.Background()
819-
actual, diag := tt.state.toModelV0(ctx, &tt.input)
835+
actual, diag := tt.state.toModelV0(ctx, &tt.input, tt.state.SpaceID.ValueString())
820836
assert.False(t, diag.HasError())
821837
assert.NotNil(t, actual)
822838
assert.Equal(t, &tt.expected, actual)

internal/kibana/synthetics/update.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package synthetics
33
import (
44
"context"
55
"fmt"
6+
67
"github.com/disaster37/go-kibana-rest/v8/kbapi"
78
"github.com/hashicorp/terraform-plugin-framework/resource"
89
)
@@ -33,14 +34,14 @@ func (r *Resource) Update(ctx context.Context, request resource.UpdateRequest, r
3334
return
3435
}
3536

36-
namespace := plan.SpaceID.ValueString()
37-
result, err := kibanaClient.KibanaSynthetics.Monitor.Update(ctx, kbapi.MonitorID(monitorId.ResourceId), input.config, input.fields, namespace)
37+
spaceId := plan.SpaceID.ValueString()
38+
result, err := kibanaClient.KibanaSynthetics.Monitor.Update(ctx, kbapi.MonitorID(monitorId.ResourceId), input.config, input.fields, spaceId)
3839
if err != nil {
39-
response.Diagnostics.AddError(fmt.Sprintf("Failed to update Kibana monitor `%s`, namespace %s", input.config.Name, namespace), err.Error())
40+
response.Diagnostics.AddError(fmt.Sprintf("Failed to update Kibana monitor `%s`, space %s", input.config.Name, spaceId), err.Error())
4041
return
4142
}
4243

43-
plan, diags = plan.toModelV0(ctx, result)
44+
plan, diags = plan.toModelV0(ctx, result, spaceId)
4445
response.Diagnostics.Append(diags...)
4546
if response.Diagnostics.HasError() {
4647
return

0 commit comments

Comments
 (0)