netboxgo/sites.go

193 lines
4.9 KiB
Go
Raw Normal View History

2021-06-01 22:53:19 +02:00
package netboxgo
import (
2021-11-26 11:09:27 +01:00
"context"
"fmt"
2021-06-01 22:53:19 +02:00
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
)
2021-11-26 11:09:27 +01:00
type SitesService service
type Sites struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []Site `json:"results"`
2021-06-01 22:53:19 +02:00
}
2021-11-26 11:09:27 +01:00
type Site struct {
2021-06-01 22:53:19 +02:00
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"`
2021-11-26 11:09:27 +01:00
CustomFields struct{} `json:"custom_fields"`
2021-06-01 22:53:19 +02:00
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"`
}
2021-11-26 11:09:27 +01:00
const sitesPath = "/dcim/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
2021-06-01 22:53:19 +02:00
encoder := schema.NewEncoder()
form := url.Values{}
2021-11-26 11:09:27 +01:00
err = encoder.Encode(f, form)
2021-06-01 22:53:19 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &sites, err
2021-06-01 22:53:19 +02:00
}
2021-11-26 11:09:27 +01:00
query = form.Encode()
2021-06-01 22:53:19 +02:00
2021-11-26 11:09:27 +01:00
req, err = s.client.newRequest(ctx, "GET", sitesPath, query, nil)
2021-06-01 22:53:19 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &sites, err
2021-06-01 22:53:19 +02:00
}
2021-11-26 11:09:27 +01:00
_, err = s.client.do(req, &sites)
2021-06-01 22:53:19 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &sites, err
2021-06-01 22:53:19 +02:00
}
2021-11-26 11:09:27 +01:00
return &sites, nil
}
2021-06-01 22:53:19 +02:00
2021-11-26 11:09:27 +01:00
// Get a Site based on id
func (s *SitesService) Get(ctx context.Context, id string) (*Site, error) {
var site Site
var err error
2021-06-01 22:53:19 +02:00
2021-11-26 11:09:27 +01:00
path := fmt.Sprintf("%s/%s", sitesPath, id)
req, err := s.client.newRequest(ctx, "GET", path, "", nil)
2021-06-01 22:53:19 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &site, err
2021-06-01 22:53:19 +02:00
}
2021-11-26 11:09:27 +01:00
_, err = s.client.do(req, &site)
2021-06-01 22:53:19 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &site, err
2021-06-01 22:53:19 +02:00
}
2021-11-26 11:09:27 +01:00
return &site, err
2021-06-01 22:53:19 +02:00
}
2021-11-26 11:09:27 +01:00
// // Create a site
// func (s *SitesService) Create(ctx context.Context, c *NewSite) error {
// var err error
// var req *http.Request
2021-06-01 22:53:19 +02:00
2021-11-26 11:09:27 +01:00
// req, err = s.client.newRequest(ctx, "POST", sitesPath, "", c)
// if err != nil {
// return fmt.Errorf("unable to create request: %w", err)
// }
2021-06-01 22:53:19 +02:00
2021-11-26 11:09:27 +01:00
// _, err = s.client.do(req, nil)
// if err != nil {
// return fmt.Errorf("unable to do request: %w", err)
// }
2021-06-01 22:53:19 +02:00
2021-11-26 11:09:27 +01:00
// return nil
// }
2021-06-01 22:53:19 +02:00
2021-11-26 11:09:27 +01:00
// // 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
// }