Skip to content

chore: added new module for btp cf enviroment setup #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions released/modules/btp-cf/btp-cf-env-instance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Module: modules - cloudfoundry - envinstance_cf

## Overview

This module is executing the following tasks:
- creates a Cloudfoundry environment instance in a subaccount
- assigns users to the newly created Cloudfoundry environment (CF org).

## Content of setup

The setup comprises the following resources:

- Creation of a SAP BTP subaccount
- Entitlements of all services and app subscriptions for SAP Build Apps
- Role collection assignments to users

## Pre-requisites

The following things need to be available before calling this:
- subaccount needs to exist
- subaacount needs to be entitled for service `cloudfoundry` with plan `standard`
64 changes: 64 additions & 0 deletions released/modules/btp-cf/btp-cf-env-instance/btp_env_cf.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# ------------------------------------------------------------------------------------------------------
# Define the required providers for this module
# ------------------------------------------------------------------------------------------------------
terraform {
required_providers {
btp = {
source = "sap/btp"
version = "~> 1.4.0"
}
cloudfoundry = {
source = "SAP/cloudfoundry"
version = "0.1.0-beta"
}
}
}

# ------------------------------------------------------------------------------------------------------
# Fetch all available environments for the subaccount
# ------------------------------------------------------------------------------------------------------
data "btp_subaccount_environments" "all" {
subaccount_id = var.subaccount_id
}

# ------------------------------------------------------------------------------------------------------
# Take the landscape label from the first CF environment if no environment label is provided
# ------------------------------------------------------------------------------------------------------
resource "null_resource" "cache_target_environment" {
triggers = {
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
}

lifecycle {
ignore_changes = all
}
}

# ------------------------------------------------------------------------------------------------------
# Create the Cloud Foundry environment instance
# ------------------------------------------------------------------------------------------------------
resource "btp_subaccount_environment_instance" "cf" {
subaccount_id = var.subaccount_id
name = var.instance_name
environment_type = "cloudfoundry"
service_name = "cloudfoundry"
plan_name = var.plan_name
landscape_label = null_resource.cache_target_environment.triggers.label
parameters = jsonencode({
instance_name = var.cf_org_name
})
timeouts = {
create = "1h"
update = "35m"
delete = "30m"
}
}
# ------------------------------------------------------------------------------------------------------
# Create the Cloud Foundry org users
# ------------------------------------------------------------------------------------------------------
resource "cloudfoundry_org_role" "org_role" {
for_each = var.cf_org_user
username = each.value
type = "organization_user"
org = btp_subaccount_environment_instance.cf.platform_id
}
14 changes: 14 additions & 0 deletions released/modules/btp-cf/btp-cf-env-instance/btp_env_cf_outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "cf_env_instance_id" {
value = btp_subaccount_environment_instance.cf.id
description = "ID of the Cloud Foundry environment instance."
}

output "cf_org_id" {
value = btp_subaccount_environment_instance.cf.platform_id
description = "ID of the Cloud Foundry org."
}

output "cf_api_endpoint" {
value = lookup(jsondecode(btp_subaccount_environment_instance.cf.labels), "API Endpoint", "not found")
description = "API endpoint of the Cloud Foundry environment."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
variable "instance_name" {
type = string
description = "Name of the Cloud Foundry environment instance."
validation {
condition = can(regex("^[a-zA-Z0-9_\\-\\.]{1,32}$", var.instance_name))
error_message = "Please provide a valid instance name (^[a-zA-Z0-9_\\-\\.]{1,32})."
}
}

variable "subaccount_id" {
type = string
description = "ID of the subaccount where the Cloud Foundry environment shall be enabled."
}

variable "plan_name" {
type = string
description = "Desired service plan for the Cloud Foundry environment instance."
default = "standard"
}

variable "environment_label" {
type = string
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."
default = ""
}

variable "cf_org_name" {
type = string
description = "Name of the Cloud Foundry org."

validation {
condition = can(regex("^.{1,255}$", var.cf_org_name))
error_message = "The Cloud Foundry org name must not be emtpy and not exceed 255 characters."
}
}

variable "cf_org_user" {
type = set(string)
description = "Defines the colleagues who are added to each subaccount as subaccount administrators."
default = ["user1@sap.com","user2@sap.com"]
}
6 changes: 2 additions & 4 deletions released/usecases/subaccount_setup/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ resource "btp_subaccount" "project" {
# Creation of Cloud Foundry environment
###
module "cloudfoundry_environment" {
source = "../../modules/environment/cloudfoundry/envinstance_cf"
source = "../../modules/environment/btp-cf/btp-cf-env-instance"

subaccount_id = btp_subaccount.project.id
instance_name = local.project_subaccount_cf_org
cf_org_name = local.project_subaccount_cf_org
cf_org_managers = []
cf_org_billing_managers = []
cf_org_auditors = []
cf_org_user = var.cf_org_user
}

###
Expand Down
9 changes: 5 additions & 4 deletions released/usecases/subaccount_setup/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ terraform {
version = "~> 1.4.0"
}
cloudfoundry = {
source = "cloudfoundry-community/cloudfoundry"
version = "0.53.1"
source = "SAP/cloudfoundry"
version = "0.2.1-beta"
}
}

}

# Please checkout documentation on how best to authenticate against SAP BTP
# via the Terraform provider for SAP BTP
provider "btp" {
globalaccount = "<YOUR GLOBALACCOUNT SUBDOMAIN>"
globalaccount = "terraformintcanary"
cli_server_url = "https://canary.cli.btp.int.sap"

}

provider "cloudfoundry" {
Expand Down
11 changes: 10 additions & 1 deletion released/usecases/subaccount_setup/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,14 @@ variable "stage" {
variable "region" {
type = string
description = "The region where the project account shall be created in."
default = "us10"
default = "eu12"
}
variable "cf_org_user" {
type = set(string)
description = "Defines the colleagues who are added to each subaccount as subaccount administrators."
}
# CLI server
variable "cli_server_url" {
type = string
description = "The BTP CLI server URL."
}
Loading