Normalize OCI Notifications topic identifiers during adoption
This commit is contained in:
parent
0f276c02ec
commit
77d4c1b53b
|
|
@ -62,6 +62,13 @@ def emit(values):
|
||||||
print(f"export {name}={shlex.quote(str(value))}")
|
print(f"export {name}={shlex.quote(str(value))}")
|
||||||
|
|
||||||
|
|
||||||
|
def object_id(item, label):
|
||||||
|
identifier = item.get("id") if isinstance(item, dict) else None
|
||||||
|
if not isinstance(identifier, str) or not identifier:
|
||||||
|
raise ValueError(f"OCI {label} response has no resource identifier")
|
||||||
|
return identifier
|
||||||
|
|
||||||
|
|
||||||
def config_text(values):
|
def config_text(values):
|
||||||
return "# Generated by Jeannie; canonical OCI foundation configuration.\n" + "".join(
|
return "# Generated by Jeannie; canonical OCI foundation configuration.\n" + "".join(
|
||||||
f"export {key}={shlex.quote(str(value))}\n"
|
f"export {key}={shlex.quote(str(value))}\n"
|
||||||
|
|
@ -268,7 +275,17 @@ class OCI:
|
||||||
# OCI deliberately conflates missing resources with denied access.
|
# OCI deliberately conflates missing resources with denied access.
|
||||||
# A 404 must never switch an entire foundation into create mode.
|
# A 404 must never switch an entire foundation into create mode.
|
||||||
raise ValueError(f"OCI {' '.join(arguments[:3])} failed: {process.stderr[-1800:]}")
|
raise ValueError(f"OCI {' '.join(arguments[:3])} failed: {process.stderr[-1800:]}")
|
||||||
return json.loads(process.stdout).get("data")
|
data = json.loads(process.stdout).get("data")
|
||||||
|
if arguments[:2] == ("ons", "topic"):
|
||||||
|
# Notifications calls this field topicId (topic-id in CLI JSON).
|
||||||
|
# Normalize both get and list results before adoption/deduplication.
|
||||||
|
rows = data if isinstance(data, list) else [data]
|
||||||
|
for row in rows:
|
||||||
|
if not isinstance(row, dict):
|
||||||
|
raise ValueError("OCI Notifications topic response is not an object")
|
||||||
|
row["id"] = row.get("topic-id") or row.get("id")
|
||||||
|
object_id(row, "Notifications topic")
|
||||||
|
return data
|
||||||
|
|
||||||
def compartments(self):
|
def compartments(self):
|
||||||
if self.compartments_cache is None:
|
if self.compartments_cache is None:
|
||||||
|
|
@ -283,7 +300,7 @@ class OCI:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def unique(rows, label):
|
def unique(rows, label):
|
||||||
by_id = {row["id"]: row for row in rows}
|
by_id = {object_id(row, label): row for row in rows}
|
||||||
if len(by_id) > 1:
|
if len(by_id) > 1:
|
||||||
raise ValueError(f"Ambiguous {label}: {', '.join(by_id)}; select its OCID explicitly")
|
raise ValueError(f"Ambiguous {label}: {', '.join(by_id)}; select its OCID explicitly")
|
||||||
return next(iter(by_id.values()), None)
|
return next(iter(by_id.values()), None)
|
||||||
|
|
@ -385,8 +402,9 @@ def reconcile(args):
|
||||||
|
|
||||||
def adopt(address, item, import_id=None):
|
def adopt(address, item, import_id=None):
|
||||||
if item and address not in managed:
|
if item and address not in managed:
|
||||||
imports.append({"to": address + "[0]", "id": import_id or item["id"]})
|
identity = import_id or object_id(item, address)
|
||||||
print(f"Adopting existing OCI {address}: {import_id or item['id']}", file=sys.stderr)
|
imports.append({"to": address + "[0]", "id": identity})
|
||||||
|
print(f"Adopting existing OCI {address}: {identity}", file=sys.stderr)
|
||||||
|
|
||||||
# Establish each VM's real compartment and subnet from its primary VNIC.
|
# Establish each VM's real compartment and subnet from its primary VNIC.
|
||||||
# Never search a VNIC using the foundation compartment as a substitute.
|
# Never search a VNIC using the foundation compartment as a substitute.
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,28 @@ class ResolverTests(unittest.TestCase):
|
||||||
with patch("subprocess.run", return_value=response), self.assertRaises(ValueError):
|
with patch("subprocess.run", return_value=response), self.assertRaises(ValueError):
|
||||||
resolver.OCI().call("compute", "instance", "get", "--instance-id", "recorded")
|
resolver.OCI().call("compute", "instance", "get", "--instance-id", "recorded")
|
||||||
|
|
||||||
|
def test_notifications_topic_id_is_normalized_for_get_and_list(self):
|
||||||
|
topic = {"topic-id": "ocid1.onstopic.existing", "name": "homelab-monitoring",
|
||||||
|
"compartment-id": "ocid1.compartment.existing", "lifecycle-state": "ACTIVE"}
|
||||||
|
for operation, data in (("get", topic), ("list", [topic])):
|
||||||
|
with self.subTest(operation=operation):
|
||||||
|
response = SimpleNamespace(returncode=0, stdout=json.dumps({"data": data}))
|
||||||
|
with patch("subprocess.run", return_value=response):
|
||||||
|
result = resolver.OCI().call("ons", "topic", operation)
|
||||||
|
rows = result if isinstance(result, list) else [result]
|
||||||
|
selected = resolver.OCI.unique(rows, "monitoring topic")
|
||||||
|
self.assertEqual(resolver.object_id(selected, "topic"), "ocid1.onstopic.existing")
|
||||||
|
self.assertEqual(selected["compartment-id"], topic["compartment-id"])
|
||||||
|
|
||||||
|
def test_missing_identifier_reports_a_schema_error(self):
|
||||||
|
response = SimpleNamespace(returncode=0, stdout='{"data": {"name": "invalid-topic"}}')
|
||||||
|
with patch("subprocess.run", return_value=response), self.assertRaisesRegex(
|
||||||
|
ValueError, "no resource identifier"
|
||||||
|
):
|
||||||
|
resolver.OCI().call("ons", "topic", "get")
|
||||||
|
with self.assertRaisesRegex(ValueError, "no resource identifier"):
|
||||||
|
resolver.OCI.unique([{"name": "invalid-resource"}], "resource")
|
||||||
|
|
||||||
def test_edge_separates_dns_and_ssh(self):
|
def test_edge_separates_dns_and_ssh(self):
|
||||||
client = SimpleNamespace(
|
client = SimpleNamespace(
|
||||||
instance=lambda role: {"id": "ocid1.instance.current", "compartment-id": "owner",
|
instance=lambda role: {"id": "ocid1.instance.current", "compartment-id": "owner",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue