Skip to content

Commit b232d66

Browse files
Benbentwomilldrcloudpossebot
authored
Upstream Datadog-Integration (cloudposse/terraform-aws-components#478)
* Upstream Datadog Integration * remove component and introspection.mixin.tf * Update modules/datadog-integration/default.auto.tfvars Co-authored-by: Dan Miller <[email protected]> * pre-commit fixes Co-authored-by: Dan Miller <[email protected]> Co-authored-by: cloudpossebot <[email protected]>
1 parent d667780 commit b232d66

File tree

10 files changed

+380
-182
lines changed

10 files changed

+380
-182
lines changed

src/README.md

Lines changed: 55 additions & 41 deletions
Large diffs are not rendered by default.

src/asm.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
data "aws_secretsmanager_secret" "datadog_api_key" {
2+
count = local.asm_enabled ? 1 : 0
3+
name = format(var.datadog_api_secret_key_source_pattern, var.datadog_api_secret_key)
4+
5+
provider = aws.api_keys
6+
}
7+
8+
data "aws_secretsmanager_secret_version" "datadog_api_key" {
9+
count = local.asm_enabled ? 1 : 0
10+
secret_id = data.aws_secretsmanager_secret.datadog_api_key[0].id
11+
12+
provider = aws.api_keys
13+
}
14+
15+
data "aws_secretsmanager_secret" "datadog_app_key" {
16+
count = local.asm_enabled ? 1 : 0
17+
name = format(var.datadog_app_secret_key_source_pattern, var.datadog_app_secret_key)
18+
19+
provider = aws.api_keys
20+
}
21+
22+
data "aws_secretsmanager_secret_version" "datadog_app_key" {
23+
count = local.asm_enabled ? 1 : 0
24+
secret_id = data.aws_secretsmanager_secret.datadog_app_key[0].id
25+
26+
provider = aws.api_keys
27+
}

src/context.tf

Lines changed: 141 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# Cloud Posse's standard configuration inputs suitable for passing
99
# to Cloud Posse modules.
1010
#
11+
# curl -sL https://raw.githubusercontent.com/cloudposse/terraform-null-label/master/exports/context.tf -o context.tf
12+
#
1113
# Modules should access the whole context as `module.this.context`
1214
# to get the input variables with nulls for defaults,
1315
# for example `context = module.this.context`,
@@ -19,10 +21,12 @@
1921
#
2022

2123
module "this" {
22-
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.21.0"
24+
source = "cloudposse/label/null"
25+
version = "0.25.0" # requires Terraform >= 0.13.0
2326

2427
enabled = var.enabled
2528
namespace = var.namespace
29+
tenant = var.tenant
2630
environment = var.environment
2731
stage = var.stage
2832
name = var.name
@@ -33,30 +37,22 @@ module "this" {
3337
label_order = var.label_order
3438
regex_replace_chars = var.regex_replace_chars
3539
id_length_limit = var.id_length_limit
40+
label_key_case = var.label_key_case
41+
label_value_case = var.label_value_case
42+
descriptor_formats = var.descriptor_formats
43+
labels_as_tags = var.labels_as_tags
3644

3745
context = var.context
3846
}
3947

4048
# Copy contents of cloudposse/terraform-null-label/variables.tf here
4149

4250
variable "context" {
43-
type = object({
44-
enabled = bool
45-
namespace = string
46-
environment = string
47-
stage = string
48-
name = string
49-
delimiter = string
50-
attributes = list(string)
51-
tags = map(string)
52-
additional_tag_map = map(string)
53-
regex_replace_chars = string
54-
label_order = list(string)
55-
id_length_limit = number
56-
})
51+
type = any
5752
default = {
5853
enabled = true
5954
namespace = null
55+
tenant = null
6056
environment = null
6157
stage = null
6258
name = null
@@ -67,6 +63,17 @@ variable "context" {
6763
regex_replace_chars = null
6864
label_order = []
6965
id_length_limit = null
66+
label_key_case = null
67+
label_value_case = null
68+
descriptor_formats = {}
69+
# Note: we have to use [] instead of null for unset lists due to
70+
# https://github.com/hashicorp/terraform/issues/28137
71+
# which was not fixed until Terraform 1.0.0,
72+
# but we want the default to be all the labels in `label_order`
73+
# and we want users to be able to prevent all tag generation
74+
# by setting `labels_as_tags` to `[]`, so we need
75+
# a different sentinel to indicate "default"
76+
labels_as_tags = ["unset"]
7077
}
7178
description = <<-EOT
7279
Single object for setting entire context at once.
@@ -75,6 +82,16 @@ variable "context" {
7582
Individual variable settings (non-null) override settings in context object,
7683
except for attributes, tags, and additional_tag_map, which are merged.
7784
EOT
85+
86+
validation {
87+
condition = lookup(var.context, "label_key_case", null) == null ? true : contains(["lower", "title", "upper"], var.context["label_key_case"])
88+
error_message = "Allowed values: `lower`, `title`, `upper`."
89+
}
90+
91+
validation {
92+
condition = lookup(var.context, "label_value_case", null) == null ? true : contains(["lower", "title", "upper", "none"], var.context["label_value_case"])
93+
error_message = "Allowed values: `lower`, `title`, `upper`, `none`."
94+
}
7895
}
7996

8097
variable "enabled" {
@@ -86,69 +103,107 @@ variable "enabled" {
86103
variable "namespace" {
87104
type = string
88105
default = null
89-
description = "Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp'"
106+
description = "ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique"
107+
}
108+
109+
variable "tenant" {
110+
type = string
111+
default = null
112+
description = "ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for"
90113
}
91114

92115
variable "environment" {
93116
type = string
94117
default = null
95-
description = "Environment, e.g. 'uw2', 'us-west-2', OR 'prod', 'staging', 'dev', 'UAT'"
118+
description = "ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'"
96119
}
97120

98121
variable "stage" {
99122
type = string
100123
default = null
101-
description = "Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release'"
124+
description = "ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'"
102125
}
103126

104127
variable "name" {
105128
type = string
106129
default = null
107-
description = "Solution name, e.g. 'app' or 'jenkins'"
130+
description = <<-EOT
131+
ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
132+
This is the only ID element not also included as a `tag`.
133+
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input.
134+
EOT
108135
}
109136

110137
variable "delimiter" {
111138
type = string
112139
default = null
113140
description = <<-EOT
114-
Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes`.
141+
Delimiter to be used between ID elements.
115142
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all.
116143
EOT
117144
}
118145

119146
variable "attributes" {
120147
type = list(string)
121148
default = []
122-
description = "Additional attributes (e.g. `1`)"
149+
description = <<-EOT
150+
ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
151+
in the order they appear in the list. New attributes are appended to the
152+
end of the list. The elements of the list are joined by the `delimiter`
153+
and treated as a single ID element.
154+
EOT
155+
}
156+
157+
variable "labels_as_tags" {
158+
type = set(string)
159+
default = ["default"]
160+
description = <<-EOT
161+
Set of labels (ID elements) to include as tags in the `tags` output.
162+
Default is to include all labels.
163+
Tags with empty values will not be included in the `tags` output.
164+
Set to `[]` to suppress all generated tags.
165+
**Notes:**
166+
The value of the `name` tag, if included, will be the `id`, not the `name`.
167+
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
168+
changed in later chained modules. Attempts to change it will be silently ignored.
169+
EOT
123170
}
124171

125172
variable "tags" {
126173
type = map(string)
127174
default = {}
128-
description = "Additional tags (e.g. `map('BusinessUnit','XYZ')`"
175+
description = <<-EOT
176+
Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
177+
Neither the tag keys nor the tag values will be modified by this module.
178+
EOT
129179
}
130180

131181
variable "additional_tag_map" {
132182
type = map(string)
133183
default = {}
134-
description = "Additional tags for appending to tags_as_list_of_maps. Not added to `tags`."
184+
description = <<-EOT
185+
Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
186+
This is for some rare cases where resources want additional configuration of tags
187+
and therefore take a list of maps with tag key, value, and additional configuration.
188+
EOT
135189
}
136190

137191
variable "label_order" {
138192
type = list(string)
139193
default = null
140194
description = <<-EOT
141-
The naming order of the id output and Name tag.
195+
The order in which the labels (ID elements) appear in the `id`.
142196
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
143-
You can omit any of the 5 elements, but at least one must be present.
144-
EOT
197+
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present.
198+
EOT
145199
}
146200

147201
variable "regex_replace_chars" {
148202
type = string
149203
default = null
150204
description = <<-EOT
151-
Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`.
205+
Terraform regular expression (regex) string.
206+
Characters matching the regex will be removed from the ID elements.
152207
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits.
153208
EOT
154209
}
@@ -157,11 +212,68 @@ variable "id_length_limit" {
157212
type = number
158213
default = null
159214
description = <<-EOT
160-
Limit `id` to this many characters.
215+
Limit `id` to this many characters (minimum 6).
161216
Set to `0` for unlimited length.
162-
Set to `null` for default, which is `0`.
217+
Set to `null` for keep the existing setting, which defaults to `0`.
163218
Does not affect `id_full`.
164219
EOT
220+
validation {
221+
condition = var.id_length_limit == null ? true : var.id_length_limit >= 6 || var.id_length_limit == 0
222+
error_message = "The id_length_limit must be >= 6 if supplied (not null), or 0 for unlimited length."
223+
}
224+
}
225+
226+
variable "label_key_case" {
227+
type = string
228+
default = null
229+
description = <<-EOT
230+
Controls the letter case of the `tags` keys (label names) for tags generated by this module.
231+
Does not affect keys of tags passed in via the `tags` input.
232+
Possible values: `lower`, `title`, `upper`.
233+
Default value: `title`.
234+
EOT
235+
236+
validation {
237+
condition = var.label_key_case == null ? true : contains(["lower", "title", "upper"], var.label_key_case)
238+
error_message = "Allowed values: `lower`, `title`, `upper`."
239+
}
240+
}
241+
242+
variable "label_value_case" {
243+
type = string
244+
default = null
245+
description = <<-EOT
246+
Controls the letter case of ID elements (labels) as included in `id`,
247+
set as tag values, and output by this module individually.
248+
Does not affect values of tags passed in via the `tags` input.
249+
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
250+
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
251+
Default value: `lower`.
252+
EOT
253+
254+
validation {
255+
condition = var.label_value_case == null ? true : contains(["lower", "title", "upper", "none"], var.label_value_case)
256+
error_message = "Allowed values: `lower`, `title`, `upper`, `none`."
257+
}
258+
}
259+
260+
variable "descriptor_formats" {
261+
type = any
262+
default = {}
263+
description = <<-EOT
264+
Describe additional descriptors to be output in the `descriptors` output map.
265+
Map of maps. Keys are names of descriptors. Values are maps of the form
266+
`{
267+
format = string
268+
labels = list(string)
269+
}`
270+
(Type is `any` so the map values can later be enhanced to provide additional options.)
271+
`format` is a Terraform format string to be passed to the `format()` function.
272+
`labels` is a list of labels, in order, to pass to `format()` function.
273+
Label values will be normalized before being passed to `format()` so they will be
274+
identical to how they appear in `id`.
275+
Default is `{}` (`descriptors` output will be empty).
276+
EOT
165277
}
166278

167279
#### End of copy of cloudposse/terraform-null-label/variables.tf

src/default.auto.tfvars

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# This file is included by default in terraform plans
22

3-
enabled = true
3+
enabled = false
4+
5+

src/main.tf

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
module "datadog_integration" {
2-
source = "git::https://github.com/cloudposse/terraform-aws-datadog-integration.git?ref=tags/0.6.1"
2+
source = "cloudposse/datadog-integration/aws"
3+
version = "0.18.0"
34

45
datadog_aws_account_id = var.datadog_aws_account_id
56
integrations = var.integrations
6-
filter_tags = var.filter_tags
7-
host_tags = var.host_tags
7+
filter_tags = local.filter_tags
8+
host_tags = local.host_tags
89
excluded_regions = var.excluded_regions
910
account_specific_namespace_rules = var.account_specific_namespace_rules
1011

1112
context = module.this.context
1213
}
14+
15+
locals {
16+
enabled = module.this.enabled
17+
asm_enabled = local.enabled && var.datadog_secrets_store_type == "ASM"
18+
ssm_enabled = local.enabled && var.datadog_secrets_store_type == "SSM"
19+
20+
# https://docs.datadoghq.com/account_management/api-app-keys/
21+
datadog_api_key = local.enabled ? (local.asm_enabled ? data.aws_secretsmanager_secret_version.datadog_api_key[0].secret_string : data.aws_ssm_parameter.datadog_api_key[0].value) : null
22+
datadog_app_key = local.enabled ? (local.asm_enabled ? data.aws_secretsmanager_secret_version.datadog_app_key[0].secret_string : data.aws_ssm_parameter.datadog_app_key[0].value) : null
23+
24+
# Get the context tags and skip tags that we don't want applied to every resource.
25+
# i.e. we don't want name since each metric would be called something other than this component's name.
26+
# i.e. we don't want environment since each metric would come from gbl or a region and this component is deployed in gbl.
27+
context_tags = [for k, v in module.this.tags : "${lower(k)}:${v}" if contains(var.context_host_and_filter_tags, lower(k))]
28+
filter_tags = distinct(concat(var.filter_tags, local.context_tags))
29+
host_tags = distinct(concat(var.host_tags, local.context_tags))
30+
}

0 commit comments

Comments
 (0)