Skip to content

Commit b0eafd4

Browse files
authored
feature(sample): new usecase for multiple providers and adjustements of documentation (#4)
1 parent b1a0969 commit b0eafd4

File tree

16 files changed

+495
-6
lines changed

16 files changed

+495
-6
lines changed

assets/README_TEMPLATE.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# <Sample Title>
2+
3+
## Overview
4+
5+
<!-- Give a short description what the sample -->
6+
7+
## Content of setup
8+
9+
<!-- Describe the content of the sample i.e. which resources will be managed by the configuration -->
10+
The setup comprises the following resources:
11+
12+
## Deploying the resources
13+
<!-- Basic setup, adjust if needed -->
14+
15+
To deploy the resources you must:
16+
17+
1. Configure the provider in the `provider.tf` file
18+
2. Initialize your workspace:
19+
20+
```bash
21+
terraform init
22+
```
23+
24+
3. You can check what Terraform plans to apply based on your configuration:
25+
26+
```bash
27+
terraform plan -var-file="terraform.tfvars
28+
```
29+
30+
4. Apply your configuration to provision the resources:
31+
32+
```bash
33+
terraform apply -var-file="terraform.tfvars
34+
```
35+
36+
## When finished
37+
38+
You probably want to remove the assets after trying them out to avoid unnecessary costs. To do so execute the following command:
39+
40+
```bash
41+
terraform destroy -var-file="terraform.tfvars
42+
```
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Sample setup with multiple providers
2+
3+
## Overview
4+
5+
This sample showcases the usage of multiple Terraform providers in one setup. This scenario is needed in case of management of a SAP BTP subaccount including the Cloud Foundry space within it.
6+
7+
## Content of the setup
8+
9+
### Environments, services and applications
10+
11+
The setup comprises the following resources:
12+
13+
- Creation of an SAP BTP subaccount
14+
- Creation of a Cloud Foundry environment within the subaccount
15+
- Creation of a Cloud Foundry space
16+
- Creation of entitlements for "XSUAA" (plan "apiaccess"), "Taskcenter" (plan "standard") and "Destination" (plan "light") in the subaccount
17+
- Assignment of the following role colections to the users defined in the `users.tfvars` file:
18+
19+
- Subaccount Administrator
20+
- Subaccount Service Administrator
21+
- SpaceManager
22+
- SpaceDeveloper
23+
- SpaceAuditor
24+
25+
## Deploying the resources
26+
27+
To deploy the resources you must:
28+
29+
1. Configure the providers in the `provider.tf` file
30+
2. Initialize your workspace:
31+
32+
```bash
33+
terraform init
34+
```
35+
36+
3. You can check what Terraform plans to apply based on your configuration:
37+
38+
```bash
39+
terraform plan -var-file="users.tfvars
40+
```
41+
42+
4. Apply your configuration to provision the resources:
43+
44+
```bash
45+
terraform apply -var-file="users.tfvars
46+
```
47+
48+
## When finished
49+
50+
You probably want to remove the assets after trying them out to avoid unnecessary costs. To do so execute the following command:
51+
52+
```bash
53+
terraform destroy -var-file="users.tfvars
54+
```

released/multi_provider_setup/main.tf

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
###
2+
# Create SAP BTP subaccount for default use caee
3+
###
4+
resource "btp_subaccount" "subaccount" {
5+
name = var.subacount_name
6+
subdomain = var.subacount_subdomain
7+
region = var.region
8+
}
9+
10+
###
11+
# Add the entitlements to the subaccount
12+
###
13+
resource "btp_subaccount_entitlement" "entitlement-taskcenter" {
14+
subaccount_id = btp_subaccount.subaccount.id
15+
for_each = var.entitlements
16+
service_name = each.value.service_name
17+
plan_name = each.value.plan_name
18+
}
19+
20+
###
21+
# Create Cloud Foundry environment
22+
###
23+
module "cloudfoundry_environment" {
24+
source = "./modules/envinstance-cloudfoundry/"
25+
subaccount_id = btp_subaccount.subaccount.id
26+
instance_name = var.cloudfoundry_org_name
27+
cloudfoundry_org_name = var.cloudfoundry_org_name
28+
}
29+
30+
###
31+
# Create Cloud Foundry space and assign users
32+
###
33+
module "cloudfoundry_space" {
34+
source = "./modules/cloudfoundry-space/"
35+
cf_org_id = module.cloudfoundry_environment.org_id
36+
name = var.cloudfoundry_space_name
37+
cf_space_managers = var.cloudfoundry_space_managers
38+
cf_space_developers = var.cloudfoundry_space_developers
39+
cf_space_auditors = var.cloudfoundry_space_auditors
40+
}
41+
42+
###
43+
# Assign the subaccount roles to the users
44+
###
45+
resource "btp_subaccount_role_collection_assignment" "subaccount-administrators" {
46+
subaccount_id = btp_subaccount.subaccount.id
47+
role_collection_name = "Subaccount Administrator"
48+
for_each = var.subaccount_admins
49+
user_name = each.value
50+
depends_on = [
51+
btp_subaccount.subaccount
52+
]
53+
}
54+
55+
56+
resource "btp_subaccount_role_collection_assignment" "subaccount-service-administrators" {
57+
subaccount_id = btp_subaccount.subaccount.id
58+
role_collection_name = "Subaccount Service Administrator"
59+
for_each = var.subaccount_service_admins
60+
user_name = each.value
61+
depends_on = [
62+
btp_subaccount.subaccount
63+
]
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
###
2+
# Create the Cloud Foundry space
3+
###
4+
resource "cloudfoundry_space" "space" {
5+
name = var.name
6+
org = var.cf_org_id
7+
}
8+
9+
###
10+
# Create the CF users
11+
###
12+
resource "cloudfoundry_space_users" "space-users" {
13+
space = cloudfoundry_space.space.id
14+
managers = var.cf_space_managers
15+
developers = var.cf_space_developers
16+
auditors = var.cf_space_auditors
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "id" {
2+
value = cloudfoundry_space.space.id
3+
description = "The GUID of the Space."
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform {
2+
required_providers {
3+
cloudfoundry = {
4+
source = "cloudfoundry-community/cloudfoundry"
5+
version = "~> 0.50"
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
variable "cf_org_id" {
2+
type = string
3+
description = "The ID of the Cloud Foundry org."
4+
}
5+
6+
variable "name" {
7+
type = string
8+
description = "The name of the Cloud Foundry space."
9+
default = "dev"
10+
}
11+
12+
variable "cf_space_managers"{
13+
type = list(string)
14+
description = "The list of Cloud Foundry space managers."
15+
default = []
16+
}
17+
18+
variable "cf_space_developers"{
19+
type = list(string)
20+
description = "The list of Cloud Foundry space developers."
21+
default = []
22+
}
23+
24+
variable "cf_space_auditors"{
25+
type = list(string)
26+
description = "The list of Cloud Foundry space auditors."
27+
default = []
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
##
2+
# The orchestration user needs administration access to the cloud foundry environment.
3+
##
4+
data "btp_whoami" "orchestrator" {}
5+
6+
resource "null_resource" "cache_orchestrator" {
7+
triggers = {
8+
id = data.btp_whoami.orchestrator.id
9+
email = data.btp_whoami.orchestrator.email
10+
}
11+
12+
lifecycle {
13+
ignore_changes = all
14+
}
15+
}
16+
17+
##
18+
# If the user doesn't provide an environment label, we have to look it up. We take the first matching entry.
19+
##
20+
data "btp_subaccount_environments" "all" {
21+
subaccount_id = var.subaccount_id
22+
}
23+
24+
resource "null_resource" "cache_target_environment" {
25+
triggers = {
26+
label = length(var.environment_label) > 0 ? var.environment_label : [for env in data.btp_subaccount_environments.all.values : env if env.service_name == "cloudfoundry" && env.environment_type == "cloudfoundry"][0].landscape_label
27+
}
28+
29+
lifecycle {
30+
ignore_changes = all
31+
}
32+
}
33+
34+
resource "btp_subaccount_environment_instance" "cloudfoundry" {
35+
subaccount_id = var.subaccount_id
36+
name = var.instance_name
37+
environment_type = "cloudfoundry"
38+
service_name = "cloudfoundry"
39+
plan_name = var.plan_name
40+
landscape_label = null_resource.cache_target_environment.triggers.label
41+
42+
parameters = jsonencode({
43+
instance_name = var.cloudfoundry_org_name
44+
users = [
45+
{
46+
id = null_resource.cache_orchestrator.triggers.id
47+
email = null_resource.cache_orchestrator.triggers.email
48+
}
49+
]
50+
})
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
output "id" {
2+
value = btp_subaccount_environment_instance.cloudfoundry.id
3+
description = "The technical ID of the Cloud Foundry environment instance."
4+
}
5+
6+
output "org_id" {
7+
value = btp_subaccount_environment_instance.cloudfoundry.platform_id
8+
description = "The technical ID of the Cloud Foundry environment instance."
9+
}
10+
11+
output "api_endpoint" {
12+
value = lookup(jsondecode(btp_subaccount_environment_instance.cloudfoundry.labels), "API Endpoint", "not found")
13+
description = "The API endpoint of the Cloud Foundry environment."
14+
}
15+
16+
output "state" {
17+
value = btp_subaccount_environment_instance.cloudfoundry.state
18+
description = "State of the Cloud Foundry environment."
19+
}
20+
21+
output "created_date" {
22+
value = btp_subaccount_environment_instance.cloudfoundry.created_date
23+
description = "The time the resource was created (ISO 8601 format)."
24+
}
25+
26+
output "last_modified" {
27+
value = btp_subaccount_environment_instance.cloudfoundry.last_modified
28+
description = "The last time the resource was updated (ISO 8601 format)."
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform {
2+
required_providers {
3+
btp = {
4+
source = "sap/btp"
5+
version = "~> 0.1"
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
variable "subaccount_id" {
2+
type = string
3+
description = "The ID of the subaccount where cloudfoundry shall be enabled."
4+
}
5+
6+
variable "plan_name" {
7+
type = string
8+
description = "The desired service plan for the cloudfoundry environment instance."
9+
default = "standard"
10+
}
11+
12+
variable "environment_label" {
13+
type = string
14+
description = "In case there are multiple environments available for a subaccount, you can use this label to choose with which one you want to go. If nothing is given, we take by default the first available."
15+
default = ""
16+
}
17+
18+
variable "instance_name" {
19+
type = string
20+
description = "The name of the cloudfoundry environment instance."
21+
22+
validation {
23+
condition = can(regex("^[a-zA-Z0-9_\\-\\.]{1,32}$", var.instance_name))
24+
error_message = "Please provide a valid instance name."
25+
}
26+
}
27+
28+
variable "cloudfoundry_org_name" {
29+
type = string
30+
description = "The name of the cloudfoundry organization."
31+
32+
validation {
33+
condition = can(regex("^.{1,255}$", var.cloudfoundry_org_name))
34+
error_message = "The cloudfoundry org name must not be emtpy and not exceed 255 characters"
35+
}
36+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
terraform {
3+
required_providers {
4+
btp = {
5+
source = "sap/btp"
6+
version = "0.1.0-beta.1"
7+
}
8+
cloudfoundry = {
9+
source = "cloudfoundry-community/cloudfoundry"
10+
version = "~> 0.50"
11+
}
12+
}
13+
}
14+
15+
provider "btp" {
16+
globalaccount = "ourglobalaccount_subdomain_id"
17+
}
18+
19+
// Configuration is described in https://registry.terraform.io/providers/cloudfoundry-community/cloudfoundry/latest/docs
20+
provider "cloudfoundry" {
21+
api_url = "https://api.cf.us10.hana.ondemand.com"
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# see documentation at https://developer.hashicorp.com/terraform/language/values/variables#assigning-values-to-root-module-variables
2+
3+
cloudfoundry_space_managers = ["[email protected]"]
4+
cloudfoundry_space_developers = ["[email protected]"]
5+
cloudfoundry_space_auditors = ["[email protected]"]
6+
subaccount_admins = ["[email protected]"]
7+
subaccount_service_admins = ["[email protected]"]

0 commit comments

Comments
 (0)