Add tenancy functionality.

This commit is contained in:
Kalle Carlbark 2020-02-04 16:39:05 +01:00
parent c7a2b3a7a7
commit c209cdd715

164
netbox_tenants.go Normal file
View file

@ -0,0 +1,164 @@
package netboxgo
import (
"bytes"
"crypto/tls"
"encoding/json"
// "fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
// TenancyTenantsCreate is used for the return values from Netbox API tenancy_tenants_create
type TenancyTenantsCreate 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"`
}
// TenancyTenantsList is used for the return value from NetBox API tenancy_tenants_list
type TenancyTenantsList 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 {
} `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 interface{} `json:"prefix_count"`
RackCount interface{} `json:"rack_count"`
SiteCount int `json:"site_count"`
VirtualmachineCount interface{} `json:"virtualmachine_count"`
VlanCount interface{} `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"`
}
// ListTenants returns Netbox tenancy_tenants_list
func (n *NetBox) ListTenants(i *TenancyTenantsList, f *TenantFilter) error {
var encoder = schema.NewEncoder()
form := url.Values{}
err := encoder.Encode(f, form)
if err != nil {
return err
}
query := form.Encode()
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
}
timeout := time.Duration(60 * time.Second)
client := &http.Client{
Timeout: timeout,
Transport: transport,
}
request, err := http.NewRequest("GET", n.RootURL+"/api/tenancy/tenants/?"+query, nil)
if err != nil {
return err
}
request.Header.Add("Accept", "application/json")
request.Header.Add("Authorization", " Token "+n.Token)
response, err := client.Do(request)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusOK)
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
err = response.Body.Close()
if err != nil {
return err
}
err = json.Unmarshal(data, &i)
if err != nil {
return err
}
return nil
}
// CreateTenants creates interfaces via Netbox API tenancy_tenants_create
func (n *NetBox) CreateTenants(i *TenancyTenantsCreate) error {
vrfData, err := json.Marshal(i)
if err != nil {
return err
}
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
}
timeout := time.Duration(60 * time.Second)
client := &http.Client{
Timeout: timeout,
Transport: transport,
}
request, err := http.NewRequest("POST", n.RootURL+"/api/tenancy/tenants/", bytes.NewBuffer(vrfData))
if err != nil {
return err
}
request.Header.Add("Accept", "application/json")
request.Header.Add("Content-Type", "application/json")
request.Header.Add("Authorization", " Token "+n.Token)
response, err := client.Do(request)
if err != nil {
return err
}
err = response.Body.Close()
if err != nil {
return err
}
if response.StatusCode == http.StatusCreated {
return nil
}
return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusCreated)
}