2021-05-26 11:03:05 +02:00
|
|
|
package netboxgo
|
|
|
|
|
|
|
|
import (
|
2021-11-26 11:09:27 +01:00
|
|
|
"context"
|
2021-05-26 11:03:05 +02:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2022-02-09 09:59:14 +01:00
|
|
|
"time"
|
2021-05-26 11:03:05 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/schema"
|
|
|
|
)
|
|
|
|
|
2021-11-26 11:09:27 +01:00
|
|
|
type DeviceRolesService service
|
|
|
|
|
2022-02-09 09:59:14 +01:00
|
|
|
// DeviceRoles is used to list device roles
|
2021-11-26 11:09:27 +01:00
|
|
|
type DeviceRoles struct {
|
2021-05-26 11:03:05 +02:00
|
|
|
Next string `json:"next"`
|
|
|
|
Previous string `json:"previous"`
|
|
|
|
Results []DeviceRole `json:"results"`
|
2022-02-09 09:59:14 +01:00
|
|
|
Count int `json:"count"`
|
2021-05-26 11:03:05 +02:00
|
|
|
}
|
|
|
|
|
2022-02-09 09:59:14 +01:00
|
|
|
// DeviceRole is a role of a device
|
2021-05-26 11:03:05 +02:00
|
|
|
type DeviceRole struct {
|
2022-02-09 09:59:14 +01:00
|
|
|
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"`
|
2021-05-26 11:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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"`
|
2022-02-09 09:59:14 +01:00
|
|
|
|
|
|
|
Offset int64 `schema:"offset,omitempty"`
|
|
|
|
Limit int64 `schema:"limit,omitempty"`
|
2021-05-26 11:03:05 +02:00
|
|
|
}
|
|
|
|
|
2021-11-27 23:31:44 +01:00
|
|
|
const deviceRolesPath = dcimPath + "/device-roles"
|
2021-05-26 11:03:05 +02:00
|
|
|
|
2021-11-26 11:09:27 +01:00
|
|
|
// 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
|
2021-05-26 11:03:05 +02:00
|
|
|
var err error
|
|
|
|
|
2021-11-26 11:09:27 +01:00
|
|
|
encoder := schema.NewEncoder()
|
2021-05-26 11:03:05 +02:00
|
|
|
|
2021-11-26 11:09:27 +01:00
|
|
|
form := url.Values{}
|
|
|
|
err = encoder.Encode(f, form)
|
2021-05-26 11:03:05 +02:00
|
|
|
if err != nil {
|
2021-11-26 11:09:27 +01:00
|
|
|
return &deviceroles, err
|
2021-05-26 11:03:05 +02:00
|
|
|
}
|
2021-11-26 11:09:27 +01:00
|
|
|
query = form.Encode()
|
2021-05-26 11:03:05 +02:00
|
|
|
|
2021-11-26 11:09:27 +01:00
|
|
|
req, err = s.client.newRequest(ctx, "GET", deviceRolesPath, query, nil)
|
2021-05-26 11:03:05 +02:00
|
|
|
if err != nil {
|
2021-11-26 11:09:27 +01:00
|
|
|
return &deviceroles, err
|
2021-05-26 11:03:05 +02:00
|
|
|
}
|
|
|
|
|
2021-11-26 11:09:27 +01:00
|
|
|
_, err = s.client.do(req, &deviceroles)
|
2021-05-26 11:03:05 +02:00
|
|
|
if err != nil {
|
2021-11-26 11:09:27 +01:00
|
|
|
return &deviceroles, err
|
2021-05-26 11:03:05 +02:00
|
|
|
}
|
2021-11-26 11:09:27 +01:00
|
|
|
|
|
|
|
return &deviceroles, nil
|
2021-05-26 11:03:05 +02:00
|
|
|
}
|
2021-11-26 11:09:27 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
// }
|