Skip to content

Commit 7b9f454

Browse files
authored
remove space_id paramter from private locations (#733)
fix for #726 private location is agnostic to kibana spaces. The field space_id was previously incorrectly used, because its value was a `namespace` of agent policy (data stream). This PR removes the field. As there is no way to update a private location via API (only replace), there is no benefit to introduce `namespace` field for private locations.
1 parent 255c0ec commit 7b9f454

File tree

13 files changed

+50
-84
lines changed

13 files changed

+50
-84
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Fix a provider panic when `elasticstack_kibana_action_connector` reads a non-existant connector ([#729](https://github.com/elastic/terraform-provider-elasticstack/pull/729))
66
- Add support for `remote_indicies` to `elasticstack_elasticsearch_security_role` & `elasticstack_kibana_security_role` (#723)[https://github.com/elastic/terraform-provider-elasticstack/pull/723]
77
- Fix error handling in `elasticstack_kibana_import_saved_objects` ([#738](https://github.com/elastic/terraform-provider-elasticstack/pull/738))
8+
- Remove `space_id` parameter from private locations to fix inconsistent state for `elasticstack_kibana_synthetics_private_location` `space_id` ([#733](https://github.com/elastic/terraform-provider-elasticstack/pull/733))
89

910
## [0.11.6] - 2024-08-20
1011

docs/resources/kibana_synthetics_private_location.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ resource "elasticstack_kibana_synthetics_private_location" "example" {
5252
### Optional
5353

5454
- `geo` (Attributes) Geographic coordinates (WGS84) for the location (see [below for nested schema](#nestedatt--geo))
55-
- `space_id` (String) An identifier for the space. If space_id is not provided, the default space is used.
5655
- `tags` (List of String) An array of tags to categorize the private location.
5756

5857
### Read-Only

internal/kibana/synthetics/acc_pl_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ func TestSyntheticPrivateLocationResource(t *testing.T) {
3939
Config: testConfig("testacc", "test_policy") + `
4040
resource "elasticstack_kibana_synthetics_private_location" "test" {
4141
label = "pl-test-label"
42-
space_id = "testacc"
4342
agent_policy_id = elasticstack_fleet_agent_policy.test_policy.policy_id
4443
tags = ["a", "b"]
4544
geo = {
@@ -50,7 +49,6 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
5049
`,
5150
Check: resource.ComposeAggregateTestCheckFunc(
5251
resource.TestCheckResourceAttr(resourceId, "label", "pl-test-label"),
53-
resource.TestCheckResourceAttr(resourceId, "space_id", "testacc"),
5452
resource.TestCheckResourceAttrSet(resourceId, "agent_policy_id"),
5553
resource.TestCheckResourceAttr(resourceId, "tags.#", "2"),
5654
resource.TestCheckResourceAttr(resourceId, "tags.0", "a"),
@@ -68,7 +66,6 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
6866
Config: testConfig("testacc", "test_policy") + `
6967
resource "elasticstack_kibana_synthetics_private_location" "test" {
7068
label = "pl-test-label"
71-
space_id = "testacc"
7269
agent_policy_id = elasticstack_fleet_agent_policy.test_policy.policy_id
7370
tags = ["a", "b"]
7471
geo = {
@@ -84,7 +81,6 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
8481
Config: testConfig("default", "test_policy_default") + `
8582
resource "elasticstack_kibana_synthetics_private_location" "test" {
8683
label = "pl-test-label-2"
87-
space_id = "default"
8884
agent_policy_id = elasticstack_fleet_agent_policy.test_policy_default.policy_id
8985
tags = ["c", "d", "e"]
9086
geo = {
@@ -95,7 +91,6 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
9591
`,
9692
Check: resource.ComposeAggregateTestCheckFunc(
9793
resource.TestCheckResourceAttr(resourceId, "label", "pl-test-label-2"),
98-
resource.TestCheckResourceAttr(resourceId, "space_id", "default"),
9994
resource.TestCheckResourceAttrSet(resourceId, "agent_policy_id"),
10095
resource.TestCheckResourceAttr(resourceId, "tags.#", "3"),
10196
resource.TestCheckResourceAttr(resourceId, "tags.0", "c"),
@@ -111,13 +106,11 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
111106
Config: testConfig("default", "test_policy_default") + `
112107
resource "elasticstack_kibana_synthetics_private_location" "test" {
113108
label = "pl-test-label-2"
114-
space_id = "default"
115109
agent_policy_id = elasticstack_fleet_agent_policy.test_policy_default.policy_id
116110
}
117111
`,
118112
Check: resource.ComposeAggregateTestCheckFunc(
119113
resource.TestCheckResourceAttr(resourceId, "label", "pl-test-label-2"),
120-
resource.TestCheckResourceAttr(resourceId, "space_id", "default"),
121114
resource.TestCheckResourceAttrSet(resourceId, "agent_policy_id"),
122115
resource.TestCheckNoResourceAttr(resourceId, "tags"),
123116
resource.TestCheckNoResourceAttr(resourceId, "geo"),
@@ -129,14 +122,12 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
129122
Config: testConfig("default", "test_policy_default") + `
130123
resource "elasticstack_kibana_synthetics_private_location" "test" {
131124
label = "pl-test-label-2"
132-
space_id = "default"
133125
agent_policy_id = elasticstack_fleet_agent_policy.test_policy_default.policy_id
134126
tags = ["c", "d", "e"]
135127
}
136128
`,
137129
Check: resource.ComposeAggregateTestCheckFunc(
138130
resource.TestCheckResourceAttr(resourceId, "label", "pl-test-label-2"),
139-
resource.TestCheckResourceAttr(resourceId, "space_id", "default"),
140131
resource.TestCheckResourceAttrSet(resourceId, "agent_policy_id"),
141132
resource.TestCheckResourceAttr(resourceId, "tags.#", "3"),
142133
resource.TestCheckResourceAttr(resourceId, "tags.0", "c"),
@@ -151,7 +142,6 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
151142
Config: testConfig("default", "test_policy_default") + `
152143
resource "elasticstack_kibana_synthetics_private_location" "test" {
153144
label = "pl-test-label-2"
154-
space_id = "default"
155145
agent_policy_id = elasticstack_fleet_agent_policy.test_policy_default.policy_id
156146
geo = {
157147
lat = -33.21
@@ -161,7 +151,6 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
161151
`,
162152
Check: resource.ComposeAggregateTestCheckFunc(
163153
resource.TestCheckResourceAttr(resourceId, "label", "pl-test-label-2"),
164-
resource.TestCheckResourceAttr(resourceId, "space_id", "default"),
165154
resource.TestCheckResourceAttrSet(resourceId, "agent_policy_id"),
166155
resource.TestCheckNoResourceAttr(resourceId, "tags"),
167156
resource.TestCheckResourceAttr(resourceId, "geo.lat", "-33.21"),

internal/kibana/synthetics/acc_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ resource "elasticstack_fleet_agent_policy" "%s" {
378378
379379
resource "elasticstack_kibana_synthetics_private_location" "%s" {
380380
label = "monitor-pll-%s"
381-
space_id = "testacc"
382381
agent_policy_id = elasticstack_fleet_agent_policy.%s.policy_id
383382
}
384383
`, agentPolicyId, name, privateLocationId, name, agentPolicyId)

internal/kibana/synthetics/private_location/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package private_location
33
import (
44
"context"
55
"fmt"
6+
67
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
78
"github.com/hashicorp/terraform-plugin-framework/resource"
89
)
@@ -23,10 +24,9 @@ func (r *Resource) Create(ctx context.Context, request resource.CreateRequest, r
2324

2425
input := plan.toPrivateLocationConfig()
2526

26-
namespace := plan.SpaceID.ValueString()
27-
result, err := kibanaClient.KibanaSynthetics.PrivateLocation.Create(ctx, input, namespace)
27+
result, err := kibanaClient.KibanaSynthetics.PrivateLocation.Create(ctx, input)
2828
if err != nil {
29-
response.Diagnostics.AddError(fmt.Sprintf("Failed to create private location `%s`, namespace %s", input.Label, namespace), err.Error())
29+
response.Diagnostics.AddError(fmt.Sprintf("Failed to create private location `%s`", input.Label), err.Error())
3030
return
3131
}
3232

internal/kibana/synthetics/private_location/delete.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package private_location
33
import (
44
"context"
55
"fmt"
6+
67
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
78
"github.com/hashicorp/terraform-plugin-framework/resource"
89
)
@@ -22,7 +23,6 @@ func (r *Resource) Delete(ctx context.Context, request resource.DeleteRequest, r
2223
}
2324

2425
resourceId := plan.ID.ValueString()
25-
namespace := plan.SpaceID.ValueString()
2626

2727
compositeId, dg := tryReadCompositeId(resourceId)
2828
response.Diagnostics.Append(dg...)
@@ -32,13 +32,12 @@ func (r *Resource) Delete(ctx context.Context, request resource.DeleteRequest, r
3232

3333
if compositeId != nil {
3434
resourceId = compositeId.ResourceId
35-
namespace = compositeId.ClusterId
3635
}
3736

38-
err := kibanaClient.KibanaSynthetics.PrivateLocation.Delete(ctx, resourceId, namespace)
37+
err := kibanaClient.KibanaSynthetics.PrivateLocation.Delete(ctx, resourceId)
3938

4039
if err != nil {
41-
response.Diagnostics.AddError(fmt.Sprintf("Failed to delete private location `%s`, namespace %s", resourceId, namespace), err.Error())
40+
response.Diagnostics.AddError(fmt.Sprintf("Failed to delete private location `%s`", resourceId), err.Error())
4241
return
4342
}
4443

internal/kibana/synthetics/private_location/read.go

Lines changed: 3 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/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
910
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -24,7 +25,6 @@ func (r *Resource) Read(ctx context.Context, request resource.ReadRequest, respo
2425
}
2526

2627
resourceId := state.ID.ValueString()
27-
namespace := state.SpaceID.ValueString()
2828

2929
compositeId, dg := tryReadCompositeId(resourceId)
3030
response.Diagnostics.Append(dg...)
@@ -34,18 +34,17 @@ func (r *Resource) Read(ctx context.Context, request resource.ReadRequest, respo
3434

3535
if compositeId != nil {
3636
resourceId = compositeId.ResourceId
37-
namespace = compositeId.ClusterId
3837
}
3938

40-
result, err := kibanaClient.KibanaSynthetics.PrivateLocation.Get(ctx, resourceId, namespace)
39+
result, err := kibanaClient.KibanaSynthetics.PrivateLocation.Get(ctx, resourceId)
4140
if err != nil {
4241
var apiError *kbapi.APIError
4342
if errors.As(err, &apiError) && apiError.Code == 404 {
4443
response.State.RemoveResource(ctx)
4544
return
4645
}
4746

48-
response.Diagnostics.AddError(fmt.Sprintf("Failed to get private location `%s`, namespace %s", resourceId, namespace), err.Error())
47+
response.Diagnostics.AddError(fmt.Sprintf("Failed to get private location `%s`", resourceId), err.Error())
4948
return
5049
}
5150

internal/kibana/synthetics/private_location/resource.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package private_location
22

33
import (
44
"context"
5+
56
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
67
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
78
"github.com/hashicorp/terraform-plugin-framework/path"

internal/kibana/synthetics/private_location/schema.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package private_location
22

33
import (
4+
"strings"
5+
46
"github.com/disaster37/go-kibana-rest/v8/kbapi"
57
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
68
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
@@ -10,13 +12,11 @@ import (
1012
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1113
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1214
"github.com/hashicorp/terraform-plugin-framework/types"
13-
"strings"
1415
)
1516

1617
type tfModelV0 struct {
1718
ID types.String `tfsdk:"id"`
1819
Label types.String `tfsdk:"label"`
19-
SpaceID types.String `tfsdk:"space_id"`
2020
AgentPolicyId types.String `tfsdk:"agent_policy_id"`
2121
Tags []types.String `tfsdk:"tags"` //> string
2222
Geo *synthetics.TFGeoConfigV0 `tfsdk:"geo"`
@@ -34,14 +34,6 @@ func privateLocationSchema() schema.Schema {
3434
stringplanmodifier.RequiresReplace(),
3535
},
3636
},
37-
"space_id": schema.StringAttribute{
38-
MarkdownDescription: "An identifier for the space. If space_id is not provided, the default space is used.",
39-
Optional: true,
40-
PlanModifiers: []planmodifier.String{
41-
stringplanmodifier.UseStateForUnknown(),
42-
stringplanmodifier.RequiresReplace(),
43-
},
44-
},
4537
"label": schema.StringAttribute{
4638
Optional: false,
4739
Required: true,
@@ -98,15 +90,9 @@ func tryReadCompositeId(id string) (*clients.CompositeId, diag.Diagnostics) {
9890

9991
func toModelV0(pLoc kbapi.PrivateLocation) tfModelV0 {
10092

101-
resourceID := clients.CompositeId{
102-
ClusterId: pLoc.Namespace,
103-
ResourceId: pLoc.Id,
104-
}
105-
10693
return tfModelV0{
107-
ID: types.StringValue(resourceID.String()),
94+
ID: types.StringValue(pLoc.Id),
10895
Label: types.StringValue(pLoc.Label),
109-
SpaceID: types.StringValue(pLoc.Namespace),
11096
AgentPolicyId: types.StringValue(pLoc.AgentPolicyId),
11197
Tags: synthetics.StringSliceValue(pLoc.Tags),
11298
Geo: synthetics.FromSyntheticGeoConfig(pLoc.Geo),

internal/kibana/synthetics/private_location/schema_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package private_location
22

33
import (
4-
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
54
"testing"
65

76
"github.com/disaster37/go-kibana-rest/v8/kbapi"
@@ -73,14 +72,8 @@ func Test_roundtrip(t *testing.T) {
7372
}
7473
modelV0 := toModelV0(input)
7574

76-
compositeId, _ := synthetics.GetCompositeId(modelV0.ID.ValueString())
77-
78-
actual := kbapi.PrivateLocation{
79-
Id: compositeId.ResourceId,
80-
Namespace: modelV0.SpaceID.ValueString(),
81-
PrivateLocationConfig: modelV0.toPrivateLocationConfig(),
82-
}
83-
assert.Equal(t, input, actual)
75+
actual := modelV0.toPrivateLocationConfig()
76+
assert.Equal(t, plc, actual)
8477
})
8578
}
8679
}

libs/go-kibana-rest/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
14+
15+
vendor/

libs/go-kibana-rest/kbapi/api.kibana_synthetics.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,14 @@ type KibanaSyntheticsMonitorGet func(ctx context.Context, id MonitorID, namespac
256256

257257
type KibanaSyntheticsMonitorDelete func(ctx context.Context, namespace string, ids ...MonitorID) ([]MonitorDeleteStatus, error)
258258

259-
type KibanaSyntheticsPrivateLocationCreate func(ctx context.Context, pLoc PrivateLocationConfig, namespace string) (*PrivateLocation, error)
259+
type KibanaSyntheticsPrivateLocationCreate func(ctx context.Context, pLoc PrivateLocationConfig) (*PrivateLocation, error)
260260

261-
type KibanaSyntheticsPrivateLocationGet func(ctx context.Context, idOrLabel string, namespace string) (*PrivateLocation, error)
261+
type KibanaSyntheticsPrivateLocationGet func(ctx context.Context, idOrLabel string) (*PrivateLocation, error)
262262

263-
type KibanaSyntheticsPrivateLocationDelete func(ctx context.Context, id string, namespace string) error
263+
type KibanaSyntheticsPrivateLocationDelete func(ctx context.Context, id string) error
264264

265265
func newKibanaSyntheticsPrivateLocationGetFunc(c *resty.Client) KibanaSyntheticsPrivateLocationGet {
266-
return func(ctx context.Context, idOrLabel string, _ string) (*PrivateLocation, error) {
266+
return func(ctx context.Context, idOrLabel string) (*PrivateLocation, error) {
267267
if idOrLabel == "" {
268268
return nil, APIError{
269269
Code: 404,
@@ -282,11 +282,11 @@ func newKibanaSyntheticsPrivateLocationGetFunc(c *resty.Client) KibanaSynthetics
282282
}
283283

284284
func newKibanaSyntheticsPrivateLocationCreateFunc(c *resty.Client) KibanaSyntheticsPrivateLocationCreate {
285-
return func(ctx context.Context, pLoc PrivateLocationConfig, namespace string) (*PrivateLocation, error) {
285+
return func(ctx context.Context, pLoc PrivateLocationConfig) (*PrivateLocation, error) {
286286
plMu.Lock()
287287
defer plMu.Unlock()
288288

289-
path := basePath(namespace, privateLocationsSuffix)
289+
path := basePath("", privateLocationsSuffix)
290290
log.Debugf("URL to create private locations: %s", path)
291291
resp, err := c.R().SetContext(ctx).SetBody(pLoc).Post(path)
292292
if err = handleKibanaError(err, resp); err != nil {
@@ -297,11 +297,11 @@ func newKibanaSyntheticsPrivateLocationCreateFunc(c *resty.Client) KibanaSynthet
297297
}
298298

299299
func newKibanaSyntheticsPrivateLocationDeleteFunc(c *resty.Client) KibanaSyntheticsPrivateLocationDelete {
300-
return func(ctx context.Context, id string, namespace string) error {
300+
return func(ctx context.Context, id string) error {
301301
plMu.Lock()
302302
defer plMu.Unlock()
303303

304-
path := basePathWithId(namespace, privateLocationsSuffix, id)
304+
path := basePathWithId("", privateLocationsSuffix, id)
305305
log.Debugf("URL to delete private locations: %s", path)
306306
resp, err := c.R().SetContext(ctx).Delete(path)
307307
err = handleKibanaError(err, resp)

0 commit comments

Comments
 (0)