Implement ipam_vrf_list and ipam_vrf_create.

This commit is contained in:
Kalle Carlbark 2019-09-25 14:42:29 +02:00
parent 620a6cf71f
commit eadad7c1fd
No known key found for this signature in database
GPG key ID: 3FC0C93C5A5A0670

161
netbox_vrfs.go Normal file
View file

@ -0,0 +1,161 @@
package netboxgo
import (
"bytes"
"crypto/tls"
"encoding/json"
// "fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
// IpamVrfsCreate is used for the return values from Netbox API ipam_vrfs_create
type IpamVrfsCreate struct {
Name string `json:"name"`
Rd string `json:"rd"`
Tenant int `json:"tenant"`
EnforceUnique bool `json:"enforce_unique"`
Description string `json:"description"`
Tags []string `json:"tags"`
CustomFields struct {
} `json:"custom_fields"`
}
// IpamVrfsList is used for the return value from NetBox API ipam_vrfs_list
type IpamVrfsList struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []struct {
ID int `json:"id"`
Name string `json:"name"`
Rd string `json:"rd"`
Tenant struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"tenant"`
EnforceUnique bool `json:"enforce_unique"`
Description string `json:"description"`
Tags []string `json:"tags"`
DisplayName string `json:"display_name"`
CustomFields struct {
} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
IpaddressCount int `json:"ipaddress_count"`
PrefixCount int `json:"prefix_count"`
} `json:"results"`
}
// VrfFilter is used to filter out returned object from Netbox API ipam_vrfs_list
type VrfFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
//User specific filters
Name string `schema:"name,omitempty"`
RD string `schema:"rd,omitempty"`
EnforceUnique string `schema:"enforce_unique,omitempty"`
TenantGroupID string `schema:"tenant_group_id,omitempty"`
TenantGroup string `schema:"tenant_group,omitempty"`
TenantID string `schema:"tenant_id,omitempty"`
Tenant string `schema:"tenant,omitempty"`
IDIn string `schema:"id__in,omitempty"`
Q string `schema:"q,omitempty"`
Tag string `schema:"tag,omitempty"`
}
// ListVrfs returns Netbox ipam_vrfs_list
func (n *NetBox) ListVrfs(i *IpamVrfsList, f *VrfFilter) 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/ipam/vrfs/?"+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
}
// CreateVrfs creates interfaces via Netbox API ipam_vrfs_create
func (n *NetBox) CreateVrfs(i *IpamVrfsCreate) 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/ipam/vrfs/", 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)
}