refs #703: configure OCI GenAI root compartment
This commit is contained in:
@@ -26,7 +26,10 @@ ALLOWED_OCI_SETTINGS = frozenset(
|
||||
"OCI_PROFILE",
|
||||
}
|
||||
)
|
||||
_COMPARTMENT_ID = re.compile(r"^ocid1\.compartment\.[A-Za-z0-9._-]+$")
|
||||
# OCI permits the tenancy OCID when the root compartment is selected.
|
||||
_COMPARTMENT_OR_ROOT_ID = re.compile(
|
||||
r"^ocid1\.(?:compartment|tenancy)\.[A-Za-z0-9._-]+$"
|
||||
)
|
||||
|
||||
|
||||
class CompletionClient(Protocol):
|
||||
@@ -95,7 +98,7 @@ def load_oci_settings() -> OCISettings:
|
||||
raise ValueError("unsupported OCI authentication mode")
|
||||
|
||||
compartment_id = values.get("OCI_GENAI_COMPARTMENT_ID", "").strip()
|
||||
if not _COMPARTMENT_ID.fullmatch(compartment_id):
|
||||
if not _COMPARTMENT_OR_ROOT_ID.fullmatch(compartment_id):
|
||||
raise ValueError("OCI Generative AI compartment is not configured")
|
||||
return OCISettings(
|
||||
auth_type=auth_type,
|
||||
|
||||
51
poc4_active_source_20260714/tests/test_oci_genai_settings.py
Normal file
51
poc4_active_source_20260714/tests/test_oci_genai_settings.py
Normal file
@@ -0,0 +1,51 @@
|
||||
"""OCI GenAI configuration validation tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from src.oci_genai_sdk import ALLOWED_OCI_SETTINGS, load_oci_settings
|
||||
|
||||
|
||||
class OCISettingsTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self._previous = {key: os.environ.get(key) for key in ALLOWED_OCI_SETTINGS}
|
||||
os.environ.update(
|
||||
{
|
||||
"OCI_AUTH_TYPE": "config_file",
|
||||
"OCI_CONFIG_FILE": "/home/opc/.oci/config",
|
||||
"OCI_PROFILE": "DEFAULT",
|
||||
}
|
||||
)
|
||||
|
||||
def tearDown(self) -> None:
|
||||
for key, value in self._previous.items():
|
||||
if value is None:
|
||||
os.environ.pop(key, None)
|
||||
else:
|
||||
os.environ[key] = value
|
||||
|
||||
def test_accepts_a_child_compartment_ocid(self) -> None:
|
||||
os.environ["OCI_GENAI_COMPARTMENT_ID"] = "ocid1.compartment.oc1..example"
|
||||
|
||||
settings = load_oci_settings()
|
||||
|
||||
self.assertEqual("ocid1.compartment.oc1..example", settings.compartment_id)
|
||||
|
||||
def test_accepts_a_tenancy_ocid_for_the_root_compartment(self) -> None:
|
||||
os.environ["OCI_GENAI_COMPARTMENT_ID"] = "ocid1.tenancy.oc1..example"
|
||||
|
||||
settings = load_oci_settings()
|
||||
|
||||
self.assertEqual("ocid1.tenancy.oc1..example", settings.compartment_id)
|
||||
|
||||
def test_rejects_an_invalid_compartment_identifier(self) -> None:
|
||||
os.environ["OCI_GENAI_COMPARTMENT_ID"] = "not-an-ocid"
|
||||
|
||||
with self.assertRaisesRegex(ValueError, "compartment is not configured"):
|
||||
load_oci_settings()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user