Skip to content

Commit 63c273e

Browse files
authored
feat: setup of integration suite on SAP BTP trial (#404)
1 parent 413111b commit 63c273e

File tree

20 files changed

+721
-0
lines changed

20 files changed

+721
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*.log
77
secret.auto.tfvars
88
terraform.tfvars
9+
*.out
910

1011

1112
# Any kind of invironment variables
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
locals {
2+
flattened_role_collection_assignments = flatten([
3+
for index, role_collection_assignment in var.role_collection_assignments : [
4+
for index, user in role_collection_assignment.users : {
5+
role_collection_name = role_collection_assignment.role_collection_name
6+
user = user
7+
}
8+
]
9+
])
10+
}
11+
12+
resource "btp_directory" "self" {
13+
name = var.directory_name
14+
description = var.directory_description
15+
features = toset(var.features)
16+
labels = {
17+
"managed_by" = ["terraform"]
18+
"scope" = ["integration"]
19+
"costcenter" = [var.project_costcenter]
20+
}
21+
}
22+
23+
resource "btp_directory_entitlement" "dir_entitlement_assignment" {
24+
for_each = { for e in var.entitlement_assignments : e.name => e }
25+
directory_id = btp_directory.self.id
26+
service_name = each.value.name
27+
plan_name = each.value.plan
28+
amount = each.value.amount != 0 ? each.value.amount : null
29+
distribute = each.value.distribute
30+
auto_assign = each.value.auto_assign
31+
}
32+
33+
34+
resource "btp_directory_role_collection_assignment" "dir_role_collection_assignment" {
35+
for_each = { for index, role_collection_assignment in local.flattened_role_collection_assignments : index => role_collection_assignment }
36+
directory_id = btp_directory.self.id
37+
role_collection_name = each.value.role_collection_name
38+
user_name = each.value.user
39+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "directory_id" {
2+
description = "The ID of the directory"
3+
value = btp_directory.self.id
4+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
terraform {
2+
required_providers {
3+
btp = {
4+
source = "SAP/btp"
5+
version = "~> 1.10.0"
6+
}
7+
}
8+
}
9+
10+
provider "btp" {
11+
globalaccount = var.globalaccount
12+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
globalaccount = "<Subdomain of your Global Account>"
2+
features = ["DEFAULT", "ENTITLEMENTS", "AUTHORIZATIONS"]
3+
project_costcenter = "54321"
4+
entitlement_assignments = [
5+
{
6+
name = "integrationsuite-trial"
7+
plan = "trial"
8+
amount = 1
9+
distribute = false
10+
auto_assign = false
11+
},
12+
// can be added only after fix of https://github.com/SAP/terraform-provider-btp/issues/930
13+
/* {
14+
name = "APPLICATION_RUNTIME"
15+
plan = "MEMORY"
16+
amount = 2
17+
distribute = false
18+
auto_assign = false
19+
},*/
20+
]
21+
// The user executing the script gets automatically added to the directory
22+
role_collection_assignments = [
23+
{
24+
role_collection_name = "Directory Administrator"
25+
users = ["[email protected]"]
26+
},
27+
{
28+
role_collection_name = "Directory Viewer"
29+
users = ["[email protected]"]
30+
}
31+
]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
variable "globalaccount" {
2+
description = "Subdomain of the global account"
3+
type = string
4+
}
5+
6+
variable "directory_name" {
7+
description = "Name of the directory"
8+
type = string
9+
default = "Integration Directory"
10+
}
11+
12+
variable "directory_description" {
13+
description = "Description of the directory"
14+
type = string
15+
default = "Directory for all integration subaccounts"
16+
}
17+
18+
variable "features" {
19+
description = "Directory features to be activated"
20+
type = list(string)
21+
default = ["DEFAULT"]
22+
validation {
23+
condition = alltrue([for feature in var.features : contains(["DEFAULT", "ENTITLEMENTS", "AUTHORIZATIONS"], feature)])
24+
error_message = "The only supported features are DEFAULT, ENTITLEMENTS and AUTHORIZATIONS"
25+
}
26+
}
27+
28+
variable "project_costcenter" {
29+
description = "Cost center of the project"
30+
type = string
31+
validation {
32+
condition = can(regex("^[0-9]{5}$", var.project_costcenter))
33+
error_message = "Cost center must be a 5 digit number"
34+
}
35+
}
36+
37+
variable "entitlement_assignments" {
38+
description = "list of entitlements to be assigned ot the directory"
39+
type = list(object({
40+
name = string
41+
plan = string
42+
amount = number
43+
distribute = bool
44+
auto_assign = bool
45+
}))
46+
default = []
47+
}
48+
49+
variable "role_collection_assignments" {
50+
description = "List of role collections to assign to a user"
51+
type = list(object({
52+
role_collection_name = string
53+
users = set(string)
54+
}))
55+
default = []
56+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
resource "random_uuid" "uuid" {}
2+
3+
locals {
4+
subaccount_name = "${var.subaccount_stage} ${var.project_name}"
5+
subaccount_description = "Subaccount for Project ${var.project_name} (stage ${var.subaccount_stage})"
6+
subaccount_subdomain = join("-", [lower(replace("${var.subaccount_stage}-${var.project_name}", " ", "-")), random_uuid.uuid.result])
7+
service_name_prefix = lower(replace("${var.subaccount_stage}-${var.project_name}", " ", "-"))
8+
subaccount_cf_org = local.subaccount_subdomain
9+
cf_space_name = lower(replace("${var.subaccount_stage}-${var.project_name}", " ", "-"))
10+
beta_enabled = var.subaccount_stage == "DEV" ? true : false
11+
usage = var.subaccount_stage == "PROD" ? "USED_FOR_PRODUCTION" : "NOT_USED_FOR_PRODUCTION"
12+
}
13+
14+
resource "btp_subaccount" "project_subaccount" {
15+
parent_id = var.parent_id
16+
name = local.subaccount_name
17+
subdomain = local.subaccount_subdomain
18+
description = var.project_name
19+
region = var.subaccount_region
20+
beta_enabled = local.beta_enabled
21+
usage = local.usage
22+
labels = {
23+
"stage" = [var.subaccount_stage]
24+
"costcenter" = [var.project_costcenter]
25+
"managed_by" = ["terraform"]
26+
"scope" = ["integration"]
27+
}
28+
}
29+
30+
resource "btp_subaccount_role_collection_assignment" "emergency_admins" {
31+
for_each = toset(var.emergency_admins)
32+
subaccount_id = btp_subaccount.project_subaccount.id
33+
role_collection_name = "Subaccount Administrator"
34+
user_name = each.value
35+
}
36+
37+
38+
resource "btp_subaccount_entitlement" "integrationsuite_app_trial" {
39+
subaccount_id = btp_subaccount.project_subaccount.id
40+
service_name = "integrationsuite-trial"
41+
plan_name = "trial"
42+
amount = 1
43+
}
44+
45+
resource "btp_subaccount_entitlement" "cf_memory" {
46+
subaccount_id = btp_subaccount.project_subaccount.id
47+
service_name = "APPLICATION_RUNTIME"
48+
plan_name = "MEMORY"
49+
amount = 1
50+
}
51+
52+
resource "btp_subaccount_environment_instance" "cloudfoundry" {
53+
subaccount_id = btp_subaccount.project_subaccount.id
54+
name = local.subaccount_cf_org
55+
environment_type = "cloudfoundry"
56+
service_name = "cloudfoundry"
57+
plan_name = "trial"
58+
landscape_label = "cf-${var.cf_landscape_label}"
59+
parameters = jsonencode({
60+
instance_name = local.subaccount_cf_org
61+
})
62+
depends_on = [btp_subaccount_entitlement.cf_memory]
63+
}
64+
65+
locals {
66+
cf_org_id = jsondecode(btp_subaccount_environment_instance.cloudfoundry.labels)["Org ID"]
67+
}
68+
69+
resource "cloudfoundry_org_role" "org_manager" {
70+
for_each = toset(var.emergency_admins)
71+
username = each.value
72+
type = "organization_user"
73+
org = local.cf_org_id
74+
}
75+
76+
resource "cloudfoundry_space" "project_space" {
77+
name = local.cf_space_name
78+
org = local.cf_org_id
79+
}
80+
81+
resource "cloudfoundry_space_role" "emergency_space_manager" {
82+
for_each = toset(var.emergency_admins)
83+
username = each.value
84+
type = "space_manager"
85+
space = cloudfoundry_space.project_space.id
86+
origin = "sap.ids"
87+
depends_on = [cloudfoundry_org_role.org_manager]
88+
}
89+
90+
resource "cloudfoundry_space_role" "space_manager" {
91+
for_each = toset(var.space_managers)
92+
username = each.value
93+
type = "space_manager"
94+
space = cloudfoundry_space.project_space.id
95+
origin = "sap.ids"
96+
depends_on = [cloudfoundry_org_role.org_manager]
97+
}
98+
99+
resource "cloudfoundry_space_role" "space_developer" {
100+
for_each = toset(var.space_managers)
101+
username = each.value
102+
type = "space_developer"
103+
space = cloudfoundry_space.project_space.id
104+
origin = "sap.ids"
105+
depends_on = [cloudfoundry_org_role.org_manager]
106+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "subaccount_id" {
2+
description = "The ID of the subaccount"
3+
value = btp_subaccount.project_subaccount.id
4+
}
5+
6+
output "cf_space_id" {
7+
description = "The ID of the Cloud Foundry space"
8+
value = cloudfoundry_space.project_space.id
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
terraform {
2+
required_providers {
3+
btp = {
4+
source = "SAP/btp"
5+
version = "~> 1.10.0"
6+
}
7+
cloudfoundry = {
8+
source = "cloudfoundry/cloudfoundry"
9+
version = "~> 1.3.0"
10+
}
11+
}
12+
}
13+
14+
provider "btp" {
15+
globalaccount = var.globalaccount
16+
}
17+
18+
// Interpolation of the API endpoint only works on trial accounts
19+
provider "cloudfoundry" {
20+
api_url = "https://api.cf.${var.cf_landscape_label}.hana.ondemand.com"
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
globalaccount = "<Subdomain of your Global Account>"
2+
parent_id = "outout directory_id of step 1"
3+
project_costcenter = "54321"
4+
emergency_admins = ["[email protected]"]
5+
space_managers = ["[email protected]"]
6+
space_developers = ["[email protected]"]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
variable "globalaccount" {
2+
description = "Subdomain of the global account"
3+
type = string
4+
}
5+
6+
variable "parent_id" {
7+
description = "The parent ID for the subaccount"
8+
type = string
9+
default = ""
10+
}
11+
12+
variable "project_name" {
13+
description = "Name of the project"
14+
type = string
15+
default = "Integration Account"
16+
}
17+
18+
variable "subaccount_stage" {
19+
description = "Stage of the subaccount"
20+
type = string
21+
default = "DEV"
22+
validation {
23+
condition = contains(["DEV", "TEST", "PROD"], var.subaccount_stage)
24+
error_message = "Stage must be one of DEV, TEST or PROD"
25+
}
26+
}
27+
28+
variable "subaccount_region" {
29+
description = "Region of the subaccount"
30+
type = string
31+
default = "us10"
32+
validation {
33+
condition = contains(["us10", "ap21"], var.subaccount_region)
34+
error_message = "Region must be one of us10 or ap21"
35+
}
36+
}
37+
38+
variable "cf_landscape_label" {
39+
description = "Label of the Cloud Foundry landscape"
40+
type = string
41+
default = "us10-001"
42+
validation {
43+
condition = contains(["us10-001", "ap21"], var.cf_landscape_label)
44+
error_message = "Trial landscape must be one of us10-001 or ap21"
45+
}
46+
}
47+
48+
variable "project_costcenter" {
49+
description = "Cost center of the project"
50+
type = string
51+
validation {
52+
condition = can(regex("^[0-9]{5}$", var.project_costcenter))
53+
error_message = "Cost center must be a 5 digit number"
54+
}
55+
}
56+
57+
variable "emergency_admins" {
58+
description = "List of emergency admins"
59+
type = list(string)
60+
default = []
61+
}
62+
63+
variable "space_managers" {
64+
description = "List of space managers"
65+
type = list(string)
66+
default = []
67+
}
68+
69+
variable "space_developers" {
70+
description = "List of space developers"
71+
type = list(string)
72+
default = []
73+
}

0 commit comments

Comments
 (0)