netboxgo/platforms.go
2022-02-09 10:43:59 +01:00

103 lines
2.9 KiB
Go

package netboxgo
import (
"context"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
)
type PlatformsService service
type Platforms struct {
Count int `json:"count"`
Next interface{} `json:"next"`
Previous interface{} `json:"previous"`
Results []Platform `json:"results"`
}
type Platform struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
Manufacturer struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
DevicetypeCount int `json:"devicetype_count"`
} `json:"manufacturer"`
NapalmDriver string `json:"napalm_driver"`
NapalmArgs string `json:"napalm_args"`
Description string `json:"description"`
Tags []struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
} `json:"tags"`
CustomFields struct{} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
}
// PlatformFilter is used to filter out platforms
type PlatformFilter struct {
// User specific filters
ID string `schema:"id,omitempty"`
Name string `schema:"name,omitempty"`
Slug string `schema:"slug,omitempty"`
Description string `schema:"description,omitempty"`
Created string `schema:"created,omitempty"`
CreatedGTE string `schema:"created__gte,omitempty"`
CreatedLTE string `schema:"created__lte,omitempty"`
LastUpdated string `schema:"last_updated,omitempty"`
LastUpdatedGTE string `schema:"last_updated__gte,omitempty"`
LastUpdatedLTE string `schema:"last_updated__lte,omitempty"`
IDIn string `schema:"id__in,omitempty"`
Q string `schema:"q,omitempty"`
Tag string `schema:"tag,omitempty"`
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
}
const platformsPath = dcimPath + "/platforms"
// List platforms. PlatformFilter is used to list based on filter queries.
func (s *PlatformsService) List(ctx context.Context, f *PlatformFilter) (*Platforms, error) {
var platforms Platforms
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 &platforms, err
}
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", platformsPath, query, nil)
if err != nil {
return &platforms, err
}
_, err = s.client.do(req, &platforms)
if err != nil {
return &platforms, err
}
return &platforms, nil
}