diff --git a/internal/kibana/synthetics/private_location/acc_test.go b/internal/kibana/synthetics/acc_pl_test.go similarity index 92% rename from internal/kibana/synthetics/private_location/acc_test.go rename to internal/kibana/synthetics/acc_pl_test.go index 57995d7f5..f4279a525 100644 --- a/internal/kibana/synthetics/private_location/acc_test.go +++ b/internal/kibana/synthetics/acc_pl_test.go @@ -1,4 +1,7 @@ -package private_location_test +package synthetics_test + +// this test is in synthetics_test package, because of https://github.com/elastic/kibana/issues/190801 +// having both tests in same package allows to use mutex in kibana API client and workaround the issue import ( "fmt" @@ -21,10 +24,10 @@ provider "elasticstack" { ) var ( - minKibanaVersion = version.Must(version.NewVersion("8.12.0")) + minKibanaPrivateLocationAPIVersion = version.Must(version.NewVersion("8.12.0")) ) -func TestPrivateLocationResource(t *testing.T) { +func TestSyntheticPrivateLocationResource(t *testing.T) { resourceId := "elasticstack_kibana_synthetics_private_location.test" resource.Test(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(t) }, @@ -32,7 +35,7 @@ func TestPrivateLocationResource(t *testing.T) { Steps: []resource.TestStep{ // Create and Read testing { - SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaVersion), + SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaPrivateLocationAPIVersion), Config: testConfig("testacc", "test_policy") + ` resource "elasticstack_kibana_synthetics_private_location" "test" { label = "pl-test-label" @@ -58,7 +61,7 @@ resource "elasticstack_kibana_synthetics_private_location" "test" { }, // ImportState testing { - SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaVersion), + SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaPrivateLocationAPIVersion), ResourceName: resourceId, ImportState: true, ImportStateVerify: true, @@ -77,7 +80,7 @@ resource "elasticstack_kibana_synthetics_private_location" "test" { }, // Update and Read testing { - SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaVersion), + SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaPrivateLocationAPIVersion), Config: testConfig("default", "test_policy_default") + ` resource "elasticstack_kibana_synthetics_private_location" "test" { label = "pl-test-label-2" @@ -104,7 +107,7 @@ resource "elasticstack_kibana_synthetics_private_location" "test" { }, // Update and Read testing { - SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaVersion), + SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaPrivateLocationAPIVersion), Config: testConfig("default", "test_policy_default") + ` resource "elasticstack_kibana_synthetics_private_location" "test" { label = "pl-test-label-2" @@ -122,7 +125,7 @@ resource "elasticstack_kibana_synthetics_private_location" "test" { }, // Update and Read testing { - SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaVersion), + SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaPrivateLocationAPIVersion), Config: testConfig("default", "test_policy_default") + ` resource "elasticstack_kibana_synthetics_private_location" "test" { label = "pl-test-label-2" @@ -144,7 +147,7 @@ resource "elasticstack_kibana_synthetics_private_location" "test" { }, // Update and Read testing { - SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaVersion), + SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaPrivateLocationAPIVersion), Config: testConfig("default", "test_policy_default") + ` resource "elasticstack_kibana_synthetics_private_location" "test" { label = "pl-test-label-2" diff --git a/internal/kibana/synthetics/acc_test.go b/internal/kibana/synthetics/acc_test.go index 264ba04c4..dce29e5fb 100644 --- a/internal/kibana/synthetics/acc_test.go +++ b/internal/kibana/synthetics/acc_test.go @@ -112,7 +112,7 @@ resource "elasticstack_kibana_synthetics_monitor" "%s" { resource "elasticstack_kibana_synthetics_monitor" "%s" { name = "TestTcpMonitorResource - %s" - space_id = "default" + space_id = "testacc" schedule = 5 private_locations = [elasticstack_kibana_synthetics_private_location.%s.label] enabled = true @@ -140,7 +140,7 @@ resource "elasticstack_kibana_synthetics_monitor" "%s" { tcpMonitorUpdated = ` resource "elasticstack_kibana_synthetics_monitor" "%s" { name = "TestTcpMonitorResource Updated - %s" - space_id = "default" + space_id = "testacc" schedule = 10 private_locations = [elasticstack_kibana_synthetics_private_location.%s.label] enabled = false @@ -285,7 +285,7 @@ func TestSyntheticMonitorTCPResource(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttrSet(tcpMonitorId, "id"), resource.TestCheckResourceAttr(tcpMonitorId, "name", "TestTcpMonitorResource - "+name), - resource.TestCheckResourceAttr(tcpMonitorId, "space_id", "default"), + resource.TestCheckResourceAttr(tcpMonitorId, "space_id", "testacc"), resource.TestCheckResourceAttr(tcpMonitorId, "schedule", "5"), resource.TestCheckResourceAttr(tcpMonitorId, "private_locations.#", "1"), resource.TestCheckResourceAttrSet(tcpMonitorId, "private_locations.0"), @@ -323,7 +323,7 @@ func TestSyntheticMonitorTCPResource(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttrSet(tcpMonitorId, "id"), resource.TestCheckResourceAttr(tcpMonitorId, "name", "TestTcpMonitorResource Updated - "+name), - resource.TestCheckResourceAttr(tcpMonitorId, "space_id", "default"), + resource.TestCheckResourceAttr(tcpMonitorId, "space_id", "testacc"), resource.TestCheckResourceAttr(tcpMonitorId, "schedule", "10"), resource.TestCheckResourceAttr(tcpMonitorId, "private_locations.#", "1"), resource.TestCheckResourceAttrSet(tcpMonitorId, "private_locations.0"), @@ -377,7 +377,7 @@ resource "elasticstack_fleet_agent_policy" "%s" { } resource "elasticstack_kibana_synthetics_private_location" "%s" { - label = "TestMonitorResource-label-%s" + label = "monitor-pll-%s" space_id = "testacc" agent_policy_id = elasticstack_fleet_agent_policy.%s.policy_id } diff --git a/libs/go-kibana-rest/docker-compose.yml b/libs/go-kibana-rest/docker-compose.yml index e0c6ac37b..1f14ad454 100644 --- a/libs/go-kibana-rest/docker-compose.yml +++ b/libs/go-kibana-rest/docker-compose.yml @@ -29,6 +29,7 @@ services: ELASTICSEARCH_USERNAME: kibana_system ELASTICSEARCH_PASSWORD: changeme XPACK_ENCRYPTEDSAVEDOBJECTS_ENCRYPTIONKEY: min-32-byte-long-strong-encryption-key +# LOGGING_ROOT_LEVEL: debug links: - elasticsearch:es ports: diff --git a/libs/go-kibana-rest/kbapi/api.kibana_synthetics.go b/libs/go-kibana-rest/kbapi/api.kibana_synthetics.go index 44193dede..578cae340 100644 --- a/libs/go-kibana-rest/kbapi/api.kibana_synthetics.go +++ b/libs/go-kibana-rest/kbapi/api.kibana_synthetics.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "sync" "time" "github.com/go-resty/resty/v2" @@ -47,6 +48,8 @@ const ( ModeAny = "any" ) +var plMu sync.Mutex + type MonitorFields interface { APIRequest(cfg SyntheticsMonitorConfig) interface{} } @@ -260,8 +263,7 @@ type KibanaSyntheticsPrivateLocationGet func(ctx context.Context, idOrLabel stri type KibanaSyntheticsPrivateLocationDelete func(ctx context.Context, id string, namespace string) error func newKibanaSyntheticsPrivateLocationGetFunc(c *resty.Client) KibanaSyntheticsPrivateLocationGet { - return func(ctx context.Context, idOrLabel string, namespace string) (*PrivateLocation, error) { - + return func(ctx context.Context, idOrLabel string, _ string) (*PrivateLocation, error) { if idOrLabel == "" { return nil, APIError{ Code: 404, @@ -269,7 +271,7 @@ func newKibanaSyntheticsPrivateLocationGetFunc(c *resty.Client) KibanaSynthetics } } - path := basePathWithId(namespace, privateLocationsSuffix, idOrLabel) + path := basePathWithId("", privateLocationsSuffix, idOrLabel) log.Debugf("URL to get private locations: %s", path) resp, err := c.R().SetContext(ctx).Get(path) if err = handleKibanaError(err, resp); err != nil { @@ -279,8 +281,26 @@ func newKibanaSyntheticsPrivateLocationGetFunc(c *resty.Client) KibanaSynthetics } } +func newKibanaSyntheticsPrivateLocationCreateFunc(c *resty.Client) KibanaSyntheticsPrivateLocationCreate { + return func(ctx context.Context, pLoc PrivateLocationConfig, namespace string) (*PrivateLocation, error) { + plMu.Lock() + defer plMu.Unlock() + + path := basePath(namespace, 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 { + return nil, err + } + return unmarshal(resp, PrivateLocation{}) + } +} + func newKibanaSyntheticsPrivateLocationDeleteFunc(c *resty.Client) KibanaSyntheticsPrivateLocationDelete { return func(ctx context.Context, id string, namespace string) error { + plMu.Lock() + defer plMu.Unlock() + path := basePathWithId(namespace, privateLocationsSuffix, id) log.Debugf("URL to delete private locations: %s", path) resp, err := c.R().SetContext(ctx).Delete(path) @@ -319,19 +339,6 @@ func newKibanaSyntheticsMonitorDeleteFunc(c *resty.Client) KibanaSyntheticsMonit } } -func newKibanaSyntheticsPrivateLocationCreateFunc(c *resty.Client) KibanaSyntheticsPrivateLocationCreate { - return func(ctx context.Context, pLoc PrivateLocationConfig, namespace string) (*PrivateLocation, error) { - - path := basePath(namespace, 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 { - return nil, err - } - return unmarshal(resp, PrivateLocation{}) - } -} - func newKibanaSyntheticsMonitorUpdateFunc(c *resty.Client) KibanaSyntheticsMonitorUpdate { return func(ctx context.Context, id MonitorID, config SyntheticsMonitorConfig, fields MonitorFields, namespace string) (*SyntheticsMonitor, error) { 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 22df4891b..265057a8a 100644 --- a/libs/go-kibana-rest/kbapi/api.kibana_synthetics_test.go +++ b/libs/go-kibana-rest/kbapi/api.kibana_synthetics_test.go @@ -313,7 +313,7 @@ func (s *KBAPITestSuite) TestKibanaSyntheticsPrivateLocationAPI() { testWithPolicy(s.T(), s.client, space, func(policyId string) { cfg := PrivateLocationConfig{ - Label: fmt.Sprintf("TestKibanaSyntheticsPrivateLocationAPI %s", testUuid), + Label: fmt.Sprintf("TestKibanaSyntheticsPrivateLocationAPI-%s", testUuid), AgentPolicyId: policyId, Tags: []string{"a", "b"}, Geo: &SyntheticGeoConfig{ @@ -322,6 +322,7 @@ func (s *KBAPITestSuite) TestKibanaSyntheticsPrivateLocationAPI() { }, } created, err := pAPI.Create(ctx, cfg, space) + assert.NoError(s.T(), err) assert.Equal(s.T(), created.Label, cfg.Label) assert.Equal(s.T(), created.AgentPolicyId, cfg.AgentPolicyId)