2021-11-26 11:09:27 +01:00
|
|
|
package netboxgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TenantsService service
|
|
|
|
|
|
|
|
// NewTenant is used for the return values from Netbox API tenancy_tenants_create
|
|
|
|
type NewTenant struct {
|
2021-12-19 21:24:29 +01:00
|
|
|
CustomFields interface{} `json:"custom_fields"`
|
2021-11-29 11:38:01 +01:00
|
|
|
Slug string `json:"slug"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Comments string `json:"comments"`
|
2021-12-19 21:24:29 +01:00
|
|
|
Name string `json:"name"`
|
2021-11-29 11:38:01 +01:00
|
|
|
Tags []string `json:"tags"`
|
2021-12-19 21:24:29 +01:00
|
|
|
Group int `json:"group"`
|
2021-11-26 11:09:27 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 21:24:29 +01:00
|
|
|
// Tenant represents a tenant
|
|
|
|
type Tenant struct {
|
2022-01-31 16:38:37 +01:00
|
|
|
LastUpdated time.Time `json:"last_updated"`
|
|
|
|
CustomFields map[string]interface{} `json:"custom_fields"`
|
|
|
|
Created string `json:"created"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Comments string `json:"comments"`
|
|
|
|
Group struct {
|
2021-12-19 21:24:29 +01:00
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
} `json:"group"`
|
2022-01-31 16:38:37 +01:00
|
|
|
Tags []interface{} `json:"tags"`
|
|
|
|
VRFCount int `json:"vrf_count"`
|
|
|
|
CircuitCount int `json:"circuit_count"`
|
|
|
|
IPAddressCount int `json:"ipaddress_count"`
|
|
|
|
VirtualmachineCount int `json:"virtualmachine_count"`
|
|
|
|
RackCount int `json:"rack_count"`
|
|
|
|
DeviceCount int `json:"device_count"`
|
|
|
|
PrefixCount int `json:"prefix_count"`
|
|
|
|
SiteCount int `json:"site_count"`
|
|
|
|
VLANCount int `json:"vlan_count"`
|
|
|
|
ID int `json:"id"`
|
2021-12-19 21:24:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tenants is represents a list of multiple tenants
|
2021-11-26 11:09:27 +01:00
|
|
|
type Tenants struct {
|
|
|
|
Previous interface{} `json:"previous"`
|
2021-12-19 21:24:29 +01:00
|
|
|
Next string `json:"next"`
|
|
|
|
Results []Tenant `json:"results"`
|
|
|
|
Count int `json:"count"`
|
2021-11-26 11:09:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TenantFilter is used to filter out returned object from Netbox API tenancy_tenants_list
|
|
|
|
type TenantFilter struct {
|
|
|
|
// User specific filters
|
|
|
|
Name string `schema:"name,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-12-19 21:24:29 +01:00
|
|
|
|
|
|
|
Offset int `schema:"offset,omitempty"`
|
|
|
|
Limit int `schema:"limit,omitempty"`
|
2021-11-26 11:09:27 +01:00
|
|
|
}
|
|
|
|
|
2021-11-27 23:31:44 +01:00
|
|
|
const tenantsPath = tenancyPath + "/tenants"
|
2021-11-26 11:09:27 +01:00
|
|
|
|
|
|
|
// List tenants. TenantFilter is used to list based on filter queries.
|
|
|
|
func (s *TenantsService) List(ctx context.Context, f *TenantFilter) (*Tenants, error) {
|
|
|
|
var tenants Tenants
|
|
|
|
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 &tenants, err
|
|
|
|
}
|
|
|
|
query = form.Encode()
|
|
|
|
|
|
|
|
req, err = s.client.newRequest(ctx, "GET", tenantsPath, query, nil)
|
|
|
|
if err != nil {
|
|
|
|
return &tenants, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.client.do(req, &tenants)
|
|
|
|
if err != nil {
|
|
|
|
return &tenants, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tenants, nil
|
|
|
|
}
|
|
|
|
|
2021-12-19 21:24:29 +01:00
|
|
|
// ListAll tenants. TenantFilter is used to list all tenants based on filters
|
|
|
|
func (s *TenantsService) ListAll(ctx context.Context, f *TenantFilter) (*Tenants, error) {
|
|
|
|
var all Tenants
|
|
|
|
var r *Tenants
|
|
|
|
var num int
|
|
|
|
var err error
|
|
|
|
|
|
|
|
f.Limit = 1
|
|
|
|
r, err = s.List(ctx, f)
|
|
|
|
if err != nil {
|
|
|
|
return &all, fmt.Errorf("unable to count prefixes with filter %+v: %w", f, err)
|
|
|
|
}
|
|
|
|
num = r.Count
|
|
|
|
|
|
|
|
for count := 0; count < num; count += 1000 {
|
|
|
|
f.Limit = 1000
|
|
|
|
f.Offset = count
|
|
|
|
|
|
|
|
r, err = s.List(ctx, f)
|
|
|
|
if err != nil {
|
|
|
|
return &all, fmt.Errorf("unable to list prefixes with filter %+v: %w", f, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
all.Results = append(all.Results, r.Results...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &all, nil
|
|
|
|
}
|
|
|
|
|
2021-11-26 11:09:27 +01:00
|
|
|
// Create a tenant
|
|
|
|
func (s *TenantsService) Create(ctx context.Context, c *NewTenant) error {
|
|
|
|
var err error
|
|
|
|
var req *http.Request
|
|
|
|
|
|
|
|
req, err = s.client.newRequest(ctx, "POST", tenantsPath, "", 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 *TenantsService) Update(ctx context.Context, id string, c *UpdateDevice) error {
|
|
|
|
// var req *http.Request
|
|
|
|
// var err error
|
|
|
|
|
|
|
|
// path := fmt.Sprintf("%s/%s", tenantsPath, 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
|
|
|
|
// }
|