Allow config contexts to append to existing values #7227

Closed
opened 2025-12-29 20:20:39 +01:00 by adam · 5 comments
Owner

Originally created by @WhaleJ84 on GitHub (Nov 14, 2022).

NetBox version

v3.3.7

Feature type

Change to existing functionality

Proposed functionality

I'm proposing some way for users to append to existing values in configuration contexts rather than just overwriting. An example of current and desired behavior is explained in the Use Case section below.

Use case

Currently, if I create two tags ["Service-HTTP", "Service-HTTPS"] with the intent of applying these tags to a device that has two services [{"name": "HTTP", "ports": [80]}, {"name": "HTTPS", "ports": [443]}], with the config-contexts:

[
    {
        "name": "Service-HTTP-config",
        "weight": 1000,
        "data": {
            {"ports:" [80]}
        }
    },
    {
        "name": "Service-HTTPS-config",
        "weight": 1000,
        "data": {
            {"ports:" [443]}
        }
    }
]

then the device's config-context would only render {"ports": [80]}. Likewise, changing with Service-HTTPS-config context weightings would overwrite the config and be {"ports": [443]}.

However, in this example, I want to dynamically render the planned open ports on the device so the firewall can be automatically configured from the context {"ports": [80, 443]}. I've looked at the documentation but did not notice a way to append to existing values - only overwrite.

Database changes

No response

External dependencies

No response

Originally created by @WhaleJ84 on GitHub (Nov 14, 2022). ### NetBox version v3.3.7 ### Feature type Change to existing functionality ### Proposed functionality I'm proposing some way for users to append to existing values in configuration contexts rather than just overwriting. An example of current and desired behavior is explained in the Use Case section below. ### Use case Currently, if I create two tags `["Service-HTTP", "Service-HTTPS"]` with the intent of applying these tags to a device that has two services `[{"name": "HTTP", "ports": [80]}, {"name": "HTTPS", "ports": [443]}]`, with the config-contexts: ``` [ { "name": "Service-HTTP-config", "weight": 1000, "data": { {"ports:" [80]} } }, { "name": "Service-HTTPS-config", "weight": 1000, "data": { {"ports:" [443]} } } ] ``` then the device's config-context would only render `{"ports": [80]}`. Likewise, changing with *Service-HTTPS-config* context weightings would overwrite the config and be `{"ports": [443]}`. However, in this example, I want to dynamically render the planned open ports on the device so the firewall can be automatically configured from the context `{"ports": [80, 443]}`. I've looked at the documentation but did not notice a way to append to existing values - only overwrite. ### Database changes _No response_ ### External dependencies _No response_
adam added the type: featurepending closurestatus: revisions needed labels 2025-12-29 20:20:39 +01:00
adam closed this issue 2025-12-29 20:20:39 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 5, 2023):

I'm proposing some way for users to append to existing values in configuration contexts rather than just overwriting.

What is the specific implementation you're proposing?

@jeremystretch commented on GitHub (Jan 5, 2023): > I'm proposing some way for users to append to existing values in configuration contexts rather than just overwriting. What is the specific implementation you're proposing?
Author
Owner

@WhaleJ84 commented on GitHub (Jan 5, 2023):

Possibly with a new checkbox on the creation screen to determine whether the context is weightless? If checked, then whatever value is entered in the mandatory 'Weight' field is ignored and other weightless contexts with the same data keys have their values merged in situations where possible (e.g. lists, dicts.).

I mocked up the image below to show it, but I have no experience with Django, so cannot say how it could be implemented into the code.
image

In my mind, the logic would be: if contexts are weightless AND data keys are matching (in this example "ports"), then merge values.
Would that work?

@WhaleJ84 commented on GitHub (Jan 5, 2023): Possibly with a new checkbox on the creation screen to determine whether the context is weightless? If checked, then whatever value is entered in the mandatory 'Weight' field is ignored and other weightless contexts with the same data keys have their values merged in situations where possible (e.g. lists, dicts.). I mocked up the image below to show it, but I have no experience with Django, so cannot say how it could be implemented into the code. ![image](https://user-images.githubusercontent.com/43613409/210837561-32d9aef9-9d40-42a1-aeff-b3b1a1679ad5.png) In my mind, the logic would be: *if contexts are weightless AND data keys are matching* (in this example "ports"), *then merge values*. Would that work?
Author
Owner

@WhaleJ84 commented on GitHub (Jan 5, 2023):

Thinking of a more complex scenario, I think weightless contexts should be used as a base - take the following contexts:
Service-HTTP (weightless)
{"ports": [80], "applications": {"web-server": "nginx"}}

Service-HTTPS (weightless)
{"ports": [443], "applications": {"web-server": "nginx", "ssl-certificates": "letsencrypt"}}

Application-Web-Server (weight 1000)
{"applications": {"web-server": "apache"}}

As the two services are weightless, they get added to the context regardless, but the application context has weight, so that would take priority and overwrite the corresponding key to make this:

{
    "ports": [80, 443],
    "applications": {
        "web-server": "apache",
        "ssl-certificates": "letsencrypt"
    }
}

By having some way to append as well as overwriting values, this could allow for some really creative contexts to be made and used in things like Ansible to dynamically provision devices.

@WhaleJ84 commented on GitHub (Jan 5, 2023): Thinking of a more complex scenario, I think weightless contexts should be used as a base - take the following contexts: **Service-HTTP** (weightless) `{"ports": [80], "applications": {"web-server": "nginx"}}` **Service-HTTPS** (weightless) `{"ports": [443], "applications": {"web-server": "nginx", "ssl-certificates": "letsencrypt"}}` **Application-Web-Server** (weight 1000) `{"applications": {"web-server": "apache"}}` As the two services are weightless, they get added to the context regardless, but the application context has weight, so that would take priority and overwrite the corresponding key to make this: ```json { "ports": [80, 443], "applications": { "web-server": "apache", "ssl-certificates": "letsencrypt" } } ``` By having some way to append as well as overwriting values, this could allow for some really creative contexts to be made and used in things like Ansible to dynamically provision devices.
Author
Owner

@github-actions[bot] commented on GitHub (Apr 6, 2023):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions[bot] commented on GitHub (Apr 6, 2023): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@github-actions[bot] commented on GitHub (May 6, 2023):

This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.

@github-actions[bot] commented on GitHub (May 6, 2023): This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7227