Skip to main content

netbox_openapi/
generic_fk.rs

1//! Generic foreign-key fields, derived from the NetBox OpenAPI schema.
2//! Auto-generated by `scripts/generate.sh` - do not edit.
3
4/// How a generic foreign key is encoded in a NetBox write payload.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum GenericFkEncoding {
7    /// A single nested `{ "object_type": ..., "object_id": ... }`.
8    Nested,
9    /// An array of nested `{ "object_type": ..., "object_id": ... }`.
10    NestedList,
11    /// A sibling scalar pair: a `<field>_type` content type and a `<field>_id`.
12    Split,
13}
14
15/// `(app_label.model, field, encoding)` for every generic foreign key in the
16/// NetBox write API. `field` is the logical base name; for `Split` it expands to
17/// `<field>_type` and `<field>_id` on the wire, otherwise it is the property
18/// carrying the (array of) `GenericObjectRequest`.
19pub const GENERIC_FK_FIELDS: &[(&str, &str, GenericFkEncoding)] = &[
20    (
21        "circuits.circuitgroupassignment",
22        "member",
23        GenericFkEncoding::Split,
24    ),
25    (
26        "circuits.circuittermination",
27        "termination",
28        GenericFkEncoding::Split,
29    ),
30    (
31        "dcim.cable",
32        "a_terminations",
33        GenericFkEncoding::NestedList,
34    ),
35    (
36        "dcim.cable",
37        "b_terminations",
38        GenericFkEncoding::NestedList,
39    ),
40    ("dcim.inventoryitem", "component", GenericFkEncoding::Split),
41    (
42        "dcim.inventoryitemtemplate",
43        "component",
44        GenericFkEncoding::Split,
45    ),
46    (
47        "dcim.macaddress",
48        "assigned_object",
49        GenericFkEncoding::Split,
50    ),
51    ("extras.bookmark", "object", GenericFkEncoding::Split),
52    (
53        "extras.eventrule",
54        "action_object",
55        GenericFkEncoding::Split,
56    ),
57    ("extras.imageattachment", "object", GenericFkEncoding::Split),
58    (
59        "extras.journalentry",
60        "assigned_object",
61        GenericFkEncoding::Split,
62    ),
63    ("extras.notification", "object", GenericFkEncoding::Split),
64    ("extras.subscription", "object", GenericFkEncoding::Split),
65    (
66        "ipam.fhrpgroupassignment",
67        "interface",
68        GenericFkEncoding::Split,
69    ),
70    (
71        "ipam.ipaddress",
72        "assigned_object",
73        GenericFkEncoding::Split,
74    ),
75    ("ipam.prefix", "scope", GenericFkEncoding::Split),
76    ("ipam.service", "parent_object", GenericFkEncoding::Split),
77    ("ipam.vlangroup", "scope", GenericFkEncoding::Split),
78    (
79        "tenancy.contactassignment",
80        "object",
81        GenericFkEncoding::Split,
82    ),
83    ("virtualization.cluster", "scope", GenericFkEncoding::Split),
84    (
85        "vpn.l2vpntermination",
86        "assigned_object",
87        GenericFkEncoding::Split,
88    ),
89    (
90        "vpn.tunneltermination",
91        "termination",
92        GenericFkEncoding::Split,
93    ),
94    ("wireless.wirelesslan", "scope", GenericFkEncoding::Split),
95];
96
97/// Returns the [`GenericFkEncoding`] for `field` on `model` (keyed as
98/// `app_label.model`, e.g. `"dcim.cable"`) if it is a generic foreign key.
99pub fn generic_fk_encoding(model: &str, field: &str) -> Option<GenericFkEncoding> {
100    GENERIC_FK_FIELDS
101        .iter()
102        .find(|(m, f, _)| *m == model && *f == field)
103        .map(|(_, _, enc)| *enc)
104}
105
106/// Returns `true` if `field` on `model` is a generic foreign key whose value
107/// must be encoded as a content-type reference rather than a bare id.
108pub fn is_generic_fk(model: &str, field: &str) -> bool {
109    generic_fk_encoding(model, field).is_some()
110}