|
| 1 | +# Needed for subdomain uniqueness |
| 2 | +resource "random_uuid" "self" {} |
| 3 | + |
| 4 | +# Needed for output to construct the subaccount URL |
| 5 | +data "btp_globalaccount" "self" {} |
| 6 | + |
| 7 | +locals { |
| 8 | + # Define a mapping of regions to their respective geo regions |
| 9 | + region_mapping = { |
| 10 | + "br10" = "LATAM" |
| 11 | + "jp10" = "APAC" |
| 12 | + "ap10" = "APAC" |
| 13 | + "ap11" = "APAC" |
| 14 | + "ap12" = "APAC" |
| 15 | + "ca10" = "AMER" |
| 16 | + "eu10" = "EMEA" |
| 17 | + "eu11" = "EMEA" |
| 18 | + "us10" = "AMER" |
| 19 | + "us11" = "AMER" |
| 20 | + "us30" = "AMER" |
| 21 | + "eu30" = "EMEA" |
| 22 | + "in30" = "APAC" |
| 23 | + "il30" = "EMEA" |
| 24 | + "jp30" = "APAC" |
| 25 | + "jp31" = "APAC" |
| 26 | + "ap30" = "APAC" |
| 27 | + "br30" = "APAC" |
| 28 | + "eu20" = "EMEA" |
| 29 | + "ap20" = "APAC" |
| 30 | + "ap21" = "APAC" |
| 31 | + "br20" = "APAC" |
| 32 | + "ca20" = "AMER" |
| 33 | + "cn20" = "APAC" |
| 34 | + "us20" = "AMER" |
| 35 | + "jp20" = "APAC" |
| 36 | + "us21" = "AMER" |
| 37 | + "ch20" = "EMEA" |
| 38 | + } |
| 39 | + |
| 40 | + # Define default entitlements per stage |
| 41 | + default_entitlements = { |
| 42 | + "Dev" = { |
| 43 | + "aicore" = ["standard"], |
| 44 | + "ai-launchpad" = ["standard"] |
| 45 | + "alert-notification" = ["standard"], |
| 46 | + "application-logs" = ["standard=1"], |
| 47 | + "build-workzone-standard" = ["standard"], |
| 48 | + "credstore" = ["standard=1"], |
| 49 | + "jobscheduler" = ["standard=1"], |
| 50 | + "hana-cloud" = ["hana"], |
| 51 | + "transport" = ["standard"], |
| 52 | + }, |
| 53 | + "Test" = { |
| 54 | + "ai-launchpad" = ["standard"] |
| 55 | + "alert-notification" = ["standard"], |
| 56 | + "application-logs" = ["standard=1"], |
| 57 | + "build-workzone-standard" = ["standard"], |
| 58 | + "credstore" = ["standard=1"], |
| 59 | + "jobscheduler" = ["standard=1"], |
| 60 | + "hana-cloud" = ["hana"], |
| 61 | + "transport" = ["standard"], |
| 62 | + }, |
| 63 | + "Prod" = { |
| 64 | + "ai-launchpad" = ["standard"] |
| 65 | + "alert-notification" = ["standard"], |
| 66 | + "application-logs" = ["standard=1"], |
| 67 | + "build-workzone-standard" = ["standard"], |
| 68 | + "credstore" = ["standard=1"], |
| 69 | + "jobscheduler" = ["standard=1"], |
| 70 | + "hana-cloud" = ["hana"], |
| 71 | + "transport" = ["standard"], |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + # Naming conventions |
| 76 | + subaccount_name = "${var.subaccount_name} ${var.stage}" |
| 77 | + subaccount_subdomain = "${replace(lower(var.subaccount_name), " ", "-")}-${lower(var.stage)}-${random_uuid.self.result}" |
| 78 | + subaccount_description = "Subaccount ${var.subaccount_name} in the ${var.stage} stage for ${var.department}." |
| 79 | + cf_org_name = "CF-${var.subaccount_name}-${var.stage}" |
| 80 | +} |
| 81 | + |
| 82 | +resource "btp_subaccount" "self" { |
| 83 | + name = local.subaccount_name |
| 84 | + subdomain = local.subaccount_subdomain |
| 85 | + region = var.region |
| 86 | + description = local.subaccount_description |
| 87 | + usage = var.stage == "Prod" ? "USED_FOR_PRODUCTION" : "NOT_USED_FOR_PRODUCTION" |
| 88 | + beta_enabled = var.stage == "Dev" ? true : false |
| 89 | + labels = { |
| 90 | + "Cost Center" = ["${var.cost_center}"] |
| 91 | + "Contact Person" = ["${var.contact_person}"] |
| 92 | + "Department" = ["${var.department}"] |
| 93 | + "Region" = ["${lookup(local.region_mapping, var.region, "UNKNOWN")}"] |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +module "sap_btp_entitlements" { |
| 98 | + source = "aydin-ozcan/sap-btp-entitlements/btp" |
| 99 | + version = "~> 1.0.1" |
| 100 | + |
| 101 | + subaccount = btp_subaccount.self.id |
| 102 | + entitlements = local.default_entitlements[var.stage] |
| 103 | +} |
| 104 | + |
| 105 | +data "btp_subaccount_environments" "all" { |
| 106 | + subaccount_id = btp_subaccount.self.id |
| 107 | +} |
| 108 | + |
| 109 | +resource "terraform_data" "cf_landscape_label" { |
| 110 | + input = [for env in data.btp_subaccount_environments.all.values : env if env.service_name == "cloudfoundry" && env.environment_type == "cloudfoundry"][0].landscape_label |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +resource "btp_subaccount_environment_instance" "cloudfoundry" { |
| 115 | + subaccount_id = btp_subaccount.self.id |
| 116 | + name = local.cf_org_name |
| 117 | + environment_type = "cloudfoundry" |
| 118 | + service_name = "cloudfoundry" |
| 119 | + plan_name = "standard" |
| 120 | + landscape_label = terraform_data.cf_landscape_label.output |
| 121 | + parameters = jsonencode({ |
| 122 | + instance_name = local.subaccount_subdomain |
| 123 | + }) |
| 124 | +} |
| 125 | + |
| 126 | +resource "btp_subaccount_role_collection_assignment" "emergency_admins" { |
| 127 | + for_each = toset(var.emergency_subaccount_admins) |
| 128 | + |
| 129 | + subaccount_id = btp_subaccount.self.id |
| 130 | + role_collection_name = "Subaccount Administrator" |
| 131 | + user_name = each.value |
| 132 | +} |
0 commit comments