netboxgo/rear_ports.go
2021-11-26 11:09:27 +01:00

84 lines
2 KiB
Go

package netboxgo
import (
"context"
"net/http"
"net/url"
"github.com/gorilla/schema"
)
type RearPortsService service
type RearPorts struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []struct {
ID int `json:"id"`
Device struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
} `json:"device"`
Name string `json:"name"`
Type struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"type"`
Positions int `json:"positions"`
Description string `json:"description"`
Cable struct {
ID int `json:"id"`
URL string `json:"url"`
Label string `json:"label"`
} `json:"cable"`
Tags []string `json:"tags"`
} `json:"results"`
}
// RearPortFilter is used to filter dcim_rearport_query to the Netbox API
type RearPortFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
// User specific filters
ID string `schema:"id,omitempty"`
Name string `schema:"name,omitempty"`
Type string `schema:"type,omitempty"`
Positions string `schema:"positions,omitempty"`
Description string `schema:"description,omitempty"`
Device string `schema:"device,omitempty"`
}
const rearPortsPath = "/dcim/rear-ports"
// List rearports. RearPortFilter is used to list based on filter queries.
func (s *RearPortsService) List(ctx context.Context, f *RearPortFilter) (*RearPorts, error) {
var rearports RearPorts
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 &rearports, err
}
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", rearPortsPath, query, nil)
if err != nil {
return &rearports, err
}
_, err = s.client.do(req, &rearports)
if err != nil {
return &rearports, err
}
return &rearports, nil
}