Skip to content

feat: IHS-190 (part of IFC-2162) Add namespace restriction parameter to generic schemas#807

Merged
polmichel merged 4 commits intoinfrahub-developfrom
pmi-20260204-namespace-restriction-ifc-2162
Apr 1, 2026
Merged

feat: IHS-190 (part of IFC-2162) Add namespace restriction parameter to generic schemas#807
polmichel merged 4 commits intoinfrahub-developfrom
pmi-20260204-namespace-restriction-ifc-2162

Conversation

@polmichel
Copy link
Copy Markdown
Contributor

@polmichel polmichel commented Feb 9, 2026

To understand the global feature, see this PR: opsmill/infrahub#8319

This PR is about updating infrahub_sdk.schema.main.GenericSchemaAPI with the new generic schema parameter "restricted_namespaces".

Goal

See opsmill/infrahub#8319

Non-goals

Added another missing attribute to infrahub_sdk.schema.main.GenericSchemaAPI: hierarchical. This attribute was already into the internal Infrahub generic schema model.

What changed

Generic schemas into SDK API.

How-to review

This PR is quite small: commit by commit, in the chronological order.

How to test

Automated back-end tests:

uv run pytest tests/unit/ctl/test_schema_app.py tests/unit/sdk/test_schema.py

  • new test loading a generic schema through infrahubctl command, Infrahub API is mocked
  • new test ensuring that a schema not following the namespace restriction rule is rejected with the correct error message when loaded from python-sdk utilities. Infrahub API is mocked.

Application testing scenarios:

  • Load a valid generic schema from SDK API using the new attribute "restricted_namespaces".
  • Load an invalid schema from SDK API not following the namespace restriction rule.

Impact & rollout

Deployment notes: safe to deploy

Closes IHS-190

Summary by CodeRabbit

  • New Features

    • Added optional "hierarchical" and "restricted namespaces" configuration options to the schema API.
  • Tests

    • Added tests for loading generic schemas that include namespace restrictions.
    • Added tests to surface API error handling when schema load requests fail.

…ed_namespaces but also missing fields IHS-190
…rough infrahubctl command layer. Infrahub API is mocked IHS-190
…nvalid namespace is loaded from SDK methods. Infrahub API is mocked IHS-190
@polmichel polmichel self-assigned this Feb 9, 2026
@polmichel polmichel changed the title feat: IHS-190 (part of IFC-2162) Add namespace restriction parameter to generic schemas#806 feat: IHS-190 (part of IFC-2162) Add namespace restriction parameter to generic schemas Feb 9, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@                 Coverage Diff                  @@
##           infrahub-develop     #807      +/-   ##
====================================================
+ Coverage             80.33%   81.04%   +0.70%     
====================================================
  Files                   115      121       +6     
  Lines                  9865    10644     +779     
  Branches               1504     1583      +79     
====================================================
+ Hits                   7925     8626     +701     
- Misses                 1419     1501      +82     
+ Partials                521      517       -4     
Flag Coverage Δ
integration-tests 40.77% <ø> (-0.68%) ⬇️
python-3.10 52.38% <ø> (+1.00%) ⬆️
python-3.11 52.38% <ø> (+1.00%) ⬆️
python-3.12 52.36% <ø> (+0.98%) ⬆️
python-3.13 52.36% <ø> (+1.00%) ⬆️
python-3.14 54.09% <ø> (+1.06%) ⬆️
python-filler-3.12 24.07% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
infrahub_sdk/schema/main.py 91.05% <ø> (+0.83%) ⬆️

... and 35 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ogenstad
Copy link
Copy Markdown
Contributor

ogenstad commented Feb 9, 2026

Small comment on this PR. It should target the infrahub-develop branch. The reason being is that anything within that branch covers features included in an upcoming Infrahub version. Where as the develop branch of the SDK might be for things that get included in an SDK release that's outside of the Infrahub release schedule.

@polmichel polmichel changed the base branch from develop to infrahub-develop February 9, 2026 11:13
@polmichel polmichel marked this pull request as ready for review February 9, 2026 13:37
@polmichel polmichel requested a review from a team as a code owner February 9, 2026 13:37
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Feb 9, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e8f2c16e-c7a2-4663-8b0d-3aea8ec56aad

📥 Commits

