diff --git a/released/discovery_center/mission_3248_trial/step1/main.tf b/released/discovery_center/mission_3248_trial/step1/main.tf index bbdb1593..9ebe93fe 100644 --- a/released/discovery_center/mission_3248_trial/step1/main.tf +++ b/released/discovery_center/mission_3248_trial/step1/main.tf @@ -1,42 +1,76 @@ -### -# Retrieval of existing trial subaccount -### -data "btp_subaccount" "trial" { - id = var.subaccount_id +# ------------------------------------------------------------------------------------------------------ +# Subaccount setup for DC mission 3248 (trial) +# ------------------------------------------------------------------------------------------------------ +# Setup subaccount domain (to ensure uniqueness in BTP global account) +resource "random_uuid" "uuid" {} + +locals { + random_uuid = random_uuid.uuid.result + subaccount_domain = "dcmission4024${local.random_uuid}" +} + +# ------------------------------------------------------------------------------------------------------ +# Creation of subaccount +# ------------------------------------------------------------------------------------------------------ +resource "btp_subaccount" "dc_mission" { + count = var.subaccount_id == "" ? 1 : 0 + + name = var.subaccount_name + subdomain = local.subaccount_domain + region = var.region } -### -# Assignment of basic entitlements for an ABAP setup -### -resource "btp_subaccount_entitlement" "abap-trial" { - subaccount_id = var.subaccount_id - service_name = "abap-trial" - plan_name = "shared" +data "btp_subaccount" "dc_mission" { + id = var.subaccount_id != "" ? var.subaccount_id : btp_subaccount.dc_mission[0].id +} + +data "btp_subaccount" "subaccount" { + id = data.btp_subaccount.dc_mission.id +} + +# ------------------------------------------------------------------------------------------------------ +# SERVICES +# ------------------------------------------------------------------------------------------------------ +# +locals { + service_name__abap_trial = "abap-trial" + service_name__cloudfoundry = "cloudfoundry" +} +# ------------------------------------------------------------------------------------------------------ +# Setup abap-trial (ABAP environment) +# ------------------------------------------------------------------------------------------------------ +# Entitle +resource "btp_subaccount_entitlement" "abap_trial" { + subaccount_id = data.btp_subaccount.subaccount.id + service_name = local.service_name__abap_trial + plan_name = var.service_plan__abap_trial amount = 1 } -### +# ------------------------------------------------------------------------------------------------------ +# Setup cloudfoundry (Cloud Foundry Environment) +# ------------------------------------------------------------------------------------------------------ +# Fetch all available environments for the subaccount # Retrieval of existing CF environment instance -### data "btp_subaccount_environment_instances" "all" { - subaccount_id = var.subaccount_id + subaccount_id = data.btp_subaccount.subaccount.id } locals { - cf_org_name = join("_", [var.globalaccount, data.btp_subaccount.trial.subdomain]) + cf_org_name = join("_", [var.globalaccount, data.btp_subaccount.subaccount.subdomain]) cf_instances = [for env in data.btp_subaccount_environment_instances.all.values : env if env.service_name == "cloudfoundry" && env.environment_type == "cloudfoundry"] cf_enabled = length(local.cf_instances) > 0 create_cf_space = var.create_cf_space || !local.cf_enabled } - +# Instance creation (optional) resource "btp_subaccount_environment_instance" "cloudfoundry" { count = local.cf_enabled ? 0 : 1 - subaccount_id = var.subaccount_id + subaccount_id = data.btp_subaccount.subaccount.id name = local.cf_org_name environment_type = "cloudfoundry" - service_name = "cloudfoundry" - plan_name = "trial" + service_name = local.service_name__cloudfoundry + plan_name = var.service_plan__cloudfoundry parameters = jsonencode({ instance_name = local.cf_org_name @@ -47,17 +81,26 @@ locals { cf_environment_instance = local.cf_enabled ? local.cf_instances[0] : btp_subaccount_environment_instance.cloudfoundry[0] } +# ------------------------------------------------------------------------------------------------------ +# Create tfvars file for step 2 (if variable `create_tfvars_file_for_step2` is set to true) +# ------------------------------------------------------------------------------------------------------ resource "local_file" "output_vars_step1" { - count = var.create_tfvars_file_for_next_stage ? 1 : 0 + count = var.create_tfvars_file_for_step2 ? 1 : 0 content = <<-EOT + globalaccount = "${var.globalaccount}" + cli_server_url = ${jsonencode(var.cli_server_url)} + cf_api_url = "${jsondecode(local.cf_environment_instance.labels)["API Endpoint"]}" cf_org_id = "${local.cf_environment_instance.platform_id}" + + abap_admin_email = "${var.abap_admin_email}" + + create_cf_space = ${local.create_cf_space} + cf_org_managers = ${jsonencode(var.cf_org_managers)} cf_space_developers = ${jsonencode(var.cf_space_developers)} cf_space_managers = ${jsonencode(var.cf_space_managers)} cf_space_name = "${var.cf_space_name}" - create_cf_space = ${local.create_cf_space} - abap_admin_email = "${var.abap_admin_email}" EOT filename = "../step2/terraform.tfvars" diff --git a/released/discovery_center/mission_3248_trial/step1/outputs.tf b/released/discovery_center/mission_3248_trial/step1/outputs.tf index fc3d6632..e1c1b35c 100644 --- a/released/discovery_center/mission_3248_trial/step1/outputs.tf +++ b/released/discovery_center/mission_3248_trial/step1/outputs.tf @@ -1,11 +1,11 @@ output "subaccount_id" { - value = var.subaccount_id + value = data.btp_subaccount.subaccount.id description = "The ID of the subaccount." } -output "cf_org_id" { - value = local.cf_environment_instance.platform_id - description = "The ID of the Cloud Foundry org connected to the subaccount." +output "abap_admin_email" { + value = var.abap_admin_email + description = "Email of the ABAP Administrator." } output "cf_api_url" { @@ -18,6 +18,21 @@ output "cf_landscape_label" { description = "Landscape label of the Cloud Foundry environment." } +output "cf_org_id" { + value = local.cf_environment_instance.platform_id + description = "The ID of the Cloud Foundry org connected to the subaccount." +} + +output "create_cf_space" { + value = local.create_cf_space + description = "Determines whether a new CF space should be created. Must be true if no space with the name cf_space_name exists for the Org, yet, and false otherwise." +} + +output "cf_space_name" { + value = var.cf_space_name + description = "The name of the CF space to use." +} + output "cf_org_managers" { value = var.cf_org_managers description = "List of managers for the Cloud Foundry org." @@ -31,19 +46,4 @@ output "cf_space_managers" { output "cf_space_developers" { value = var.cf_space_developers description = "List of developers for the Cloud Foundry space." -} - -output "cf_space_name" { - value = var.cf_space_name - description = "The name of the CF space to use." -} - -output "create_cf_space" { - value = local.create_cf_space - description = "Determines whether a new CF space should be created. Must be true if no space with the name cf_space_name exists for the Org, yet, and false otherwise." -} - -output "abap_admin_email" { - value = var.abap_admin_email - description = "Email of the ABAP Administrator." -} +} \ No newline at end of file diff --git a/released/discovery_center/mission_3248_trial/step1/sample.tfvars b/released/discovery_center/mission_3248_trial/step1/sample.tfvars new file mode 100644 index 00000000..02ac6afb --- /dev/null +++ b/released/discovery_center/mission_3248_trial/step1/sample.tfvars @@ -0,0 +1,26 @@ +# ------------------------------------------------------------------------------------------------------ +# Provider configuration +# ------------------------------------------------------------------------------------------------------ +# Your global account subdomain +globalaccount = "" // trial-ga + +# Region for your trial subaccount +region = "us10" + +# Name of your sub account +subaccount_id = "" + +# ------------------------------------------------------------------------------------------------------ +# Use case specific configurations +# ------------------------------------------------------------------------------------------------------ +abap_admin_email = "you@your.company.com" + +# This TF script allows you to create a CF space but carefully check conditions +# create_cf_space must be false, if CF is enabled and a space with the configured space name already exists +# +# create_cf_space = true // false (default) + +# ------------------------------------------------------------------------------------------------------ +# Create tfvars file for the step 2 +# ------------------------------------------------------------------------------------------------------ +create_tfvars_file_for_step2 = true \ No newline at end of file diff --git a/released/discovery_center/mission_3248_trial/step1/samples.tfvars b/released/discovery_center/mission_3248_trial/step1/samples.tfvars deleted file mode 100644 index 18f3e5ea..00000000 --- a/released/discovery_center/mission_3248_trial/step1/samples.tfvars +++ /dev/null @@ -1,28 +0,0 @@ -# ------------------------------------------------------------------------------------------------------ -# Provider configuration -# ------------------------------------------------------------------------------------------------------ -globalaccount = "subdomain of your trial globalaccount" - -# ------------------------------------------------------------------------------------------------------ -# Project specific configuration (please adapt) -# ------------------------------------------------------------------------------------------------------ -subaccount_id = "id of your trial subaccount" - -# Must be false if CF is enabled and a space with the configured space name already exists -create_cf_space = false -cf_space_name = "dev" - -cf_org_managers = ["anotheruser@test.com"] - -# If create_cf_space is true or Clouf Foundry is disabled for your trial subaccount, you must add -# yourself as a space manager and developer. DON'T add yourself if the space exists and you are -# already a space manager or developer of the space. -cf_space_developers = ["anotheruser@test.com", "you@test.com"] -cf_space_managers = ["anotheruser@test.com", "you@test.com"] - -abap_admin_email = "you@your.company.com" - -# ------------------------------------------------------------------------------------------------------ -# Create tfvars file for step 2 -# ------------------------------------------------------------------------------------------------------ -create_tfvars_file_for_next_stage = true diff --git a/released/discovery_center/mission_3248_trial/step1/variables.tf b/released/discovery_center/mission_3248_trial/step1/variables.tf index 454af981..97ab6072 100644 --- a/released/discovery_center/mission_3248_trial/step1/variables.tf +++ b/released/discovery_center/mission_3248_trial/step1/variables.tf @@ -1,6 +1,27 @@ +# ------------------------------------------------------------------------------------------------------ +# Account variables +# ------------------------------------------------------------------------------------------------------ variable "globalaccount" { type = string - description = "The subdomain of the trial account." + description = "The globalaccount subdomain where the sub account shall be created." +} + +variable "cli_server_url" { + type = string + description = "The BTP CLI server URL." + default = "https://cli.btp.cloud.sap" +} + +variable "region" { + type = string + description = "The region where the subaccount shall be created in." + default = "us10" +} + +variable "subaccount_name" { + type = string + description = "The subaccount name." + default = "My SAP DC mission trial subaccount." } variable "subaccount_id" { @@ -8,13 +29,53 @@ variable "subaccount_id" { description = "The ID of the trial subaccount." default = "" } +# ------------------------------------------------------------------------------------------------------ +# use case specific variables +# ------------------------------------------------------------------------------------------------------ +variable "abap_admin_email" { + type = string + description = "Email of the ABAP Administrator." + default = "" +} -variable "cli_server_url" { +variable "create_cf_space" { + type = bool + description = "Determines whether a new CF space should be created. Must be true if no space with the given name exists for the org, false otherwise. If CF isn't enabled for no subaccount a new space will always be created" + default = false +} + +variable "cf_space_name" { type = string - description = "The BTP CLI server URL." - default = "https://cli.btp.cloud.sap" + description = "The name of the CF space to use." + default = "dev" +} + +# ------------------------------------------------------------------------------------------------------ +# service plans +# ------------------------------------------------------------------------------------------------------ +variable "service_plan__abap_trial" { + type = string + description = "The plan for service 'ABAP environment' with technical name 'abap-trial'" + default = "shared" + validation { + condition = contains(["shared"], var.service_plan__abap_trial) + error_message = "Invalid value for service_plan__abap_trial. Only 'shared' is allowed." + } +} + +variable "service_plan__cloudfoundry" { + type = string + description = "The plan for service 'Destination Service' with technical name 'destination'" + default = "trial" + validation { + condition = contains(["trial"], var.service_plan__cloudfoundry) + error_message = "Invalid value for service_plan__cloudfoundry. Only 'trial' is allowed." + } } +# ------------------------------------------------------------------------------------------------------ +# User lists +# ------------------------------------------------------------------------------------------------------ variable "cf_org_managers" { type = list(string) description = "List of managers for the Cloud Foundry org." @@ -24,32 +85,20 @@ variable "cf_org_managers" { variable "cf_space_managers" { type = list(string) description = "List of managers for the Cloud Foundry space." + default = [] } variable "cf_space_developers" { type = list(string) description = "List of developers for the Cloud Foundry space." + default = [] } -variable "cf_space_name" { - type = string - description = "The name of the CF space to use." -} - -variable "create_cf_space" { - type = bool - description = "Determines whether a new CF space should be created. Must be true if no space with the given name exists for the org, false otherwise. If CF isn't enabled for the subaccount a new space will always be created" - default = false -} - -variable "abap_admin_email" { - type = string - description = "Email of the ABAP Administrator." - default = "" -} - -variable "create_tfvars_file_for_next_stage" { +# ------------------------------------------------------------------------------------------------------ +# Switch for creating tfvars for step 2 +# ------------------------------------------------------------------------------------------------------ +variable "create_tfvars_file_for_step2" { type = bool - description = "Switch to enable the creation of the tfvars file for the next step." + description = "Switch to enable the creation of the tfvars file for step 2." default = false -} +} \ No newline at end of file diff --git a/released/discovery_center/mission_3248_trial/step2/main.tf b/released/discovery_center/mission_3248_trial/step2/main.tf index 0091c829..0a1d8751 100644 --- a/released/discovery_center/mission_3248_trial/step2/main.tf +++ b/released/discovery_center/mission_3248_trial/step2/main.tf @@ -1,16 +1,6 @@ -### -# Assignment of Cloud Foundry space roles -### -resource "cloudfoundry_org_role" "org_managers" { - for_each = toset(var.cf_org_managers) - username = each.value - type = "organization_manager" - org = var.cf_org_id -} - -### -# Creation of Cloud Foundry space -### +# ------------------------------------------------------------------------------------------------------ +# Create the Cloud Foundry space +# ------------------------------------------------------------------------------------------------------ data "cloudfoundry_space" "dev" { count = var.create_cf_space ? 0 : 1 name = var.cf_space_name @@ -24,34 +14,18 @@ resource "cloudfoundry_space" "dev" { org = var.cf_org_id } -locals { - space_id = var.create_cf_space ? cloudfoundry_space.dev[0].id : data.cloudfoundry_space.dev[0].id -} - -### -# Assignment of Cloud Foundry space roles -### -resource "cloudfoundry_space_role" "space_managers" { - for_each = toset(var.cf_space_managers) - username = each.value - type = "space_manager" - space = local.space_id -} - -resource "cloudfoundry_space_role" "space_developers" { - for_each = toset(var.cf_space_developers) - username = each.value - type = "space_developer" - space = local.space_id -} - -### -# Creation of service instance for ABAP -### +# ------------------------------------------------------------------------------------------------------ +# SETUP ALL SERVICES FOR CF USAGE +# ------------------------------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------------------------------ +# Setup abap-trial (ABAP environment) +# ------------------------------------------------------------------------------------------------------ +# data "cloudfoundry_service" "abap_service_plans" { name = "abap-trial" } +# Instance creation resource "cloudfoundry_service_instance" "abap_trial" { depends_on = [cloudfoundry_space_role.space_managers, cloudfoundry_space_role.space_developers] name = "abap-trial" @@ -68,11 +42,54 @@ resource "cloudfoundry_service_instance" "abap_trial" { } } -### -# Creation of service key for ABAP Development Tools (ADT) -### +# Service key creation (for ABAP Development Tools (ADT)) resource "cloudfoundry_service_credential_binding" "abap_trial_service_key" { type = "key" name = "abap_trial_adt_key" service_instance = cloudfoundry_service_instance.abap_trial.id } + +# ------------------------------------------------------------------------------------------------------ +# USERS AND ROLES +# ------------------------------------------------------------------------------------------------------ +data "btp_whoami" "me" {} + +locals { + cf_org_managers = setsubtract(toset(var.cf_org_managers), [data.btp_whoami.me.email]) + + cf_space_managers = var.create_cf_space ? var.cf_space_managers : setsubtract(toset(var.cf_space_managers), [data.btp_whoami.me.email]) + cf_space_developers = var.create_cf_space ? var.cf_space_developers : setsubtract(toset(var.cf_space_developers), [data.btp_whoami.me.email]) +} +# ------------------------------------------------------------------------------------------------------ +# cf_org_managers: Assign organization_manager role +# ------------------------------------------------------------------------------------------------------ +resource "cloudfoundry_org_role" "org_managers" { + for_each = toset(local.cf_org_managers) + username = each.value + type = "organization_manager" + org = var.cf_org_id +} + +# ------------------------------------------------------------------------------------------------------ +# cf_space_managers: Assign space_manager role +# ------------------------------------------------------------------------------------------------------ +locals { + space_id = var.create_cf_space ? cloudfoundry_space.dev[0].id : data.cloudfoundry_space.dev[0].id +} + +resource "cloudfoundry_space_role" "space_managers" { + for_each = toset(local.cf_space_managers) + username = each.value + type = "space_manager" + space = local.space_id +} + +# ------------------------------------------------------------------------------------------------------ +# cf_space_developers: Assign space_developer role +# ------------------------------------------------------------------------------------------------------ +resource "cloudfoundry_space_role" "space_developers" { + for_each = toset(local.cf_space_developers) + username = each.value + type = "space_developer" + space = local.space_id +} \ No newline at end of file diff --git a/released/discovery_center/mission_3248_trial/step2/provider.tf b/released/discovery_center/mission_3248_trial/step2/provider.tf index a417494d..7d04ed08 100644 --- a/released/discovery_center/mission_3248_trial/step2/provider.tf +++ b/released/discovery_center/mission_3248_trial/step2/provider.tf @@ -1,13 +1,22 @@ terraform { required_providers { cloudfoundry = { - source = "sap/cloudfoundry" + source = "SAP/cloudfoundry" version = "1.0.0-rc1" } + btp = { + source = "SAP/btp" + version = "~> 1.5.0" + } } } -# This will only work if we know the region in advance +provider "btp" { + globalaccount = var.globalaccount + cli_server_url = var.cli_server_url +} + + provider "cloudfoundry" { api_url = var.cf_api_url } diff --git a/released/discovery_center/mission_3248_trial/step2/variables.tf b/released/discovery_center/mission_3248_trial/step2/variables.tf index a7d2e919..930060a1 100644 --- a/released/discovery_center/mission_3248_trial/step2/variables.tf +++ b/released/discovery_center/mission_3248_trial/step2/variables.tf @@ -1,3 +1,24 @@ +# Description: This file contains the input variables for step 2 + +# The globalaccount subdomain where the sub account shall be created. +variable "globalaccount" { + type = string + description = "The globalaccount subdomain where the sub account shall be created." +} + +variable "cli_server_url" { + type = string + description = "The BTP CLI server URL." + default = "https://cli.btp.cloud.sap" +} + +# ------------------------------------------------------------------------------------------------------ +# use case specific variables +# ------------------------------------------------------------------------------------------------------ +variable "abap_admin_email" { + type = string + description = "Email of the ABAP Administrator." +} variable "cf_api_url" { type = string @@ -9,6 +30,19 @@ variable "cf_org_id" { description = "The Cloud Foundry landscape (format example eu10-004)." } +variable "create_cf_space" { + type = bool + description = "Determines whether a new CF space should be created. Must be true if no space with the name cf_space_name exists for the Org, yet, and false otherwise." +} + +variable "cf_space_name" { + type = string + description = "The name of the CF space to use. If create_cf_space is true a new space with the given name will be created" +} + +# ------------------------------------------------------------------------------------------------------ +# User lists +# ------------------------------------------------------------------------------------------------------ variable "cf_org_managers" { type = list(string) description = "List of managers for the Cloud Foundry org." @@ -23,18 +57,3 @@ variable "cf_space_developers" { type = list(string) description = "List of developers for the Cloud Foundry space." } - -variable "cf_space_name" { - type = string - description = "The name of the CF space to use. If create_cf_space is true a new space with the given name will be created" -} - -variable "create_cf_space" { - type = bool - description = "Determines whether a new CF space should be created. Must be true if no space with the name cf_space_name exists for the Org, yet, and false otherwise." -} - -variable "abap_admin_email" { - type = string - description = "Email of the ABAP Administrator." -}