Skip to content

Commit d78d671

Browse files
feat: add support for access tags (#94)
1 parent 42efdad commit d78d671

File tree

7 files changed

+134
-13
lines changed

7 files changed

+134
-13
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ You need the following permissions to run this module.
109109
- `Administrator` platform access
110110
- `Manager` service access
111111

112+
Optionally, you need the following permissions to attach Access Management tags to resources in this module.
113+
114+
- IAM Services
115+
- **Tagging** service
116+
- `Administrator` platform access
117+
112118
## Note :
113119
- One worker pool should always be named as `default`. Refer [issue 2849](https://github.com/IBM-Cloud/terraform-provider-ibm/issues/2849) for further details.
114120

@@ -145,6 +151,8 @@ No modules.
145151
| [ibm_container_vpc_worker_pool.autoscaling_pool](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/container_vpc_worker_pool) | resource |
146152
| [ibm_container_vpc_worker_pool.pool](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/container_vpc_worker_pool) | resource |
147153
| [ibm_resource_instance.cos_instance](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_instance) | resource |
154+
| [ibm_resource_tag.cluster_access_tag](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_tag) | resource |
155+
| [ibm_resource_tag.cos_access_tag](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_tag) | resource |
148156
| [null_resource.confirm_network_healthy](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
149157
| [null_resource.reset_api_key](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
150158
| [ibm_container_cluster_config.cluster_config](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/container_cluster_config) | data source |
@@ -154,6 +162,7 @@ No modules.
154162

155163
| Name | Description | Type | Default | Required |
156164
|------|-------------|------|---------|:--------:|
165+
| <a name="input_access_tags"></a> [access\_tags](#input\_access\_tags) | A list of access tags to apply to the resources created by the module, see https://cloud.ibm.com/docs/account?topic=account-access-tags-tutorial for more details | `list(string)` | `[]` | no |
157166
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | The name that will be assigned to the provisioned cluster | `string` | n/a | yes |
158167
| <a name="input_cluster_ready_when"></a> [cluster\_ready\_when](#input\_cluster\_ready\_when) | The cluster is ready when one of the following: MasterNodeReady (not recommended), OneWorkerNodeReady, Normal, IngressReady | `string` | `"IngressReady"` | no |
159168
| <a name="input_cos_name"></a> [cos\_name](#input\_cos\_name) | Name of the COS instance to provision. New instance only provisioned if `use_existing_cos = false`. Default: `<cluster_name>_cos` | `string` | `null` | no |

examples/standard/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ module "ocp_base" {
8989
instance_id = module.kp_all_inclusive.key_protect_guid
9090
crk_id = module.kp_all_inclusive.keys["ocp.${var.prefix}-cluster-key"].key_id
9191
}
92+
access_tags = var.access_tags
9293
}
9394

9495
##############################################################################

examples/standard/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ variable "worker_pools" {
6060
default = []
6161
}
6262

63+
variable "access_tags" {
64+
type = list(string)
65+
description = "Optional list of access management tags to be added to the created resources."
66+
default = []
67+
}
68+
6369
##############################################################################
6470
# VPC variables
6571
##############################################################################

main.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ resource "ibm_resource_instance" "cos_instance" {
4444
location = local.cos_location
4545
}
4646

47+
resource "ibm_resource_tag" "cos_access_tag" {
48+
count = var.use_existing_cos || length(var.access_tags) == 0 ? 0 : 1
49+
resource_id = ibm_resource_instance.cos_instance[0].crn
50+
tags = var.access_tags
51+
tag_type = "access"
52+
}
53+
4754
##############################################################################
4855
# Create a OCP Cluster
4956
##############################################################################
@@ -170,6 +177,17 @@ resource "ibm_container_vpc_cluster" "autoscaling_cluster" {
170177
}
171178
}
172179

180+
##############################################################################
181+
# Cluster Access Tag
182+
##############################################################################
183+
184+
resource "ibm_resource_tag" "cluster_access_tag" {
185+
count = length(var.access_tags) == 0 ? 0 : 1
186+
resource_id = var.ignore_worker_pool_size_changes ? ibm_container_vpc_cluster.autoscaling_cluster[0].crn : ibm_container_vpc_cluster.cluster[0].crn
187+
tags = var.access_tags
188+
tag_type = "access"
189+
}
190+
173191
# Cluster provisioning will automatically create an IAM API key called "containers-kubernetes-key" if one does not exist
174192
# for the given region and resource group. The API key is used to access several services, such as the IBM Cloud classic
175193
# infrastructure portfolio, and is required to manage the cluster. Immediately after the IAM API key is created and

module-metadata.json

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
{
22
"path": ".",
33
"variables": {
4+
"access_tags": {
5+
"name": "access_tags",
6+
"type": "list(string)",
7+
"description": "A list of access tags to apply to the resources created by the module, see https://cloud.ibm.com/docs/account?topic=account-access-tags-tutorial for more details",
8+
"default": [],
9+
"source": [
10+
"ibm_resource_tag.cluster_access_tag.count",
11+
"ibm_resource_tag.cluster_access_tag.tags",
12+
"ibm_resource_tag.cos_access_tag.tags"
13+
],
14+
"pos": {
15+
"filename": "variables.tf",
16+
"line": 148
17+
},
18+
"min_length": 1,
19+
"max_length": 128,
20+
"matches": "^[A-Za-z0-9:_ .-]+$",
21+
"computed": true,
22+
"elem": {
23+
"type": "TypeString"
24+
}
25+
},
426
"cluster_name": {
527
"name": "cluster_name",
628
"type": "string",
@@ -92,14 +114,19 @@
92114
"type": "bool",
93115
"description": "Enable if using worker autoscaling. Stops Terraform managing worker count",
94116
"default": false,
117+
"required": true,
95118
"source": [
96119
"ibm_container_vpc_cluster.autoscaling_cluster.count",
97-
"ibm_container_vpc_cluster.cluster.count"
120+
"ibm_container_vpc_cluster.cluster.count",
121+
"ibm_resource_tag.cluster_access_tag.resource_id"
98122
],
99123
"pos": {
100124
"filename": "variables.tf",
101125
"line": 66
102-
}
126+
},
127+
"min_length": 1,
128+
"max_length": 1024,
129+
"matches": "^crn:v1(:[a-zA-Z0-9 \\-\\._~\\*\\+,;=!$\u0026'\\(\\)\\/\\?#\\[\\]@]*){8}$|^[0-9]+$"
103130
},
104131
"kms_config": {
105132
"name": "kms_config",
@@ -194,7 +221,8 @@
194221
"description": "Flag indicating whether or not to use an existing COS instance",
195222
"default": false,
196223
"source": [
197-
"ibm_resource_instance.cos_instance.count"
224+
"ibm_resource_instance.cos_instance.count",
225+
"ibm_resource_tag.cos_access_tag.count"
198226
],
199227
"pos": {
200228
"filename": "variables.tf",
@@ -212,7 +240,7 @@
212240
],
213241
"pos": {
214242
"filename": "variables.tf",
215-
"line": 154
243+
"line": 167
216244
}
217245
},
218246
"vpc_id": {
@@ -228,7 +256,7 @@
228256
],
229257
"pos": {
230258
"filename": "variables.tf",
231-
"line": 149
259+
"line": 162
232260
},
233261
"immutable": true
234262
},
@@ -399,7 +427,7 @@
399427
},
400428
"pos": {
401429
"filename": "main.tf",
402-
"line": 113
430+
"line": 120
403431
}
404432
},
405433
"ibm_container_vpc_cluster.cluster": {
@@ -422,7 +450,7 @@
422450
},
423451
"pos": {
424452
"filename": "main.tf",
425-
"line": 51
453+
"line": 58
426454
}
427455
},
428456
"ibm_container_vpc_worker_pool.autoscaling_pool": {
@@ -438,7 +466,7 @@
438466
},
439467
"pos": {
440468
"filename": "main.tf",
441-
"line": 253
469+
"line": 271
442470
}
443471
},
444472
"ibm_container_vpc_worker_pool.pool": {
@@ -454,7 +482,7 @@
454482
},
455483
"pos": {
456484
"filename": "main.tf",
457-
"line": 212
485+
"line": 230
458486
}
459487
},
460488
"ibm_resource_instance.cos_instance": {
@@ -473,6 +501,39 @@
473501
"line": 37
474502
}
475503
},
504+
"ibm_resource_tag.cluster_access_tag": {
505+
"mode": "managed",
506+
"type": "ibm_resource_tag",
507+
"name": "cluster_access_tag",
508+
"attributes": {
509+
"count": "access_tags",
510+
"resource_id": "ignore_worker_pool_size_changes",
511+
"tags": "access_tags"
512+
},
513+
"provider": {
514+
"name": "ibm"
515+
},
516+
"pos": {
517+
"filename": "main.tf",
518+
"line": 184
519+
}
520+
},
521+
"ibm_resource_tag.cos_access_tag": {
522+
"mode": "managed",
523+
"type": "ibm_resource_tag",
524+
"name": "cos_access_tag",
525+
"attributes": {
526+
"count": "use_existing_cos",
527+
"tags": "access_tags"
528+
},
529+
"provider": {
530+
"name": "ibm"
531+
},
532+
"pos": {
533+
"filename": "main.tf",
534+
"line": 47
535+
}
536+
},
476537
"null_resource.confirm_network_healthy": {
477538
"mode": "managed",
478539
"type": "null_resource",
@@ -485,7 +546,7 @@
485546
},
486547
"pos": {
487548
"filename": "main.tf",
488-
"line": 313
549+
"line": 331
489550
}
490551
},
491552
"null_resource.reset_api_key": {
@@ -497,7 +558,7 @@
497558
},
498559
"pos": {
499560
"filename": "main.tf",
500-
"line": 187
561+
"line": 205
501562
}
502563
}
503564
},
@@ -515,7 +576,7 @@
515576
},
516577
"pos": {
517578
"filename": "main.tf",
518-
"line": 201
579+
"line": 219
519580
}
520581
},
521582
"data.ibm_container_cluster_versions.cluster_versions": {

tests/pr_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,42 @@
22
package test
33

44
import (
5+
"log"
56
"os"
67
"testing"
78

89
"github.com/stretchr/testify/assert"
910
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/cloudinfo"
11+
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/common"
1012
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper"
1113
)
1214

1315
const resourceGroup = "geretain-test-base-ocp-vpc"
1416
const standardExampleTerraformDir = "examples/standard"
1517

18+
// Define a struct with fields that match the structure of the YAML data
19+
const yamlLocation = "../common-dev-assets/common-go-assets/common-permanent-resources.yaml"
20+
1621
// Ensure there is one test per supported OCP version
1722
const ocpVersion1 = "4.12"
1823
const ocpVersion2 = "4.11"
1924
const ocpVersion3 = "4.10"
2025
const ocpVersion4 = "4.9"
2126

2227
var sharedInfoSvc *cloudinfo.CloudInfoService
28+
var permanentResources map[string]interface{}
2329

2430
// TestMain will be run before any parallel tests, used to set up a shared InfoService object to track region usage
2531
// for multiple tests
2632
func TestMain(m *testing.M) {
27-
2833
sharedInfoSvc, _ = cloudinfo.NewCloudInfoServiceFromEnv("TF_VAR_ibmcloud_api_key", cloudinfo.CloudInfoServiceOptions{})
34+
35+
var err error
36+
permanentResources, err = common.LoadMapFromYaml(yamlLocation)
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
2941
os.Exit(m.Run())
3042
}
3143

@@ -38,6 +50,7 @@ func setupOptions(t *testing.T, prefix string, terraformDir string) *testhelper.
3850
CloudInfoService: sharedInfoSvc,
3951
TerraformVars: map[string]interface{}{
4052
"ocp_version": ocpVersion1,
53+
"access_tags": permanentResources["accessTags"],
4154
},
4255
})
4356

variables.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ variable "kms_config" {
145145
default = null
146146
}
147147

148+
variable "access_tags" {
149+
type = list(string)
150+
description = "A list of access tags to apply to the resources created by the module, see https://cloud.ibm.com/docs/account?topic=account-access-tags-tutorial for more details"
151+
default = []
152+
153+
validation {
154+
condition = alltrue([
155+
for tag in var.access_tags : can(regex("[\\w\\-_\\.]+:[\\w\\-_\\.]+", tag)) && length(tag) <= 128
156+
])
157+
error_message = "Tags must match the regular expression \"[\\w\\-_\\.]+:[\\w\\-_\\.]+\", see https://cloud.ibm.com/docs/account?topic=account-tag&interface=ui#limits for more details"
158+
}
159+
}
160+
148161
# VPC Variables
149162
variable "vpc_id" {
150163
type = string

0 commit comments

Comments
 (0)