Skip to content

Commit 121bee1

Browse files
committed
chore: update SIT scripts
1 parent b4d25fb commit 121bee1

File tree

16 files changed

+244
-31
lines changed

16 files changed

+244
-31
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ In this HandsOn we want to make you familiar with the Terraform Provider for SAP
1010

1111
1. [Exercise 1 - Configure the Terraform Provider for SAP BTP](exercises/EXERCISE1/README.md)
1212
1. [Exercise 2 - Setup of a subaccount](exercises/EXERCISE2/README.md)
13-
1. [Exercise 3 - Assignment of subaccount emergency administrators](exercises/EXERCISE3/README.md)
14-
1. [Exercise 4 - Assign entitlements to a subaccount](exercises/EXERCISE4/README.md)
15-
1. [Exercise 5 - Create a service instance](exercises/EXERCISE5/README.md)
16-
1. [Exercise 6 - Handle configuration drift](exercises/EXERCISE6/README.md)
17-
1. [Exercise 7 - Setup a Cloud Foundry environment and a space (optional)](exercises/EXERCISE7/README.md)
18-
1. [Exercise 8 - Cleanup](exercises/EXERCISE8/README.md)
13+
1. [Exercise 3 - Assign entitlement,Subscription and its role assignments to a subaccount](exercises/EXERCISE3/README.md)
14+
1. [Exercise 4 - Setup a Cloud Foundry environment and a space (optional)](exercises/EXERCISE4/README.md)
15+
1. [Exercise 5 - Exercise 5 - Assignment of subaccount emergency administrators](exercises/EXERCISE5/README.md)
16+
1. [Exercise 6 - Cleanup](exercises/EXERCISE6/README.md)
1917

2018
The level of the exercises is beginner. You don't need any prior knowledge about Terraform or the Terraform Provider for SAP BTP. We will guide you through the exercises step by step.
2119

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,4 @@ You can also check that everything is in place via the SAP BTP cockpit. You shou
120120

121121
You've now successfully entitled services and applications to the subaccount.
122122
123-
Continue to - [Exercise 4 - Create service instances and app subscriptions](../EXERCISE5/README.md).
123+
Continue to - [Exercise 4 - Setup a Cloud Foundry environment](../EXERCISE4/README.md).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,4 @@ You can also check that everything is in place via the SAP BTP cockpit. You shou
286286

287287
You've now successfully created a Cloud Foundry environment instance as well as a Cloud Foundry space in SAP BTP.
288288
289-
Continue to - [Exercise 5 - Cleanup](../EXERCISE8/README.md).
289+
Continue to - [Exercise 5 - Adding Emergency Administrators](../EXERCISE5/README.md).
Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,81 @@
1-
# Exercise 5 - Cleanup
1+
# Exercise 5 - Assignment of subaccount emergency administrators
22

33
## Goal of this Exercise 🎯
44

5-
The goal of this exercise is to delete the resources that were created in the previous exercises. This is important to avoid unnecessary costs and to keep the environment clean.
5+
In this exercise you will learn how to assign users to role collections in the subaccount. We assume that for each subaccount we want to add emergency administrators to the role collection `Subacount Administrator`.
66

7-
## Step 1: Trigger the deletion of the resources
7+
## Assign role collection to users
88

9-
You can delete the resources by running the following command:
9+
### Step 1: Enhance the variables
1010

