netboxgo/device_roles.go
2022-02-09 09:59:14 +01:00

126 lines
3.2 KiB
Go

package netboxgo
import (
"context"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
)
type DeviceRolesService service
// DeviceRoles is used to list device roles
type DeviceRoles struct {
Next string `json:"next"`
Previous string `json:"previous"`
Results []DeviceRole `json:"results"`
Count int `json:"count"`
}
// DeviceRole is a role of a device
type DeviceRole struct {
LastUpdated time.Time `json:"last_updated"`
CustomFields struct{} `json:"custom_fields"`
URL string `json:"url"`
Display string `json:"display"`
Slug string `json:"slug"`
Color string `json:"color"`
Created string `json:"created"`
Description string `json:"description"`
Name string `json:"name"`
Tags []struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
ID int `json:"id"`
} `json:"tags"`
ID int `json:"id"`
DeviceCount int `json:"device_count"`
VirtualMachineCount int `json:"virtualmachine_count"`
VMRole bool `json:"vm_role"`
}
// DeviceTypeFilter is used to filter dcim_device_roles query to the Netbox API
type DeviceRoleFilter struct {
// User specific filters
ID string `schema:"id,omitempty"`
Name string `schema:"name,omitempty"`
Slug string `schema:"slug,omitempty"`
Color string `schema:"color,omitempty"`
VMRole string `schema:"vm_role,omitempty"`
Q string `schema:"q,omitempty"`
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
}
const deviceRolesPath = dcimPath + "/device-roles"
// List deviceroles. DeviceRoleFilter is used to list based on filter queries.
func (s *DeviceRolesService) List(ctx context.Context, f *DeviceRoleFilter) (*DeviceRoles, error) {
var deviceroles DeviceRoles
var query string
var req *http.Request
var err error
encoder := schema.NewEncoder()
form := url.Values{}
err = encoder.Encode(f, form)
if err != nil {
return &deviceroles, err
}
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", deviceRolesPath, query, nil)
if err != nil {
return &deviceroles, err
}
_, err = s.client.do(req, &deviceroles)
if err != nil {
return &deviceroles, err
}
return &deviceroles, nil
}
// var err error
// var req *http.Request
// req, err = s.client.newRequest(ctx, "POST", deviceRolesPath, "", c)
// if err != nil {
// return fmt.Errorf("unable to create request: %w", err)
// }
// _, err = s.client.do(req, nil)
// if err != nil {
// return fmt.Errorf("unable to do request: %w", err)
// }
// return nil
// }
// // Update a device-role
// func (s *DeviceRolesService) Update(ctx context.Context, id string, c *UpdateDevice) error {
// var req *http.Request
// var err error
// path := fmt.Sprintf("%s/%s", deviceRolesPath, id)
// req, err = s.client.newRequest(ctx, "PATCH", path, "", c)
// if err != nil {
// return fmt.Errorf("unable to create request: %w", err)
// }
// _, err = s.client.do(req, nil)
// if err != nil {
// return fmt.Errorf("unable to do request: %w", err)
// }
// return nil
// }