151 lines
3.7 KiB
Go
151 lines
3.7 KiB
Go
package netboxgo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/gorilla/schema"
|
|
)
|
|
|
|
type SiteGroupsService service
|
|
|
|
type SiteGroups struct {
|
|
Count int `json:"count"`
|
|
Next string `json:"next"`
|
|
Previous string `json:"previous"`
|
|
Results []SiteGroup `json:"results"`
|
|
}
|
|
|
|
type SiteGroup struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Display string `json:"display"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
Parent struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Display string `json:"display"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
SiteCount int `json:"site_count"`
|
|
Depth int `json:"_depth"`
|
|
} `json:"parent"`
|
|
Description string `json:"description"`
|
|
CustomFields interface{} `json:"custom_fields"`
|
|
Created string `json:"created"`
|
|
LastUpdated time.Time `json:"last_updated"`
|
|
SiteCount int `json:"site_count"`
|
|
Depth int `json:"_depth"`
|
|
}
|
|
|
|
// SiteFilter is used to filter out returned object from Netbox API tenancy_tenants_list
|
|
type SiteGroupFilter struct {
|
|
Offset int64 `schema:"offset,omitempty"`
|
|
Limit int64 `schema:"limit,omitempty"`
|
|
|
|
// User specific filters
|
|
Name string `schema:"name,omitempty"`
|
|
ID string `schema:"id,omitempty"`
|
|
Slug string `schema:"slug,omitempty"`
|
|
Description string `schema:"description,omitempty"`
|
|
Group string `schema:"group,omitempty"`
|
|
GroupID string `schema:"group_id,omitempty"`
|
|
Parent string `schema:"parent,omitempty"`
|
|
IDIn string `schema:"id__in,omitempty"`
|
|
Q string `schema:"q,omitempty"`
|
|
Tag string `schema:"tag,omitempty"`
|
|
}
|
|
|
|
const siteGroupsPath = dcimPath + "/site-groups"
|
|
|
|
// List sitegroups. SiteGroupFilter is used to list based on filter queries.
|
|
func (s *SiteGroupsService) List(ctx context.Context, f *SiteGroupFilter) (*SiteGroups, error) {
|
|
var sitegroups SiteGroups
|
|
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 &sitegroups, err
|
|
}
|
|
query = form.Encode()
|
|
|
|
req, err = s.client.newRequest(ctx, "GET", siteGroupsPath, query, nil)
|
|
if err != nil {
|
|
return &sitegroups, err
|
|
}
|
|
|
|
_, err = s.client.do(req, &sitegroups)
|
|
if err != nil {
|
|
return &sitegroups, err
|
|
}
|
|
|
|
return &sitegroups, nil
|
|
}
|
|
|
|
// Create a sitegroup
|
|
// func (s *SiteGroupsService) Create(ctx context.Context, c *NewSiteGroup) error {
|
|
// var err error
|
|
// var req *http.Request
|
|
//
|
|
// req, err = s.client.newRequest(ctx, "POST", siteGroupsPath, "", 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
|
|
// }
|
|
|
|
// Get a SiteGroup based on id
|
|
func (s *SiteGroupsService) Get(ctx context.Context, id string) (*SiteGroup, error) {
|
|
var sitegroup SiteGroup
|
|
|
|
var err error
|
|
|
|
path := fmt.Sprintf("%s/%s", siteGroupsPath, id)
|
|
|
|
req, err := s.client.newRequest(ctx, "GET", path, "", nil)
|
|
if err != nil {
|
|
return &sitegroup, err
|
|
}
|
|
|
|
_, err = s.client.do(req, &sitegroup)
|
|
if err != nil {
|
|
return &sitegroup, err
|
|
}
|
|
|
|
return &sitegroup, err
|
|
}
|
|
|
|
// // Update a device
|
|
// func (s *SiteGroupsService) Update(ctx context.Context, id string, c *UpdateDevice) error {
|
|
// var req *http.Request
|
|
// var err error
|
|
|
|
// path := fmt.Sprintf("%s/%s", siteGroupsPath, 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
|
|
// }
|