|
| 1 | +# ############################################################################## |
| 2 | +# # Get Cloud Account ID |
| 3 | +# ############################################################################## |
| 4 | +data "ibm_iam_account_settings" "iam_account_settings" { |
| 5 | +} |
| 6 | + |
| 7 | +############################################################################## |
| 8 | +# |
| 9 | +# CBR Rule for a list of target services |
| 10 | +############################################################################## |
| 11 | +locals { |
| 12 | + # tflint-ignore: terraform_unused_declarations |
| 13 | + 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 | + |
| 15 | + # Restrict and allow the api types as per the target service |
| 16 | + icd_api_types = ["crn:v1:bluemix:public:context-based-restrictions::::api-type:data-plane"] |
| 17 | + operations_apitype_val = { |
| 18 | + databases-for-enterprisedb = local.icd_api_types, |
| 19 | + containers-kubernetes = ["crn:v1:bluemix:public:containers-kubernetes::::api-type:cluster", "crn:v1:bluemix:public:containers-kubernetes::::api-type:management"], |
| 20 | + databases-for-cassandra = local.icd_api_types, |
| 21 | + databases-for-elasticsearch = local.icd_api_types, |
| 22 | + databases-for-etcd = local.icd_api_types, |
| 23 | + databases-for-mongodb = local.icd_api_types, |
| 24 | + databases-for-postgresql = local.icd_api_types, |
| 25 | + databases-for-redis = local.icd_api_types, |
| 26 | + messages-for-rabbitmq = local.icd_api_types |
| 27 | + } |
| 28 | + |
| 29 | + vpc_zone_list = (length(var.zone_vpc_crn_list) > 0) ? [{ |
| 30 | + name = "${var.prefix}-cbr-vpc-zone" |
| 31 | + account_id = data.ibm_iam_account_settings.iam_account_settings.account_id |
| 32 | + zone_description = "cbr-vpc-zone-terraform" |
| 33 | + addresses = [ |
| 34 | + for zone_vpc_crn in var.zone_vpc_crn_list : |
| 35 | + { "type" = "vpc", value = zone_vpc_crn } |
| 36 | + ] |
| 37 | + }] : [] |
| 38 | + |
| 39 | + service_ref_zone_list = (length(var.zone_service_ref_list) > 0) ? [{ |
| 40 | + name = "${var.prefix}-cbr-serviceref-zone" |
| 41 | + account_id = data.ibm_iam_account_settings.iam_account_settings.account_id |
| 42 | + zone_description = "cbr-serviceref-zone-terraform" |
| 43 | + # when the target service is containers-kubernetes or any icd services, context cannot have a serviceref |
| 44 | + addresses = [ |
| 45 | + for serviceref in var.zone_service_ref_list : { |
| 46 | + type = "serviceRef" |
| 47 | + ref = { |
| 48 | + account_id = data.ibm_iam_account_settings.iam_account_settings.account_id |
| 49 | + service_name = serviceref |
| 50 | + } |
| 51 | + } |
| 52 | + ] |
| 53 | + }] : [] |
| 54 | + zone_list = concat(tolist(local.vpc_zone_list), tolist(local.service_ref_zone_list)) |
| 55 | +} |
| 56 | + |
| 57 | +module "cbr_zone" { |
| 58 | + count = length(local.zone_list) |
| 59 | + source = "../cbr-zone-module" |
| 60 | + name = local.zone_list[count.index].name |
| 61 | + zone_description = local.zone_list[count.index].zone_description |
| 62 | + account_id = local.zone_list[count.index].account_id |
| 63 | + addresses = local.zone_list[count.index].addresses |
| 64 | +} |
| 65 | + |
| 66 | +locals { |
| 67 | + rule_contexts = [{ |
| 68 | + attributes = [ |
| 69 | + { |
| 70 | + "name" : "endpointType", |
| 71 | + "value" : "private" |
| 72 | + }, |
| 73 | + { |
| 74 | + name = "networkZoneId" |
| 75 | + value = join(",", ([for zone in module.cbr_zone : zone.zone_id])) |
| 76 | + }] |
| 77 | + }] |
| 78 | +} |
| 79 | + |
| 80 | +module "cbr_rule" { |
| 81 | + count = length(var.target_service_details) |
| 82 | + source = "../cbr-rule-module" |
| 83 | + rule_description = "${var.prefix}-${var.target_service_details[count.index].target_service_name}-serviceprofile-rule" |
| 84 | + enforcement_mode = var.target_service_details[count.index].enforcement_mode |
| 85 | + rule_contexts = local.rule_contexts |
| 86 | + operations = (length(lookup(local.operations_apitype_val, var.target_service_details[count.index].target_service_name, [])) > 0) ? [{ |
| 87 | + api_types = [ |
| 88 | + # lookup the map for the target service name, if not present make api_type_id as empty |
| 89 | + for apitype in lookup(local.operations_apitype_val, var.target_service_details[count.index].target_service_name, []) : { |
| 90 | + api_type_id = apitype |
| 91 | + }] |
| 92 | + }] : [] |
| 93 | + |
| 94 | + resources = [{ |
| 95 | + tags = var.target_service_details[count.index].tags != null ? [for tag in var.target_service_details[count.index].tags : { |
| 96 | + name = split(":", tag)[0] |
| 97 | + value = split(":", tag)[1] |
| 98 | + }] : [] |
| 99 | + attributes = var.target_service_details[count.index].target_rg != null ? [ |
| 100 | + { |
| 101 | + name = "accountId", |
| 102 | + operator = "stringEquals", |
| 103 | + value = data.ibm_iam_account_settings.iam_account_settings.account_id |
| 104 | + }, |
| 105 | + { |
| 106 | + name = "resourceGroupId", |
| 107 | + operator = "stringEquals", |
| 108 | + value = var.target_service_details[count.index].target_rg |
| 109 | + }, |
| 110 | + { |
| 111 | + name = "serviceName", |
| 112 | + operator = "stringEquals", |
| 113 | + value = var.target_service_details[count.index].target_service_name |
| 114 | + }] : [ |
| 115 | + { |
| 116 | + name = "accountId", |
| 117 | + operator = "stringEquals", |
| 118 | + value = data.ibm_iam_account_settings.iam_account_settings.account_id |
| 119 | + }, |
| 120 | + { |
| 121 | + name = "serviceName", |
| 122 | + operator = "stringEquals", |
| 123 | + value = var.target_service_details[count.index].target_service_name |
| 124 | + }] |
| 125 | + }] |
| 126 | +} |
0 commit comments