Add netbox_prefixes functionality.
This commit is contained in:
parent
2c8bee4804
commit
c7a2b3a7a7
1 changed files with 185 additions and 0 deletions
185
netbox_prefixes.go
Normal file
185
netbox_prefixes.go
Normal file
|
@ -0,0 +1,185 @@
|
|||
package netboxgo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
|
||||
// "fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/schema"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// IpamPrefixesCreate is used for the return values from Netbox API ipam_prefixes_create
|
||||
type IpamPrefixesCreate struct {
|
||||
Prefix string `json:"prefix"`
|
||||
Site int `json:"site"`
|
||||
Vrf int `json:"vrf"`
|
||||
Tenant int `json:"tenant"`
|
||||
Vlan int `json:"vlan"`
|
||||
Status int `json:"status"`
|
||||
Role int `json:"role"`
|
||||
IsPool bool `json:"is_pool"`
|
||||
Description string `json:"description"`
|
||||
Tags []string `json:"tags"`
|
||||
CustomFields struct {
|
||||
} `json:"custom_fields"`
|
||||
}
|
||||
|
||||
// IpamPrefixList is used for the return value from NetBox API ipam_prefixes_list
|
||||
type IpamPrefixesList struct {
|
||||
Count int `json:"count"`
|
||||
Next string `json:"next"`
|
||||
Previous interface{} `json:"previous"`
|
||||
Results []struct {
|
||||
ID int `json:"id"`
|
||||
Family struct {
|
||||
Value int `json:"value"`
|
||||
Label string `json:"label"`
|
||||
} `json:"family"`
|
||||
Prefix string `json:"prefix"`
|
||||
Site interface{} `json:"site"`
|
||||
Vrf interface{} `json:"vrf"`
|
||||
Tenant interface{} `json:"tenant"`
|
||||
Vlan interface{} `json:"vlan"`
|
||||
Status struct {
|
||||
Value int `json:"value"`
|
||||
Label string `json:"label"`
|
||||
} `json:"status"`
|
||||
Role interface{} `json:"role"`
|
||||
IsPool bool `json:"is_pool"`
|
||||
Description string `json:"description"`
|
||||
Tags []interface{} `json:"tags"`
|
||||
CustomFields struct {
|
||||
IpamPrefixRouted interface{} `json:"ipam_prefix_routed"`
|
||||
IpamPrefixRoutedLastUpdate interface{} `json:"ipam_prefix_routed_last_update"`
|
||||
IpamPrefixExitpoint interface{} `json:"ipam_prefix_exitpoint"`
|
||||
} `json:"custom_fields"`
|
||||
Created string `json:"created"`
|
||||
LastUpdated time.Time `json:"last_updated"`
|
||||
} `json:"results"`
|
||||
}
|
||||
|
||||
// PrefixFilter is used to filter out returned object from Netbox API ipam_prefixes_list
|
||||
type PrefixFilter struct {
|
||||
Offset int64 `schema:"offset,omitempty"`
|
||||
Limit int64 `schema:"limit,omitempty"`
|
||||
|
||||
//User specific filters
|
||||
Family string `schema:"family,omitempty"`
|
||||
IsPool string `schema:"is_pool,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"`
|
||||
Prefix string `schema:"prefix,omitempty"`
|
||||
Within string `schema:"within,omitempty"`
|
||||
WithinInclude string `schema:"within_include,omitempty"`
|
||||
Contains string `schema:"contains,omitempty"`
|
||||
MaskLength string `schema:"mask_length,omitempty"`
|
||||
VrfId string `schema:"vrf_id,omitempty"`
|
||||
Vrf string `schema:"vrf,omitempty"`
|
||||
RegionId string `schema:"region_id,omitempty"`
|
||||
Region string `schema:"region,omitempty"`
|
||||
SiteID string `schema:"site_id,omitempty"`
|
||||
Site string `schema:"site,omitempty"`
|
||||
VlanID string `schema:"vlan_id,omitempty"`
|
||||
Role string `schema:"role,omitempty"`
|
||||
Status string `schema:"status,omitempty"`
|
||||
}
|
||||
|
||||
// ListVrfs returns Netbox ipam_vrfs_list
|
||||
func (n *NetBox) ListPrefixes(i *IpamPrefixesList, f *PrefixFilter) 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) CreatePrefixes(i *IpamPrefixesCreate) 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)
|
||||
}
|
Loading…
Reference in a new issue