public inbox for kdevops@lists.linux.dev
 help / color / mirror / Atom feed
From: cel@kernel.org
To: <kdevops@lists.linux.dev>
Cc: Chandan Babu R <chandanbabu@kernel.org>,
	Chuck Lever <chuck.lever@oracle.com>
Subject: [RFC PATCH 14/31] terraform/OCI: Select your compartment by name instead of by OCID
Date: Mon, 31 Mar 2025 20:59:43 -0400	[thread overview]
Message-ID: <20250401010000.764234-15-cel@kernel.org> (raw)
In-Reply-To: <20250401010000.764234-1-cel@kernel.org>

From: Chuck Lever <chuck.lever@oracle.com>

Terraform provides a data source that can map a human-readable
compartment name to an OCID. This avoids the need to copy and paste
the compartment OCID blob from the OCI cloud console. Just use the
human-readable compartment name.

Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---
 .../templates/oci/terraform.tfvars.j2          |  2 +-
 scripts/terraform.Makefile                     |  1 -
 terraform/oci/kconfigs/Kconfig.location        |  9 ++++-----
 terraform/oci/main.tf                          | 18 +++++++++++++-----
 terraform/oci/vars.tf                          | 10 +++++-----
 5 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/playbooks/roles/gen_tfvars/templates/oci/terraform.tfvars.j2 b/playbooks/roles/gen_tfvars/templates/oci/terraform.tfvars.j2
index 864d5d5a89f0..be0c154802ff 100644
--- a/playbooks/roles/gen_tfvars/templates/oci/terraform.tfvars.j2
+++ b/playbooks/roles/gen_tfvars/templates/oci/terraform.tfvars.j2
@@ -4,7 +4,7 @@ oci_user_ocid = "{{ terraform_oci_user_ocid }}"
 oci_user_private_key_path = "{{ terraform_oci_user_private_key_path }}"
 oci_user_fingerprint = "{{ terraform_oci_user_fingerprint }}"
 oci_ad_number = {{ terraform_oci_ad_number }}
-oci_compartment_ocid = "{{ terraform_oci_compartment_ocid }}"
+oci_compartment_name = "{{ terraform_oci_compartment_name }}"
 oci_shape = "{{ terraform_oci_shape }}"
 oci_instance_flex_ocpus = {{ terraform_oci_instance_flex_ocpus }}
 oci_instance_flex_memory_in_gbs = {{ terraform_oci_instance_flex_memory_in_gbs }}
