Skip to content

Commit 3142064

Browse files
authored
feat: Ability to add optional location for each serviceRef when creating zone (#457)
1 parent 2e9cff1 commit 3142064

File tree

6 files changed

+62
-35
lines changed

6 files changed

+62
-35
lines changed

examples/multi-service-profile/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,4 @@ module "cbr_rule_multi_service_profile" {
6565
zone_vpc_crn_list = local.zone_vpc_crn_list
6666
target_service_details = local.target_services_details
6767
endpoints = var.endpoints
68-
location = var.location
6968
}

examples/multi-service-profile/variables.tf

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ variable "region" {
1414
type = string
1515
}
1616

17-
variable "location" {
18-
description = "The region in which the network zone is scoped"
19-
type = string
20-
default = "dal" # dal metro is the equivalent location for the us-south region
21-
}
22-
2317
variable "resource_group" {
2418
type = string
2519
description = "An existing resource group name to use for this example, if unset a new resource group will be created"
@@ -33,11 +27,23 @@ variable "resource_tags" {
3327
}
3428

3529
variable "zone_service_ref_list" {
36-
type = list(string)
37-
default = ["cloud-object-storage", "server-protect"]
38-
description = "(List) Service reference for the zone creation"
30+
type = map(object({
31+
serviceRef_location = optional(list(string), [])
32+
}))
33+
description = "Provide a valid service reference with the location where the context-based restriction zones are created. If no value is specified for `serviceRef_location`, the zones are not scoped to any location."
34+
default = {
35+
"cloud-object-storage" = {
36+
serviceRef_location = ["syd", "au"]
37+
},
38+
"server-protect" = {
39+
serviceRef_location = ["au"]
40+
},
41+
"directlink" = {}, # directlink does not support restriction per location, hence no value is specified for serviceRef_location.
42+
"event-notifications" = {}
43+
}
3944
}
4045

46+
4147
variable "endpoints" {
4248
type = list(string)
4349
description = "List specific endpoint types for target services, valid values for endpoints are 'public', 'private' or 'direct'"

modules/cbr-service-profile/README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@ Accepts a list of VPC crns / service references to create CBR zones and a list o
66

77
```hcl
88
module "cbr_rule_multi_service_profile" {
9-
source = "terraform-ibm-modules/cbr/ibm//modules/multi-service-profile"
9+
source = "terraform-ibm-modules/cbr/ibm//modules/cbr-service-profile"
1010
version = "X.X.X" # Replace "X.X.X" with a release version to lock into a specific release
1111
prefix = "multi-service-profile"
12-
zone_service_ref_list = ["cloud-object-storage", "containers-kubernetes", "server-protect"]
12+
zone_service_ref_list = {
13+
"cloud-object-storage" = {
14+
serviceRef_location = = ["syd", "au"]
15+
},
16+
"server-protect" = {
17+
serviceRef_location = ["au"]
18+
},
19+
"directlink" = {}, # directlink does not support restriction per location, hence no value is specified for serviceRef_location.
20+
"event-notifications" = {}
21+
}
1322
zone_vpc_crn_list = ["crn:v1:bluemix:public:is:us-south:a/abac0df06b644a9cabc6e44f55b3880e::vpc:r006-069c6449-03a9-49f1-9070-4d23fc79285e"]
1423
target_service_details = [
1524
{
@@ -19,7 +28,6 @@ module "cbr_rule_multi_service_profile" {
1928
}
2029
]
2130
endpoints = "private"
22-
location = "us-south"
2331
}
2432
```
2533

@@ -49,10 +57,9 @@ module "cbr_rule_multi_service_profile" {
4957
| Name | Description | Type | Default | Required |
5058
|------|-------------|------|---------|:--------:|
5159
| <a name="input_endpoints"></a> [endpoints](#input\_endpoints) | List specific endpoint types for target services, valid values for endpoints are 'public', 'private' or 'direct' | `list(string)` | <pre>[<br> "private"<br>]</pre> | no |
52-
| <a name="input_location"></a> [location](#input\_location) | The region in which the network zone is scoped | `string` | n/a | yes |
5360
| <a name="input_prefix"></a> [prefix](#input\_prefix) | Prefix to append to all vpc\_zone\_list, service\_ref\_zone\_list and cbr\_rule\_description created by this submodule | `string` | `"serviceprofile"` | no |
5461
| <a name="input_target_service_details"></a> [target\_service\_details](#input\_target\_service\_details) | (String) Details of the target service for which the rule has to be created | <pre>list(object({<br> target_service_name = string<br> target_rg = optional(string)<br> enforcement_mode = string<br> tags = optional(list(string))<br> }))</pre> | n/a | yes |
55-
| <a name="input_zone_service_ref_list"></a> [zone\_service\_ref\_list](#input\_zone\_service\_ref\_list) | (List) Service reference for the zone creation | `list(string)` | `[]` | no |
62+
| <a name="input_zone_service_ref_list"></a> [zone\_service\_ref\_list](#input\_zone\_service\_ref\_list) | Provide a valid service reference with the location where the context-based restriction zones are created. If no value is specified for `serviceRef_location`, the zones are not scoped to any location. | <pre>map(object({<br> serviceRef_location = optional(list(string), [])<br> }))</pre> | n/a | yes |
5663
| <a name="input_zone_vpc_crn_list"></a> [zone\_vpc\_crn\_list](#input\_zone\_vpc\_crn\_list) | (List) VPC CRN for the zones | `list(string)` | `[]` | no |
5764

5865
### Outputs

modules/cbr-service-profile/main.tf

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ data "ibm_iam_account_settings" "iam_account_settings" {
1111
locals {
1212
# tflint-ignore: terraform_unused_declarations
1313
validate_zone_inputs = ((length(var.zone_vpc_crn_list) == 0) && (length(var.zone_service_ref_list) == 0)) ? tobool("Error: Provide a valid zone vpc and/or service references") : true
14-
# tflint-ignore: terraform_unused_declarations
15-
validate_location_and_service_name = (length(setintersection(["directlink", "globalcatalog-collection", "iam-groups", "user-management"], var.zone_service_ref_list)) > 0 && var.location != null) ? tobool("Error: The services 'directlink', 'globalcatalog-collection', 'iam-groups' and 'user-management' does not support location") : true
16-
1714

15+
# tflint-ignore: terraform_unused_declarations
16+
validate_location_and_service_name = [
17+
for item in ["directlink", "globalcatalog-collection", "iam-groups", "user-management"] :
18+
contains(keys(var.zone_service_ref_list), item) ? length(var.zone_service_ref_list[item].serviceRef_location) == 0 ? true : tobool("Error: The services 'directlink', 'globalcatalog-collection', 'iam-groups' and 'user-management' do not support location") : true
19+
]
1820

1921
# Restrict and allow the api types as per the target service
2022
icd_api_types = ["crn:v1:bluemix:public:context-based-restrictions::::api-type:data-plane"]
@@ -33,33 +35,43 @@ locals {
3335
vpc_zone_list = (length(var.zone_vpc_crn_list) > 0) ? [{
3436
name = "${var.prefix}-cbr-vpc-zone"
3537
account_id = data.ibm_iam_account_settings.iam_account_settings.account_id
36-
zone_description = "${var.prefix}-cbr-vpc-zone-terraform"
38+
zone_description = "${var.prefix}-cbr-vpc-zone"
3739
addresses = [
3840
for zone_vpc_crn in var.zone_vpc_crn_list :
3941
{ "type" = "vpc", value = zone_vpc_crn }
4042
]
4143
}] : []
4244

4345
service_ref_zone_list = (length(var.zone_service_ref_list) > 0) ? [
44-
for serviceref in var.zone_service_ref_list : {
46+
for serviceref, location in var.zone_service_ref_list : {
4547
name = "${var.prefix}-${serviceref}-cbr-serviceref-zone"
4648
account_id = data.ibm_iam_account_settings.iam_account_settings.account_id
47-
zone_description = "${serviceref}-cbr-serviceref-zone-terraform"
49+
zone_description = "${serviceref}-cbr-serviceref-zone"
4850
# when the target service is containers-kubernetes or any icd services, context cannot have a serviceref
49-
addresses = [
51+
addresses = length(location.serviceRef_location) == 0 ? [
5052
{
5153
type = "serviceRef"
5254
ref = {
5355
account_id = data.ibm_iam_account_settings.iam_account_settings.account_id
5456
service_name = serviceref
55-
location = var.location
57+
location = null
58+
}
59+
}
60+
] : [for loc in location.serviceRef_location :
61+
{
62+
type = "serviceRef"
63+
ref = {
64+
account_id = data.ibm_iam_account_settings.iam_account_settings.account_id
65+
service_name = serviceref
66+
location = loc
5667
}
5768
}
5869
]
5970
}] : []
6071

6172
zone_list = concat(tolist(local.vpc_zone_list), tolist(local.service_ref_zone_list))
6273
}
74+
6375
module "cbr_zone" {
6476
count = length(local.zone_list)
6577
source = "../cbr-zone-module"

modules/cbr-service-profile/variables.tf

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ variable "zone_vpc_crn_list" {
1515
}
1616

1717
variable "zone_service_ref_list" {
18-
type = list(string)
18+
type = map(object({
19+
serviceRef_location = optional(list(string), [])
20+
}))
21+
description = "Provide a valid service reference with the location where the context-based restriction zones are created. If no value is specified for `serviceRef_location`, the zones are not scoped to any location."
22+
# Validation to restrict the target service name to be the list of supported targets only.
1923
validation {
2024
condition = alltrue([
21-
for service_ref in var.zone_service_ref_list :
25+
for service_ref, service_ref_location in var.zone_service_ref_list :
2226
contains(["cloud-object-storage", "codeengine", "containers-kubernetes",
2327
"databases-for-cassandra", "databases-for-elasticsearch", "databases-for-enterprisedb",
2428
"databases-for-etcd", "databases-for-mongodb",
@@ -29,15 +33,8 @@ variable "zone_service_ref_list" {
2933
"apprapp", "compliance", "event-notifications", "logdna", "logdnaat",
3034
"cloudantnosqldb", "globalcatalog-collection", "sysdig-monitor", "sysdig-secure", "toolchain"], service_ref)
3135
])
32-
error_message = "Provide a valid service reference for zone creation"
36+
error_message = "Provide a valid target service name that is supported by context-based restrictions."
3337
}
34-
default = []
35-
description = "(List) Service reference for the zone creation"
36-
}
37-
38-
variable "location" {
39-
type = string
40-
description = "The region in which the network zone is scoped"
4138
}
4239

4340
variable "target_service_details" {

tests/pr_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,26 @@ func TestMultiServiceProfileExample(t *testing.T) {
212212
t.Run("verify service reference exist", func(t *testing.T) {
213213
var serviceRefExists bool
214214
var actual_references []string
215-
expected_references := []string{"cloud-object-storage", "server-protect"}
215+
expected_references := []string{"cloud-object-storage", "server-protect", "directlink", "event-notifications"}
216216

217217
zoneIds := zones[0].([]interface{})
218218
for index := range zoneIds {
219219
zone := zoneIds[index].(string)
220220
zone_details, err := cloudInfoSvc.GetCBRZoneByID(zone)
221221
if assert.Nil(t, err, "Failed to get the zone") &&
222222
assert.NotNil(t, zone_details, "No zone found") {
223+
uniqueMap := make(map[string]struct{})
223224
for addr_index := range zone_details.Addresses {
224225
switch zone_details.Addresses[addr_index].(type) {
225226
case *contextbasedrestrictionsv1.AddressServiceRef:
226227
serviceRefExists = true
227228
serviceRef := zone_details.Addresses[addr_index].(*contextbasedrestrictionsv1.AddressServiceRef)
228-
actual_references = append(actual_references, *serviceRef.Ref.ServiceName)
229+
serviceName := *serviceRef.Ref.ServiceName
230+
if _, ok := uniqueMap[serviceName]; !ok {
231+
// Adding multilocation support for COS causing same number of COS as items in list as serviceRef, to avoid duplicates, making the list as unique
232+
uniqueMap[serviceName] = struct{}{}
233+
actual_references = append(actual_references, serviceName)
234+
}
229235
}
230236
}
231237
}

0 commit comments

Comments
 (0)