diff --git a/CHANGELOG.md b/CHANGELOG.md index 7415aa3c..ea4cdf1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## [Unreleased] +- Allow `elasticstack_kibana_alerting_rule` to be used without Elasticsearch being configured. ([#869](https://github.com/elastic/terraform-provider-elasticstack/pull/869)) - Add resource `elasticstack_elasticsearch_data_stream_lifecycle` ([838](https://github.com/elastic/terraform-provider-elasticstack/issues/838)) ## [0.11.10] - 2024-10-23 @@ -379,8 +380,8 @@ - CI integration [Unreleased]: https://github.com/elastic/terraform-provider-elasticstack/compare/v0.11.10...HEAD -[0.11.9]: https://github.com/elastic/terraform-provider-elasticstack/compare/v0.11.9...v0.11.10 -[0.11.8]: https://github.com/elastic/terraform-provider-elasticstack/compare/v0.11.8...v0.11.9 +[0.11.10]: https://github.com/elastic/terraform-provider-elasticstack/compare/v0.11.9...v0.11.10 +[0.11.9]: https://github.com/elastic/terraform-provider-elasticstack/compare/v0.11.8...v0.11.9 [0.11.8]: https://github.com/elastic/terraform-provider-elasticstack/compare/v0.11.7...v0.11.8 [0.11.7]: https://github.com/elastic/terraform-provider-elasticstack/compare/v0.11.6...v0.11.7 [0.11.6]: https://github.com/elastic/terraform-provider-elasticstack/compare/v0.11.5...v0.11.6 diff --git a/internal/clients/api_client.go b/internal/clients/api_client.go index 35f3721a..6c16802d 100644 --- a/internal/clients/api_client.go +++ b/internal/clients/api_client.go @@ -358,6 +358,43 @@ func (a *ApiClient) serverInfo(ctx context.Context) (*models.ClusterInfo, diag.D } func (a *ApiClient) ServerVersion(ctx context.Context) (*version.Version, diag.Diagnostics) { + if a.elasticsearch != nil { + return a.versionFromElasticsearch(ctx) + } + + return a.versionFromKibana() +} + +func (a *ApiClient) versionFromKibana() (*version.Version, diag.Diagnostics) { + kibClient, err := a.GetKibanaClient() + if err != nil { + return nil, diag.FromErr(err) + } + + status, err := kibClient.KibanaStatus.Get() + if err != nil { + return nil, diag.FromErr(err) + } + + vMap, ok := status["version"].(map[string]interface{}) + if !ok { + return nil, diag.Errorf("failed to get version from Kibana API") + } + + rawVersion, ok := vMap["number"].(string) + if !ok { + return nil, diag.Errorf("failed to get version number from Kibana status") + } + + serverVersion, err := version.NewVersion(rawVersion) + if err != nil { + return nil, diag.FromErr(err) + } + + return serverVersion, nil +} + +func (a *ApiClient) versionFromElasticsearch(ctx context.Context) (*version.Version, diag.Diagnostics) { info, diags := a.serverInfo(ctx) if diags.HasError() { return nil, diags diff --git a/internal/kibana/alerting_test.go b/internal/kibana/alerting_test.go index 412687f5..6119c599 100644 --- a/internal/kibana/alerting_test.go +++ b/internal/kibana/alerting_test.go @@ -189,7 +189,6 @@ func TestAccResourceAlertingRule(t *testing.T) { func testAccResourceAlertingRuleCreate(name string) string { return fmt.Sprintf(` provider "elasticstack" { - elasticsearch {} kibana {} } @@ -221,7 +220,6 @@ resource "elasticstack_kibana_alerting_rule" "test_rule" { func testAccResourceAlertingRuleUpdate(name string) string { return fmt.Sprintf(` provider "elasticstack" { - elasticsearch {} kibana {} } @@ -256,7 +254,6 @@ resource "elasticstack_kibana_alerting_rule" "test_rule" { func testAccResourceAlertingRuleWithFrequencyCreate(name string) string { return fmt.Sprintf(` provider "elasticstack" { - elasticsearch {} kibana {} } @@ -314,7 +311,6 @@ resource "elasticstack_kibana_alerting_rule" "test_rule" { func testAccResourceAlertingRuleWithFrequencyUpdate(name string) string { return fmt.Sprintf(` provider "elasticstack" { - elasticsearch {} kibana {} } @@ -375,7 +371,6 @@ resource "elasticstack_kibana_alerting_rule" "test_rule" { func testAccResourceAlertingRuleWithAlertsFilterCreate(name string) string { return fmt.Sprintf(` provider "elasticstack" { - elasticsearch {} kibana {} } @@ -450,7 +445,6 @@ resource "elasticstack_kibana_alerting_rule" "test_rule" { func testAccResourceAlertingRuleWithAlertsFilterUpdate(name string) string { return fmt.Sprintf(` provider "elasticstack" { - elasticsearch {} kibana {} } @@ -527,7 +521,6 @@ resource "elasticstack_kibana_alerting_rule" "test_rule" { func testAccResourceAlertingRuleWithAlertDelayCreate(name string) string { return fmt.Sprintf(` provider "elasticstack" { - elasticsearch {} kibana {} } @@ -587,7 +580,6 @@ resource "elasticstack_kibana_alerting_rule" "test_rule" { func testAccResourceAlertingRuleWithAlertDelayUpdate(name string) string { return fmt.Sprintf(` provider "elasticstack" { - elasticsearch {} kibana {} }