获取 OCI Tenancy 中所有隔间的列表

Ibp*_*rof 4 oracle-cloud-infrastructure oci-python-sdk

我正在尝试获取使用compartments中的所有(包括子隔间)的列表。OCI TenancyPython SDK

但下文OCI API并未给出root compartment详细内容。

root compartment有没有办法直接通过 API获取详细信息?

下面是我的代码:

import oci
from oci.config import from_file
from oci.signer import Signer

config = from_file()

COMPARTMENT_ID="ocid1.tenancy.oc1..a"

identity_client = oci.identity.IdentityClient(config)

list_compartments_response = identity_client.list_compartments(
    compartment_id=COMPARTMENT_ID,
    compartment_id_in_subtree=True)

compartmentlist = list_compartments_response.data
Run Code Online (Sandbox Code Playgroud)

compartmentlist字典不包含root compartment详细信息。

请帮忙。

编辑1:

COMPARTMENT_ID上面代码中给出的是root compartment ID. root compartment我什至需要API 的最终响应中的详细信息。

小智 5

list_compartments提供了向 API 提供的sub-compartments特定项下的所有详细信息。compartment OCID

如果您向此 API 提供信息root compartment OCID,那么它将提供除.compartmentroot compartmentroot

要附加root compartment详细信息,可以使用以下 API。我不知道还有其他人OCI API可以完成这项任务。

希望这对您有帮助。

import oci
from oci.config import from_file

config = from_file()  # Config file is read from user's home location i.e., ~/.oci/config

COMPARTMENT_ID="ocid1.tenancy.oc1..a" # root compartment OCID

identity_client = oci.identity.IdentityClient(config)

list_compartments_response = identity_client.list_compartments(
    compartment_id=COMPARTMENT_ID,
    compartment_id_in_subtree=True)

# Get the list of compartments including child compartments except root compartment
compartmentlist = list_compartments_response.data

# Get the details of root compartment & append to the compartment list so that we have the full list of compartments in the given tenancy

root_compartment = identity_client.get_compartment(
    compartment_id=COMPARTMENT_ID).data
compartmentlist.append(root_compartment)

Run Code Online (Sandbox Code Playgroud)