11-
```bash
12-
terraform destroy
11+
First we need to enhance the `variables.tf` file to add the new variable `emergency_admins`. This variable will be used to define the list of users that will be assigned to the role collection `Subaccount Administrator`. Open the `variables.tf` file and add the following code:
12+
13+
```terraform
14+
variable "emergency_admins" {
15+
type = list(string)
16+
description = "Defines the colleagues who are added to each subaccount as emergency administrators."
17+
18+
}
19+
```
20+
21+
As you can see, the variable type can be a complex one. In this case, it is a list of strings. We define a default value for the variable, which is a list of dummy two email addresses.
22+
23+
This variable will be used in the next step to assign the users to the role collection. Save the changes.
24+
25+
### Step 2: Add the role collection configuration
26+
27+
Now we need to add the configuration to assign the users to the role collection. We use the resource [btp_subaccount_role_collection_assignment](https://registry.terraform.io/providers/SAP/btp/latest/docs/resources/subaccount_role_collection_assignment) to achieve this.
28+
In addition we must iterate through the list of names and assign each user to the role collection. Open the `main.tf` file and add the following code:
29+
30+
```terraform
31+
resource "btp_subaccount_role_collection_assignment" "subaccount_users" {
32+
for_each = toset(var.emergency_admins)
33+
subaccount_id = btp_subaccount.project.id
34+
role_collection_name = "Subaccount Administrator"
35+
user_name = each.value
36+
}
1337
```
1438

15-
The output of the command will look similar to this:
39+
To create a resource for our user we make use of the [`for_each`](https://developer.hashicorp.com/terraform/language/meta-arguments/for_each) meta-argument provided by Terraform. This allows us to create a resource for each element in the list of users. The `for_each` argument works on a map or a set, so we must transform our list of strings into a set via the [`toset`](https://www.terraform.io/docs/language/functions/toset.html) function. We access the value of the current iteration via `each.value`. The `subaccount_id` is set to the id of the subaccount we created in the previous exercise.
40+
41+
> [!NOTE]
42+
> How does Terraform know that it first needs to create the subaccount and then assign the users to the role collection? Terraform automatically detects this dependency as we are using the output of the subaccount creation namely the `btp_subaccount.project.id` as parameter for the role collection assignment. Due to this Terraform knows that the role collection assignment can only be created after the subaccount has been created and creates a corresponding execution plan. Sometimes you must explicitly model this dependency and we will see how to that in [exercise 5](../EXERCISE5/README.md).
43+
44+
That is already all we need to do. Save the changes.
45+
46+
### Step 3: Apply the changes
1647

17-
<img width="600px" src="assets/ex8_1.png" alt="executing terraform destroy">
48+
Now we can apply the changes to our subaccount. Run the following commands:
1849

19-
Take also a look at the summary. This will show how the number of resources to be deleted:
50+
> [!NOTE]
51+
> As we did not change the configuration of the provider or add any Terraform [modules](https://developer.hashicorp.com/terraform/language/modules), we do not need to run `terraform init` again.
2052
21-
<img width="600px" src="assets/ex8_2.png" alt="overview of deleted resources">
53+
1. Plan the Terraform configuration to see what will be created:
2254

23-
Terraform will calculate the changes it will execute, namely the deletion of the resources. You will be prompted to confirm the deletion. Check the output of the plan. Type `yes` and hit `Enter` to confirm the deletion.
55+
```bash
56+
terraform plan
57+
```
2458

25-
This will now recursively delete all resources that were created by Terraform. This might take a bit of time. At the end you should see a message like this:
59+
You should see the following output:
2660

27-
<img width="600px" src="assets/ex8_3.png" alt="result of terraform destroy">
61+
<img width="600px" src="assets/ex3_1.png" alt="terraform plan output for role collection assignment">
2862

29-
## Step 2: Verify the deletion of the resources
63+
2. Apply the Terraform configuration to create the assignment of the role collections:
3064

31-
After the deletion was executed successfully, verify that the resources were deleted. You can check the status of the resources in the SAP BTP cockpit.
65+
```bash
66+
terraform apply
67+
```
3268

33-
## Step 3: Check the Terraform state
69+
You will be prompted to confirm the creation the assignment of the role collections. Type `yes` and press `Enter` to continue. You should see the following output:
3470

35-
After the deletion of the resources, check the Terraform state. Walk through the following questions:
71+
<img width="600px" src="assets/ex3_2.png" alt="terraform apply output for role collection assignment">
3672

37-
- Did the state file disappear or is it still there?
38-
- If it is still there what is the content of the state file?
73+
You can also check that everything is in place via the SAP BTP cockpit. You should see the assigned users in the role collection `Subaccount Administrator`:
3974

40-
> [!TIP]
41-
> You can also use the `terraform state list` command to inspect the state of the resources. You find more information about that command in the [Terraform documentation](https://developer.hashicorp.com/terraform/cli/state/inspect).
75+
<img width="600px" src="assets/ex3_3.png" alt="role collection assignment in SAP BTP">
4276

4377
## Summary
4478

45-
Congrats - you've successfully completed the HandsOn. You've now learned how to use Terraform to manage resources in SAP BTP, how to deal with drift and also how to delete the resources again.
79+
You've now successfully assigned emergency administrators to the subaccount.
4680
47-
Happy Terraforming!
81+
Continue to - [# Exercise 6 - clean up](../EXERCISE6/README.md).
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
###
2+
# Setup of names in accordance to the company's naming conventions
3+
###
4+
locals {
5+
project_subaccount_name = "${var.org_name} | ${var.project_name}: CF - ${var.stage}"
6+
project_subaccount_domain = lower(replace("${var.org_name}-${var.project_name}-${var.stage}", " ", "-"))
7+
project_subaccount_cf_org = replace("${var.org_name}_${lower(var.project_name)}-${lower(var.stage)}", " ", "_")
8+
}
9+
10+
###
11+
# Creation of subaccount
12+
###
13+
resource "btp_subaccount" "project" {
14+
name = local.project_subaccount_name
15+
subdomain = local.project_subaccount_domain
16+
region = lower(var.region)
17+
labels = {
18+
"stage" = ["${var.stage}"],
19+
"costcenter" = ["${var.costcenter}"]
20+
}
21+
usage = "NOT_USED_FOR_PRODUCTION"
22+
}
23+
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)
29+
subaccount_id = btp_subaccount.project.id
30+
role_collection_name = "Subaccount Administrator"
31+
user_name = each.value
32+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "subaccount_id" {
2+
value = btp_subaccount.project.id
3+
description = "The ID of the project subaccount."
4+
}
5+
6+
output "subaccount_name" {
7+
value = btp_subaccount.project.name
8+
description = "The name of the project subaccount."
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
terraform {
3+
required_providers {
4+
btp = {
5+
source = "sap/btp"
6+
version = "~> 1.8.0"
7+
}
8+
}
9+
10+
}
11+
12+
# Please checkout documentation on how best to authenticate against SAP BTP
13+
# via the Terraform provider for SAP BTP
14+
provider "btp" {
15+
globalaccount = var.globalaccount
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
globalaccount = "<YOUR GLOBAL ACCOUT SUBDOMAIN>"
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
###
2+
# Provider configuration
3+
###
4+
variable "globalaccount" {
5+
type = string
6+
description = "The subdomain of the SAP BTP global account."
7+
}
8+
9+
variable "region" {
10+
type = string
11+
description = "The region where the project account shall be created in."
12+
default = "us10"
13+
}
14+
15+
###
16+
# Subaccount setup
17+
###
18+
variable "project_name" {
19+
type = string
20+
description = "The subaccount name."
21+
default = "proj-1234"
22+
23+
validation {
24+
condition = can(regex("^[a-zA-Z0-9_\\-]{1,200}", var.project_name))
25+
error_message = "Provide a valid project name."
26+
}
27+
}
28+
29+
variable "stage" {
30+
type = string
31+
description = "The stage/tier the account will be used for."
32+
default = "DEV"
33+
34+
validation {
35+
condition = contains(["DEV", "TST", "PRD"], var.stage)
36+
error_message = "Select a valid stage for the project account."
37+
}
38+
}
39+
40+
variable "costcenter" {
41+
type = string
42+
description = "The cost center the account will be billed to."
43+
default = "1234567890"
44+
45+
validation {
46+
condition = can(regex("^[0-9]{10}", var.costcenter))
47+
error_message = "Provide a valid cost center."
48+
}
49+
}
50+
51+
variable "org_name" {
52+
type = string
53+
description = "Defines to which organisation the project account shall belong to."
54+
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+
}
67+
}
68+
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+
76+
}
2.69 MB
Loading

0 commit comments

Comments
 (0)