-
Notifications
You must be signed in to change notification settings - Fork 44
Adding inline M2M fields #728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| Added the ability to assign secrets to a secrets group when using the `secrets_group` module. | ||
| Added the ability to assign static group associations when using the `dynamic_group` module. | ||
| Added the ability to assign prefixes to a cloud network when using the `cloud_network` module. | ||
| Added the ability to assign cloud networks to a cloud service when using the `cloud_service` module. | ||
| Added the ability to assign vrfs to a device, virtual machine, or virtual device context when using the respective module. | ||
| Added the ability to assign clusters to a device when using the `device` module. | ||
| Added the ability to assign ip addresses to a device interface or vm interface when using the respective module. | ||
| Added the ability to assign prefixes or vlans to a location when using the `location` module. | ||
| Added the ability to assign provider networks to a provider when using the `provider` module. | ||
| Added the ability to manage custom field choices to a custom field when using the `custom_field` module. | ||
| Added the ability to manage metadata choices to a metadata type when using the `metadata_type` module. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| Deprecated the `secrets_groups_association` module. Use the `secrets` option of the `secrets_group` module instead. | ||
| Deprecated the `static_group_association` module. Use the `static_group_associations` option of the `dynamic_group` module instead. | ||
| Deprecated the `cloud_network_prefix_assignment` module. Use the `prefixes` option of the `cloud_network` module instead. | ||
| Deprecated the `cloud_service_network_assignment` module. Use the `cloud_networks` option of the `cloud_service` module instead. | ||
| Deprecated the `vrf_device_assignment` module. Use the `vrfs` option of the `device`, `virtual_machine`, or `virtual_device_context` module instead. | ||
| Deprecated the `device_cluster_assignment` module. Use the `clusters` option of the `device` module instead. | ||
| Deprecated the `ip_address_to_interface` module. Use the `ip_addresses` option of the `device_interface` or `vm_interface` module instead. | ||
| Deprecated the `prefix_location` module. Use the `prefixes` option of the `location` module instead. | ||
| Deprecated the `vlan_location` module. Use the `vlans` option of the `location` module instead. | ||
| Deprecated the `custom_field_choice` module. Use the `custom_field_choices` option of the `custom_field` module instead. | ||
| Deprecated the `metadata_choice` module. Use the `metadata_choices` option of the `metadata_type` module instead. | ||
| Deprecated the `provider_network` module. Use the `provider_networks` option of the `provider` module instead. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| # Inline Many-to-Many Associations | ||
|
|
||
| +++ 6.2.0 | ||
|
|
||
| Many Nautobot models have many-to-many (M2M) relationships with other models. For example, a device can be associated with multiple VRFs, a location can have multiple prefixes, and a secrets group can contain multiple secrets. | ||
|
|
||
| Previously, managing these associations required using separate standalone modules (e.g., `vrf_device_assignment`, `prefix_location`, `secrets_groups_association`). Now you can manage them inline on the parent module using the M2M field options. | ||
|
|
||
| ## Supported Parent Modules and Fields | ||
|
|
||
| | Parent Module | M2M Field | Child Object Key | Description | | ||
| |---|---|---|---| | ||
| | `device` | `vrfs` | `vrf` | VRF assignments | | ||
| | `device` | `clusters` | `cluster` | Cluster assignments | | ||
| | `virtual_machine` | `vrfs` | `vrf` | VRF assignments | | ||
| | `virtual_device_context` | `vrfs` | `vrf` | VRF assignments | | ||
| | `device_interface` | `ip_addresses` | `ip_address` | IP address to interface | | ||
| | `vm_interface` | `ip_addresses` | `ip_address` | IP address to VM interface | | ||
| | `location` | `prefixes` | `prefix` | Prefix to location | | ||
| | `location` | `vlans` | `vlan` | VLAN to location | | ||
| | `cloud_network` | `prefixes` | `prefix` | Cloud network prefix assignments | | ||
| | `cloud_service` | `cloud_networks` | `cloud_network` | Cloud service network assignments | | ||
| | `secrets_group` | `secrets` | `secret` | Secrets group associations | | ||
| | `dynamic_group` | `static_group_associations` | `associated_object_type` / `associated_object_id` | Static group memberships | | ||
| | `custom_field` | `custom_field_choices` | `value` | Custom field choices | | ||
| | `metadata_type` | `metadata_choices` | `value` | Metadata type choices | | ||
| | `provider` | `provider_networks` | `name` | Provider networks | | ||
|
|
||
| ## M2M Field Structure | ||
|
|
||
| All M2M fields follow the same structure: | ||
|
|
||
| ```yaml | ||
| <m2m_field>: | ||
| state: merge # merge (default), replace, or delete | ||
| objects: | ||
| - <child_key>: "value" | ||
| - <child_key>: "another value" | ||
| ``` | ||
|
|
||
| ### States | ||
|
|
||
| - **merge** (default): Adds the specified associations without removing existing ones. Safe for incremental changes. | ||
| - **replace**: Enforces exactly the listed associations. Any existing associations not in the list are removed. | ||
| - **delete**: Removes only the specified associations. Other existing associations are left intact. | ||
|
|
||
| ## Basic Examples | ||
|
|
||
| ### Adding VRFs to a Device | ||
|
|
||
| ```yaml | ||
| - name: Create a device with VRF associations | ||
| networktocode.nautobot.device: | ||
| url: "{{ nautobot_url }}" | ||
| token: "{{ nautobot_token }}" | ||
| name: "my-router" | ||
| device_type: "Cisco CSR1000v" | ||
| role: "Router" | ||
| location: "Main Site" | ||
| status: "Active" | ||
| vrfs: | ||
| objects: | ||
| - vrf: "Management VRF" | ||
| - vrf: "Production VRF" | ||
| state: present | ||
| ``` | ||
|
|
||
| Since no `state` is specified on the `vrfs` field, it defaults to `merge` -- the VRFs are added without affecting any other existing VRF associations. | ||
|
|
||
| ### Adding IP Addresses to an Interface | ||
|
|
||
| The child key accepts either a simple string or a dictionary for more specific lookups: | ||
|
|
||
| ```yaml | ||
| # Simple string -- looks up the IP address by address | ||
| - name: Associate IP addresses with an interface | ||
| networktocode.nautobot.device_interface: | ||
| url: "{{ nautobot_url }}" | ||
| token: "{{ nautobot_token }}" | ||
| device: "my-router" | ||
| name: "GigabitEthernet0/0" | ||
| ip_addresses: | ||
| objects: | ||
| - ip_address: "10.0.0.1/24" | ||
| state: present | ||
|
|
||
| # Dictionary -- useful when disambiguation is needed (e.g., multiple namespaces) | ||
| - name: Associate IP address with namespace specified | ||
| networktocode.nautobot.device_interface: | ||
| url: "{{ nautobot_url }}" | ||
| token: "{{ nautobot_token }}" | ||
| device: "my-router" | ||
| name: "GigabitEthernet0/0" | ||
| ip_addresses: | ||
| objects: | ||
| - ip_address: | ||
| address: "10.0.0.1/24" | ||
| namespace: "Production" | ||
| state: present | ||
| ``` | ||
|
|
||
| ### Adding Secrets to a Secrets Group | ||
|
|
||
| Some M2M associations have extra fields beyond just the child identifier. For secrets group associations, `access_type` and `secret_type` are part of the association itself: | ||
|
|
||
| ```yaml | ||
| - name: Create secrets group with secret associations | ||
| networktocode.nautobot.secrets_group: | ||
| url: "{{ nautobot_url }}" | ||
| token: "{{ nautobot_token }}" | ||
| name: "Device Credentials" | ||
| secrets: | ||
| objects: | ||
| - secret: "admin-username" | ||
| access_type: "SSH" | ||
| secret_type: "username" | ||
| - secret: "admin-password" | ||
| access_type: "SSH" | ||
| secret_type: "password" | ||
| state: present | ||
| ``` | ||
|
|
||
| ## Managing Association State | ||
|
|
||
| ### Merge (Default) | ||
|
|
||
| Merge adds new associations without removing existing ones. Running the same task twice is idempotent. | ||
|
|
||
| ```yaml | ||
| # First run: adds VRF-A | ||
| - name: Add first VRF | ||
| networktocode.nautobot.device: | ||
| url: "{{ nautobot_url }}" | ||
| token: "{{ nautobot_token }}" | ||
| name: "my-router" | ||
| vrfs: | ||
| objects: | ||
| - vrf: "VRF-A" | ||
| state: present | ||
|
|
||
| # Second run: adds VRF-B, VRF-A is untouched | ||
| - name: Add second VRF | ||
| networktocode.nautobot.device: | ||
| url: "{{ nautobot_url }}" | ||
| token: "{{ nautobot_token }}" | ||
| name: "my-router" | ||
| vrfs: | ||
| objects: | ||
| - vrf: "VRF-B" | ||
| state: present | ||
| # Result: device has both VRF-A and VRF-B | ||
| ``` | ||
|
|
||
| ### Replace | ||
|
|
||
| Replace enforces exactly the listed set of associations. Any existing associations not in the list are removed. | ||
|
|
||
| ```yaml | ||
| # Device currently has VRF-A and VRF-B | ||
| - name: Replace all VRFs with only VRF-C | ||
| networktocode.nautobot.device: | ||
| url: "{{ nautobot_url }}" | ||
| token: "{{ nautobot_token }}" | ||
| name: "my-router" | ||
| vrfs: | ||
| state: replace | ||
| objects: | ||
| - vrf: "VRF-C" | ||
| state: present | ||
| # Result: device has only VRF-C (VRF-A and VRF-B removed) | ||
| ``` | ||
|
|
||
| ### Delete | ||
|
|
||
| Delete removes only the specified associations. Other associations are left intact. | ||
|
|
||
| ```yaml | ||
| # Device currently has VRF-A and VRF-B | ||
| - name: Remove only VRF-B | ||
| networktocode.nautobot.device: | ||
| url: "{{ nautobot_url }}" | ||
| token: "{{ nautobot_token }}" | ||
| name: "my-router" | ||
| vrfs: | ||
| state: delete | ||
| objects: | ||
| - vrf: "VRF-B" | ||
| state: present | ||
| # Result: device has only VRF-A | ||
| ``` | ||
|
|
||
| ## Diff Output | ||
|
|
||
| When M2M fields change, the diff output includes the before and after state as sorted lists of child object UUIDs: | ||
|
|
||
| ```json | ||
| { | ||
| "diff": { | ||
| "before": { | ||
| "vrfs": ["<uuid-of-vrf-a>"] | ||
| }, | ||
| "after": { | ||
| "vrfs": ["<uuid-of-vrf-a>", "<uuid-of-vrf-b>"] | ||
| } | ||
| } | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.