From bc0219bd2c3237646500dc42497724e997936bec Mon Sep 17 00:00:00 2001 From: Jens Glander Date: Wed, 4 Sep 2024 16:52:00 +0200 Subject: [PATCH 1/2] 3585_trial: add cicd (optional app subscription) --- .../mission_3585_trial/README.md | 5 +- .../mission_3585_trial/main.tf | 48 +++++++++++++++++++ .../mission_3585_trial/outputs.tf | 5 ++ .../mission_3585_trial/sample.tfvars | 6 ++- .../mission_3585_trial/variables.tf | 40 +++++++++++++++- 5 files changed, 99 insertions(+), 5 deletions(-) diff --git a/released/discovery_center/mission_3585_trial/README.md b/released/discovery_center/mission_3585_trial/README.md index 171173e7..5959e8b1 100644 --- a/released/discovery_center/mission_3585_trial/README.md +++ b/released/discovery_center/mission_3585_trial/README.md @@ -14,9 +14,10 @@ In a newly created trial account this is already true and you are good to go imm But if you have already used services and/or setup subscriptions in your trial account, you have to make sure that you free up these resources to start with this setup here (i.e. delete the corresponding services/subscriptions used for this Discover Center Mission setup). Otherwise the setup would fail! -For this mission setup the following resource (app subscription) is used: +For this mission setup the following resources (app subscriptions) is used: -- SAP Build Work Zone, standard edition (Subscription) +- SAP Build Work Zone, standard edition +- Continuous Integration & Delivery You could delete these resources in your [BTP Trial Cockpit](https://cockpit.btp.cloud.sap/trial) on the corresponding trial subaccount pages - Services > Instances and Subscriptions diff --git a/released/discovery_center/mission_3585_trial/main.tf b/released/discovery_center/mission_3585_trial/main.tf index 2310a8ef..29ed603a 100644 --- a/released/discovery_center/mission_3585_trial/main.tf +++ b/released/discovery_center/mission_3585_trial/main.tf @@ -28,7 +28,10 @@ data "btp_subaccount" "dc_mission" { # locals { service_name__sap_launchpad = "SAPLaunchpad" + # optional + service_name__cicd_app = "cicd-app" } + # ------------------------------------------------------------------------------------------------------ # Setup SAPLaunchpad (SAP Build Work Zone, standard edition) # ------------------------------------------------------------------------------------------------------ @@ -53,6 +56,26 @@ data "btp_subaccount_subscription" "sap_launchpad" { depends_on = [btp_subaccount_subscription.sap_launchpad] } +# ------------------------------------------------------------------------------------------------------ +# Setup cicd-app (Continuous Integration & Delivery) +# ------------------------------------------------------------------------------------------------------ +# Entitle +resource "btp_subaccount_entitlement" "cicd_app" { + count = var.use_optional_resources ? 1 : 0 + subaccount_id = data.btp_subaccount.dc_mission.id + service_name = local.service_name__cicd_app + plan_name = var.service_plan__cicd_app + amount = var.service_plan__cicd_app == "free" ? 1 : null +} +# Subscribe +resource "btp_subaccount_subscription" "cicd_app" { + count = var.use_optional_resources ? 1 : 0 + subaccount_id = data.btp_subaccount.dc_mission.id + app_name = local.service_name__cicd_app + plan_name = var.service_plan__cicd_app + depends_on = [btp_subaccount_entitlement.cicd_app] +} + # ------------------------------------------------------------------------------------------------------ # USERS AND ROLES # ------------------------------------------------------------------------------------------------------ @@ -77,4 +100,29 @@ resource "btp_subaccount_role_collection_assignment" "launchpad_admin" { role_collection_name = "Launchpad_Admin" user_name = each.value depends_on = [btp_subaccount_subscription.sap_launchpad] +} + +# ------------------------------------------------------------------------------------------------------ +# Assign role collection "CICD Service Administrator" +# ------------------------------------------------------------------------------------------------------ +# optional app subscription + +resource "btp_subaccount_role_collection_assignment" "cicd_admins" { + for_each = toset(var.use_optional_resources == true ? var.cicd_admins : []) + subaccount_id = data.btp_subaccount.dc_mission.id + role_collection_name = "CICD Service Administrator" + user_name = each.value + depends_on = [btp_subaccount_subscription.cicd_app] +} + +# ------------------------------------------------------------------------------------------------------ +# Assign role collection "CICD Service Developer" +# ------------------------------------------------------------------------------------------------------ +# optional app subscription +resource "btp_subaccount_role_collection_assignment" "cicd_developers" { + for_each = toset(var.use_optional_resources == true ? var.cicd_developers : []) + subaccount_id = data.btp_subaccount.dc_mission.id + role_collection_name = "CICD Service Developer" + user_name = each.value + depends_on = [btp_subaccount_subscription.cicd_app] } \ No newline at end of file diff --git a/released/discovery_center/mission_3585_trial/outputs.tf b/released/discovery_center/mission_3585_trial/outputs.tf index 020a442b..de4900c1 100644 --- a/released/discovery_center/mission_3585_trial/outputs.tf +++ b/released/discovery_center/mission_3585_trial/outputs.tf @@ -6,4 +6,9 @@ output "subaccount_id" { output "sap_launchpad_subscription_url" { value = data.btp_subaccount_subscription.sap_launchpad.subscription_url description = "SAP Build Work Zone, standard edition subscription URL." +} + +output "cicd_app_subscription_url" { + value = var.use_optional_resources ? btp_subaccount_subscription.cicd_app[0].subscription_url : null + description = "Continuous Integration & Delivery subscription URL." } \ No newline at end of file diff --git a/released/discovery_center/mission_3585_trial/sample.tfvars b/released/discovery_center/mission_3585_trial/sample.tfvars index 86f32910..4fb6cb92 100644 --- a/released/discovery_center/mission_3585_trial/sample.tfvars +++ b/released/discovery_center/mission_3585_trial/sample.tfvars @@ -11,7 +11,9 @@ region = "us10" subaccount_id = "" # ------------------------------------------------------------------------------------------------------ -# USER ROLES +# Use case specific configuration (please adapt!) # ------------------------------------------------------------------------------------------------------ subaccount_admins = ["another.user@test.com"] -launchpad_admins = ["another.user@test.com", "you@test.com"] \ No newline at end of file +launchpad_admins = ["another.user@test.com", "you@test.com"] +cicd_admins = ["another.user@test.com", "you@test.com"] +cicd_developers = ["another.user@test.com", "you@test.com"] \ No newline at end of file diff --git a/released/discovery_center/mission_3585_trial/variables.tf b/released/discovery_center/mission_3585_trial/variables.tf index 41eb1226..896a06e1 100644 --- a/released/discovery_center/mission_3585_trial/variables.tf +++ b/released/discovery_center/mission_3585_trial/variables.tf @@ -36,6 +36,11 @@ variable "subaccount_id" { default = "" } +variable "use_optional_resources" { + type = bool + description = "optional resources are ignored if value is false" + default = true +} # ------------------------------------------------------------------------------------------------------ # app subscription plans @@ -50,6 +55,16 @@ variable "service_plan__sap_launchpad" { } } +variable "service_plan__cicd_app" { + type = string + description = "The plan for app subscription 'SAP Continuous Integration and Delivery' with technical name 'cicd-app'" + default = "trial" + validation { + condition = contains(["trial"], var.service_plan__cicd_app) + error_message = "Invalid value for service_plan__cicd_app. Only 'trial' are allowed." + } +} + # ------------------------------------------------------------------------------------------------------ # User lists # ------------------------------------------------------------------------------------------------------ @@ -73,4 +88,27 @@ variable "launchpad_admins" { condition = length([for email in var.launchpad_admins : can(regex("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$", email))]) == length(var.launchpad_admins) error_message = "Please enter a valid email address." } -} \ No newline at end of file +} + +variable "cicd_admins" { + type = list(string) + description = "Defines the colleagues who are administrators for the CI/CD service." + + # add validation to check if admins contains a list of valid email addresses + validation { + condition = length([for email in var.cicd_admins : can(regex("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$", email))]) == length(var.cicd_admins) + error_message = "Please enter a valid email address." + } +} + +variable "cicd_developers" { + type = list(string) + description = "Defines the colleagues who are developers for the CI/CD service." + + # add validation to check if admins contains a list of valid email addresses + validation { + condition = length([for email in var.cicd_developers : can(regex("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$", email))]) == length(var.cicd_developers) + error_message = "Please enter a valid email address." + } +} + From b32082b0ce4193f415149c48b36fddd4c5616009 Mon Sep 17 00:00:00 2001 From: Jens Glander Date: Wed, 4 Sep 2024 16:58:38 +0200 Subject: [PATCH 2/2] fixed tf formatting issues --- released/discovery_center/mission_3585_trial/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/released/discovery_center/mission_3585_trial/main.tf b/released/discovery_center/mission_3585_trial/main.tf index 29ed603a..93238757 100644 --- a/released/discovery_center/mission_3585_trial/main.tf +++ b/released/discovery_center/mission_3585_trial/main.tf @@ -29,7 +29,7 @@ data "btp_subaccount" "dc_mission" { locals { service_name__sap_launchpad = "SAPLaunchpad" # optional - service_name__cicd_app = "cicd-app" + service_name__cicd_app = "cicd-app" } # ------------------------------------------------------------------------------------------------------