Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 5 additions & 5 deletions docs/resources/kibana_synthetics_monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ resource "elasticstack_kibana_synthetics_monitor" "my_monitor" {

### Required

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

### Optional

Expand All @@ -85,14 +85,14 @@ resource "elasticstack_kibana_synthetics_monitor" "my_monitor" {
- `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.
- `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`
- `params` (String) Monitor parameters. Raw JSON object, use `jsonencode` function to represent JSON
- `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.
- `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.
- `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`.
- `schedule` (Number) The monitors schedule in minutes. Supported values are 1, 3, 5, 10, 15, 30, 60, 120 and 240.
- `schedule` (Number) The monitor's schedule in minutes. Supported values are 1, 3, 5, 10, 15, 30, 60, 120 and 240.
- `service_name` (String) The APM service name.
- `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.
- `tags` (List of String) An array of tags.
- `tcp` (Attributes) TCP Monitor specific fields (see [below for nested schema](#nestedatt--tcp))
- `timeout` (Number) The monitor timeout in seconds, monitor will fail if it doesnt complete within this time. Default: `16`
- `timeout` (Number) The monitor timeout in seconds, monitor will fail if it doesn't complete within this time. Default: `16`

### Read-Only

Expand Down Expand Up @@ -151,7 +151,7 @@ Optional:
- `ipv4` (Boolean) Whether to ping using the ipv4 protocol.
- `ipv6` (Boolean) Whether to ping using the ipv6 protocol.
- `max_redirects` (Number) The maximum number of redirects to follow. Default: `0`
- `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.
- `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.
- `password` (String) The password for authenticating with the server. The credentials are passed with the request.
- `proxy_header` (String) Additional headers to send to proxies during CONNECT requests.. Raw JSON object, use `jsonencode` function to represent JSON
- `proxy_url` (String) The URL of the proxy to use for this monitor.
Expand Down
51 changes: 51 additions & 0 deletions internal/kibana/synthetics/api_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package synthetics

import (
"github.com/disaster37/go-kibana-rest/v8"
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana_oapi"
"github.com/hashicorp/terraform-plugin-framework/diag"
)

// ESApiClient interface provides access to the underlying API client
type ESApiClient interface {
GetClient() *clients.ApiClient
}

// GetKibanaClient returns a configured Kibana client for the given ESApiClient
func GetKibanaClient(c ESApiClient, dg diag.Diagnostics) *kibana.Client {
client := c.GetClient()
if client == nil {
dg.AddError(
"Unconfigured Client",
"Expected configured client. Please report this issue to the provider developers.",
)
return nil
}

kibanaClient, err := client.GetKibanaClient()
if err != nil {
dg.AddError("unable to get kibana client", err.Error())
return nil
}
return kibanaClient
}

// GetKibanaOAPIClient returns a configured Kibana OpenAPI client for the given ESApiClient
func GetKibanaOAPIClient(c ESApiClient, dg diag.Diagnostics) *kibana_oapi.Client {
client := c.GetClient()
if client == nil {
dg.AddError(
"Unconfigured Client",
"Expected configured client. Please report this issue to the provider developers.",
)
return nil
}

kibanaClient, err := client.GetKibanaOapiClient()
if err != nil {
dg.AddError("unable to get kibana oapi client", err.Error())
return nil
}
return kibanaClient
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package synthetics_test
package monitor_test

import (
"fmt"
"testing"

"github.com/elastic/terraform-provider-elasticstack/internal/acctest"
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics/monitor"
"github.com/elastic/terraform-provider-elasticstack/internal/versionutils"
"github.com/hashicorp/go-version"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand Down Expand Up @@ -882,7 +882,7 @@ func TestSyntheticMonitorLabelsResource(t *testing.T) {
Steps: []resource.TestStep{
// Create and Read monitor with labels
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(synthetics.MinLabelsVersion),
SkipFunc: versionutils.CheckIfVersionIsUnsupported(monitor.MinLabelsVersion),
Config: labelsConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(labelsMonitorId, "id"),
Expand All @@ -896,15 +896,15 @@ func TestSyntheticMonitorLabelsResource(t *testing.T) {
},
// ImportState testing
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(synthetics.MinLabelsVersion),
SkipFunc: versionutils.CheckIfVersionIsUnsupported(monitor.MinLabelsVersion),
ResourceName: labelsMonitorId,
ImportState: true,
ImportStateVerify: true,
Config: labelsConfig,
},
// Update labels - change values but keep same keys
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(synthetics.MinLabelsVersion),
SkipFunc: versionutils.CheckIfVersionIsUnsupported(monitor.MinLabelsVersion),
Config: labelsConfigUpdated,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(labelsMonitorId, "id"),
Expand All @@ -917,7 +917,7 @@ func TestSyntheticMonitorLabelsResource(t *testing.T) {
},
// Remove all labels - this tests the round-trip consistency fix
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(synthetics.MinLabelsVersion),
SkipFunc: versionutils.CheckIfVersionIsUnsupported(monitor.MinLabelsVersion),
Config: labelsConfigRemoved,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(labelsMonitorId, "id"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package synthetics
package monitor

import (
"context"
"fmt"

"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
"github.com/hashicorp/go-version"

"github.com/hashicorp/terraform-plugin-framework/resource"
)

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

func (r *Resource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
kibanaClient := GetKibanaClient(r, response.Diagnostics)
kibanaClient := synthetics.GetKibanaClient(r, response.Diagnostics)
if kibanaClient == nil {
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package synthetics
package monitor

import (
"context"
"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"
)

func (r *Resource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {

kibanaClient := GetKibanaClient(r, response.Diagnostics)
kibanaClient := synthetics.GetKibanaClient(r, response.Diagnostics)
if kibanaClient == nil {
return
}
Expand All @@ -21,7 +22,7 @@ func (r *Resource) Delete(ctx context.Context, request resource.DeleteRequest, r
return
}

compositeId, dg := GetCompositeId(plan.ID.ValueString())
compositeId, dg := synthetics.GetCompositeId(plan.ID.ValueString())
response.Diagnostics.Append(dg...)
if response.Diagnostics.HasError() {
return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package synthetics
package monitor

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"
)

func (r *Resource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {

kibanaClient := GetKibanaClient(r, response.Diagnostics)
kibanaClient := synthetics.GetKibanaClient(r, response.Diagnostics)
if kibanaClient == nil {
return
}
Expand All @@ -23,7 +23,7 @@ func (r *Resource) Read(ctx context.Context, request resource.ReadRequest, respo
return
}

compositeId, dg := GetCompositeId(state.ID.ValueString())
compositeId, dg := synthetics.GetCompositeId(state.ID.ValueString())
response.Diagnostics.Append(dg...)
if response.Diagnostics.HasError() {
return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,76 +1,38 @@
package synthetics
package monitor

import (
"context"

"github.com/disaster37/go-kibana-rest/v8"
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana_oapi"
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
)

const resourceName = MetadataPrefix + "monitor"
const resourceName = synthetics.MetadataPrefix + "monitor"

// NewResource creates a new synthetics monitor resource
func NewResource() resource.Resource {
return &Resource{}
}

// Ensure provider defined types fully satisfy framework interfaces
var _ resource.Resource = &Resource{}
var _ resource.ResourceWithConfigure = &Resource{}
var _ resource.ResourceWithImportState = &Resource{}
var _ resource.ResourceWithConfigValidators = &Resource{}
var _ ESApiClient = &Resource{}

type ESApiClient interface {
GetClient() *clients.ApiClient
}

func GetKibanaClient(c ESApiClient, dg diag.Diagnostics) *kibana.Client {

client := c.GetClient()
if client == nil {
dg.AddError(
"Unconfigured Client",
"Expected configured client. Please report this issue to the provider developers.",
)
return nil
}

kibanaClient, err := client.GetKibanaClient()
if err != nil {
dg.AddError("unable to get kibana client", err.Error())
return nil
}
return kibanaClient
}

func GetKibanaOAPIClient(c ESApiClient, dg diag.Diagnostics) *kibana_oapi.Client {

client := c.GetClient()
if client == nil {
dg.AddError(
"Unconfigured Client",
"Expected configured client. Please report this issue to the provider developers.",
)
return nil
}

kibanaClient, err := client.GetKibanaOapiClient()
if err != nil {
dg.AddError("unable to get kibana oapi client", err.Error())
return nil
}
return kibanaClient
}
var _ synthetics.ESApiClient = &Resource{}

// Resource represents a synthetics monitor resource
type Resource struct {
client *clients.ApiClient
ESApiClient
}

func (r *Resource) GetClient() *clients.ApiClient {
return r.client
}

func (r *Resource) ConfigValidators(ctx context.Context) []resource.ConfigValidator {
return []resource.ConfigValidator{
resourcevalidator.ExactlyOneOf(
Expand Down Expand Up @@ -102,5 +64,4 @@ func (r *Resource) Metadata(ctx context.Context, request resource.MetadataReques

func (r *Resource) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) {
response.Schema = monitorConfigSchema()

}
Loading
Loading