Skip to content

Commit 6ad3e92

Browse files
kierramarieshemautoddgiguere
authored
feat: allow referencing existing ES instance (#243)
* feat: allow exist es * fix: module is a tuple * feat: add exist rg id var * feat: output rg id * fix: output fix * refactor: variable tidying * fix: for testing * refactor: some renaming * fix: some variable updates * docs: var description * refactor: added move block * fix: merge issue * fix: moved block * fix: module moved block * fix: move block fix * refactor: moved.tf --------- Co-authored-by: [email protected] <[email protected]> Co-authored-by: shemau <[email protected]> Co-authored-by: toddgiguere <[email protected]>
1 parent 62ea376 commit 6ad3e92

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

solutions/standard/main.tf

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ locals {
1010
elasticsearch_key_name = var.prefix != null ? "${var.prefix}-${var.elasticsearch_key_name}" : var.elasticsearch_key_name
1111
elasticsearch_key_ring_name = var.prefix != null ? "${var.prefix}-${var.elasticsearch_key_ring_name}" : var.elasticsearch_key_ring_name
1212

13-
kms_key_crn = var.existing_kms_key_crn != null ? var.existing_kms_key_crn : module.kms[0].keys[format("%s.%s", local.elasticsearch_key_ring_name, local.elasticsearch_key_name)].crn
13+
14+
kms_key_crn = var.existing_kms_key_crn != null ? var.existing_kms_key_crn : module.kms[0].keys[format("%s.%s", local.elasticsearch_key_ring_name, local.elasticsearch_key_name)].crn
15+
16+
existing_db_instance_guid = var.existing_db_instance_crn != null ? element(split(":", var.existing_db_instance_crn), length(split(":", var.existing_db_instance_crn)) - 3) : null
17+
use_existing_db_instance = var.existing_db_instance_crn != null
18+
1419
create_cross_account_auth_policy = !var.skip_iam_authorization_policy && var.ibmcloud_kms_api_key != null
1520
kms_service_name = local.kms_key_crn != null ? (
1621
can(regex(".*kms.*", local.kms_key_crn)) ? "kms" : can(regex(".*hs-crypto.*", local.kms_key_crn)) ? "hs-crypto" : null
@@ -90,6 +95,7 @@ module "kms" {
9095
#######################################################################################################################
9196

9297
module "elasticsearch" {
98+
count = local.use_existing_db_instance ? 0 : 1
9399
source = "../../modules/fscloud"
94100
depends_on = [time_sleep.wait_for_authorization_policy]
95101
resource_group_id = module.resource_group.resource_group_id
@@ -113,3 +119,26 @@ module "elasticsearch" {
113119
service_credential_names = var.service_credential_names
114120
enable_elser_model = var.enable_elser_model
115121
}
122+
123+
# this extra block is needed when passing in an existing ES instance - the database data block
124+
# requires a name and resource_id to retrieve the data
125+
data "ibm_resource_instance" "existing_instance_resource" {
126+
count = local.use_existing_db_instance ? 1 : 0
127+
identifier = local.existing_db_instance_guid
128+
}
129+
130+
data "ibm_database" "existing_db_instance" {
131+
count = local.use_existing_db_instance ? 1 : 0
132+
name = data.ibm_resource_instance.existing_instance_resource[0].name
133+
resource_group_id = data.ibm_resource_instance.existing_instance_resource[0].resource_group_id
134+
location = var.region
135+
service = "databases-for-elasticsearch"
136+
}
137+
138+
data "ibm_database_connection" "existing_connection" {
139+
count = local.use_existing_db_instance ? 1 : 0
140+
endpoint_type = "private"
141+
deployment_id = data.ibm_database.existing_db_instance[0].id
142+
user_id = data.ibm_database.existing_db_instance[0].adminuser
143+
user_type = "database"
144+
}

solutions/standard/moved.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
moved {
2+
from = module.elasticsearch
3+
to = module.elasticsearch[0]
4+
}

solutions/standard/outputs.tf

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,47 @@
44

55
output "id" {
66
description = "Elasticsearch instance id"
7-
value = module.elasticsearch.id
7+
value = local.use_existing_db_instance ? data.ibm_database.existing_db_instance[0].id : module.elasticsearch[0].id
88
}
99

1010
output "guid" {
1111
description = "Elasticsearch instance guid"
12-
value = module.elasticsearch.guid
12+
value = local.use_existing_db_instance ? data.ibm_database.existing_db_instance[0].guid : module.elasticsearch[0].guid
1313
}
1414

1515
output "version" {
1616
description = "Elasticsearch instance version"
17-
value = module.elasticsearch.version
17+
value = local.use_existing_db_instance ? data.ibm_database.existing_db_instance[0].version : module.elasticsearch[0].version
1818
}
1919

2020
output "crn" {
2121
description = "Elasticsearch instance crn"
22-
value = module.elasticsearch.crn
22+
value = local.use_existing_db_instance ? var.existing_db_instance_crn : module.elasticsearch[0].crn
2323
}
2424

2525
output "cbr_rule_ids" {
2626
description = "CBR rule ids created to restrict Elasticsearch"
27-
value = module.elasticsearch.cbr_rule_ids
27+
value = local.use_existing_db_instance ? null : module.elasticsearch[0].cbr_rule_ids
2828
}
2929

3030
output "service_credentials_json" {
3131
description = "Service credentials json map"
32-
value = module.elasticsearch.service_credentials_json
32+
value = local.use_existing_db_instance ? null : module.elasticsearch[0].service_credentials_json
3333
sensitive = true
3434
}
3535

3636
output "service_credentials_object" {
3737
description = "Service credentials object"
38-
value = module.elasticsearch.service_credentials_object
38+
value = local.use_existing_db_instance ? null : module.elasticsearch[0].service_credentials_object
3939
sensitive = true
4040
}
4141

4242
output "hostname" {
4343
description = "Elasticsearch instance hostname"
44-
value = module.elasticsearch.hostname
44+
value = local.use_existing_db_instance ? data.ibm_database_connection.existing_connection[0].https[0].hosts[0].hostname : module.elasticsearch[0].hostname
4545
}
4646

4747
output "port" {
4848
description = "Elasticsearch instance port"
49-
value = module.elasticsearch.port
49+
value = local.use_existing_db_instance ? data.ibm_database_connection.existing_connection[0].https[0].hosts[0].port : module.elasticsearch[0].port
5050
}

solutions/standard/variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,17 @@ variable "plan" {
6060
default = "platinum"
6161
}
6262

63+
variable "existing_db_instance_crn" {
64+
type = string
65+
default = null
66+
description = "The CRN of an existing Databases for Elasticsearch instance. If no value is specified, a new instance is created."
67+
}
68+
6369
##############################################################################
6470
# ICD hosting model properties
6571
##############################################################################
6672

73+
6774
variable "members" {
6875
type = number
6976
description = "The number of members that are allocated. [Learn more](https://cloud.ibm.com/docs/databases-for-elasticsearch?topic=databases-for-elasticsearch-resources-scaling)."

0 commit comments

Comments
 (0)