Reviewing files that changed from the base of the PR and between 1083525 and 79816b6.

📒 Files selected for processing (1)
  • tests/unit/sdk/test_schema.py
✅ Files skipped from review due to trivial changes (1)
  • tests/unit/sdk/test_schema.py

Walkthrough

Adds two optional fields to GenericSchemaAPI: hierarchical: bool | None = None and restricted_namespaces: list[str] | None = None. Introduces a JSON fixture tests/fixtures/models/valid_generic_schema.json defining a generic Animal in namespace Testing and a Dog node deriving from it. Adds unit tests: test_load_valid_generic_schema (appears twice) validating loading and that restricted_namespaces includes ["Dog"], and test_schema_load_surfaces_api_error_on_422 asserting client behavior when the server returns HTTP 422 with GraphQL-style errors.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically summarizes the main change: adding a namespace restriction parameter (restricted_namespaces) to generic schemas in the SDK API.
Description check ✅ Passed The description covers key sections including goal/context (linked PR), what changed (restricted_namespaces parameter), how to test (automated tests provided), and deployment notes. Some optional template sections are omitted but non-critical.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@infrahub_sdk/schema/main.py`:
- Around line 292-294: GenericSchema is missing the hierarchical and
restricted_namespaces attributes present in GenericSchemaAPI, causing those
values (e.g., restricted_namespaces from valid_generic_schema.json) to be
dropped when parsed via SchemaRoot; add the two fields to GenericSchema by
declaring hierarchical: bool | None = None and restricted_namespaces: list[str]
| None = None on the GenericSchema dataclass/model so it matches
GenericSchemaAPI and preserves round-trip parsing of those properties.
🧹 Nitpick comments (2)
tests/unit/sdk/test_schema.py (2)

516-537: Variable error_message is shadowed on Line 536.

error_message is first defined on Line 516 as the mock response string, then reassigned on Line 536 to the extracted value from the response. This shadowing is confusing — use a distinct name for one of them.

Suggested fix
-    error_message = response.errors["errors"][0]["message"]
-    assert re.search(r"(?s)restricted namespaces(?=.*Dog)(?=.*Cat)", error_message)
+    actual_error = response.errors["errors"][0]["message"]
+    assert re.search(r"(?s)restricted namespaces(?=.*Dog)(?=.*Cat)", actual_error)

488-538: Test only covers the async path; consider adding sync coverage.

Existing tests in this file consistently parametrize over ["standard", "sync"] client types (e.g., Lines 47, 66, 93). This test only exercises InfrahubClient (async). Consider adding a sync variant using InfrahubClientSync.schema.load for parity, consistent with the existing pattern in this file. As per coding guidelines, infrahub_sdk/**/*.py: Follow async/sync dual pattern for new features in the Python SDK.

Comment thread infrahub_sdk/schema/main.py
Copy link
Copy Markdown
Contributor

@ogenstad ogenstad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but as indicated by the comment I'm not sure about that second test.

Comment thread tests/unit/sdk/test_schema.py Outdated
assert InfrahubSchemaBase._get_schema_name(schema="BuiltinIPAddress") == "BuiltinIPAddress"


async def test_schema_load_rejected_when_node_namespace_violates_generic_restricted_namespaces(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this test actually tests anything meaningful, since we're just mocking the response. I.e. are we doing something here that isn't already covered by other tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests that the error message is going from the API response to the user through the SDK layer. This does not bring much value, I could understand if you would rather delete it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case that the test name is a bit confusing as it really doesn't have anything to do with generics and restricted names but only how the SDK responds to errors in general. It's just that the dummy data happens to be one of this new kind, does that make sense?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I generalized and simplified the unit test.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Apr 1, 2026

Deploying infrahub-sdk-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: 79816b6
Status: ✅  Deploy successful!
Preview URL: https://2e1aab34.infrahub-sdk-python.pages.dev
Branch Preview URL: https://pmi-20260204-namespace-restr.infrahub-sdk-python.pages.dev

View logs

@polmichel polmichel requested a review from ogenstad April 1, 2026 11:54
@polmichel polmichel merged commit 6614764 into infrahub-develop Apr 1, 2026
21 checks passed
@polmichel polmichel deleted the pmi-20260204-namespace-restriction-ifc-2162 branch April 1, 2026 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants