Skip to content

Add per-instance group membership fields on Object Templates#9142

Merged
gmazoyer merged 7 commits into
developfrom
gma-20260430-9094
May 11, 2026
Merged

Add per-instance group membership fields on Object Templates#9142
gmazoyer merged 7 commits into
developfrom
gma-20260430-9094

Conversation

@gmazoyer
Copy link
Copy Markdown
Contributor

@gmazoyer gmazoyer commented May 5, 2026

Why

When you set member_of_groups on an Object Template today, the template node itself becomes a member of the group, but objects created from that template don't inherit the membership. So a template can express "things like this" for attributes and most relationships, but not for groups.

This change adds a way to express per-instance group membership on a template, mirroring the _from_resource_pool sister-field pattern we already use for IPs and number pools.

Closes #9094

What changed

Templates now expose two extra relationships: member_of_groups_for_instances and subscriber_of_groups_for_instances. They have kind GENERIC, so adding peers there does not put the template in those groups.

At template application time, NodeTemplateApplier reads the peers from those fields and writes them under member_of_groups / subscriber_of_groups on the new object. User-supplied groups on the create call still take precedence.

The existing member_of_groups and subscriber_of_groups on a template keep their original meaning: they describe the template's own membership and are not propagated to instances.

How to test

uv run pytest backend/tests/unit/core/schema/test_template_group_relationships.py
uv run pytest backend/tests/component/templates/test_template_applier.py

Checklist

  • Tests added/updated
  • Changelog entry added (changelog/9094.added.md)
  • External docs updated (docs/docs/topics/object-template.mdx, docs/docs/guides/object-template.mdx, docs/docs/topics/groups.mdx)
  • Internal .md docs updated (dev/knowledge/backend/templates.md added; architecture.md and schema-definitions.md cross-link to it)
  • I have reviewed AI generated content

gmazoyer added 4 commits May 5, 2026 21:29
Object templates now expose member_of_groups_for_instances and
subscriber_of_groups_for_instances. Groups assigned through these are
copied onto each object created from the template, while the template's
own member_of_groups keeps its existing meaning.
@github-actions github-actions Bot added type/documentation Improvements or additions to documentation group/backend Issue related to the backend (API Server, Git Agent) labels May 5, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 5, 2026

Merging this PR will not alter performance

✅ 12 untouched benchmarks


Comparing gma-20260430-9094 (c420358) with develop (2391aac)

Open in CodSpeed

Comment on lines +2160 to +2161
("member_of_groups_for_instances", "template_group_member_for_instances"),
("subscriber_of_groups_for_instances", "template_group_subscriber_for_instances"),
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.

Names are up for debate.

@gmazoyer gmazoyer marked this pull request as ready for review May 6, 2026 12:16
@gmazoyer gmazoyer requested review from a team as code owners May 6, 2026 12:16
Copy link
Copy Markdown
Contributor

@ajtmccarty ajtmccarty left a comment

Choose a reason for hiding this comment

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

functionally looks great
all comments are little naming/docs things

self.set(name=node_name, schema=schema)

def _add_template_group_for_instances_relationships(
self, schema: MainSchemaTypes, changed: bool
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.

changed seems like it should be inverted and called duplicate_before_update or something similar

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.

I changed the var name but did not call it duplicate_before_update because that would imply to do a bunch of not swap to make it behaves as its name would imply; and that would have made this hard to follow (nobody wants to read a var that is the result of a "not of not of not of …").

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.

good point. name is better now

Comment thread backend/tests/unit/core/schema/test_template_group_relationships.py
Comment thread docs/docs/guides/object-template.mdx Outdated
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 we're still in some sort of docs freeze, so this may need to change once that is unfrozen
  • personally, I'd say we should try to remove the em-dashes that the LLMs love so much b/c it makes it look like our docs are written by robots and that we are lazy

Comment thread docs/docs/guides/object-template.mdx Outdated
Comment thread docs/docs/topics/object-template.mdx Outdated
Comment thread docs/docs/topics/object-template.mdx Outdated
@gmazoyer gmazoyer force-pushed the gma-20260430-9094 branch from 2550e22 to 02a166e Compare May 7, 2026 08:11
Copy link
Copy Markdown
Contributor

@FragmentedPacket FragmentedPacket left a comment

Choose a reason for hiding this comment

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

Tested locally and using object templates and it successfully added the devices to the group when creating devices from that template.

---
apiVersion: infrahub.app/v1
kind: Object
spec:
  kind: CoreStandardGroup
  data:
    - name: test-group
# Device template — management_address allocated from "Device and Interface IP Pool"
# Includes nested interface template — primary_ip also allocated from the same pool
---
apiVersion: infrahub.app/v1
kind: Object
spec:
  kind: TemplateDcimDevice
  data:
    - template_name: "Standard Device"
      management_address_from_resource_pool: "Device and Interface IP Pool"
      member_of_groups_for_instances: ["test-group"]
      interfaces:
        kind: TemplateDcimInterface
        data:
          - template_name: "Standard Device eth0"
            name: eth0
            primary_ip_from_resource_pool: "Device and Interface IP Pool"

@gmazoyer gmazoyer force-pushed the gma-20260430-9094 branch from 02a166e to c420358 Compare May 11, 2026 06:26
@gmazoyer gmazoyer merged commit 2406fae into develop May 11, 2026
50 checks passed
@gmazoyer gmazoyer deleted the gma-20260430-9094 branch May 11, 2026 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

group/backend Issue related to the backend (API Server, Git Agent) type/documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: member_of_groups on an Object Template applies to the template itself and is not inherited by instances

3 participants