Skip to content

Commit

Permalink
Add fileset support to create indices etc
Browse files Browse the repository at this point in the history
  • Loading branch information
steveteuber committed Dec 15, 2021
1 parent 6948e2a commit b126532
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 30 deletions.
29 changes: 5 additions & 24 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,9 @@ module "opensearch" {
saml_metadata_content = data.http.saml_metadata.body
saml_session_timeout = 120

index_templates = {
for filename in fileset(path.module, "index-templates/*.{yml,yaml}") :
replace(basename(filename), "/.ya?ml$/", "") => yamldecode(file(filename))
}

ism_policies = {
for filename in fileset(path.module, "ism-policies/*.{yml,yaml}") :
replace(basename(filename), "/.ya?ml$/", "") => yamldecode(file(filename))
}

indices = {
for filename in fileset(path.module, "indices/*.{yml,yaml}") :
replace(basename(filename), "/.ya?ml$/", "") => yamldecode(file(filename))
}

roles = {
for filename in fileset(path.module, "roles/*.{yml,yaml}") :
replace(basename(filename), "/.ya?ml$/", "") => yamldecode(file(filename))
}

role_mappings = {
for filename in fileset(path.module, "role-mappings/*.{yml,yaml}") :
replace(basename(filename), "/.ya?ml$/", "") => yamldecode(file(filename))
}
index_files = fileset(path.module, "indices/*.{yml,yaml}")
index_template_files = fileset(path.module, "index-templates/*.{yml,yaml}")
ism_policy_files = fileset(path.module, "ism-policies/*.{yml,yaml}")
role_files = fileset(path.module, "roles/*.{yml,yaml}")
role_mapping_files = fileset(path.module, "role-mappings/*.{yml,yaml}")
}
2 changes: 1 addition & 1 deletion index.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "elasticsearch_index" "index" {
for_each = var.indices
for_each = local.indices

name = each.key
number_of_shards = try(each.value.number_of_shards, "")
Expand Down
2 changes: 1 addition & 1 deletion index_template.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "elasticsearch_index_template" "index_template" {
for_each = var.index_templates
for_each = local.index_templates

name = each.key
body = jsonencode(each.value)
Expand Down
2 changes: 1 addition & 1 deletion ism_policy.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "elasticsearch_opendistro_ism_policy" "ism_policy" {
for_each = var.ism_policies
for_each = local.ism_policies

policy_id = each.key
body = jsonencode({ "policy" = each.value })
Expand Down
31 changes: 31 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
locals {
indices = merge({
for filename in var.index_files :
replace(basename(filename), "/\\.(ya?ml|json)$/", "") =>
length(regexall("\\.ya?ml$", filename)) > 0 ? yamldecode(file(filename)) : jsondecode(file(filename))
}, var.indices)

index_templates = merge({
for filename in var.index_template_files :
replace(basename(filename), "/\\.(ya?ml|json)$/", "") =>
length(regexall("\\.ya?ml$", filename)) > 0 ? yamldecode(file(filename)) : jsondecode(file(filename))
}, var.index_templates)

ism_policies = merge({
for filename in var.ism_policy_files :
replace(basename(filename), "/\\.(ya?ml|json)$/", "") =>
length(regexall("\\.ya?ml$", filename)) > 0 ? yamldecode(file(filename)) : jsondecode(file(filename))
}, var.ism_policies)

roles = merge({
for filename in var.role_files :
replace(basename(filename), "/\\.(ya?ml|json)$/", "") =>
length(regexall("\\.ya?ml$", filename)) > 0 ? yamldecode(file(filename)) : jsondecode(file(filename))
}, var.roles)

role_mappings = merge({
for filename in var.role_mapping_files :
replace(basename(filename), "/\\.(ya?ml|json)$/", "") =>
length(regexall("\\.ya?ml$", filename)) > 0 ? yamldecode(file(filename)) : jsondecode(file(filename))
}, var.role_mappings)
}
4 changes: 2 additions & 2 deletions role_mapping.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
resource "elasticsearch_opendistro_roles_mapping" "role_mapping" {
for_each = {
for key, value in var.role_mappings :
for key, value in local.role_mappings :
key => value if !contains(["all_access", "security_manager"], key)
}

Expand All @@ -16,7 +16,7 @@ resource "elasticsearch_opendistro_roles_mapping" "role_mapping" {
resource "elasticsearch_opendistro_roles_mapping" "master_user_arn" {
for_each = {
for key in ["all_access", "security_manager"] :
key => try(var.role_mappings[key], {})
key => try(local.role_mappings[key], {})
}

role_name = each.key
Expand Down
2 changes: 1 addition & 1 deletion roles.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "elasticsearch_opendistro_role" "role" {
for_each = var.roles
for_each = local.roles

role_name = each.key
description = try(each.value.description, "")
Expand Down
30 changes: 30 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -131,30 +131,60 @@ variable "index_templates" {
default = {}
}

variable "index_template_files" {
description = "A set of all index template files to create."
type = set(string)
default = []
}

variable "ism_policies" {
description = "A map of all ISM policies to create."
type = map(any)
default = {}
}

variable "ism_policy_files" {
description = "A set of all ISM policy files to create."
type = set(string)
default = []
}

variable "indices" {
description = "A map of all indices to create."
type = map(any)
default = {}
}

variable "index_files" {
description = "A set of all index files to create."
type = set(string)
default = []
}

variable "roles" {
description = "A map of all roles to create."
type = map(any)
default = {}
}

variable "role_files" {
description = "A set of all role files to create."
type = set(string)
default = []
}

variable "role_mappings" {
description = "A map of all role mappings to create."
type = map(any)
default = {}
}

variable "role_mapping_files" {
description = "A set of all role mapping files to create."
type = set(string)
default = []
}

variable "tags" {
description = "A map of tags to add to all resources."
type = map(string)
Expand Down

0 comments on commit b126532

Please sign in to comment.