146 lines
4 KiB
Go
146 lines
4 KiB
Go
|
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 {
|
||
|
Name string `json:"name"`
|
||
|
Slug string `json:"slug"`
|
||
|
Group int `json:"group"`
|
||
|
Description string `json:"description"`
|
||
|
Comments string `json:"comments"`
|
||
|
Tags []string `json:"tags"`
|
||
|
CustomFields struct{} `json:"custom_fields"`
|
||
|
}
|
||
|
|
||
|
// Tenants is used for the return value from NetBox API tenancy_tenants_list
|
||
|
type Tenants struct {
|
||
|
Count int `json:"count"`
|
||
|
Next string `json:"next"`
|
||
|
Previous interface{} `json:"previous"`
|
||
|
Results []struct {
|
||
|
ID int `json:"id"`
|
||
|
Name string `json:"name"`
|
||
|
Slug string `json:"slug"`
|
||
|
Group struct {
|
||
|
ID int `json:"id"`
|
||
|
URL string `json:"url"`
|
||
|
Name string `json:"name"`
|
||
|
Slug string `json:"slug"`
|
||
|
} `json:"group"`
|
||
|
Description string `json:"description"`
|
||
|
Comments string `json:"comments"`
|
||
|
Tags []interface{} `json:"tags"`
|
||
|
CustomFields struct {
|
||
|
AccountNumber string `json:"account_number"`
|
||
|
AtlasCustNumber interface{} `json:"atlas_cust_number"`
|
||
|
TextField interface{} `json:"text_field"`
|
||
|
} `json:"custom_fields"`
|
||
|
Created string `json:"created"`
|
||
|
LastUpdated time.Time `json:"last_updated"`
|
||
|
CircuitCount interface{} `json:"circuit_count"`
|
||
|
DeviceCount int `json:"device_count"`
|
||
|
IpaddressCount interface{} `json:"ipaddress_count"`
|
||
|
PrefixCount int `json:"prefix_count"`
|
||
|
RackCount interface{} `json:"rack_count"`
|
||
|
SiteCount int `json:"site_count"`
|
||
|
VirtualmachineCount interface{} `json:"virtualmachine_count"`
|
||
|
VlanCount int `json:"vlan_count"`
|
||
|
VrfCount interface{} `json:"vrf_count"`
|
||
|
} `json:"results"`
|
||
|
}
|
||
|
|
||
|
// TenantFilter is used to filter out returned object from Netbox API tenancy_tenants_list
|
||
|
type TenantFilter struct {
|
||
|
Offset int64 `schema:"offset,omitempty"`
|
||
|
Limit int64 `schema:"limit,omitempty"`
|
||
|
|
||
|
// 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"`
|
||
|
}
|
||
|
|
||
|
const tenantsPath = "/tenancy/tenants"
|
||
|
|
||
|
// 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
|
||
|
}
|
||
|
|
||
|
// 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
|
||
|
// }
|