Skip to content

Commit

Permalink
refactor: rework all resources to standarize custom fields processing
Browse files Browse the repository at this point in the history
Signed-off-by: Xabier Napal <[email protected]>
  • Loading branch information
xabinapal committed Dec 24, 2024
1 parent 3074410 commit 4f86408
Show file tree
Hide file tree
Showing 31 changed files with 212 additions and 413 deletions.
1 change: 0 additions & 1 deletion docs/resources/custom_field_choice_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ resource "netbox_custom_field_choice_set" "test" {
### Optional

- `base_choices` (String) Valid values are `IATA`, `ISO_3166` and `UN_LOCODE`. At least one of `base_choices` or `extra_choices` must be given.
- `custom_fields` (Map of String)
- `description` (String)
- `extra_choices` (List of List of String) This length of the inner lists must be exactly two, where the first value is the value of a choice and the second value is the label of the choice. At least one of `base_choices` or `extra_choices` must be given.
- `order_alphabetically` (Boolean) experimental. Defaults to `false`.
Expand Down
11 changes: 4 additions & 7 deletions netbox/data_source_netbox_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ func dataSourceNetboxCluster() *schema.Resource {
Computed: true,
Optional: true,
},
"custom_fields": {
Type: schema.TypeMap,
Computed: true,
},
customFieldsKey: customFieldsSchemaRead,
tagsKey: tagsSchemaRead,
},
}
Expand Down Expand Up @@ -116,10 +113,10 @@ func dataSourceNetboxClusterRead(d *schema.ResourceData, m interface{}) error {
} else {
d.Set("site_id", nil)
}
if result.CustomFields != nil {
d.Set("custom_fields", result.CustomFields)
}

d.Set(customFieldsKey, result.CustomFields)

d.Set(tagsKey, getTagListFromNestedTagList(result.Tags))

