Skip to content

Commit

Permalink
[provider] Recognize Opensearch version without compatibility mode.
Browse files Browse the repository at this point in the history
Closes #218
  • Loading branch information
phillbaker committed Jan 30, 2022
1 parent e376cc3 commit 33bd59a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 29 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ jobs:
# ensure that ES has come up and is available
run: |
./script/wait-for-endpoint --timeout=20 http://localhost:9200
if [ -n "$OPENSEARCH_PREFIX" ]; then
curl -s -v -X PUT -H 'Content-type: application/json' -d '{"persistent": {"compatibility.override_main_response_version": true}}' http://localhost:9200/_cluster/settings
fi
- name: Wait for Kibana
# ensure that Kibana API has come up and is available
run: |
Expand All @@ -122,15 +119,11 @@ jobs:
- name: Warm up OpenDistro/Opensearch
# - OpenDistro lazily initializes its indexes, see
# https://github.com/opendistro-for-elasticsearch/alerting/issues/60
# - OpenSearch 1.x uses a compatibility mode to maintain ESv7.x version
run: |
if [ -n "$ES_OPENDISTRO_IMAGE" ]; then
./script/wait-for-endpoint --timeout=120 http://admin:admin@localhost:9220
curl -s -v -X POST -H 'Content-type: application/json' -d '{"name":"_warmup","type":"slack","slack":{"url": "http://www.example.com"}}' http://admin:admin@localhost:9220/_opendistro/_alerting/destinations
fi
if [ -n "$OPENSEARCH_PREFIX" ]; then
curl -s -v -X PUT -H 'Content-type: application/json' -d '{"persistent": {"compatibility.override_main_response_version": true}}' http://admin:admin@localhost:9220/_cluster/settings
fi
- name: Dump docker logs on failure
if: failure()
uses: jwalton/gh-docker-logs@v2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![Test](https://github.com/phillbaker/terraform-provider-elasticsearch/workflows/Test/badge.svg?branch=master)

This is a terraform provider that lets you provision Elasticsearch and Opensearch resources, compatible with v6 and v7 of Elasticsearch and v1 of Opensearch ([via compatibility mode](https://opensearch.org/docs/latest/clients/agents-and-ingestion-tools/index/)). Based off of an [original PR to Terraform](https://github.com/hashicorp/terraform/pull/13238).
This is a terraform provider that lets you provision Elasticsearch and Opensearch resources, compatible with v6 and v7 of Elasticsearch and v1 of Opensearch. Based off of an [original PR to Terraform](https://github.com/hashicorp/terraform/pull/13238).

## Using the Provider

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The provider is used to interact with the resources supported by
Elasticsearch/Opensearch. The provider needs to be configured with an endpoint
URL before it can be used.

AWS Opensearch Service domains and OpenSearch clusters deployed on Kubernetes and other infrastructure are supported [via compatibility mode](https://opensearch.org/docs/latest/clients/agents-and-ingestion-tools/index/).
AWS Opensearch Service domains and OpenSearch clusters deployed on Kubernetes and other infrastructure are supported.

Use the navigation to the left to read about the available resources.

Expand Down
6 changes: 5 additions & 1 deletion es/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,12 @@ func getClient(conf *ProviderConf) (interface{}, error) {
if err != nil {
return nil, err
}
} else if conf.flavor == Unknown && conf.esVersion < "2.0.0" && conf.esVersion >= "1.0.0" {
// Version 1.x of OpenSearch very likely. Nothing to do since it's API
// compatible with 7.x of ES. If elastic client library supports detecting
// flavor, update to Opensearch.
} else if conf.esVersion < "6.0.0" {
return nil, fmt.Errorf("ElasticSearch version %s is older than 6.0.0 and is not supported.", conf.esVersion)
return nil, fmt.Errorf("ElasticSearch version %s is older than 6.0.0 and is not supported, flavor: %v.", conf.esVersion, conf.flavor)
}

return relevantClient, nil
Expand Down
24 changes: 14 additions & 10 deletions es/resource_elasticsearch_component_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
elastic7 "github.com/olivere/elastic/v7"
)

var componentTemplateMinimalVersion, _ = version.NewVersion("7.8.0")
var esComponentTemplateMinimalVersion, _ = version.NewVersion("7.8.0")

func resourceElasticsearchComponentTemplate() *schema.Resource {
return &schema.Resource{
Expand Down Expand Up @@ -67,10 +67,10 @@ func resourceElasticsearchComponentTemplateRead(d *schema.ResourceData, meta int
case *elastic7.Client:
elasticVersion, err = version.NewVersion(providerConf.esVersion)
if err == nil {
if elasticVersion.LessThan(componentTemplateMinimalVersion) {
err = fmt.Errorf("component_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
} else {
if resourceElasticsearchComponentTemplateAvailable(elasticVersion, providerConf) {
result, err = elastic7GetComponentTemplate(client, id)
} else {
err = fmt.Errorf("component_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
}
}
default:
Expand Down Expand Up @@ -127,10 +127,10 @@ func resourceElasticsearchComponentTemplateDelete(d *schema.ResourceData, meta i
case *elastic7.Client:
elasticVersion, err = version.NewVersion(providerConf.esVersion)
if err == nil {
if elasticVersion.LessThan(componentTemplateMinimalVersion) {
err = fmt.Errorf("component_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
} else {
if resourceElasticsearchComponentTemplateAvailable(elasticVersion, providerConf) {
err = elastic7DeleteComponentTemplate(client, id)
} else {
err = fmt.Errorf("component_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
}
}
default:
Expand All @@ -144,6 +144,10 @@ func resourceElasticsearchComponentTemplateDelete(d *schema.ResourceData, meta i
return nil
}

func resourceElasticsearchComponentTemplateAvailable(v *version.Version, c *ProviderConf) bool {
return v.GreaterThanOrEqual(esComponentTemplateMinimalVersion) || c.flavor == Unknown
}

func elastic7DeleteComponentTemplate(client *elastic7.Client, id string) error {
_, err := client.IndexDeleteComponentTemplate(id).Do(context.TODO())
return err
Expand All @@ -165,10 +169,10 @@ func resourceElasticsearchPutComponentTemplate(d *schema.ResourceData, meta inte
case *elastic7.Client:
elasticVersion, err = version.NewVersion(providerConf.esVersion)
if err == nil {
if elasticVersion.LessThan(componentTemplateMinimalVersion) {
err = fmt.Errorf("component_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
} else {
if resourceElasticsearchComponentTemplateAvailable(elasticVersion, providerConf) {
err = elastic7PutComponentTemplate(client, name, body, create)
} else {
err = fmt.Errorf("component_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
}
}
default:
Expand Down
22 changes: 13 additions & 9 deletions es/resource_elasticsearch_composable_index_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func resourceElasticsearchComposableIndexTemplateCreate(d *schema.ResourceData,
return nil
}

func resourceElasticsearchComposableIndexTemplateAvailable(v *version.Version, c *ProviderConf) bool {
return v.GreaterThanOrEqual(minimalESComposableTemplateVersion) || c.flavor == Unknown
}

func resourceElasticsearchComposableIndexTemplateRead(d *schema.ResourceData, meta interface{}) error {
id := d.Id()

Expand All @@ -64,10 +68,10 @@ func resourceElasticsearchComposableIndexTemplateRead(d *schema.ResourceData, me
case *elastic7.Client:
elasticVersion, err = version.NewVersion(providerConf.esVersion)
if err == nil {
if elasticVersion.LessThan(minimalESComposableTemplateVersion) {
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
} else {
if resourceElasticsearchComposableIndexTemplateAvailable(elasticVersion, providerConf) {
result, err = elastic7GetIndexTemplate(client, id)
} else {
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
}
}
default:
Expand Down Expand Up @@ -124,10 +128,10 @@ func resourceElasticsearchComposableIndexTemplateDelete(d *schema.ResourceData,
case *elastic7.Client:
elasticVersion, err = version.NewVersion(providerConf.esVersion)
if err == nil {
if elasticVersion.LessThan(minimalESComposableTemplateVersion) {
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
} else {
if resourceElasticsearchComposableIndexTemplateAvailable(elasticVersion, providerConf) {
err = elastic7DeleteIndexTemplate(client, id)
} else {
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
}
}
default:
Expand Down Expand Up @@ -162,10 +166,10 @@ func resourceElasticsearchPutComposableIndexTemplate(d *schema.ResourceData, met
case *elastic7.Client:
elasticVersion, err = version.NewVersion(providerConf.esVersion)
if err == nil {
if elasticVersion.LessThan(minimalESComposableTemplateVersion) {
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
} else {
if resourceElasticsearchComposableIndexTemplateAvailable(elasticVersion, providerConf) {
err = elastic7PutIndexTemplate(client, name, body, create)
} else {
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
}
}
default:
Expand Down

0 comments on commit 33bd59a

Please sign in to comment.