From 64439f8b433829f73c3d178a109d6173fb25817e Mon Sep 17 00:00:00 2001 From: Domonkos Cinke Date: Fri, 6 Jan 2023 12:38:18 +0100 Subject: [PATCH 1/3] Add and unify tag attributes --- docs/data-sources/devices.md | 2 + docs/data-sources/virtual_machines.md | 1 + netbox/data_source_netbox_devices.go | 48 +++++++++++++------ netbox/data_source_netbox_virtual_machines.go | 18 +++++-- 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/docs/data-sources/devices.md b/docs/data-sources/devices.md index e753eddd..ced93606 100644 --- a/docs/data-sources/devices.md +++ b/docs/data-sources/devices.md @@ -54,6 +54,8 @@ Read-Only: - `serial` (String) - `site_id` (Number) - `status` (String) +- `tag_ids` (List of Number) +- `tag_slugs` (List of String) - `tenant_id` (Number) diff --git a/docs/data-sources/virtual_machines.md b/docs/data-sources/virtual_machines.md index fa21d81e..4e4de0af 100644 --- a/docs/data-sources/virtual_machines.md +++ b/docs/data-sources/virtual_machines.md @@ -71,6 +71,7 @@ Read-Only: - `site_id` (Number) - `status` (String) - `tag_ids` (List of Number) +- `tag_slugs` (List of String) - `tenant_id` (Number) - `vcpus` (Number) - `vm_id` (Number) diff --git a/netbox/data_source_netbox_devices.go b/netbox/data_source_netbox_devices.go index aabac7aa..661ac7c2 100644 --- a/netbox/data_source_netbox_devices.go +++ b/netbox/data_source_netbox_devices.go @@ -94,6 +94,20 @@ func dataSourceNetboxDevices() *schema.Resource { Type: schema.TypeInt, Computed: true, }, + "tag_ids": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "tag_slugs": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, "tenant_id": { Type: schema.TypeInt, Computed: true, @@ -127,28 +141,22 @@ func dataSourceNetboxDevicesRead(d *schema.ResourceData, m interface{}) error { for _, f := range filterParams.List() { k := f.(map[string]interface{})["name"] v := f.(map[string]interface{})["value"] + vString := v.(string) switch k { case "asset_tag": - var assetTagString = v.(string) - params.AssetTag = &assetTagString + params.AssetTag = &vString case "cluster_id": - var clusterString = v.(string) - params.ClusterID = &clusterString + params.ClusterID = &vString case "name": - var nameString = v.(string) - params.Name = &nameString + params.Name = &vString case "region": - var regionString = v.(string) - params.Region = ®ionString + params.Region = &vString case "role_id": - var roleIdString = v.(string) - params.RoleID = &roleIdString + params.RoleID = &vString case "site_id": - var siteIdString = v.(string) - params.SiteID = &siteIdString + params.SiteID = &vString case "tenant_id": - var tenantIdString = v.(string) - params.TenantID = &tenantIdString + params.TenantID = &vString default: return fmt.Errorf("'%s' is not a supported filter parameter", k) } @@ -211,6 +219,18 @@ func dataSourceNetboxDevicesRead(d *schema.ResourceData, m interface{}) error { if device.Site != nil { mapping["site_id"] = device.Site.ID } + if device.Tags != nil { + var tagsIds []int64 + var tagsSlugs []string + for _, t := range device.Tags { + tagsIds = append(tagsIds, t.ID) + } + for _, t := range device.Tags { + tagsSlugs = append(tagsSlugs, *t.Slug) + } + mapping["tag_ids"] = tagsIds + mapping["tag_slugs"] = tagsSlugs + } if device.Tenant != nil { mapping["tenant_id"] = device.Tenant.ID } diff --git a/netbox/data_source_netbox_virtual_machines.go b/netbox/data_source_netbox_virtual_machines.go index 99433620..e666e1cc 100644 --- a/netbox/data_source_netbox_virtual_machines.go +++ b/netbox/data_source_netbox_virtual_machines.go @@ -116,6 +116,13 @@ func dataSourceNetboxVirtualMachine() *schema.Resource { Type: schema.TypeInt, }, }, + "tag_slugs": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, "tenant_id": { Type: schema.TypeInt, Computed: true, @@ -249,11 +256,16 @@ func dataSourceNetboxVirtualMachineRead(d *schema.ResourceData, m interface{}) e mapping["status"] = v.Status.Value } if v.Tags != nil { - var tags []int64 + var tagsIds []int64 + var tagsSlugs []string + for _, t := range v.Tags { + tagsIds = append(tagsIds, t.ID) + } for _, t := range v.Tags { - tags = append(tags, t.ID) + tagsSlugs = append(tagsSlugs, *t.Slug) } - mapping["tag_ids"] = tags + mapping["tag_ids"] = tagsIds + mapping["tag_slugs"] = tagsSlugs } if v.Tenant != nil { mapping["tenant_id"] = v.Tenant.ID From 219f9a54e1668003b86e5f1fb0523a9ea3b1cf2f Mon Sep 17 00:00:00 2001 From: Domonkos Cinke Date: Fri, 6 Jan 2023 12:46:53 +0100 Subject: [PATCH 2/3] Simplify in one loop --- netbox/data_source_netbox_devices.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/netbox/data_source_netbox_devices.go b/netbox/data_source_netbox_devices.go index 661ac7c2..84213523 100644 --- a/netbox/data_source_netbox_devices.go +++ b/netbox/data_source_netbox_devices.go @@ -224,8 +224,6 @@ func dataSourceNetboxDevicesRead(d *schema.ResourceData, m interface{}) error { var tagsSlugs []string for _, t := range device.Tags { tagsIds = append(tagsIds, t.ID) - } - for _, t := range device.Tags { tagsSlugs = append(tagsSlugs, *t.Slug) } mapping["tag_ids"] = tagsIds From 22f6e9c6c9cba63f933bf706b01db5888f3a4e79 Mon Sep 17 00:00:00 2001 From: Domonkos Cinke Date: Fri, 6 Jan 2023 12:48:19 +0100 Subject: [PATCH 3/3] Simplify in one loop --- netbox/data_source_netbox_virtual_machines.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/netbox/data_source_netbox_virtual_machines.go b/netbox/data_source_netbox_virtual_machines.go index e666e1cc..6843a71c 100644 --- a/netbox/data_source_netbox_virtual_machines.go +++ b/netbox/data_source_netbox_virtual_machines.go @@ -260,8 +260,6 @@ func dataSourceNetboxVirtualMachineRead(d *schema.ResourceData, m interface{}) e var tagsSlugs []string for _, t := range v.Tags { tagsIds = append(tagsIds, t.ID) - } - for _, t := range v.Tags { tagsSlugs = append(tagsSlugs, *t.Slug) } mapping["tag_ids"] = tagsIds