diff --git a/scripts/terraform.Makefile b/scripts/terraform.Makefile
index 0aca0b508086..02db2569ee08 100644
--- a/scripts/terraform.Makefile
+++ b/scripts/terraform.Makefile
@@ -95,7 +95,6 @@ TERRAFORM_EXTRA_VARS += terraform_gce_credentials=$(subst ",,$(CONFIG_TERRAFORM_
 endif
 
 ifeq (y,$(CONFIG_TERRAFORM_OCI))
-TERRAFORM_EXTRA_VARS += terraform_oci_compartment_ocid=$(subst ",,$(CONFIG_TERRAFORM_OCI_COMPARTMENT_OCID))
 TERRAFORM_EXTRA_VARS += terraform_oci_shape=$(subst ",,$(CONFIG_TERRAFORM_OCI_SHAPE))
 TERRAFORM_EXTRA_VARS += terraform_oci_instance_flex_ocpus=$(subst ",,$(CONFIG_TERRAFORM_OCI_INSTANCE_FLEX_OCPUS))
 TERRAFORM_EXTRA_VARS += terraform_oci_instance_flex_memory_in_gbs=$(subst ",,$(CONFIG_TERRAFORM_OCI_INSTANCE_FLEX_MEMORY_IN_GBS))
diff --git a/terraform/oci/kconfigs/Kconfig.location b/terraform/oci/kconfigs/Kconfig.location
index 7bf5ae49ba03..eff58202f8c7 100644
--- a/terraform/oci/kconfigs/Kconfig.location
+++ b/terraform/oci/kconfigs/Kconfig.location
@@ -45,9 +45,8 @@ source "terraform/oci/kconfigs/regions/Kconfig.us-chicago-1"
 source "terraform/oci/kconfigs/regions/Kconfig.us-phoenix-1"
 source "terraform/oci/kconfigs/regions/Kconfig.us-sanjose-1"
 
-config TERRAFORM_OCI_COMPARTMENT_OCID
-	string "OCI compartment OCID"
+config TERRAFORM_OCI_COMPARTMENT_NAME
+	string "OCI compartment name"
+	output yaml
 	help
-	  The OCI ID of user's compartment.
-	  Read this:
-	  https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/terraformproviderconfiguration.htm
+	  The compartment name where your instances are to be created.
diff --git a/terraform/oci/main.tf b/terraform/oci/main.tf
index 5520fdbed517..6991644d83b8 100644
--- a/terraform/oci/main.tf
+++ b/terraform/oci/main.tf
@@ -1,3 +1,11 @@
+data "oci_identity_compartments" "kdevops_compartment" {
+  access_level              = "ANY"
+  compartment_id            = var.oci_tenancy_ocid
+  compartment_id_in_subtree = true
+  name                      = var.oci_compartment_name
+  state                     = "ACTIVE"
+}
+
 data "oci_identity_availability_domain" "kdevops_av_domain" {
   ad_number      = var.oci_ad_number
   compartment_id = var.oci_tenancy_ocid
@@ -7,8 +15,8 @@ resource "oci_core_instance" "kdevops_instance" {
   count = local.kdevops_num_boxes
 
   availability_domain = data.oci_identity_availability_domain.kdevops_av_domain.name
-  compartment_id = var.oci_compartment_ocid
-  shape = var.oci_shape
+  compartment_id      = data.oci_identity_compartments.kdevops_compartment.compartments[0].id
+  shape               = var.oci_shape
   shape_config {
     memory_in_gbs = var.oci_instance_flex_memory_in_gbs
     ocpus = var.oci_instance_flex_ocpus
@@ -36,7 +44,7 @@ resource "oci_core_volume" "kdevops_data_disk" {
   count = var.oci_volumes_enable_extra == "true" ? 0 : local.kdevops_num_boxes
 
   availability_domain = data.oci_identity_availability_domain.kdevops_av_domain.name
-  compartment_id = var.oci_compartment_ocid
+  compartment_id = data.oci_identity_compartments.kdevops_compartment.compartments[0].id
   display_name = var.oci_data_volume_display_name
   size_in_gbs = 50
 }
@@ -45,7 +53,7 @@ resource "oci_core_volume" "kdevops_sparse_disk" {
   count = var.oci_volumes_enable_extra == "true" ? 0 : local.kdevops_num_boxes
 
   availability_domain = data.oci_identity_availability_domain.kdevops_av_domain.name
-  compartment_id = var.oci_compartment_ocid
+  compartment_id = data.oci_identity_compartments.kdevops_compartment.compartments[0].id
   display_name = var.oci_sparse_volume_display_name
   size_in_gbs = 120
 }
@@ -75,7 +83,7 @@ module "volumes" {
   source = "./volumes"
 
   vol_availability_domain = data.oci_identity_availability_domain.kdevops_av_domain.name
-  vol_compartment_ocid    = var.oci_compartment_ocid
+  vol_compartment_ocid    = data.oci_identity_compartments.kdevops_compartment.compartments[0].id
   vol_instance_id         = element(oci_core_instance.kdevops_instance.*.id, count.index)
   vol_instance_name       = element(var.kdevops_nodes, count.index)
   vol_volume_count        = var.oci_volumes_per_instance
diff --git a/terraform/oci/vars.tf b/terraform/oci/vars.tf
index 05c3f3769e94..f760fbb776fa 100644
--- a/terraform/oci/vars.tf
+++ b/terraform/oci/vars.tf
@@ -3,6 +3,11 @@ variable "oci_ad_number" {
   type        = number
 }
 
+variable "oci_compartment_name" {
+  description = "Name of compartment in which to create resources"
+  type        = string
+}
+
 variable "oci_region" {
   description = "An OCI region"
   type        = string
@@ -28,11 +33,6 @@ variable "oci_user_fingerprint" {
   type        = string
 }
 
-variable "oci_compartment_ocid" {
-  description = "OCID of compartment"
-  type        = string
-}
-
 variable "oci_shape" {
   description = "Shape name"
   type        = string
-- 
2.48.1


  parent reply	other threads:[~2025-04-01  1:00 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-01  0:59 [RFC PATCH 00/31] Simplify OCI configuration menu cel
2025-04-01  0:59 ` [RFC PATCH 01/31] terraform/OCI: Remove terraform_oci_instance_display_name cel
2025-04-01  0:59 ` [RFC PATCH 02/31] terraform/OCI: One default value to rule them cel
2025-04-01  0:59 ` [RFC PATCH 03/31] terraform/OCI: Add an "Identity & Access" submenu cel
2025-04-01  0:59 ` [RFC PATCH 04/31] terraform/OCI: Add a "Resource location" submenu cel
2025-04-01  0:59 ` [RFC PATCH 05/31] terraform/OCI: Add a "Compute" Kconfig submenu cel
2025-04-01  0:59 ` [RFC PATCH 06/31] terraform/OCI: Add a "Storage" " cel
2025-04-01  0:59 ` [RFC PATCH 07/31] terraform/OCI: Add a "Networking" " cel
2025-04-01  0:59 ` [RFC PATCH 08/31] terraform/OCI: Use "output yaml" for the tenancy OCID cel
2025-04-01  0:59 ` [RFC PATCH 09/31] terraform/OCI: Use "output yaml" for the user OCID cel
2025-04-01  0:59 ` [RFC PATCH 10/31] terraform/OCI: Use "output yaml" for the signing key cel
2025-04-01  0:59 ` [RFC PATCH 11/31] terraform/OCI: Use "output yaml" for the fingerprint cel
2025-04-01  0:59 ` [RFC PATCH 12/31] terraform/OCI: Add a Region selector cel
2025-04-01  0:59 ` [RFC PATCH 13/31] terraform/OCI: Add an availability domain selector cel
2025-04-01  0:59 ` cel [this message]
2025-04-01  0:59 ` [RFC PATCH 15/31] terraform/OCI: Use "output yaml" for the instance shape setting cel
2025-04-01  0:59 ` [RFC PATCH 16/31] terraform/OCI: Add a shape selector for Flex shapes cel
2025-04-01  0:59 ` [RFC PATCH 17/31] terraform/OCI: Use "output yaml" for the OCPUs setting cel
2025-04-01  0:59 ` [RFC PATCH 18/31] terraform/OCI: Use "output yaml" for the memory_in_gbs setting cel
2025-04-01  0:59 ` [RFC PATCH 19/31] terraform/OCI: Add a shape family selector cel
2025-04-01  0:59 ` [RFC PATCH 20/31] terraform/OCI: Add a bare metal shape selector cel
2025-04-01  0:59 ` [RFC PATCH 21/31] terraform/OCI: Use "output yaml" for the source image setting cel
2025-04-01  0:59 ` [RFC PATCH 22/31] terraform/OCI: Simplify image selection cel
2025-04-01  0:59 ` [RFC PATCH 23/31] terraform/OCI: Remove TERRAFORM_OCI_VOLUMES_ENABLE_EXTRA cel
2025-04-01  0:59 ` [RFC PATCH 24/31] terraform/OCI: Use "output yaml" for the assign_public_ip" setting cel
2025-04-01  0:59 ` [RFC PATCH 25/31] terraform/OCI: Use "output yaml" for the subnet_ocid setting cel
2025-04-01  0:59 ` [RFC PATCH 26/31] terraform/OCI: Add a default VCN cel
2025-04-01  0:59 ` [RFC PATCH 27/31] terraform/OCI: Add a Kconfig switch to create a VCN on the fly cel
2025-04-01  0:59 ` [RFC PATCH 28/31] terraform/OCI: Run "terraform fmt" on provider.tf cel
2025-04-01  0:59 ` [RFC PATCH 29/31] terraform/OCI: Run "terraform fmt" on main.tf cel
2025-04-01  0:59 ` [RFC PATCH 30/31] terraform/OCI: Nit: alphabetize vars.tf cel
2025-04-01  1:00 ` [RFC PATCH 31/31] terraform/OCI: Update the OCI section of docs/kdevops-terraform.md cel
2025-04-02 19:21 ` [RFC PATCH 00/31] Simplify OCI configuration menu Luis Chamberlain
2025-04-02 19:24   ` Luis Chamberlain
2025-04-02 19:38   ` Chuck Lever
2025-04-02 20:08     ` Luis Chamberlain
2025-04-08 12:42       ` Chandan Babu R
2025-04-08 13:20         ` Chuck Lever
2025-04-09  4:04           ` Chandan Babu R

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250401010000.764234-15-cel@kernel.org \
    --to=cel@kernel.org \
    --cc=chandanbabu@kernel.org \
    --cc=chuck.lever@oracle.com \
    --cc=kdevops@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox