Skip to content

Commit 33bd59a

Browse files
committed
[provider] Recognize Opensearch version without compatibility mode.
Closes #218
1 parent e376cc3 commit 33bd59a

File tree

6 files changed

+34
-29
lines changed

6 files changed

+34
-29
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ jobs:
110110
# ensure that ES has come up and is available
111111
run: |
112112
./script/wait-for-endpoint --timeout=20 http://localhost:9200
113-
if [ -n "$OPENSEARCH_PREFIX" ]; then
114-
curl -s -v -X PUT -H 'Content-type: application/json' -d '{"persistent": {"compatibility.override_main_response_version": true}}' http://localhost:9200/_cluster/settings
115-
fi
116113
- name: Wait for Kibana
117114
# ensure that Kibana API has come up and is available
118115
run: |
@@ -122,15 +119,11 @@ jobs:
122119
- name: Warm up OpenDistro/Opensearch
123120
# - OpenDistro lazily initializes its indexes, see
124121
# https://github.com/opendistro-for-elasticsearch/alerting/issues/60
125-
# - OpenSearch 1.x uses a compatibility mode to maintain ESv7.x version
126122
run: |
127123
if [ -n "$ES_OPENDISTRO_IMAGE" ]; then
128124
./script/wait-for-endpoint --timeout=120 http://admin:admin@localhost:9220
129125
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
130126
fi
131-
if [ -n "$OPENSEARCH_PREFIX" ]; then
132-
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
133-
fi
134127
- name: Dump docker logs on failure
135128
if: failure()
136129
uses: jwalton/gh-docker-logs@v2

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
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).
5+
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).
66

77
## Using the Provider
88

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The provider is used to interact with the resources supported by
1010
Elasticsearch/Opensearch. The provider needs to be configured with an endpoint
1111
URL before it can be used.
1212

13-
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/).
13+
AWS Opensearch Service domains and OpenSearch clusters deployed on Kubernetes and other infrastructure are supported.
1414

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

es/provider.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,12 @@ func getClient(conf *ProviderConf) (interface{}, error) {
438438
if err != nil {
439439
return nil, err
440440
}
441+
} else if conf.flavor == Unknown && conf.esVersion < "2.0.0" && conf.esVersion >= "1.0.0" {
442+
// Version 1.x of OpenSearch very likely. Nothing to do since it's API
443+
// compatible with 7.x of ES. If elastic client library supports detecting
444+
// flavor, update to Opensearch.
441445
} else if conf.esVersion < "6.0.0" {
442-
return nil, fmt.Errorf("ElasticSearch version %s is older than 6.0.0 and is not supported.", conf.esVersion)
446+
return nil, fmt.Errorf("ElasticSearch version %s is older than 6.0.0 and is not supported, flavor: %v.", conf.esVersion, conf.flavor)
443447
}
444448

445449
return relevantClient, nil

es/resource_elasticsearch_component_template.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
elastic7 "github.com/olivere/elastic/v7"
1313
)
1414

15-
var componentTemplateMinimalVersion, _ = version.NewVersion("7.8.0")
15+
var esComponentTemplateMinimalVersion, _ = version.NewVersion("7.8.0")
1616

