192 lines
4.9 KiB
Go
192 lines
4.9 KiB
Go
package netboxgo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/gorilla/schema"
|
|
)
|
|
|
|
type SitesService service
|
|
|
|
type Sites struct {
|
|
Count int `json:"count"`
|
|
Next string `json:"next"`
|
|
Previous string `json:"previous"`
|
|
Results []Site `json:"results"`
|
|
}
|
|
|
|
type Site struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Display string `json:"display"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
Status struct {
|
|
Label string `json:"label"`
|
|
Value string `json:"value"`
|
|
} `json:"status"`
|
|
Region 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:"region"`
|
|
Group 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:"group"`
|
|
Tenant struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Display string `json:"display"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
} `json:"tenant"`
|
|
Facility string `json:"facility"`
|
|
Asn int `json:"asn"`
|
|
TimeZone string `json:"time_zone"`
|
|
Description string `json:"description"`
|
|
PhysicalAddress string `json:"physical_address"`
|
|
ShippingAddress string `json:"shipping_address"`
|
|
Latitude string `json:"latitude"`
|
|
Longitude string `json:"longitude"`
|
|
ContactName string `json:"contact_name"`
|
|
ContactPhone string `json:"contact_phone"`
|
|
ContactEmail string `json:"contact_email"`
|
|
Comments string `json:"comments"`
|
|
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 interface{} `json:"custom_fields"`
|
|
Created string `json:"created"`
|
|
LastUpdated time.Time `json:"last_updated"`
|
|
CircuitCount int `json:"circuit_count"`
|
|
DeviceCount int `json:"device_count"`
|
|
PrefixCount int `json:"prefix_count"`
|
|
RackCount int `json:"rack_count"`
|
|
VirtualmachineCount int `json:"virtualmachine_count"`
|
|
VlanCount int `json:"vlan_count"`
|
|
}
|
|
|
|
// SiteFilter is used to filter out returned object from Netbox API tenancy_tenants_list
|
|
type SiteFilter 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"`
|
|
Group string `schema:"group,omitempty"`
|
|
GroupID string `schema:"group_id,omitempty"`
|
|
IDIn string `schema:"id__in,omitempty"`
|
|
Q string `schema:"q,omitempty"`
|
|
Tag string `schema:"tag,omitempty"`
|
|
}
|
|
|
|
const sitesPath = dcimPath + "/sites"
|
|
|
|
// List sites. SiteFilter is used to list based on filter queries.
|
|
func (s *SitesService) List(ctx context.Context, f *SiteFilter) (*Sites, error) {
|
|
var sites Sites
|
|
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 &sites, err
|
|
}
|
|
query = form.Encode()
|
|
|
|
req, err = s.client.newRequest(ctx, "GET", sitesPath, query, nil)
|
|
if err != nil {
|
|
return &sites, err
|
|
}
|
|
|
|
_, err = s.client.do(req, &sites)
|
|
if err != nil {
|
|
return &sites, err
|
|
}
|
|
|
|
return &sites, nil
|
|
}
|
|
|
|
// Get a Site based on id
|
|
func (s *SitesService) Get(ctx context.Context, id string) (*Site, error) {
|
|
var site Site
|
|
|
|
var err error
|
|
|
|
path := fmt.Sprintf("%s/%s", sitesPath, id)
|
|
|
|
req, err := s.client.newRequest(ctx, "GET", path, "", nil)
|
|
if err != nil {
|
|
return &site, err
|
|
}
|
|
|
|
_, err = s.client.do(req, &site)
|
|
if err != nil {
|
|
return &site, err
|
|
}
|
|
|
|
return &site, err
|
|
}
|
|
|
|
// // Create a site
|
|
// func (s *SitesService) Create(ctx context.Context, c *NewSite) error {
|
|
// var err error
|
|
// var req *http.Request
|
|
|
|
// req, err = s.client.newRequest(ctx, "POST", sitesPath, "", 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
|
|
// func (s *SitesService) Update(ctx context.Context, id string, c *UpdateDevice) error {
|
|
// var req *http.Request
|
|
// var err error
|
|
|
|
// path := fmt.Sprintf("%s/%s", sitesPath, 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
|
|
// }
|