'How to get compartment ID by name in Terraform OCI

Using Terraform v1.1.6 with Provider OCI. I would like to create a new child compartment under an existing parent compartment to which I only have the 'name'. For example, currently, I create a new compartment using the parent ID:

resource "oci_identity_compartment" "compartment" {
  compartment_id = var.compartment_id
  name           = "child_compartment"
}

However, I would like to do something like this, where I only know the "name" of the parent...

resource "oci_identity_compartment" "compartment" {
  compartment_name = "parent-name"
  name           = "child-compartment"
}

Is this possible? Could somehow use Terraform's data sources to look up the parent's name and return its ID, if so how?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source