|
| 1 | +# ------------------------------------------------------------------------------------------------------ |
| 2 | +# Subaccount setup for DC mission 3260 |
| 3 | +# ------------------------------------------------------------------------------------------------------ |
| 4 | +# Setup subaccount domain (to ensure uniqueness in BTP global account) |
| 5 | +resource "random_uuid" "uuid" {} |
| 6 | + |
| 7 | +locals { |
| 8 | + random_uuid = random_uuid.uuid.result |
| 9 | + subaccount_domain = lower(replace("mission-3260-${local.random_uuid}", "_", "-")) |
| 10 | +} |
| 11 | + |
| 12 | +# ------------------------------------------------------------------------------------------------------ |
| 13 | +# Creation of subaccount |
| 14 | +# ------------------------------------------------------------------------------------------------------ |
| 15 | +resource "btp_subaccount" "dc_mission" { |
| 16 | + count = var.subaccount_id == "" ? 1 : 0 |
| 17 | + name = var.subaccount_name |
| 18 | + subdomain = local.subaccount_domain |
| 19 | + region = var.region |
| 20 | +} |
| 21 | + |
| 22 | +data "btp_subaccount" "dc_mission" { |
| 23 | + id = var.subaccount_id != "" ? var.subaccount_id : btp_subaccount.dc_mission[0].id |
| 24 | +} |
| 25 | + |
| 26 | +# ------------------------------------------------------------------------------------------------------ |
| 27 | +# Assign custom IDP to sub account (if custom_idp is set) |
| 28 | +# ------------------------------------------------------------------------------------------------------ |
| 29 | +resource "btp_subaccount_trust_configuration" "fully_customized" { |
| 30 | + # Only create trust configuration if custom_idp has been set |
| 31 | + count = var.custom_idp == "" ? 0 : 1 |
| 32 | + subaccount_id = data.btp_subaccount.dc_mission.id |
| 33 | + identity_provider = var.custom_idp |
| 34 | +} |
| 35 | + |
| 36 | +# ------------------------------------------------------------------------------------------------------ |
| 37 | +# SERVICES |
| 38 | +# ------------------------------------------------------------------------------------------------------ |
| 39 | +# |
| 40 | +locals { |
| 41 | + service_name__cloudfoundry = "cloudfoundry" |
| 42 | +} |
| 43 | + |
| 44 | +# ------------------------------------------------------------------------------------------------------ |
| 45 | +# Setup cloudfoundry (Cloud Foundry Environment) |
| 46 | +# ------------------------------------------------------------------------------------------------------ |
| 47 | +# Fetch all available environments for the subaccount |
| 48 | +data "btp_subaccount_environments" "all" { |
| 49 | + subaccount_id = data.btp_subaccount.dc_mission.id |
| 50 | +} |
| 51 | +# Take the landscape label from the first CF environment if no environment label is provided (this replaces the previous null_resource) |
| 52 | +resource "terraform_data" "cf_landscape_label" { |
| 53 | + input = length(var.cf_landscape_label) > 0 ? var.cf_landscape_label : [for env in data.btp_subaccount_environments.all.values : env if env.service_name == "cloudfoundry" && env.environment_type == "cloudfoundry"][0].landscape_label |
| 54 | +} |
| 55 | +# Create instance |
| 56 | +resource "btp_subaccount_environment_instance" "cloudfoundry" { |
| 57 | + subaccount_id = data.btp_subaccount.dc_mission.id |
| 58 | + name = "cf-${random_uuid.uuid.result}" |
| 59 | + environment_type = "cloudfoundry" |
| 60 | + service_name = local.service_name__cloudfoundry |
| 61 | + plan_name = var.service_plan__cloudfoundry |
| 62 | + landscape_label = terraform_data.cf_landscape_label.output |
| 63 | + |
| 64 | + parameters = jsonencode({ |
| 65 | + instance_name = "cf-${random_uuid.uuid.result}" |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +# ------------------------------------------------------------------------------------------------------ |
| 70 | +# APP SUBSCRIPTIONS |
| 71 | +# ------------------------------------------------------------------------------------------------------ |
| 72 | +# |
| 73 | +locals { |
| 74 | + service_name__sap_process_automation = "process-automation" |
| 75 | +} |
| 76 | +# ------------------------------------------------------------------------------------------------------ |
| 77 | +# Setup process-automation (SAP Build Process Automation) |
| 78 | +# ------------------------------------------------------------------------------------------------------ |
| 79 | +# Entitle |
| 80 | +resource "btp_subaccount_entitlement" "build_process_automation" { |
| 81 | + subaccount_id = data.btp_subaccount.dc_mission.id |
| 82 | + service_name = local.service_name__sap_process_automation |
| 83 | + plan_name = var.service_plan__sap_process_automation |
| 84 | +} |
| 85 | +# Subscribe |
| 86 | +resource "btp_subaccount_subscription" "build_process_automation" { |
| 87 | + subaccount_id = data.btp_subaccount.dc_mission.id |
| 88 | + app_name = local.service_name__sap_process_automation |
| 89 | + plan_name = var.service_plan__sap_process_automation |
| 90 | + depends_on = [btp_subaccount_entitlement.build_process_automation] |
| 91 | +} |
| 92 | +# ------------------------------------------------------------------------------------------------------ |
| 93 | +# USERS AND ROLES |
| 94 | +# ------------------------------------------------------------------------------------------------------ |
| 95 | +data "btp_whoami" "me" {} |
| 96 | +# |
| 97 | +locals { |
| 98 | + subaccount_admins = var.subaccount_admins |
| 99 | + subaccount_service_admins = var.subaccount_service_admins |
| 100 | + |
| 101 | + process_automation_admins = var.process_automation_admins |
| 102 | + process_automation_developers = var.process_automation_developers |
| 103 | + process_automation_participants = var.process_automation_participants |
| 104 | + |
| 105 | + custom_idp_tenant = var.custom_idp != "" ? element(split(".", var.custom_idp), 0) : "" |
| 106 | + origin_key = local.custom_idp_tenant != "" ? "${local.custom_idp_tenant}-platform" : "" |
| 107 | +} |
| 108 | + |
| 109 | +# ------------------------------------------------------------------------------------------------------ |
| 110 | +# Assign role collection "Subaccount Administrator" |
| 111 | +# ------------------------------------------------------------------------------------------------------ |
| 112 | +resource "btp_subaccount_role_collection_assignment" "subaccount_admin" { |
| 113 | + for_each = toset("${local.subaccount_admins}") |
| 114 | + subaccount_id = data.btp_subaccount.dc_mission.id |
| 115 | + role_collection_name = "Subaccount Administrator" |
| 116 | + user_name = each.value |
| 117 | + origin = local.origin_key |
| 118 | + depends_on = [btp_subaccount.dc_mission] |
| 119 | +} |
| 120 | + |
| 121 | +# ------------------------------------------------------------------------------------------------------ |
| 122 | +# Assign role collection "Subaccount Service Administrator" |
| 123 | +# ------------------------------------------------------------------------------------------------------ |
| 124 | +resource "btp_subaccount_role_collection_assignment" "subaccount_service_admin" { |
| 125 | + for_each = toset("${local.subaccount_service_admins}") |
| 126 | + subaccount_id = data.btp_subaccount.dc_mission.id |
| 127 | + role_collection_name = "Subaccount Service Administrator" |
| 128 | + user_name = each.value |
| 129 | + origin = local.origin_key |
| 130 | + depends_on = [btp_subaccount.dc_mission] |
| 131 | +} |
| 132 | + |
| 133 | +# ------------------------------------------------------------------------------------------------------ |
| 134 | +# Assign role collection "ProcessAutomationAdmin" |
| 135 | +# ------------------------------------------------------------------------------------------------------ |
| 136 | +resource "btp_subaccount_role_collection_assignment" "process_automation_admins" { |
| 137 | + for_each = toset(local.process_automation_admins) |
| 138 | + subaccount_id = data.btp_subaccount.dc_mission.id |
| 139 | + role_collection_name = "ProcessAutomationAdmin" |
| 140 | + user_name = each.value |
| 141 | + origin = var.custom_idp_apps_origin_key |
| 142 | + depends_on = [btp_subaccount_subscription.build_process_automation] |
| 143 | +} |
| 144 | + |
| 145 | +# Assign logged in user to the role collection "ProcessAutomationAdmin" if not custom idp user |
| 146 | +resource "btp_subaccount_role_collection_assignment" "process_automation_admins_default" { |
| 147 | + count = data.btp_whoami.me.issuer != var.custom_idp ? 1 : 0 |
| 148 | + subaccount_id = data.btp_subaccount.dc_mission.id |
| 149 | + role_collection_name = "ProcessAutomationAdmin" |
| 150 | + user_name = data.btp_whoami.me.email |
| 151 | + origin = "sap.default" |
| 152 | + depends_on = [btp_subaccount_subscription.build_process_automation] |
| 153 | +} |
| 154 | + |
| 155 | +# ------------------------------------------------------------------------------------------------------ |
| 156 | +# Assign role collection "ProcessAutomationDeveloper" |
| 157 | +# ------------------------------------------------------------------------------------------------------ |
| 158 | +resource "btp_subaccount_role_collection_assignment" "process_automation_developers" { |
| 159 | + for_each = toset(local.process_automation_developers) |
| 160 | + subaccount_id = data.btp_subaccount.dc_mission.id |
| 161 | + role_collection_name = "ProcessAutomationDeveloper" |
| 162 | + user_name = each.value |
| 163 | + origin = var.custom_idp_apps_origin_key |
| 164 | + depends_on = [btp_subaccount_subscription.build_process_automation] |
| 165 | +} |
| 166 | + |
| 167 | +# Assign logged in user to the role collection "ProcessAutomationDeveloper" if not custom idp user |
| 168 | +resource "btp_subaccount_role_collection_assignment" "process_automation_developers_default" { |
| 169 | + count = data.btp_whoami.me.issuer != var.custom_idp ? 1 : 0 |
| 170 | + subaccount_id = data.btp_subaccount.dc_mission.id |
| 171 | + role_collection_name = "ProcessAutomationDeveloper" |
| 172 | + user_name = data.btp_whoami.me.email |
| 173 | + origin = "sap.default" |
| 174 | + depends_on = [btp_subaccount_subscription.build_process_automation] |
| 175 | +} |
| 176 | + |
| 177 | +# ------------------------------------------------------------------------------------------------------ |
| 178 | +# Assign role collection "ProcessAutomationParticipant" |
| 179 | +# ------------------------------------------------------------------------------------------------------ |
| 180 | +resource "btp_subaccount_role_collection_assignment" "process_automation_participants" { |
| 181 | + for_each = toset(local.process_automation_admins) |
| 182 | + subaccount_id = data.btp_subaccount.dc_mission.id |
| 183 | + role_collection_name = "ProcessAutomationParticipant" |
| 184 | + user_name = each.value |
| 185 | + origin = var.custom_idp_apps_origin_key |
| 186 | + depends_on = [btp_subaccount_subscription.build_process_automation] |
| 187 | +} |
| 188 | + |
| 189 | +# Assign logged in user to the role collection "ProcessAutomationParticipant" if not custom idp user |
| 190 | +resource "btp_subaccount_role_collection_assignment" "process_automation_participants_default" { |
| 191 | + count = data.btp_whoami.me.issuer != var.custom_idp ? 1 : 0 |
| 192 | + subaccount_id = data.btp_subaccount.dc_mission.id |
| 193 | + role_collection_name = "ProcessAutomationParticipant" |
| 194 | + user_name = data.btp_whoami.me.email |
| 195 | + origin = "sap.default" |
| 196 | + depends_on = [btp_subaccount_subscription.build_process_automation] |
| 197 | +} |
| 198 | +# ------------------------------------------------------------------------------------------------------ |
| 199 | +# Create tfvars file for step 2 (if variable `create_tfvars_file_for_step2` is set to true) |
| 200 | +# ------------------------------------------------------------------------------------------------------ |
| 201 | +resource "local_file" "output_vars_step1" { |
| 202 | + count = var.create_tfvars_file_for_step2 ? 1 : 0 |
| 203 | + content = <<-EOT |
| 204 | + globalaccount = "${var.globalaccount}" |
| 205 | + cli_server_url = ${jsonencode(var.cli_server_url)} |
| 206 | + custom_idp = ${jsonencode(var.custom_idp)} |
| 207 | +
|
| 208 | + subaccount_id = "${data.btp_subaccount.dc_mission.id}" |
| 209 | +
|
| 210 | + cf_api_url = "${jsondecode(btp_subaccount_environment_instance.cloudfoundry.labels)["API Endpoint"]}" |
| 211 | +
|
| 212 | + cf_org_id = "${jsondecode(btp_subaccount_environment_instance.cloudfoundry.labels)["Org ID"]}" |
| 213 | + cf_org_name = "${jsondecode(btp_subaccount_environment_instance.cloudfoundry.labels)["Org Name"]}" |
| 214 | +
|
| 215 | + origin_key = "${local.origin_key}" |
| 216 | +
|
| 217 | + cf_space_name = "${var.cf_space_name}" |
| 218 | +
|
| 219 | + cf_org_admins = ${jsonencode(var.cf_org_admins)} |
| 220 | + cf_org_users = ${jsonencode(var.cf_org_users)} |
| 221 | + cf_space_developers = ${jsonencode(var.cf_space_developers)} |
| 222 | + cf_space_managers = ${jsonencode(var.cf_space_managers)} |
| 223 | +
|
| 224 | + EOT |
| 225 | + filename = "../step2/terraform.tfvars" |
| 226 | +} |
0 commit comments