Skip to content

Commit 2140d23

Browse files
authored
chore: fetch vpc details from the workspace (#626)
1 parent 45a943d commit 2140d23

File tree

6 files changed

+71
-19
lines changed

6 files changed

+71
-19
lines changed

patterns/vsi-extension/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,23 @@ This logic creates a VSI to an existing landing zone VPC.
55
This code creates and configures the following infrastructure:
66
- Adds an SSH key to IBM Cloud or uses an existing one.
77
- Adds a VSI in each subnet of the landing zone VPC.
8+
9+
There are two ways through which a user can pass the VPC details for deploying the VSI, both the approaches are mutually exclusive.
10+
11+
## Using `vpc_id`
12+
13+
The VPC ID of the landing zone VPC can be assigned to the variable vpc_id in order to create a VSI within that specific VPC.
14+
15+
## Using `prerequisite_workspace_id` and `existing_vpc_name`
16+
17+
The user can specify the workspace ID associated with the deployment of the landing zone VPC when creating a new VSI.
18+
19+
Follow these steps to get the schematics workspace ID.
20+
21+
1. Click the Navigation menu icon, and then click Schematics > Workspaces.
22+
1. Select the Workspace that is associated with landing zone VPC.
23+
1. Click the Settings.
24+
1. In the Details section, you can find the Workspace ID.
25+
26+
Pass the Workspace ID to the `prerequisite_workspace_id` variable and pass the name of the VPC to the `existing_vpc_name` to choosse the name of the VPC to which the user wants to deploy the VSI.
27+
Please provide the Workspace ID for the prerequisite workspace and the name of the existing VPC to the `prerequisite_workspace_id` and `existing_vpc_name` variables respectively, to identify the VPC where you want to deploy the VSI.

patterns/vsi-extension/main.tf

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
##############################################################################
2-
# Locals
2+
# Schematics Data
33
##############################################################################
44

55
locals {
6-
ssh_key_id = var.ssh_public_key != null ? ibm_is_ssh_key.ssh_key[0].id : data.ibm_is_ssh_key.ssh_key[0].id
6+
# tflint-ignore: terraform_unused_declarations
7+
validate_vpc_vars = var.prerequisite_workspace_id == null && var.vpc_id == null ? tobool("var.prerequisite_workspace_id and var.vpc_id cannot be both set to null.") : true
8+
# tflint-ignore: terraform_unused_declarations
9+
validate_vpc_names = var.prerequisite_workspace_id != null && var.existing_vpc_name == null ? tobool("A value must be passed for var.existing_vpc_name to choose a VPC from the list of VPCs from the schematics workspace.") : true
10+
11+
location = var.prerequisite_workspace_id != null ? regex("^[a-z/-]+", var.prerequisite_workspace_id) : null
12+
fullstack_output = length(data.ibm_schematics_output.schematics_output) > 0 ? jsondecode(data.ibm_schematics_output.schematics_output[0].output_json) : null
13+
vpc_id = var.prerequisite_workspace_id != null ? [
14+
for vpc in local.fullstack_output[0].vpc_data.value :
15+
vpc.vpc_data.id if vpc.vpc_data.name == var.existing_vpc_name
16+
][0] : var.vpc_id
17+
}
18+
19+
data "ibm_schematics_workspace" "schematics_workspace" {
20+
count = var.prerequisite_workspace_id != null ? 1 : 0
21+
workspace_id = var.prerequisite_workspace_id
22+
location = local.location
23+
}
24+
25+
data "ibm_schematics_output" "schematics_output" {
26+
count = var.prerequisite_workspace_id != null ? 1 : 0
27+
workspace_id = var.prerequisite_workspace_id
28+
location = local.location
29+
template_id = data.ibm_schematics_workspace.schematics_workspace[0].runtime_data[0].id
730
}
831

932
##############################################################################
10-
# Resource Group
33+
# Locals
1134
##############################################################################
1235

13-
data "ibm_resource_group" "existing_resource_group" {
14-
name = var.resource_group
36+
locals {
37+
ssh_key_id = var.ssh_public_key != null ? ibm_is_ssh_key.ssh_key[0].id : data.ibm_is_ssh_key.ssh_key[0].id
1538
}
1639

1740
##############################################################################
@@ -21,7 +44,7 @@ resource "ibm_is_ssh_key" "ssh_key" {
2144
count = local.ssh_keys != null ? 1 : 0
2245
name = local.ssh_keys.name
2346
public_key = replace(local.ssh_keys.public_key, "/==.*$/", "==")
24-
resource_group = data.ibm_resource_group.existing_resource_group.id
47+
resource_group = data.ibm_is_vpc.vpc_by_id.resource_group
2548
tags = var.resource_tags
2649
}
2750

@@ -31,10 +54,9 @@ data "ibm_is_ssh_key" "ssh_key" {
3154
}
3255

3356
data "ibm_is_vpc" "vpc_by_id" {
34-
identifier = var.vpc_id
57+
identifier = local.vpc_id
3558
}
3659

37-
3860
data "ibm_is_image" "image" {
3961
name = var.image_name
4062
}
@@ -49,10 +71,10 @@ locals {
4971
module "vsi" {
5072
source = "terraform-ibm-modules/landing-zone-vsi/ibm"
5173
version = "2.8.2"
52-
resource_group_id = data.ibm_resource_group.existing_resource_group.id
74+
resource_group_id = data.ibm_is_vpc.vpc_by_id.resource_group
5375
create_security_group = true
5476
prefix = "${var.prefix}-vsi"
55-
vpc_id = var.vpc_id
77+
vpc_id = local.vpc_id
5678
subnets = var.subnet_names != null ? local.subnets : data.ibm_is_vpc.vpc_by_id.subnets
5779
tags = var.resource_tags
5880
access_tags = var.access_tags

patterns/vsi-extension/variables.tf

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ variable "ibmcloud_api_key" {
44
sensitive = true
55
}
66

7-
variable "resource_group" {
8-
type = string
9-
description = "The resource group name of the landing zone VPC."
10-
}
11-
127
variable "region" {
138
description = "The region of the landing zone VPC."
149
type = string
@@ -23,6 +18,7 @@ variable "prefix" {
2318
variable "vpc_id" {
2419
description = "The ID of the VPC where the VSI will be created."
2520
type = string
21+
default = null
2622
}
2723

2824
variable "existing_ssh_key_name" {
@@ -186,3 +182,15 @@ variable "load_balancers" {
186182
)
187183
default = []
188184
}
185+
186+
variable "prerequisite_workspace_id" {
187+
type = string
188+
description = "IBM Cloud Schematics workspace ID of the prerequisite IBM VPC landing zone. If you do not have an existing deployment yet, create a new architecture using the same catalog tile."
189+
default = null
190+
}
191+
192+
variable "existing_vpc_name" {
193+
type = string
194+
description = "Name of the VPC to be used for deploying the VSI from the list of VPCs retrived from the IBM Cloud Schematics workspace."
195+
default = null
196+
}

patterns/vsi-extension/version.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ terraform {
66
required_version = ">= 1.3, < 1.6"
77
# Pin to the lowest provider version of the range defined in the main module's version.tf to ensure lowest version still works
88
required_providers {
9+
# Pin to 1.58.1 due to https://github.com/IBM-Cloud/terraform-provider-ibm/issues/4907
910
ibm = {
1011
source = "IBM-Cloud/ibm"
11-
version = "1.59.0"
12+
version = "1.58.1"
1213
}
1314
# tflint-ignore: terraform_unused_required_providers
1415
external = {

renovate.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"matchPaths": [
88
"patterns/vsi-quickstart",
99
"patterns/vsi",
10-
"patterns/roks"
10+
"patterns/roks",
11+
"patterns/vsi-extension"
1112
],
1213
"semanticCommitType": "fix"
1314
},
@@ -21,7 +22,8 @@
2122
"semanticCommitType": "fix",
2223
"group": true,
2324
"groupName": "required_provider",
24-
"commitMessageExtra": "to latest for the landing zone DAs"
25+
"commitMessageExtra": "to latest for the landing zone DAs",
26+
"ignorePaths": ["patterns/vsi-extension/"]
2527
}
2628
]
2729
}

tests/pr_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,6 @@ func TestRunVsiExtention(t *testing.T) {
574574
TerraformVars: map[string]interface{}{
575575
"prefix": prefix,
576576
"region": region,
577-
"resource_group": fmt.Sprintf("%s-management-rg", prefix),
578577
"existing_kms_instance_guid": terraform.Output(t, existingTerraformOptions, "key_management_guid"),
579578
"boot_volume_encryption_key": keyID,
580579
"vpc_id": managementVpcID,

0 commit comments

Comments
 (0)