Skip to content

Commit c4297a1

Browse files
tobiobiscout42
andauthored
Synthetics tidy (#1370)
* Move private location tests to the private_location package * Move monitor resource to it's own package * Move geo config schema to private locations * Remove duplicate map transforms * Renamed resource_test files which were actually acceptance tests to acc_test --------- Co-authored-by: Boris Ilyushonak <57406418+biscout42@users.noreply.github.com>
1 parent 79bb2a2 commit c4297a1

File tree

21 files changed

+1190
-1274
lines changed

21 files changed

+1190
-1274
lines changed

docs/resources/kibana_synthetics_monitor.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ resource "elasticstack_kibana_synthetics_monitor" "my_monitor" {
7272

7373
### Required
7474

75-
- `name` (String) The monitors name.
75+
- `name` (String) The monitor's name.
7676

7777
### Optional
7878

@@ -85,14 +85,14 @@ resource "elasticstack_kibana_synthetics_monitor" "my_monitor" {
8585
- `locations` (List of String) Where to deploy the monitor. Monitors can be deployed in multiple locations so that you can detect differences in availability and response times across those locations.
8686
- `namespace` (String) The data stream namespace. Note: if you change its value, kibana creates new datastream. A user needs permissions for new/old datastream in update case to be able to see full monitor history. The `namespace` field should be lowercase and not contain spaces. The namespace must not include any of the following characters: *, \, /, ?, ", <, >, |, whitespace, ,, #, :, or -. Default: `default`
8787
- `params` (String) Monitor parameters. Raw JSON object, use `jsonencode` function to represent JSON
88-
- `private_locations` (List of String) These Private Locations refer to locations hosted and managed by you, whereas locations are hosted by Elastic. You can specify a Private Location using the locations name.
88+
- `private_locations` (List of String) These Private Locations refer to locations hosted and managed by you, whereas locations are hosted by Elastic. You can specify a Private Location using the location's name.
8989
- `retest_on_failure` (Boolean) Enable or disable retesting when a monitor fails. By default, monitors are automatically retested if the monitor goes from "up" to "down". If the result of the retest is also "down", an error will be created, and if configured, an alert sent. Then the monitor will resume running according to the defined schedule. Using retest_on_failure can reduce noise related to transient problems. Default: `true`.
90-
- `schedule` (Number) The monitors schedule in minutes. Supported values are 1, 3, 5, 10, 15, 30, 60, 120 and 240.
90+
- `schedule` (Number) The monitor's schedule in minutes. Supported values are 1, 3, 5, 10, 15, 30, 60, 120 and 240.
9191
- `service_name` (String) The APM service name.
9292
- `space_id` (String) 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.
9393
- `tags` (List of String) An array of tags.
9494
- `tcp` (Attributes) TCP Monitor specific fields (see [below for nested schema](#nestedatt--tcp))
95-
- `timeout` (Number) The monitor timeout in seconds, monitor will fail if it doesnt complete within this time. Default: `16`
95+
- `timeout` (Number) The monitor timeout in seconds, monitor will fail if it doesn't complete within this time. Default: `16`
9696

9797
### Read-Only
9898

@@ -151,7 +151,7 @@ Optional:
151151
- `ipv4` (Boolean) Whether to ping using the ipv4 protocol.
152152
- `ipv6` (Boolean) Whether to ping using the ipv6 protocol.
153153
- `max_redirects` (Number) The maximum number of redirects to follow. Default: `0`
154-
- `mode` (String) The mode of the monitor. Can be "all" or "any". If youre using a DNS-load balancer and want to ping every IP address for the specified hostname, you should use all.
154+
- `mode` (String) The mode of the monitor. Can be "all" or "any". If you're using a DNS-load balancer and want to ping every IP address for the specified hostname, you should use all.
155155
- `password` (String) The password for authenticating with the server. The credentials are passed with the request.
156156
- `proxy_header` (String) Additional headers to send to proxies during CONNECT requests.. Raw JSON object, use `jsonencode` function to represent JSON
157157
- `proxy_url` (String) The URL of the proxy to use for this monitor.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package synthetics
2+
3+
import (
4+
"github.com/disaster37/go-kibana-rest/v8"
5+
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
6+
"github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana_oapi"
7+
"github.com/hashicorp/terraform-plugin-framework/diag"
8+
)
9+
10+
// ESApiClient interface provides access to the underlying API client
11+
type ESApiClient interface {
12+
GetClient() *clients.ApiClient
13+
}
14+
15+
// GetKibanaClient returns a configured Kibana client for the given ESApiClient
16+
func GetKibanaClient(c ESApiClient, dg diag.Diagnostics) *kibana.Client {
17+
client := c.GetClient()
18+
if client == nil {
19+
dg.AddError(
20+
"Unconfigured Client",
21+
"Expected configured client. Please report this issue to the provider developers.",
22+
)
23+
return nil
24+
}
25+
26+
kibanaClient, err := client.GetKibanaClient()
27+
if err != nil {
28+
dg.AddError("unable to get kibana client", err.Error())
29+
return nil
30+
}
31+
return kibanaClient
32+
}
33+
34+
// GetKibanaOAPIClient returns a configured Kibana OpenAPI client for the given ESApiClient
35+
func GetKibanaOAPIClient(c ESApiClient, dg diag.Diagnostics) *kibana_oapi.Client {
36+
client := c.GetClient()
37+
if client == nil {
38+
dg.AddError(
39+
"Unconfigured Client",
40+
"Expected configured client. Please report this issue to the provider developers.",
41+
)
42+
return nil
43+
}
44+
45+
kibanaClient, err := client.GetKibanaOapiClient()
46+
if err != nil {
47+
dg.AddError("unable to get kibana oapi client", err.Error())
48+
return nil
49+
}
50+
return kibanaClient
51+
}

internal/kibana/synthetics/acc_test.go renamed to internal/kibana/synthetics/monitor/acc_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package synthetics_test
1+
package monitor_test
22

33
import (
44
"fmt"
55
"testing"
66

77
"github.com/elastic/terraform-provider-elasticstack/internal/acctest"
8-
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
8+
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics/monitor"
99
"github.com/elastic/terraform-provider-elasticstack/internal/versionutils"
1010
"github.com/hashicorp/go-version"
1111
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
@@ -882,7 +882,7 @@ func TestSyntheticMonitorLabelsResource(t *testing.T) {
882882
Steps: []resource.TestStep{
883883
// Create and Read monitor with labels
884884
{
885-
SkipFunc: versionutils.CheckIfVersionIsUnsupported(synthetics.MinLabelsVersion),
885+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(monitor.MinLabelsVersion),
886886
Config: labelsConfig,
887887
Check: resource.ComposeAggregateTestCheckFunc(
888888
resource.TestCheckResourceAttrSet(labelsMonitorId, "id"),
@@ -896,15 +896,15 @@ func TestSyntheticMonitorLabelsResource(t *testing.T) {
896896
},
897897
// ImportState testing
898898
{
899-
SkipFunc: versionutils.CheckIfVersionIsUnsupported(synthetics.MinLabelsVersion),
899+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(monitor.MinLabelsVersion),
900900
ResourceName: labelsMonitorId,
901901
ImportState: true,
902902
ImportStateVerify: true,
903903
Config: labelsConfig,
904904
},
905905
// Update labels - change values but keep same keys
906906
{
907-
SkipFunc: versionutils.CheckIfVersionIsUnsupported(synthetics.MinLabelsVersion),
907+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(monitor.MinLabelsVersion),
908908
Config: labelsConfigUpdated,
909909
Check: resource.ComposeAggregateTestCheckFunc(
910910
resource.TestCheckResourceAttrSet(labelsMonitorId, "id"),
@@ -917,7 +917,7 @@ func TestSyntheticMonitorLabelsResource(t *testing.T) {
917917
},
918918
// Remove all labels - this tests the round-trip consistency fix
919919
{
920-
SkipFunc: versionutils.CheckIfVersionIsUnsupported(synthetics.MinLabelsVersion),
920+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(monitor.MinLabelsVersion),
921921
Config: labelsConfigRemoved,
922922
Check: resource.ComposeAggregateTestCheckFunc(
923923
resource.TestCheckResourceAttrSet(labelsMonitorId, "id"),

internal/kibana/synthetics/create.go renamed to internal/kibana/synthetics/monitor/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
package synthetics
1+
package monitor
22

33
import (
44
"context"
55
"fmt"
66

7+
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
78
"github.com/hashicorp/go-version"
8-
99
"github.com/hashicorp/terraform-plugin-framework/resource"
1010
)
1111

1212
var MinLabelsVersion = version.Must(version.NewVersion("8.16.0"))
1313

1414
func (r *Resource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
15-
kibanaClient := GetKibanaClient(r, response.Diagnostics)
15+
kibanaClient := synthetics.GetKibanaClient(r, response.Diagnostics)
1616
if kibanaClient == nil {
1717
return
1818
}

internal/kibana/synthetics/delete.go renamed to internal/kibana/synthetics/monitor/delete.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
package synthetics
1+
package monitor
22

33
import (
44
"context"
55
"fmt"
6+
67
"github.com/disaster37/go-kibana-rest/v8/kbapi"
8+
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
79
"github.com/hashicorp/terraform-plugin-framework/resource"
810
)
911

1012
func (r *Resource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
11-
12-
kibanaClient := GetKibanaClient(r, response.Diagnostics)
13+
kibanaClient := synthetics.GetKibanaClient(r, response.Diagnostics)
1314
if kibanaClient == nil {
1415
return
1516
}
@@ -21,7 +22,7 @@ func (r *Resource) Delete(ctx context.Context, request resource.DeleteRequest, r
2122
return
2223
}
2324

24-
compositeId, dg := GetCompositeId(plan.ID.ValueString())
25+
compositeId, dg := synthetics.GetCompositeId(plan.ID.ValueString())
2526
response.Diagnostics.Append(dg...)
2627
if response.Diagnostics.HasError() {
2728
return

0 commit comments

Comments
 (0)