netboxgo/device_roles.go

112 lines
2.7 KiB
Go
Raw Normal View History

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"
"github.com/gorilla/schema"
)
2021-11-26 11:09:27 +01:00
type DeviceRolesService service
type DeviceRoles struct {
2021-05-26 11:03:05 +02:00
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []DeviceRole `json:"results"`
}
type DeviceRole struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
VMRole bool `json:"vm_role"`
Description string `json:"description"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
}
// DeviceTypeFilter is used to filter dcim_device_roles query to the Netbox API
type DeviceRoleFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
// 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"`
}
2021-11-26 11:09:27 +01:00
const deviceRolesPath = "/dcim/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
// }