Skip to content

remove space_id parameter from private locations #733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fix a provider panic when `elasticstack_kibana_action_connector` reads a non-existant connector ([#729](https://github.com/elastic/terraform-provider-elasticstack/pull/729))
- Add support for `remote_indicies` to `elasticstack_elasticsearch_security_role` & `elasticstack_kibana_security_role` (#723)[https://github.com/elastic/terraform-provider-elasticstack/pull/723]
- Fix error handling in `elasticstack_kibana_import_saved_objects` ([#738](https://github.com/elastic/terraform-provider-elasticstack/pull/738))
- 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))

## [0.11.6] - 2024-08-20

Expand Down
1 change: 0 additions & 1 deletion docs/resources/kibana_synthetics_private_location.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ resource "elasticstack_kibana_synthetics_private_location" "example" {
### Optional

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

### Read-Only
Expand Down
11 changes: 0 additions & 11 deletions internal/kibana/synthetics/acc_pl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func TestSyntheticPrivateLocationResource(t *testing.T) {
Config: testConfig("testacc", "test_policy") + `
resource "elasticstack_kibana_synthetics_private_location" "test" {
label = "pl-test-label"
space_id = "testacc"
agent_policy_id = elasticstack_fleet_agent_policy.test_policy.policy_id
tags = ["a", "b"]
geo = {
Expand All @@ -50,7 +49,6 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceId, "label", "pl-test-label"),
resource.TestCheckResourceAttr(resourceId, "space_id", "testacc"),
resource.TestCheckResourceAttrSet(resourceId, "agent_policy_id"),
resource.TestCheckResourceAttr(resourceId, "tags.#", "2"),
resource.TestCheckResourceAttr(resourceId, "tags.0", "a"),
Expand All @@ -68,7 +66,6 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
Config: testConfig("testacc", "test_policy") + `
resource "elasticstack_kibana_synthetics_private_location" "test" {
label = "pl-test-label"
space_id = "testacc"
agent_policy_id = elasticstack_fleet_agent_policy.test_policy.policy_id
tags = ["a", "b"]
geo = {
Expand All @@ -84,7 +81,6 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
Config: testConfig("default", "test_policy_default") + `
resource "elasticstack_kibana_synthetics_private_location" "test" {
label = "pl-test-label-2"
space_id = "default"
agent_policy_id = elasticstack_fleet_agent_policy.test_policy_default.policy_id
tags = ["c", "d", "e"]
geo = {
Expand All @@ -95,7 +91,6 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceId, "label", "pl-test-label-2"),
resource.TestCheckResourceAttr(resourceId, "space_id", "default"),
resource.TestCheckResourceAttrSet(resourceId, "agent_policy_id"),
resource.TestCheckResourceAttr(resourceId, "tags.#", "3"),
resource.TestCheckResourceAttr(resourceId, "tags.0", "c"),
Expand All @@ -111,13 +106,11 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
Config: testConfig("default", "test_policy_default") + `
resource "elasticstack_kibana_synthetics_private_location" "test" {
label = "pl-test-label-2"
space_id = "default"
agent_policy_id = elasticstack_fleet_agent_policy.test_policy_default.policy_id
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceId, "label", "pl-test-label-2"),
resource.TestCheckResourceAttr(resourceId, "space_id", "default"),
resource.TestCheckResourceAttrSet(resourceId, "agent_policy_id"),
resource.TestCheckNoResourceAttr(resourceId, "tags"),
resource.TestCheckNoResourceAttr(resourceId, "geo"),
Expand All @@ -129,14 +122,12 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
Config: testConfig("default", "test_policy_default") + `
resource "elasticstack_kibana_synthetics_private_location" "test" {
label = "pl-test-label-2"
space_id = "default"
agent_policy_id = elasticstack_fleet_agent_policy.test_policy_default.policy_id
tags = ["c", "d", "e"]
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceId, "label", "pl-test-label-2"),
resource.TestCheckResourceAttr(resourceId, "space_id", "default"),
resource.TestCheckResourceAttrSet(resourceId, "agent_policy_id"),
resource.TestCheckResourceAttr(resourceId, "tags.#", "3"),
resource.TestCheckResourceAttr(resourceId, "tags.0", "c"),
Expand All @@ -151,7 +142,6 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
Config: testConfig("default", "test_policy_default") + `
resource "elasticstack_kibana_synthetics_private_location" "test" {
label = "pl-test-label-2"
space_id = "default"
agent_policy_id = elasticstack_fleet_agent_policy.test_policy_default.policy_id
geo = {
lat = -33.21
Expand All @@ -161,7 +151,6 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceId, "label", "pl-test-label-2"),
resource.TestCheckResourceAttr(resourceId, "space_id", "default"),
resource.TestCheckResourceAttrSet(resourceId, "agent_policy_id"),
resource.TestCheckNoResourceAttr(resourceId, "tags"),
resource.TestCheckResourceAttr(resourceId, "geo.lat", "-33.21"),
Expand Down
1 change: 0 additions & 1 deletion internal/kibana/synthetics/acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ resource "elasticstack_fleet_agent_policy" "%s" {

resource "elasticstack_kibana_synthetics_private_location" "%s" {
label = "monitor-pll-%s"
space_id = "testacc"
agent_policy_id = elasticstack_fleet_agent_policy.%s.policy_id
}
`, agentPolicyId, name, privateLocationId, name, agentPolicyId)
Expand Down
6 changes: 3 additions & 3 deletions internal/kibana/synthetics/private_location/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package private_location
import (
"context"
"fmt"

"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
"github.com/hashicorp/terraform-plugin-framework/resource"
)
Expand All @@ -23,10 +24,9 @@ func (r *Resource) Create(ctx context.Context, request resource.CreateRequest, r

input := plan.toPrivateLocationConfig()

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

Expand Down
7 changes: 3 additions & 4 deletions internal/kibana/synthetics/private_location/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package private_location
import (
"context"
"fmt"

"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
"github.com/hashicorp/terraform-plugin-framework/resource"
)
Expand All @@ -22,7 +23,6 @@ func (r *Resource) Delete(ctx context.Context, request resource.DeleteRequest, r
}

resourceId := plan.ID.ValueString()
namespace := plan.SpaceID.ValueString()

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

if compositeId != nil {
resourceId = compositeId.ResourceId
namespace = compositeId.ClusterId
}

err := kibanaClient.KibanaSynthetics.PrivateLocation.Delete(ctx, resourceId, namespace)
err := kibanaClient.KibanaSynthetics.PrivateLocation.Delete(ctx, resourceId)

if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("Failed to delete private location `%s`, namespace %s", resourceId, namespace), err.Error())
response.Diagnostics.AddError(fmt.Sprintf("Failed to delete private location `%s`", resourceId), err.Error())
return
}

Expand Down
7 changes: 3 additions & 4 deletions internal/kibana/synthetics/private_location/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"

"github.com/disaster37/go-kibana-rest/v8/kbapi"
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -24,7 +25,6 @@ func (r *Resource) Read(ctx context.Context, request resource.ReadRequest, respo
}

resourceId := state.ID.ValueString()
namespace := state.SpaceID.ValueString()

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

if compositeId != nil {
resourceId = compositeId.ResourceId
namespace = compositeId.ClusterId
}

result, err := kibanaClient.KibanaSynthetics.PrivateLocation.Get(ctx, resourceId, namespace)
result, err := kibanaClient.KibanaSynthetics.PrivateLocation.Get(ctx, resourceId)
if err != nil {
var apiError *kbapi.APIError
if errors.As(err, &apiError) && apiError.Code == 404 {
response.State.RemoveResource(ctx)
return
}

response.Diagnostics.AddError(fmt.Sprintf("Failed to get private location `%s`, namespace %s", resourceId, namespace), err.Error())
response.Diagnostics.AddError(fmt.Sprintf("Failed to get private location `%s`", resourceId), err.Error())
return
}

Expand Down
1 change: 1 addition & 0 deletions internal/kibana/synthetics/private_location/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package private_location

import (
"context"

"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down
20 changes: 3 additions & 17 deletions internal/kibana/synthetics/private_location/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package private_location

import (
"strings"

"github.com/disaster37/go-kibana-rest/v8/kbapi"
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
Expand All @@ -10,13 +12,11 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"strings"
)

type tfModelV0 struct {
ID types.String `tfsdk:"id"`
Label types.String `tfsdk:"label"`
SpaceID types.String `tfsdk:"space_id"`
AgentPolicyId types.String `tfsdk:"agent_policy_id"`
Tags []types.String `tfsdk:"tags"` //> string
Geo *synthetics.TFGeoConfigV0 `tfsdk:"geo"`
Expand All @@ -34,14 +34,6 @@ func privateLocationSchema() schema.Schema {
stringplanmodifier.RequiresReplace(),
},
},
"space_id": schema.StringAttribute{
MarkdownDescription: "An identifier for the space. If space_id is not provided, the default space is used.",
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
stringplanmodifier.RequiresReplace(),
},
},
"label": schema.StringAttribute{
Optional: false,
Required: true,
Expand Down Expand Up @@ -98,15 +90,9 @@ func tryReadCompositeId(id string) (*clients.CompositeId, diag.Diagnostics) {

func toModelV0(pLoc kbapi.PrivateLocation) tfModelV0 {

resourceID := clients.CompositeId{
ClusterId: pLoc.Namespace,
ResourceId: pLoc.Id,
}

return tfModelV0{
ID: types.StringValue(resourceID.String()),
ID: types.StringValue(pLoc.Id),
Label: types.StringValue(pLoc.Label),
SpaceID: types.StringValue(pLoc.Namespace),
AgentPolicyId: types.StringValue(pLoc.AgentPolicyId),
Tags: synthetics.StringSliceValue(pLoc.Tags),
Geo: synthetics.FromSyntheticGeoConfig(pLoc.Geo),
Expand Down
11 changes: 2 additions & 9 deletions internal/kibana/synthetics/private_location/schema_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package private_location

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

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

compositeId, _ := synthetics.GetCompositeId(modelV0.ID.ValueString())

actual := kbapi.PrivateLocation{
Id: compositeId.ResourceId,
Namespace: modelV0.SpaceID.ValueString(),
PrivateLocationConfig: modelV0.toPrivateLocationConfig(),
}
assert.Equal(t, input, actual)
actual := modelV0.toPrivateLocationConfig()
assert.Equal(t, plc, actual)
})
}
}
3 changes: 3 additions & 0 deletions libs/go-kibana-rest/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out


vendor/
16 changes: 8 additions & 8 deletions libs/go-kibana-rest/kbapi/api.kibana_synthetics.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,14 @@ type KibanaSyntheticsMonitorGet func(ctx context.Context, id MonitorID, namespac

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

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

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

type KibanaSyntheticsPrivateLocationDelete func(ctx context.Context, id string, namespace string) error
type KibanaSyntheticsPrivateLocationDelete func(ctx context.Context, id string) error

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

func newKibanaSyntheticsPrivateLocationCreateFunc(c *resty.Client) KibanaSyntheticsPrivateLocationCreate {
return func(ctx context.Context, pLoc PrivateLocationConfig, namespace string) (*PrivateLocation, error) {
return func(ctx context.Context, pLoc PrivateLocationConfig) (*PrivateLocation, error) {
plMu.Lock()
defer plMu.Unlock()

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

func newKibanaSyntheticsPrivateLocationDeleteFunc(c *resty.Client) KibanaSyntheticsPrivateLocationDelete {
return func(ctx context.Context, id string, namespace string) error {
return func(ctx context.Context, id string) error {
plMu.Lock()
defer plMu.Unlock()

path := basePathWithId(namespace, privateLocationsSuffix, id)
path := basePathWithId("", privateLocationsSuffix, id)
log.Debugf("URL to delete private locations: %s", path)
resp, err := c.R().SetContext(ctx).Delete(path)
err = handleKibanaError(err, resp)
Expand Down
Loading
Loading