Skip to content
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

Add and unify some tag attributes for Devices and Virtual Machines #321

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/data-sources/devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


1 change: 1 addition & 0 deletions docs/data-sources/virtual_machines.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 32 additions & 14 deletions netbox/data_source_netbox_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = &regionString
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)
}
Expand Down Expand Up @@ -211,6 +219,16 @@ 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)
tagsSlugs = append(tagsSlugs, *t.Slug)
}
mapping["tag_ids"] = tagsIds
mapping["tag_slugs"] = tagsSlugs
}
if device.Tenant != nil {
mapping["tenant_id"] = device.Tenant.ID
}
Expand Down
16 changes: 13 additions & 3 deletions netbox/data_source_netbox_virtual_machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -249,11 +256,14 @@ 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 {
tags = append(tags, t.ID)
tagsIds = append(tagsIds, 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
Expand Down