1717
func resourceElasticsearchComponentTemplate() *schema.Resource {
1818
return &schema.Resource{
@@ -67,10 +67,10 @@ func resourceElasticsearchComponentTemplateRead(d *schema.ResourceData, meta int
6767
case *elastic7.Client:
6868
elasticVersion, err = version.NewVersion(providerConf.esVersion)
6969
if err == nil {
70-
if elasticVersion.LessThan(componentTemplateMinimalVersion) {
71-
err = fmt.Errorf("component_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
72-
} else {
70+
if resourceElasticsearchComponentTemplateAvailable(elasticVersion, providerConf) {
7371
result, err = elastic7GetComponentTemplate(client, id)
72+
} else {
73+
err = fmt.Errorf("component_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
7474
}
7575
}
7676
default:
@@ -127,10 +127,10 @@ func resourceElasticsearchComponentTemplateDelete(d *schema.ResourceData, meta i
127127
case *elastic7.Client:
128128
elasticVersion, err = version.NewVersion(providerConf.esVersion)
129129
if err == nil {
130-
if elasticVersion.LessThan(componentTemplateMinimalVersion) {
131-
err = fmt.Errorf("component_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
132-
} else {
130+
if resourceElasticsearchComponentTemplateAvailable(elasticVersion, providerConf) {
133131
err = elastic7DeleteComponentTemplate(client, id)
132+
} else {
133+
err = fmt.Errorf("component_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
134134
}
135135
}
136136
default:
@@ -144,6 +144,10 @@ func resourceElasticsearchComponentTemplateDelete(d *schema.ResourceData, meta i
144144
return nil
145145
}
146146

147+
func resourceElasticsearchComponentTemplateAvailable(v *version.Version, c *ProviderConf) bool {
148+
return v.GreaterThanOrEqual(esComponentTemplateMinimalVersion) || c.flavor == Unknown
149+
}
150+
147151
func elastic7DeleteComponentTemplate(client *elastic7.Client, id string) error {
148152
_, err := client.IndexDeleteComponentTemplate(id).Do(context.TODO())
149153
return err
@@ -165,10 +169,10 @@ func resourceElasticsearchPutComponentTemplate(d *schema.ResourceData, meta inte
165169
case *elastic7.Client:
166170
elasticVersion, err = version.NewVersion(providerConf.esVersion)
167171
if err == nil {
168-
if elasticVersion.LessThan(componentTemplateMinimalVersion) {
169-
err = fmt.Errorf("component_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
170-
} else {
172+
if resourceElasticsearchComponentTemplateAvailable(elasticVersion, providerConf) {
171173
err = elastic7PutComponentTemplate(client, name, body, create)
174+
} else {
175+
err = fmt.Errorf("component_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
172176
}
173177
}
174178
default:

es/resource_elasticsearch_composable_index_template.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func resourceElasticsearchComposableIndexTemplateCreate(d *schema.ResourceData,
4848
return nil
4949
}
5050

51+
func resourceElasticsearchComposableIndexTemplateAvailable(v *version.Version, c *ProviderConf) bool {
52+
return v.GreaterThanOrEqual(minimalESComposableTemplateVersion) || c.flavor == Unknown
53+
}
54+
5155
func resourceElasticsearchComposableIndexTemplateRead(d *schema.ResourceData, meta interface{}) error {
5256
id := d.Id()
5357

@@ -64,10 +68,10 @@ func resourceElasticsearchComposableIndexTemplateRead(d *schema.ResourceData, me
6468
case *elastic7.Client:
6569
elasticVersion, err = version.NewVersion(providerConf.esVersion)
6670
if err == nil {
67-
if elasticVersion.LessThan(minimalESComposableTemplateVersion) {
68-
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
69-
} else {
71+
if resourceElasticsearchComposableIndexTemplateAvailable(elasticVersion, providerConf) {
7072
result, err = elastic7GetIndexTemplate(client, id)
73+
} else {
74+
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
7175
}
7276
}
7377
default:
@@ -124,10 +128,10 @@ func resourceElasticsearchComposableIndexTemplateDelete(d *schema.ResourceData,
124128
case *elastic7.Client:
125129
elasticVersion, err = version.NewVersion(providerConf.esVersion)
126130
if err == nil {
127-
if elasticVersion.LessThan(minimalESComposableTemplateVersion) {
128-
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
129-
} else {
131+
if resourceElasticsearchComposableIndexTemplateAvailable(elasticVersion, providerConf) {
130132
err = elastic7DeleteIndexTemplate(client, id)
133+
} else {
134+
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
131135
}
132136
}
133137
default:
@@ -162,10 +166,10 @@ func resourceElasticsearchPutComposableIndexTemplate(d *schema.ResourceData, met
162166
case *elastic7.Client:
163167
elasticVersion, err = version.NewVersion(providerConf.esVersion)
164168
if err == nil {
165-
if elasticVersion.LessThan(minimalESComposableTemplateVersion) {
166-
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
167-
} else {
169+
if resourceElasticsearchComposableIndexTemplateAvailable(elasticVersion, providerConf) {
168170
err = elastic7PutIndexTemplate(client, name, body, create)
171+
} else {
172+
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", elasticVersion.String())
169173
}
170174
}
171175
default:

0 commit comments

Comments
 (0)