|
1 |
| -# Exercise 5 - Cleanup |
| 1 | +# Exercise 5 - Assignment of subaccount emergency administrators |
2 | 2 |
|
3 | 3 | ## Goal of this Exercise 🎯
|
4 | 4 |
|
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`. |
6 | 6 |
|
7 |
| -## Step 1: Trigger the deletion of the resources |
| 7 | +## Assign role collection to users |
8 | 8 |
|
9 |
| -You can delete the resources by running the following command: |
| 9 | +### Step 1: Enhance the variables |
10 | 10 |
|
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 | +} |
13 | 37 | ```
|
14 | 38 |
|
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 |
16 | 47 |
|
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: |
18 | 49 |
|
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. |
20 | 52 |
|
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: |
22 | 54 |
|
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 | + ``` |
24 | 58 |
|
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: |
26 | 60 |
|
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"> |
28 | 62 |
|
29 |
| -## Step 2: Verify the deletion of the resources |
| 63 | +2. Apply the Terraform configuration to create the assignment of the role collections: |
30 | 64 |
|
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 | + ``` |
32 | 68 |
|
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: |
34 | 70 |
|
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"> |
36 | 72 |
|
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`: |
39 | 74 |
|
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"> |
42 | 76 |
|
43 | 77 | ## Summary
|
44 | 78 |
|
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. |
46 | 80 |
|
47 |
| -Happy Terraforming! |
| 81 | +Continue to - [# Exercise 6 - clean up](../EXERCISE6/README.md). |
0 commit comments