Skip to content

Commit 84a48bc

Browse files
committed
chore: update exercises for SIT
1 parent 1de2c2a commit 84a48bc

File tree

13 files changed

+169
-106
lines changed

13 files changed

+169
-106
lines changed

released/SAP-Inside-Tracks/SITBLR_DEC_2024/exercises/EXERCISE2/README.md

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ variable "stage" {
7272

7373
You can see a different type of validation here. We use the [`contains` function](https://developer.hashicorp.com/terraform/language/functions/contains) to check if the input is part of the list of valid stages.
7474

75+
7576
#### Input variable *costcenter*
7677

7778
Let us move on to the *cost center* variable. Add the following code to the `variables.tf` file:
@@ -91,6 +92,9 @@ variable "costcenter" {
9192

9293
Nothing new here that we have not seen before. We use the `can` expression to check if the input is valid when compared to a specific regular expression leveraging the `regex` function.
9394

95+
> [!NOTE]
96+
> You can also use functions inside of functions to express more complex conditions such as [concat function](https://developer.hashicorp.com/terraform/language/functions/concat) to combine multiple lists into one list and then use the `contains` function to check if the input is part of that list.
97+
9498
#### Input variable *org_name*
9599

96100
And last but not least we want to add and validate the *organization name* variable. Add the following code to the `variables.tf` file:
@@ -100,23 +104,9 @@ variable "org_name" {
100104
type = string
101105
description = "Defines to which organization the project account shall belong to."
102106
default = "B2C"
103-
104-
validation {
105-
condition = contains(concat(
106-
// Cross Development
107-
["B2B", "B2C", "ECOMMERCE"],
108-
// Internal IT
109-
["PLATFORMDEV", "INTIT"],
110-
// Financial Services
111-
["FSIT"],
112-
), var.org_name)
113-
error_message = "Please select a valid org name for the project account."
114-
}
115107
}
116108
```
117109

118-
As you can see you can also use functions inside of functions to express more complex conditions. In this case we use the [`concat` function](https://developer.hashicorp.com/terraform/language/functions/concat) to combine multiple lists into one list and then use the `contains` function to check if the input is part of the list of valid organization names.
119-
120110
As we have all variables in place, you should save the changes now.
121111

122112
> [!NOTE]
@@ -157,11 +147,10 @@ resource "btp_subaccount" "project" {
157147
"stage" = ["${var.stage}"],
158148
"costcenter" = ["${var.costcenter}"]
159149
}
160-
usage = "NOT_USED_FOR_PRODUCTION"
161150
}
162151
```
163152

164-
We used the resource [`btp_subaccount`](https://registry.terraform.io/providers/SAP/btp/latest/docs/resources/subaccount) to create the subaccount. We referenced the local values to set the name and domain. We added labels to the subaccount to display the stage and cost center. The usage of the subaccount is hardcoded to `NOT_USED_FOR_PRODUCTION`.
153+
We used the resource [`btp_subaccount`](https://registry.terraform.io/providers/SAP/btp/latest/docs/resources/subaccount) to create the subaccount. We referenced the local values to set the name and domain. We added labels to the subaccount to display the stage and cost center.
165154

166155
As we have the configuration in place, you should save the changes now.
167156

released/SAP-Inside-Tracks/SITBLR_DEC_2024/exercises/EXERCISE2/SOLUTION_EX2/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ resource "btp_subaccount" "project" {
1717
"stage" = ["${var.stage}"],
1818
"costcenter" = ["${var.costcenter}"]
1919
}
20-
usage = "NOT_USED_FOR_PRODUCTION"
2120
}

released/SAP-Inside-Tracks/SITBLR_DEC_2024/exercises/EXERCISE2/SOLUTION_EX2/variables.tf

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,4 @@ variable "org_name" {
5252
type = string
5353
description = "Defines to which organisation the project account shall belong to."
5454
default = "B2C"
55-
56-
validation {
57-
condition = contains(concat(
58-
// Cross Development
59-
["B2B", "B2C", "ECOMMERCE"],
60-
// Internal IT
61-
["PLATFORMDEV", "INTIT"],
62-
// Financial Services
63-
["FSIT"],
64-
), var.org_name)
65-
error_message = "Please select a valid org name for the project account."
66-
}
6755
}

released/SAP-Inside-Tracks/SITBLR_DEC_2024/exercises/EXERCISE3/SOLUTION_EX3/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ variable "bas_developers" {
7979
description = "List of users to assign the Developer role."
8080
}
8181
variable "bas_service_name" {
82-
type = string
82+
type = string
8383
description = "Service name for Business Application Studio."
8484
default = "sapappstudio"
8585

released/SAP-Inside-Tracks/SITBLR_DEC_2024/exercises/EXERCISE4/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ resource "btp_subaccount_entitlement" "cf_application_runtime" {
3333
subaccount_id = btp_subaccount.project.id
3434
service_name = "APPLICATION_RUNTIME"
3535
plan_name = "MEMORY"
36-
amount = 1
3736
}
3837
3938
resource "btp_subaccount_environment_instance" "cloudfoundry" {
@@ -46,7 +45,6 @@ resource "btp_subaccount_environment_instance" "cloudfoundry" {
4645
plan_name = "trial"
4746
parameters = jsonencode({
4847
instance_name = local.project_subaccount_cf_org
49-
memory = 1024
5048
})
5149
timeouts = {
5250
create = "1h"

released/SAP-Inside-Tracks/SITBLR_DEC_2024/exercises/EXERCISE4/SOLUTION_EX4/main.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ resource "btp_subaccount" "project" {
1818
"stage" = ["${var.stage}"],
1919
"costcenter" = ["${var.costcenter}"]
2020
}
21-
usage = "NOT_USED_FOR_PRODUCTION"
2221
}
2322

2423

@@ -29,7 +28,6 @@ resource "btp_subaccount_entitlement" "cf_application_runtime" {
2928
subaccount_id = btp_subaccount.project.id
3029
service_name = "APPLICATION_RUNTIME"
3130
plan_name = "MEMORY"
32-
amount = 1
3331
}
3432

3533
resource "btp_subaccount_environment_instance" "cloudfoundry" {
Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
###
2-
# Setup of names in accordance to the company's naming conventions
3-
###
41
locals {
52
project_subaccount_name = "${var.org_name} | ${var.project_name}: CF - ${var.stage}"
63
project_subaccount_domain = lower(replace("${var.org_name}-${var.project_name}-${var.stage}", " ", "-"))
74
project_subaccount_cf_org = replace("${var.org_name}_${lower(var.project_name)}-${lower(var.stage)}", " ", "_")
85
}
96

10-
###
11-
# Creation of subaccount
12-
###
137
resource "btp_subaccount" "project" {
148
name = local.project_subaccount_name
159
subdomain = local.project_subaccount_domain
@@ -18,15 +12,91 @@ resource "btp_subaccount" "project" {
1812
"stage" = ["${var.stage}"],
1913
"costcenter" = ["${var.costcenter}"]
2014
}
21-
usage = "NOT_USED_FOR_PRODUCTION"
2215
}
2316

24-
###
25-
# Assignment of emergency admins to subaccount
26-
###
27-
resource "btp_subaccount_role_collection_assignment" "subaccount_users" {
28-
for_each = toset(var.emergency_admins)
17+
resource "btp_subaccount_entitlement" "bas" {
18+
subaccount_id = btp_subaccount.project.id
19+
service_name = "sapappstudiotrial"
20+
plan_name = var.bas_plan_name
21+
}
22+
resource "btp_subaccount_subscription" "bas-subscribe" {
23+
subaccount_id = btp_subaccount.project.id
24+
app_name = "sapappstudiotrial"
25+
plan_name = var.bas_plan_name
26+
depends_on = [btp_subaccount_entitlement.bas]
27+
}
28+
resource "btp_subaccount_role_collection_assignment" "Business_Application_Studio_Administrator" {
29+
for_each = toset(var.bas_admins)
30+
subaccount_id = btp_subaccount.project.id
31+
role_collection_name = "Business_Application_Studio_Administrator"
32+
user_name = each.value
33+
depends_on = [btp_subaccount_subscription.bas-subscribe]
34+
}
35+
36+
resource "btp_subaccount_role_collection_assignment" "Business_Application_Studio_Developer" {
2937
subaccount_id = btp_subaccount.project.id
30-
role_collection_name = "Subaccount Administrator"
38+
role_collection_name = "Business_Application_Studio_Developer"
39+
for_each = toset(var.bas_developers)
3140
user_name = each.value
41+
depends_on = [btp_subaccount_subscription.bas-subscribe]
42+
}
43+
44+
resource "btp_subaccount_entitlement" "cf_application_runtime" {
45+
subaccount_id = btp_subaccount.project.id
46+
service_name = "APPLICATION_RUNTIME"
47+
plan_name = "MEMORY"
48+
}
49+
50+
resource "btp_subaccount_environment_instance" "cloudfoundry" {
51+
depends_on = [btp_subaccount_entitlement.cf_application_runtime]
52+
subaccount_id = btp_subaccount.project.id
53+
name = local.project_subaccount_cf_org
54+
landscape_label = var.cf_landscape_label
55+
environment_type = "cloudfoundry"
56+
service_name = "cloudfoundry"
57+
plan_name = "trial"
58+
parameters = jsonencode({
59+
instance_name = local.project_subaccount_cf_org
60+
})
61+
timeouts = {
62+
create = "1h"
63+
update = "35m"
64+
delete = "30m"
65+
}
66+
}
67+
68+
resource "cloudfoundry_org_role" "my_role" {
69+
for_each = var.cf_org_user
70+
username = each.value
71+
type = "organization_user"
72+
org = btp_subaccount_environment_instance.cloudfoundry.platform_id
73+
}
74+
75+
resource "cloudfoundry_space" "space" {
76+
name = var.name
77+
org = btp_subaccount_environment_instance.cloudfoundry.platform_id
78+
}
79+
80+
resource "cloudfoundry_space_role" "cf_space_managers" {
81+
for_each = toset(var.cf_space_managers)
82+
username = each.value
83+
type = "space_manager"
84+
space = cloudfoundry_space.space.id
85+
depends_on = [cloudfoundry_org_role.my_role]
86+
}
87+
88+
resource "cloudfoundry_space_role" "cf_space_developers" {
89+
for_each = toset(var.cf_space_developers)
90+
username = each.value
91+
type = "space_developer"
92+
space = cloudfoundry_space.space.id
93+
depends_on = [cloudfoundry_org_role.my_role]
94+
}
95+
96+
resource "cloudfoundry_space_role" "cf_space_auditors" {
97+
for_each = toset(var.cf_space_auditors)
98+
username = each.value
99+
type = "space_auditor"
100+
space = cloudfoundry_space.space.id
101+
depends_on = [cloudfoundry_org_role.my_role]
32102
}

released/SAP-Inside-Tracks/SITBLR_DEC_2024/exercises/EXERCISE5/SOLUTION_EX5/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ output "subaccount_name" {
77
value = btp_subaccount.project.name
88
description = "The name of the project subaccount."
99
}
10+
11+
output "cloudfoundry_org_name" {
12+
value = local.project_subaccount_cf_org
13+
description = "The name of the cloudfoundry org connected to the project account."
14+
}
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
21
terraform {
32
required_providers {
43
btp = {
54
source = "sap/btp"
65
version = "~> 1.8.0"
76
}
7+
cloudfoundry = {
8+
source = "cloudfoundry/cloudfoundry"
9+
version = "~> 1.1.0"
10+
}
811
}
912

1013
}
1114

12-
# Please checkout documentation on how best to authenticate against SAP BTP
13-
# via the Terraform provider for SAP BTP
1415
provider "btp" {
1516
globalaccount = var.globalaccount
1617
}
18+
19+
provider "cloudfoundry" {
20+
api_url = "https://api.cf.${var.region}-001.hana.ondemand.com"
21+
}

released/SAP-Inside-Tracks/SITBLR_DEC_2024/exercises/EXERCISE5/SOLUTION_EX5/variables.tf

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
###
2-
# Provider configuration
3-
###
41
variable "globalaccount" {
52
type = string
63
description = "The subdomain of the SAP BTP global account."
@@ -12,13 +9,10 @@ variable "region" {
129
default = "us10"
1310
}
1411

15-
###
16-
# Subaccount setup
17-
###
1812
variable "project_name" {
1913
type = string
2014
description = "The subaccount name."
21-
default = "proj-1234"
15+
default = "myproj-1234"
2216

2317
validation {
2418
condition = can(regex("^[a-zA-Z0-9_\\-]{1,200}", var.project_name))
@@ -48,29 +42,74 @@ variable "costcenter" {
4842
}
4943
}
5044

45+
variable "bas_plan_name" {
46+
description = "BAS plan"
47+
type = string
48+
default = "free"
49+
}
50+
51+
variable "bas_developers" {
52+
description = "BAS developers"
53+
type = list(string)
54+
55+
}
56+
57+
variable "bas_admins" {
58+
description = "BAS Admininstrators"
59+
type = list(string)
60+
61+
}
62+
5163
variable "org_name" {
5264
type = string
5365
description = "Defines to which organisation the project account shall belong to."
5466
default = "B2C"
67+
}
5568

56-
validation {
57-
condition = contains(concat(
58-
// Cross Development
59-
["B2B", "B2C", "ECOMMERCE"],
60-
// Internal IT
61-
["PLATFORMDEV", "INTIT"],
62-
// Financial Services
63-
["FSIT"],
64-
), var.org_name)
65-
error_message = "Please select a valid org name for the project account."
66-
}
69+
variable "cf_space_name" {
70+
type = string
71+
description = "The name of the Cloud Foundry space."
72+
default = "dev"
6773
}
6874

69-
###
70-
# Emergency admin setup
71-
###
72-
variable "emergency_admins" {
73-
type = list(string)
74-
description = "Defines the colleagues who are added to each subaccount as emergency administrators."
75+
variable "cf_landscape_label" {
76+
type = string
77+
description = "The region where the project account shall be created in."
78+
default = "cf-us10-001"
79+
}
80+
81+
variable "cf_org_name" {
82+
type = string
83+
description = "The name for the Cloud Foundry Org."
84+
default = ""
85+
}
86+
87+
variable "cf_org_user" {
88+
type = set(string)
89+
description = "Defines the colleagues who are added to each subaccount as subaccount administrators."
7590
7691
}
92+
93+
variable "name" {
94+
type = string
95+
description = "The name of the Cloud Foundry space."
96+
default = "dev"
97+
}
98+
99+
variable "cf_space_managers" {
100+
type = list(string)
101+
description = "The list of Cloud Foundry space managers."
102+
default = []
103+
}
104+
105+
variable "cf_space_developers" {
106+
type = list(string)
107+
description = "The list of Cloud Foundry space developers."
108+
default = []
109+
}
110+
111+
variable "cf_space_auditors" {
112+
type = list(string)
113+
description = "The list of Cloud Foundry space auditors."
114+
default = []
115+
}

0 commit comments

Comments
 (0)