return nil
}
13 changes: 6 additions & 7 deletions netbox/data_source_netbox_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func dataSourceNetboxPrefix() *schema.Resource {
ValidateFunc: validation.IsCIDR,
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "site_id", "role_id", "cidr", "tag"},
},
customFieldsKey: customFieldsSchema,
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -94,7 +93,8 @@ for more information on available lookup expressions.`,
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaRead,
customFieldsKey: customFieldsSchemaRead,
tagsKey: tagsSchemaRead,
},
}
}
Expand Down Expand Up @@ -174,12 +174,11 @@ func dataSourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error {
d.Set("status", result.Status.Value)
d.Set("description", result.Description)
d.Set("family", int(*result.Family.Value))
d.Set("tags", getTagListFromNestedTagList(result.Tags))

cf := getCustomFields(result.CustomFields)
if cf != nil {
d.Set(customFieldsKey, cf)
}
d.Set(customFieldsKey, result.CustomFields)

d.Set(tagsKey, getTagListFromNestedTagList(result.Tags))

if result.Role != nil {
d.Set("role_id", result.Role.ID)
}
Expand Down
13 changes: 6 additions & 7 deletions netbox/data_source_netbox_racks.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func dataSourceNetboxRacks() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
tagsKey: tagsSchemaRead,
"tenant_id": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -136,10 +135,8 @@ func dataSourceNetboxRacks() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"custom_fields": {
Type: schema.TypeMap,
Computed: true,
},
customFieldsKey: customFieldsSchemaRead,
tagsKey: tagsSchemaRead,
},
},
},
Expand Down Expand Up @@ -246,7 +243,6 @@ func dataSourceNetboxRacksRead(d *schema.ResourceData, m interface{}) error {
mapping["width"] = v.Width.Value
}
mapping["u_height"] = v.UHeight
mapping["tags"] = getTagListFromNestedTagList(v.Tags)
if v.Tenant != nil {
mapping["tenant_id"] = v.Tenant.ID
}
Expand All @@ -273,7 +269,10 @@ func dataSourceNetboxRacksRead(d *schema.ResourceData, m interface{}) error {
mapping["mounting_depth"] = v.MountingDepth
mapping["description"] = v.Description
mapping["comments"] = v.Comments
mapping["custom_fields"] = getCustomFields(v.CustomFields)

mapping[customFieldsKey] = v.CustomFields

mapping[tagsKey] = getTagListFromNestedTagList(v.Tags)

s = append(s, mapping)
}
Expand Down
10 changes: 4 additions & 6 deletions netbox/data_source_netbox_tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ func dataSourceNetboxTenants() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"custom_fields": {
Type: schema.TypeMap,
Computed: true,
},

"site_count": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -141,6 +136,7 @@ func dataSourceNetboxTenants() *schema.Resource {
},
},
},
customFieldsKey: customFieldsSchemaRead,
},
},
},
Expand Down Expand Up @@ -196,7 +192,6 @@ func dataSourceNetboxTenantsRead(d *schema.ResourceData, m interface{}) error {
mapping["created"] = v.Created.String()
mapping["last_updated"] = v.LastUpdated.String()
mapping["comments"] = v.Comments
mapping["custom_fields"] = v.CustomFields

mapping["site_count"] = v.SiteCount
mapping["rack_count"] = v.RackCount
Expand All @@ -210,6 +205,9 @@ func dataSourceNetboxTenantsRead(d *schema.ResourceData, m interface{}) error {
mapping["cluster_count"] = v.ClusterCount

mapping["tenant_group"] = flattenTenantGroup(v.Group)

mapping[customFieldsKey] = v.CustomFields

s = append(s, mapping)
}

Expand Down
23 changes: 8 additions & 15 deletions netbox/resource_netbox_cable.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ func resourceNetboxCable() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
tagsKey: tagsSchema,
customFieldsKey: customFieldsSchema,
tagsKey: tagsSchema,
},
CustomizeDiff: customFieldsDiff,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand Down Expand Up @@ -110,12 +111,9 @@ func resourceNetboxCableCreate(d *schema.ResourceData, m interface{}) error {
bTerminations := d.Get("b_termination").(*schema.Set)
data.BTerminations = getGenericObjectsFromSchemaSet(bTerminations)

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))
data.CustomFields = computeCustomFieldsModel(d)

ct, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = ct
}
data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))

params := dcim.NewDcimCablesCreateParams().WithData(&data)

Expand Down Expand Up @@ -178,10 +176,8 @@ func resourceNetboxCableRead(d *schema.ResourceData, m interface{}) error {
d.Set("description", cable.Description)
d.Set("comments", cable.Comments)

cf := getCustomFields(res.GetPayload().CustomFields)
if cf != nil {
d.Set(customFieldsKey, cf)
}
d.Set(customFieldsKey, res.GetPayload().CustomFields)

d.Set(tagsKey, getTagListFromNestedTagList(res.GetPayload().Tags))

return nil
Expand Down Expand Up @@ -210,12 +206,9 @@ func resourceNetboxCableUpdate(d *schema.ResourceData, m interface{}) error {
bTerminations := d.Get("b_termination").(*schema.Set)
data.BTerminations = getGenericObjectsFromSchemaSet(bTerminations)

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))
data.CustomFields = computeCustomFieldsModel(d)

ct, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = ct
}
data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))

params := dcim.NewDcimCablesPartialUpdateParams().WithID(id).WithData(&data)

Expand Down
24 changes: 8 additions & 16 deletions netbox/resource_netbox_circuit_termination.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ func resourceNetboxCircuitTermination() *schema.Resource {
ValidateFunc: validation.StringInSlice(resourceNetboxCircuitTerminationTermSideOptions, false),
Description: buildValidValueDescription(resourceNetboxCircuitTerminationTermSideOptions),
},
tagsKey: tagsSchema,
customFieldsKey: customFieldsSchema,
tagsKey: tagsSchema,
},
CustomizeDiff: customFieldsDiff,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand Down Expand Up @@ -85,12 +86,9 @@ func resourceNetboxCircuitTerminationCreate(d *schema.ResourceData, m interface{
data.UpstreamSpeed = int64ToPtr(int64(upstreamspeedValue.(int)))
}

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))
data.CustomFields = computeCustomFieldsModel(d)

ct, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = ct
}
data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))

params := circuits.NewCircuitsCircuitTerminationsCreateParams().WithData(&data)

Expand Down Expand Up @@ -151,12 +149,9 @@ func resourceNetboxCircuitTerminationRead(d *schema.ResourceData, m interface{})
d.Set("upstream_speed", nil)
}

d.Set(tagsKey, getTagListFromNestedTagList(term.Tags))
d.Set(customFieldsKey, res.GetPayload().CustomFields)

cf := getCustomFields(term.CustomFields)
if cf != nil {
d.Set(customFieldsKey, cf)
}
d.Set(tagsKey, getTagListFromNestedTagList(term.Tags))

return nil
}
Expand Down Expand Up @@ -190,12 +185,9 @@ func resourceNetboxCircuitTerminationUpdate(d *schema.ResourceData, m interface{
data.UpstreamSpeed = int64ToPtr(int64(upstreamspeedValue.(int)))
}

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))
data.CustomFields = computeCustomFieldsModel(d)

cf, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = cf
}
data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))

params := circuits.NewCircuitsCircuitTerminationsPartialUpdateParams().WithID(id).WithData(&data)

Expand Down
1 change: 0 additions & 1 deletion netbox/resource_netbox_custom_field_choice_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ A choice set must define a base choice set and/or a set of arbitrary extra choic
Description: "experimental",
Default: false,
},
customFieldsKey: customFieldsSchema,
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand Down
18 changes: 5 additions & 13 deletions netbox/resource_netbox_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func resourceNetboxDevice() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
tagsKey: tagsSchema,
"primary_ipv4": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -135,7 +134,9 @@ func resourceNetboxDevice() *schema.Resource {
Description: "This is best managed through the use of `jsonencode` and a map of settings.",
},
customFieldsKey: customFieldsSchema,
tagsKey: tagsSchema,
},
CustomizeDiff: customFieldsDiff,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand Down Expand Up @@ -235,10 +236,7 @@ func resourceNetboxDeviceCreate(ctx context.Context, d *schema.ResourceData, m i
}
}

ct, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = ct
}
data.CustomFields = computeCustomFieldsModel(d)

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))

Expand Down Expand Up @@ -350,10 +348,7 @@ func resourceNetboxDeviceRead(ctx context.Context, d *schema.ResourceData, m int
d.Set("config_template_id", nil)
}

cf := getCustomFields(res.GetPayload().CustomFields)
if cf != nil {
d.Set(customFieldsKey, cf)
}
d.Set(customFieldsKey, res.GetPayload().CustomFields)

d.Set("asset_tag", device.AssetTag)

Expand Down Expand Up @@ -481,10 +476,7 @@ func resourceNetboxDeviceUpdate(ctx context.Context, d *schema.ResourceData, m i
}
}

cf, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = cf
}
data.CustomFields = computeCustomFieldsModel(d)

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))

Expand Down
23 changes: 8 additions & 15 deletions netbox/resource_netbox_device_console_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ func resourceNetboxDeviceConsolePort() *schema.Resource {
Default: false,
Optional: true,
},
tagsKey: tagsSchema,
customFieldsKey: customFieldsSchema,
tagsKey: tagsSchema,
},
CustomizeDiff: customFieldsDiff,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -79,12 +80,9 @@ func resourceNetboxDeviceConsolePortCreate(d *schema.ResourceData, m interface{}
MarkConnected: d.Get("mark_connected").(bool),
}

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))
data.CustomFields = computeCustomFieldsModel(d)

ct, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = ct
}
data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))

params := dcim.NewDcimConsolePortsCreateParams().WithData(&data)

Expand Down Expand Up @@ -147,10 +145,8 @@ func resourceNetboxDeviceConsolePortRead(d *schema.ResourceData, m interface{})
d.Set("description", consolePort.Description)
d.Set("mark_connected", consolePort.MarkConnected)

cf := getCustomFields(res.GetPayload().CustomFields)
if cf != nil {
d.Set(customFieldsKey, cf)
}
d.Set(customFieldsKey, res.GetPayload().CustomFields)

d.Set(tagsKey, getTagListFromNestedTagList(res.GetPayload().Tags))

return nil
Expand All @@ -172,12 +168,9 @@ func resourceNetboxDeviceConsolePortUpdate(d *schema.ResourceData, m interface{}
MarkConnected: d.Get("mark_connected").(bool),
}

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))
data.CustomFields = computeCustomFieldsModel(d)

ct, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = ct
}
data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))

params := dcim.NewDcimConsolePortsPartialUpdateParams().WithID(id).WithData(&data)

Expand Down
Loading

0 comments on commit 4f86408

Please sign in to comment.