Skip to content

Commit 73bba85

Browse files
authored
feat: add capability to create service credentials in root module (#41)
1 parent 835483c commit 73bba85

File tree

10 files changed

+151
-35
lines changed

10 files changed

+151
-35
lines changed

.secrets.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "go.sum|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2023-02-07T13:40:16Z",
6+
"generated_at": "2023-02-22T09:31:45Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ You need the following permissions to run this module.
6161
| Name | Type |
6262
|------|------|
6363
| [ibm_database.postgresql_db](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/database) | resource |
64+
| [ibm_resource_key.service_credentials](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/resource_key) | resource |
6465

6566
## Inputs
6667

@@ -83,6 +84,7 @@ You need the following permissions to run this module.
8384
| <a name="input_region"></a> [region](#input\_region) | The region postgresql is to be created on. The region must support BYOK if key\_protect\_key\_crn is used | `string` | `"us-south"` | no |
8485
| <a name="input_resource_group_id"></a> [resource\_group\_id](#input\_resource\_group\_id) | The resource group ID where the postgresql will be created | `string` | n/a | yes |
8586
| <a name="input_resource_tags"></a> [resource\_tags](#input\_resource\_tags) | Optional list of tags to be added to created resources | `list(string)` | `[]` | no |
87+
| <a name="input_service_credential_names"></a> [service\_credential\_names](#input\_service\_credential\_names) | Map of name, role for service credentials that you want to create for the database | `map(string)` | `{}` | no |
8688
| <a name="input_service_endpoints"></a> [service\_endpoints](#input\_service\_endpoints) | Sets the endpoint of the Postgresql instance, valid values are 'public', 'private', or 'public-and-private' | `string` | `"private"` | no |
8789

8890
## Outputs
@@ -91,6 +93,8 @@ You need the following permissions to run this module.
9193
|------|-------------|
9294
| <a name="output_guid"></a> [guid](#output\_guid) | Postgresql instance guid |
9395
| <a name="output_id"></a> [id](#output\_id) | Postgresql instance id |
96+
| <a name="output_service_credentials_json"></a> [service\_credentials\_json](#output\_service\_credentials\_json) | Service credentials json map |
97+
| <a name="output_service_credentials_object"></a> [service\_credentials\_object](#output\_service\_credentials\_object) | Service credentials object |
9498
| <a name="output_version"></a> [version](#output\_version) | Postgresql instance version |
9599
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
96100
<!-- BEGIN CONTRIBUTING HOOK -->

examples/complete/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
An end-to-end example that uses the module's default variable values. This example uses the IBM Cloud terraform provider to:
44

55
- Create a new resource group if one is not passed in.
6-
- Create a new ICD Postgresql database instance and credentials.
6+
- Create a new ICD Postgresql database instance.
77
- Create Key Protect instance with root key.
88
- Backend encryption using generated Key Protect key.
99
- Create a Sample VPC.

examples/complete/main.tf

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ module "cbr_zone" {
7676
##############################################################################
7777

7878
module "postgresql_db" {
79-
source = "../../"
80-
resource_group_id = module.resource_group.resource_group_id
81-
name = "${var.prefix}-postgres"
82-
region = var.region
83-
service_endpoints = "private"
84-
pg_version = var.pg_version
85-
key_protect_key_crn = module.key_protect_all_inclusive.keys["icd-pg.${var.prefix}-pg"].crn
86-
resource_tags = var.resource_tags
79+
source = "../../"
80+
resource_group_id = module.resource_group.resource_group_id
81+
name = "${var.prefix}-postgres"
82+
region = var.region
83+
service_endpoints = "private"
84+
pg_version = var.pg_version
85+
key_protect_key_crn = module.key_protect_all_inclusive.keys["icd-pg.${var.prefix}-pg"].crn
86+
resource_tags = var.resource_tags
87+
service_credential_names = var.service_credential_names
8788
cbr_rules = [
8889
{
8990
description = "${var.prefix}-postgres access only from vpc"
@@ -103,14 +104,3 @@ module "postgresql_db" {
103104
}
104105
]
105106
}
106-
107-
##############################################################################
108-
# Service Credentials
109-
##############################################################################
110-
111-
resource "ibm_resource_key" "service_credentials" {
112-
count = length(var.service_credentials)
113-
name = var.service_credentials[count.index]
114-
resource_instance_id = module.postgresql_db.id
115-
tags = var.resource_tags
116-
}

examples/complete/outputs.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,15 @@ output "version" {
1010
description = "Postgresql instance version"
1111
value = module.postgresql_db.version
1212
}
13+
14+
output "service_credentials_json" {
15+
description = "Service credentials json map"
16+
value = module.postgresql_db.service_credentials_json
17+
sensitive = true
18+
}
19+
20+
output "service_credentials_object" {
21+
description = "Service credentials object"
22+
value = module.postgresql_db.service_credentials_object
23+
sensitive = true
24+
}

examples/complete/variables.tf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ variable "pg_version" {
3434
default = null
3535
}
3636

37-
variable "service_credentials" {
38-
description = "A list of service credentials that you want to create for the database"
39-
type = list(string)
40-
default = ["postgressql_credential_microservices", "postgressql_credential_dev_1", "postgressql_credential_dev_2"]
37+
variable "service_credential_names" {
38+
description = "Map of name, role for service credentials that you want to create for the database"
39+
type = map(string)
40+
default = {
41+
"postgressql_credential_microservices" : "Administrator",
42+
"postgressql_credential_dev_1" : "Administrator",
43+
"postgressql_credential_dev_2" : "Administrator"
44+
}
4145
}

main.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,35 @@ module "cbr_rule" {
126126
]
127127
}]
128128
}
129+
130+
##############################################################################
131+
# Service Credentials
132+
##############################################################################
133+
134+
resource "ibm_resource_key" "service_credentials" {
135+
for_each = var.service_credential_names
136+
name = each.key
137+
role = each.value
138+
resource_instance_id = ibm_database.postgresql_db.id
139+
tags = var.resource_tags
140+
}
141+
142+
locals {
143+
# used for output only
144+
service_credentials_json = length(var.service_credential_names) > 0 ? {
145+
for service_credential in ibm_resource_key.service_credentials :
146+
service_credential["name"] => service_credential["credentials_json"]
147+
} : null
148+
149+
service_credentials_object = length(var.service_credential_names) > 0 ? {
150+
hostname = ibm_resource_key.service_credentials[keys(var.service_credential_names)[0]].credentials["connection.postgres.hosts.0.hostname"]
151+
certificate = ibm_resource_key.service_credentials[keys(var.service_credential_names)[0]].credentials["connection.postgres.certificate.certificate_base64"]
152+
credentials = {
153+
for service_credential in ibm_resource_key.service_credentials :
154+
service_credential["name"] => {
155+
username = service_credential.credentials["connection.postgres.authentication.username"]
156+
password = service_credential.credentials["connection.postgres.authentication.password"]
157+
}
158+
}
159+
} : null
160+
}

module-metadata.json

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"default": [],
99
"pos": {
1010
"filename": "variables.tf",
11-
"line": 126
11+
"line": 137
1212
}
1313
},
1414
"auto_scaling": {
@@ -22,7 +22,7 @@
2222
},
2323
"pos": {
2424
"filename": "variables.tf",
25-
"line": 153
25+
"line": 164
2626
}
2727
},
2828
"backup_crn": {
@@ -43,7 +43,7 @@
4343
"description": "(Optional) The CRN of a key protect key, that you want to use for encrypting disk that holds deployment backups. If null, will use 'key_protect_key_crn' as encryption key. If 'key_protect_key_crn' is also null database is encrypted by using randomly generated keys.",
4444
"pos": {
4545
"filename": "variables.tf",
46-
"line": 196
46+
"line": 207
4747
}
4848
},
4949
"cbr_rules": {
@@ -60,7 +60,7 @@
6060
],
6161
"pos": {
6262
"filename": "variables.tf",
63-
"line": 207
63+
"line": 218
6464
}
6565
},
6666
"configuration": {
@@ -72,7 +72,7 @@
7272
],
7373
"pos": {
7474
"filename": "variables.tf",
75-
"line": 135
75+
"line": 146
7676
}
7777
},
7878
"key_protect_key_crn": {
@@ -84,7 +84,7 @@
8484
],
8585
"pos": {
8686
"filename": "variables.tf",
87-
"line": 190
87+
"line": 201
8888
},
8989
"immutable": true
9090
},
@@ -125,7 +125,7 @@
125125
"default": 3,
126126
"pos": {
127127
"filename": "variables.tf",
128-
"line": 97
128+
"line": 108
129129
}
130130
},
131131
"name": {
@@ -208,12 +208,14 @@
208208
"description": "Optional list of tags to be added to created resources",
209209
"default": [],
210210
"source": [
211-
"ibm_database.postgresql_db.tags"
211+
"ibm_database.postgresql_db.tags",
212+
"ibm_resource_key.service_credentials.tags"
212213
],
213214
"pos": {
214215
"filename": "variables.tf",
215-
"line": 120
216+
"line": 131
216217
},
218+
"cloud_data_type": "tags",
217219
"min_length": 1,
218220
"max_length": 128,
219221
"matches": "^[A-Za-z0-9:_ .-]+$",
@@ -222,6 +224,19 @@
222224
"type": "TypeString"
223225
}
224226
},
227+
"service_credential_names": {
228+
"name": "service_credential_names",
229+
"type": "map(string)",
230+
"description": "Map of name, role for service credentials that you want to create for the database",
231+
"default": {},
232+
"source": [
233+
"ibm_resource_key.service_credentials.for_each"
234+
],
235+
"pos": {
236+
"filename": "variables.tf",
237+
"line": 95
238+
}
239+
},
225240
"service_endpoints": {
226241
"name": "service_endpoints",
227242
"type": "string",
@@ -232,7 +247,7 @@
232247
],
233248
"pos": {
234249
"filename": "variables.tf",
235-
"line": 110
250+
"line": 121
236251
},
237252
"options": "public, private, public-and-private"
238253
}
@@ -257,6 +272,26 @@
257272
"line": 5
258273
}
259274
},
275+
"service_credentials_json": {
276+
"name": "service_credentials_json",
277+
"description": "Service credentials json map",
278+
"value": "local.service_credentials_json",
279+
"sensitive": true,
280+
"pos": {
281+
"filename": "outputs.tf",
282+
"line": 20
283+
}
284+
},
285+
"service_credentials_object": {
286+
"name": "service_credentials_object",
287+
"description": "Service credentials object",
288+
"value": "local.service_credentials_object",
289+
"sensitive": true,
290+
"pos": {
291+
"filename": "outputs.tf",
292+
"line": 26
293+
}
294+
},
260295
"version": {
261296
"name": "version",
262297
"description": "Postgresql instance version",
@@ -303,6 +338,22 @@
303338
"filename": "main.tf",
304339
"line": 12
305340
}
341+
},
342+
"ibm_resource_key.service_credentials": {
343+
"mode": "managed",
344+
"type": "ibm_resource_key",
345+
"name": "service_credentials",
346+
"attributes": {
347+
"for_each": "service_credential_names",
348+
"tags": "resource_tags"
349+
},
350+
"provider": {
351+
"name": "ibm"
352+
},
353+
"pos": {
354+
"filename": "main.tf",
355+
"line": 134
356+
}
306357
}
307358
},
308359
"data_resources": {},

outputs.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ output "version" {
1616
description = "Postgresql instance version"
1717
value = ibm_database.postgresql_db.version
1818
}
19+
20+
output "service_credentials_json" {
21+
description = "Service credentials json map"
22+
value = local.service_credentials_json
23+
sensitive = true
24+
}
25+
26+
output "service_credentials_object" {
27+
description = "Service credentials object"
28+
value = local.service_credentials_object
29+
sensitive = true
30+
}

variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ variable "member_cpu_count" {
9292
}
9393
}
9494

95+
variable "service_credential_names" {
96+
description = "Map of name, role for service credentials that you want to create for the database"
97+
type = map(string)
98+
default = {}
99+
100+
validation {
101+
condition = alltrue([for name, role in var.service_credential_names : contains(["Administrator", "Operator", "Viewer", "Editor"], role)])
102+
error_message = "Valid values for service credential roles are 'Administrator', 'Operator', 'Viewer', and `Editor`"
103+
}
104+
}
105+
95106
# actual scaling of the resources could take some time to apply
96107
# Members can be scaled up but not down
97108
variable "members" {

0 commit comments

Comments
 (0)