netboxgo/rear_ports.go

85 lines
2 KiB
Go
Raw Normal View History

2020-09-03 13:52:52 +02:00
package netboxgo
import (
2021-11-26 11:09:27 +01:00
"context"
2020-09-03 13:52:52 +02:00
"net/http"
"net/url"
"github.com/gorilla/schema"
)
2021-11-26 11:09:27 +01:00
type RearPortsService service
type RearPorts struct {
2020-09-03 13:52:52 +02:00
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 = dcimPath + "/rear-ports"
2020-09-03 13:52:52 +02:00
2021-11-26 11:09:27 +01:00
// 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
2020-09-03 13:52:52 +02:00
var err error
2021-11-26 11:09:27 +01:00
encoder := schema.NewEncoder()
2020-09-03 13:52:52 +02:00
2021-11-26 11:09:27 +01:00
form := url.Values{}
err = encoder.Encode(f, form)
2020-09-03 13:52:52 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &rearports, err
2020-09-03 13:52:52 +02:00
}
2021-11-26 11:09:27 +01:00
query = form.Encode()
2020-09-03 13:52:52 +02:00
2021-11-26 11:09:27 +01:00
req, err = s.client.newRequest(ctx, "GET", rearPortsPath, query, nil)
2020-09-03 13:52:52 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &rearports, err
2020-09-03 13:52:52 +02:00
}
2021-11-26 11:09:27 +01:00
_, err = s.client.do(req, &rearports)
2020-09-03 13:52:52 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &rearports, err
2020-09-03 13:52:52 +02:00
}
2021-11-26 11:09:27 +01:00
return &rearports, nil
2020-09-03 13:52:52 +02:00
}