diff --git a/CHANGELOG.md b/CHANGELOG.md index c075572b5..e638f5149 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/resources/kibana_synthetics_private_location.md b/docs/resources/kibana_synthetics_private_location.md index 527554a61..13b65f930 100644 --- a/docs/resources/kibana_synthetics_private_location.md +++ b/docs/resources/kibana_synthetics_private_location.md @@ -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 diff --git a/internal/kibana/synthetics/acc_pl_test.go b/internal/kibana/synthetics/acc_pl_test.go index f4279a525..012b04cfd 100644 --- a/internal/kibana/synthetics/acc_pl_test.go +++ b/internal/kibana/synthetics/acc_pl_test.go @@ -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 = { @@ -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"), @@ -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 = { @@ -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 = { @@ -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"), @@ -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"), @@ -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"), @@ -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 @@ -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"), diff --git a/internal/kibana/synthetics/acc_test.go b/internal/kibana/synthetics/acc_test.go index dce29e5fb..487353d4e 100644 --- a/internal/kibana/synthetics/acc_test.go +++ b/internal/kibana/synthetics/acc_test.go @@ -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) diff --git a/internal/kibana/synthetics/private_location/create.go b/internal/kibana/synthetics/private_location/create.go index 7fd4c8a34..e52c06a98 100644 --- a/internal/kibana/synthetics/private_location/create.go +++ b/internal/kibana/synthetics/private_location/create.go @@ -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" ) @@ -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 } diff --git a/internal/kibana/synthetics/private_location/delete.go b/internal/kibana/synthetics/private_location/delete.go index 34cf9a097..25cd5426d 100644 --- a/internal/kibana/synthetics/private_location/delete.go +++ b/internal/kibana/synthetics/private_location/delete.go @@ -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" ) @@ -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...) @@ -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 } diff --git a/internal/kibana/synthetics/private_location/read.go b/internal/kibana/synthetics/private_location/read.go index dcd004d2d..10f8045cc 100644 --- a/internal/kibana/synthetics/private_location/read.go +++ b/internal/kibana/synthetics/private_location/read.go @@ -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" @@ -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...) @@ -34,10 +34,9 @@ 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 { @@ -45,7 +44,7 @@ func (r *Resource) Read(ctx context.Context, request resource.ReadRequest, respo 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 } diff --git a/internal/kibana/synthetics/private_location/resource.go b/internal/kibana/synthetics/private_location/resource.go index c4023e7de..a66f2c3f8 100644 --- a/internal/kibana/synthetics/private_location/resource.go +++ b/internal/kibana/synthetics/private_location/resource.go @@ -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" diff --git a/internal/kibana/synthetics/private_location/schema.go b/internal/kibana/synthetics/private_location/schema.go index 6a78942c5..26e31b1fb 100644 --- a/internal/kibana/synthetics/private_location/schema.go +++ b/internal/kibana/synthetics/private_location/schema.go @@ -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" @@ -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"` @@ -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, @@ -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), diff --git a/internal/kibana/synthetics/private_location/schema_test.go b/internal/kibana/synthetics/private_location/schema_test.go index 60ba3b047..25d466421 100644 --- a/internal/kibana/synthetics/private_location/schema_test.go +++ b/internal/kibana/synthetics/private_location/schema_test.go @@ -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" @@ -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) }) } } diff --git a/libs/go-kibana-rest/.gitignore b/libs/go-kibana-rest/.gitignore index f1c181ec9..f2b36326b 100644 --- a/libs/go-kibana-rest/.gitignore +++ b/libs/go-kibana-rest/.gitignore @@ -10,3 +10,6 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out + + +vendor/ \ No newline at end of file diff --git a/libs/go-kibana-rest/kbapi/api.kibana_synthetics.go b/libs/go-kibana-rest/kbapi/api.kibana_synthetics.go index 578cae340..54a2f3404 100644 --- a/libs/go-kibana-rest/kbapi/api.kibana_synthetics.go +++ b/libs/go-kibana-rest/kbapi/api.kibana_synthetics.go @@ -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, @@ -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 { @@ -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) diff --git a/libs/go-kibana-rest/kbapi/api.kibana_synthetics_test.go b/libs/go-kibana-rest/kbapi/api.kibana_synthetics_test.go index 265057a8a..351c39e5e 100644 --- a/libs/go-kibana-rest/kbapi/api.kibana_synthetics_test.go +++ b/libs/go-kibana-rest/kbapi/api.kibana_synthetics_test.go @@ -13,7 +13,7 @@ import ( ) var ( - namespaces = []string{"", "default", "testacc"} + spaces = []string{"", "default", "testacc"} ) func testWithPolicy(t *testing.T, client *resty.Client, namespace string, f func(policyId string)) { @@ -62,7 +62,7 @@ func (s *KBAPITestSuite) TestKibanaSyntheticsMonitorAPI() { ctx := context.Background() - for _, n := range namespaces { + for _, n := range spaces { testUuid := uuid.New().String() space := n syntheticsAPI := s.API.KibanaSynthetics @@ -72,10 +72,10 @@ func (s *KBAPITestSuite) TestKibanaSyntheticsMonitorAPI() { Label: fmt.Sprintf("TestKibanaSyntheticsMonitorAdd %s", testUuid), AgentPolicyId: policyId, } - location, err := syntheticsAPI.PrivateLocation.Create(ctx, locationConfig, space) + location, err := syntheticsAPI.PrivateLocation.Create(ctx, locationConfig) assert.NoError(s.T(), err) defer func(id string) { - syntheticsAPI.PrivateLocation.Delete(ctx, id, space) + syntheticsAPI.PrivateLocation.Delete(ctx, id) }(location.Id) f := new(bool) @@ -304,13 +304,13 @@ func (s *KBAPITestSuite) TestKibanaSyntheticsPrivateLocationAPI() { ctx := context.Background() - for _, n := range namespaces { + for _, n := range spaces { testUuid := uuid.New().String() - space := n + namespace := n pAPI := s.API.KibanaSynthetics.PrivateLocation s.Run(fmt.Sprintf("TestKibanaSyntheticsPrivateLocationAPI - %s", n), func() { - testWithPolicy(s.T(), s.client, space, func(policyId string) { + testWithPolicy(s.T(), s.client, namespace, func(policyId string) { cfg := PrivateLocationConfig{ Label: fmt.Sprintf("TestKibanaSyntheticsPrivateLocationAPI-%s", testUuid), @@ -321,24 +321,24 @@ func (s *KBAPITestSuite) TestKibanaSyntheticsPrivateLocationAPI() { Lon: -42.42, }, } - created, err := pAPI.Create(ctx, cfg, space) + created, err := pAPI.Create(ctx, cfg) assert.NoError(s.T(), err) assert.Equal(s.T(), created.Label, cfg.Label) assert.Equal(s.T(), created.AgentPolicyId, cfg.AgentPolicyId) - get, err := pAPI.Get(ctx, created.Id, space) + get, err := pAPI.Get(ctx, created.Id) assert.NoError(s.T(), err) assert.Equal(s.T(), created, get) - get, err = pAPI.Get(ctx, created.Label, space) + get, err = pAPI.Get(ctx, created.Label) assert.NoError(s.T(), err) assert.Equal(s.T(), created, get) - err = pAPI.Delete(ctx, created.Id, space) + err = pAPI.Delete(ctx, created.Id) assert.NoError(s.T(), err) - _, err = pAPI.Get(ctx, created.Id, space) + _, err = pAPI.Get(ctx, created.Id) assert.Error(s.T(), err) }) }) @@ -346,21 +346,18 @@ func (s *KBAPITestSuite) TestKibanaSyntheticsPrivateLocationAPI() { } func (s *KBAPITestSuite) TestKibanaSyntheticsPrivateLocationNotFound() { - for _, n := range namespaces { - testUuid := uuid.New().String() - space := n - pAPI := s.API.KibanaSynthetics.PrivateLocation + testUuid := uuid.New().String() + pAPI := s.API.KibanaSynthetics.PrivateLocation - ids := []string{"", "not-found", testUuid} - ctx := context.Background() + ids := []string{"", "not-found", testUuid} + ctx := context.Background() - for _, id := range ids { - s.Run(fmt.Sprintf("TestKibanaSyntheticsPrivateLocationNotFound - %s - %s", n, id), func() { - _, err := pAPI.Get(ctx, id, space) - assert.Error(s.T(), err) - assert.IsType(s.T(), APIError{}, err) - assert.Equal(s.T(), 404, err.(APIError).Code) - }) - } + for _, id := range ids { + s.Run(fmt.Sprintf("TestKibanaSyntheticsPrivateLocationNotFound - %s", id), func() { + _, err := pAPI.Get(ctx, id) + assert.Error(s.T(), err) + assert.IsType(s.T(), APIError{}, err) + assert.Equal(s.T(), 404, err.(APIError).Code) + }) } }