Skip to content

Commit 2f89973

Browse files
authored
fix: added a default value for the operations variable to make it backward compatible with recent CBR service updates around api types (#305)
1 parent 3ea272b commit 2f89973

File tree

9 files changed

+45
-33
lines changed

9 files changed

+45
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ You need the following permissions to run this module.
110110
| <a name="input_enforcement_mode"></a> [enforcement\_mode](#input\_enforcement\_mode) | (String) The rule enforcement mode | `string` | `"report"` | no |
111111
| <a name="input_excluded_addresses"></a> [excluded\_addresses](#input\_excluded\_addresses) | (Optional, List) The list of excluded addresses in the zone | <pre>list(object({<br> type = optional(string)<br> value = optional(string)<br> }))</pre> | `[]` | no |
112112
| <a name="input_name"></a> [name](#input\_name) | (Optional, String) The name of the zone | `string` | `null` | no |
113-
| <a name="input_operations"></a> [operations](#input\_operations) | (Optional, List) The operations this rule applies to | <pre>list(object({<br> api_types = list(object({<br> api_type_id = string<br> }))<br> }))</pre> | `[]` | no |
113+
| <a name="input_operations"></a> [operations](#input\_operations) | (Optional, List) The operations this rule applies to | <pre>list(object({<br> api_types = list(object({<br> api_type_id = string<br> }))<br> }))</pre> | <pre>[<br> {<br> "api_types": [<br> {<br> "api_type_id": "crn:v1:bluemix:public:context-based-restrictions::::api-type:"<br> }<br> ]<br> }<br>]</pre> | no |
114114
| <a name="input_resources"></a> [resources](#input\_resources) | (Optional, List) The resources this rule apply to | <pre>list(object({<br> attributes = list(object({<br> name = string<br> value = string<br> operator = optional(string)<br> }))<br> tags = optional(list(object({ #These access tags should match to the target service access tags for the CBR rules to work<br> name = string<br> value = string<br> operator = optional(string)<br> })))<br> }))</pre> | `[]` | no |
115115
| <a name="input_rule_contexts"></a> [rule\_contexts](#input\_rule\_contexts) | (List) The contexts the rule applies to | <pre>list(object({<br> attributes = list(object({<br> name = string<br> value = string<br> }))<br> }))</pre> | <pre>[<br> {<br> "attributes": [<br> {<br> "name": "va",<br> "value": "va"<br> }<br> ]<br> }<br>]</pre> | no |
116116
| <a name="input_rule_description"></a> [rule\_description](#input\_rule\_description) | (Optional, String) The description of the rule | `string` | `null` | no |

examples/multizone-rule/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,4 @@ module "cbr_rule" {
144144
enforcement_mode = var.enforcement_mode
145145
rule_contexts = local.rule_contexts
146146
resources = local.rule_resources
147-
operations = []
148147
}

module-metadata.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@
6161
"name": "operations",
6262
"type": "list(object({\n api_types = list(object({\n api_type_id = string\n }))\n }))",
6363
"description": "(Optional, List) The operations this rule applies to",
64-
"default": [],
64+
"default": [
65+
{
66+
"api_types": [
67+
{
68+
"api_type_id": "crn:v1:bluemix:public:context-based-restrictions::::api-type:"
69+
}
70+
]
71+
}
72+
],
6573
"source": [
6674
"module.cbr_rule"
6775
],

modules/cbr-rule-module/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ No modules.
2525
| Name | Description | Type | Default | Required |
2626
|------|-------------|------|---------|:--------:|
2727
| <a name="input_enforcement_mode"></a> [enforcement\_mode](#input\_enforcement\_mode) | (String) The rule enforcement mode | `string` | `"report"` | no |
28-
| <a name="input_operations"></a> [operations](#input\_operations) | (Optional, List) The operations this rule applies to | <pre>list(object({<br> api_types = list(object({<br> api_type_id = string<br> }))<br> }))</pre> | `[]` | no |
28+
| <a name="input_operations"></a> [operations](#input\_operations) | (Optional, List) The operations this rule applies to, by default it will protect all of the service and platform APIs the target service supports. | <pre>list(object({<br> api_types = list(object({<br> api_type_id = string<br> }))<br> }))</pre> | <pre>[<br> {<br> "api_types": [<br> {<br> "api_type_id": "crn:v1:bluemix:public:context-based-restrictions::::api-type:"<br> }<br> ]<br> }<br>]</pre> | no |
2929
| <a name="input_resources"></a> [resources](#input\_resources) | (List) The resources this rule apply to | <pre>list(object({<br> attributes = optional(list(object({<br> name = string<br> value = string<br> operator = optional(string)<br> })))<br> tags = optional(list(object({<br> name = string<br> value = string<br> operator = optional(string)<br> })))<br> }))</pre> | n/a | yes |
3030
| <a name="input_rule_contexts"></a> [rule\_contexts](#input\_rule\_contexts) | (List) The contexts the rule applies to | <pre>list(object({<br> attributes = optional(list(object({<br> name = string<br> value = string<br> })))<br> }))</pre> | n/a | yes |
3131
| <a name="input_rule_description"></a> [rule\_description](#input\_rule\_description) | (Optional, String) The description of the rule | `string` | `null` | no |

modules/cbr-rule-module/variables.tf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,14 @@ variable "operations" {
8181
api_type_id = string
8282
}))
8383
}))
84-
description = "(Optional, List) The operations this rule applies to"
85-
default = []
84+
description = "(Optional, List) The operations this rule applies to, by default it will protect all of the service and platform APIs the target service supports."
85+
default = [{
86+
api_types = [{
87+
api_type_id = "crn:v1:bluemix:public:context-based-restrictions::::api-type:"
88+
}]
89+
}]
8690
validation {
87-
condition = var.operations != null
88-
error_message = "operations cannot be null, an empty list is valid"
91+
condition = length(var.operations) > 0
92+
error_message = "Operations cannot be null or an empty list"
8993
}
9094
}

modules/cbr-service-profile/main.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,15 @@ module "cbr_rule" {
9090
rule_contexts = local.rule_contexts
9191
operations = (length(lookup(local.operations_apitype_val, var.target_service_details[count.index].target_service_name, [])) > 0) ? [{
9292
api_types = [
93-
# lookup the map for the target service name, if not present make api_type_id as empty
93+
# lookup the map for the target service name, if empty then pass default value
9494
for apitype in lookup(local.operations_apitype_val, var.target_service_details[count.index].target_service_name, []) : {
9595
api_type_id = apitype
9696
}]
97-
}] : []
97+
}] : [{
98+
api_types = [{
99+
api_type_id = "crn:v1:bluemix:public:context-based-restrictions::::api-type:"
100+
}]
101+
}]
98102

99103
resources = [{
100104
tags = var.target_service_details[count.index].tags != null ? [for tag in var.target_service_details[count.index].tags : {

modules/fscloud/main.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,15 @@ module "cbr_rule" {
311311
rule_contexts = lookup(local.allow_rules_by_service, each.key, [])
312312
operations = (length(lookup(local.operations_apitype_val, each.key, [])) > 0) ? [{
313313
api_types = [
314-
# lookup the map for the target service name, if not present make api_type_id as empty
314+
# lookup the map for the target service name, if empty then pass default value
315315
for apitype in lookup(local.operations_apitype_val, each.key, []) : {
316316
api_type_id = apitype
317317
}]
318-
}] : []
318+
}] : [{
319+
api_types = [{
320+
api_type_id = "crn:v1:bluemix:public:context-based-restrictions::::api-type:"
321+
}]
322+
}]
319323

320324
resources = [{
321325
tags = try(each.value.tags, null) != null ? [for tag in each.value.tags : {

tests/pr_test.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,26 +167,15 @@ func TestRunCompleteExample(t *testing.T) {
167167

168168
assert.ElementsMatch(t, expectedTags, rules.Resources[0].Tags, "expected resource tags not found")
169169
})
170-
t.Run("verify no operation set", func(t *testing.T) {
171-
// Note: COS has no operations that can be set
172-
// Leaving this code here as a reference for others
173-
//expectedOperations := []contextbasedrestrictionsv1.OperationsList{
174-
// {APITypes: []contextbasedrestrictionsv1.APIType{{
175-
// APITypeID: core.StringPtr(""),
176-
// DisplayName: core.StringPtr(""),
177-
// Description: core.StringPtr(""),
178-
// Actions: []contextbasedrestrictionsv1.Action{{
179-
// ActionID: core.StringPtr(""),
180-
// Description: core.StringPtr(""),
181-
// }},
182-
// },
183-
// }},
184-
//}
185-
//
186-
//assert.ElementsMatch(t, expectedOperations, rules.Operations)
187-
188-
// Assert COS has no operations set as expected
189-
assert.Nil(t, rules.Operations)
170+
t.Run("verify rule operation set", func(t *testing.T) {
171+
expectedOperations := &contextbasedrestrictionsv1.NewRuleOperations{
172+
APITypes: []contextbasedrestrictionsv1.NewRuleOperationsAPITypesItem{
173+
{
174+
APITypeID: core.StringPtr("crn:v1:bluemix:public:context-based-restrictions::::api-type:"),
175+
},
176+
},
177+
}
178+
assert.Equal(t, expectedOperations, rules.Operations, "expected operations not found")
190179
})
191180

192181
}

variables.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,9 @@ variable "operations" {
9696
}))
9797
}))
9898
description = "(Optional, List) The operations this rule applies to"
99-
default = []
99+
default = [{
100+
api_types = [{
101+
api_type_id = "crn:v1:bluemix:public:context-based-restrictions::::api-type:"
102+
}]
103+
}]
100104
}

0 commit comments

Comments
 (0)