add vlans and prefixes fixes.

This commit is contained in:
Kalle Carlbark 2020-02-05 15:02:32 +01:00
parent c209cdd715
commit a94cdc3159
No known key found for this signature in database
GPG key ID: 5E4BAB01A57CDD62
2 changed files with 197 additions and 4 deletions

View file

@ -97,7 +97,7 @@ type PrefixFilter struct {
Status string `schema:"status,omitempty"` Status string `schema:"status,omitempty"`
} }
// ListVrfs returns Netbox ipam_vrfs_list // ListVrfs returns Netbox ipam_prefixes_list
func (n *NetBox) ListPrefixes(i *IpamPrefixesList, f *PrefixFilter) error { func (n *NetBox) ListPrefixes(i *IpamPrefixesList, f *PrefixFilter) error {
var encoder = schema.NewEncoder() var encoder = schema.NewEncoder()
@ -116,7 +116,7 @@ func (n *NetBox) ListPrefixes(i *IpamPrefixesList, f *PrefixFilter) error {
Timeout: timeout, Timeout: timeout,
Transport: transport, Transport: transport,
} }
request, err := http.NewRequest("GET", n.RootURL+"/api/ipam/vrfs/?"+query, nil) request, err := http.NewRequest("GET", n.RootURL+"/api/ipam/prefixes/?"+query, nil)
if err != nil { if err != nil {
return err return err
} }
@ -148,7 +148,7 @@ func (n *NetBox) ListPrefixes(i *IpamPrefixesList, f *PrefixFilter) error {
return nil return nil
} }
// CreateVrfs creates interfaces via Netbox API ipam_vrfs_create // CreateVrfs creates interfaces via Netbox API ipam_prefixes_create
func (n *NetBox) CreatePrefixes(i *IpamPrefixesCreate) error { func (n *NetBox) CreatePrefixes(i *IpamPrefixesCreate) error {
vrfData, err := json.Marshal(i) vrfData, err := json.Marshal(i)
if err != nil { if err != nil {
@ -162,7 +162,7 @@ func (n *NetBox) CreatePrefixes(i *IpamPrefixesCreate) error {
Timeout: timeout, Timeout: timeout,
Transport: transport, Transport: transport,
} }
request, err := http.NewRequest("POST", n.RootURL+"/api/ipam/vrfs/", bytes.NewBuffer(vrfData)) request, err := http.NewRequest("POST", n.RootURL+"/api/ipam/prefixes/", bytes.NewBuffer(vrfData))
if err != nil { if err != nil {
return err return err
} }

193
netbox_vlans.go Normal file
View file

@ -0,0 +1,193 @@
package netboxgo
import (
"bytes"
"crypto/tls"
"encoding/json"
// "fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
// IpamVlansCreate is used for the return values from Netbox API ipam_vlans_create
type IpamVlansCreate struct {
Site int `json:"site"`
Group int `json:"group"`
Vid int `json:"vid"`
Name string `json:"name"`
Tenant int `json:"tenant"`
Status int `json:"status"`
Role int `json:"role"`
Description string `json:"description"`
Tags []string `json:"tags"`
CustomFields struct {
} `json:"custom_fields"`
}
// IpamVlanList is used for the return value from NetBox API ipam_vlans_list
type IpamVlansList struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []struct {
ID int `json:"id"`
Site struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"site"`
Group struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
VlanCount int `json:"vlan_count"`
} `json:"group"`
Vid int `json:"vid"`
Name string `json:"name"`
Tenant struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"tenant"`
Status struct {
Label string `json:"label"`
Value int `json:"value"`
} `json:"status"`
Role struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
PrefixCount int `json:"prefix_count"`
VlanCount int `json:"vlan_count"`
} `json:"role"`
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"`
PrefixCount int `json:"prefix_count"`
} `json:"results"`
}
// VlanFilter is used to filter out returned object from Netbox API ipam_vlans_list
type VlanFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
//User specific filters
Vid string `schema:"vid,omitempty"`
Name string `schema:"name,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"`
SiteID string `schema:"site_id,omitempty"`
Site string `schema:"site,omitempty"`
GroupID string `schema:"group_id,omitempty"`
Group string `schema:"group,omitempty"`
RoleID string `schema:"role_id,omitempty"`
Role string `schema:"role,omitempty"`
Status string `schema:"status,omitempty"`
Tag string `schema:"tag,omitempty"`
}
// ListVlans returns Netbox ipam_vlans_list
func (n *NetBox) ListVlans(i *IpamVlansList, f *VlanFilter) 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/vlans/?"+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
}
// CreateVlans creates interfaces via Netbox API ipam_vlans_create
func (n *NetBox) CreateVlans(i *IpamVlansCreate) 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/vlans/", 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)
}