2021-11-26 11:09:27 +01:00
|
|
|
package netboxgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
// "fmt"
|
|
|
|
|
|
|
|
"github.com/gorilla/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PrefixesService service
|
|
|
|
|
|
|
|
// NewPrefix is used for the return values from Netbox API ipam_prefixes_create
|
|
|
|
type NewPrefix struct {
|
2021-12-19 21:24:29 +01:00
|
|
|
CustomFields interface{} `json:"custom_fields"`
|
2021-11-29 11:38:01 +01:00
|
|
|
Prefix string `json:"prefix"`
|
2021-12-19 21:24:29 +01:00
|
|
|
Description string `json:"description"`
|
|
|
|
Tags []string `json:"tags"`
|
2021-11-29 11:38:01 +01:00
|
|
|
Site int `json:"site"`
|
2021-12-19 21:24:29 +01:00
|
|
|
Status int `json:"status"`
|
|
|
|
Role int `json:"role"`
|
2021-11-29 11:38:01 +01:00
|
|
|
Vrf int `json:"vrf"`
|
|
|
|
Tenant int `json:"tenant"`
|
|
|
|
VLAN int `json:"vlan"`
|
|
|
|
IsPool bool `json:"is_pool"`
|
2021-11-26 11:09:27 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 16:38:37 +01:00
|
|
|
// Prefix is a prefix
|
|
|
|
type Prefix struct {
|
|
|
|
LastUpdated time.Time `json:"last_updated"`
|
|
|
|
CustomFields interface{} `json:"custom_fields"`
|
|
|
|
VLAN struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
DisplayName string `json:"display_name"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
VID int `json:"vid"`
|
|
|
|
} `json:"vlan"`
|
|
|
|
Tenant struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
} `json:"tenant"`
|
|
|
|
Site struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
} `json:"site"`
|
|
|
|
Status struct {
|
|
|
|
Label string `json:"label"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"status"`
|
|
|
|
Prefix string `json:"prefix"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Created string `json:"created"`
|
|
|
|
VRF struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Rd string `json:"rd"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
PrefixCount int `json:"prefix_count"`
|
|
|
|
} `json:"vrf"`
|
|
|
|
Tags []struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
} `json:"tags"`
|
|
|
|
Family struct {
|
|
|
|
Label string `json:"label"`
|
|
|
|
Value int `json:"value"`
|
|
|
|
} `json:"family"`
|
|
|
|
Role struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
PrefixCount int `json:"prefix_count"`
|
|
|
|
VLANCount int `json:"vlan_count"`
|
|
|
|
} `json:"role"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
IsPool bool `json:"is_pool"`
|
|
|
|
}
|
|
|
|
|
2021-11-26 11:09:27 +01:00
|
|
|
// Prefixes is used for the return value from NetBox API ipam_prefixes_list
|
|
|
|
type Prefixes struct {
|
2022-01-31 16:38:37 +01:00
|
|
|
Next string `json:"next"`
|
|
|
|
Previous string `json:"previous"`
|
|
|
|
Results []Prefix `json:"results"`
|
|
|
|
Count int `json:"count"`
|
2021-11-26 11:09:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrefixFilter is used to filter out returned object from Netbox API ipam_prefixes_list
|
|
|
|
type PrefixFilter struct {
|
|
|
|
// 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"`
|
2021-12-19 21:24:29 +01:00
|
|
|
|
|
|
|
Offset int `schema:"offset,omitempty"`
|
|
|
|
Limit int `schema:"limit,omitempty"`
|
2021-11-26 11:09:27 +01:00
|
|
|
}
|
|
|
|
|
2021-11-27 23:31:44 +01:00
|
|
|
const prefixesPath = ipamPath + "/prefixes"
|
2021-11-26 11:09:27 +01:00
|
|
|
|
|
|
|
// List prefixes. PrefixFilter is used to list based on filter queries.
|
|
|
|
func (s *PrefixesService) List(ctx context.Context, f *PrefixFilter) (*Prefixes, error) {
|
|
|
|
var prefixes Prefixes
|
|
|
|
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 &prefixes, err
|
|
|
|
}
|
|
|
|
query = form.Encode()
|
|
|
|
|
|
|
|
req, err = s.client.newRequest(ctx, "GET", prefixesPath, query, nil)
|
|
|
|
if err != nil {
|
|
|
|
return &prefixes, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.client.do(req, &prefixes)
|
|
|
|
if err != nil {
|
|
|
|
return &prefixes, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &prefixes, nil
|
|
|
|
}
|
|
|
|
|
2021-12-19 21:24:29 +01:00
|
|
|
// ListAll prefixes. PrefixFilter is used to list all prefixes based on filters
|
|
|
|
func (s *PrefixesService) ListAll(ctx context.Context, f *PrefixFilter) (*Prefixes, error) {
|
|
|
|
var all Prefixes
|
|
|
|
var r *Prefixes
|
|
|
|
var num int
|
|
|
|
var err error
|
|
|
|
|
|
|
|
f.Limit = 1
|
|
|
|
r, err = s.List(ctx, f)
|
|
|
|
if err != nil {
|
|
|
|
return &all, fmt.Errorf("unable to count prefixes with filter %+v: %w", f, err)
|
|
|
|
}
|
|
|
|
num = r.Count
|
|
|
|
|
|
|
|
for count := 0; count < num; count += 1000 {
|
|
|
|
f.Limit = 1000
|
|
|
|
f.Offset = count
|
|
|
|
|
|
|
|
r, err = s.List(ctx, f)
|
|
|
|
if err != nil {
|
|
|
|
return &all, fmt.Errorf("unable to list prefixes with filter %+v: %w", f, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
all.Results = append(all.Results, r.Results...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &all, nil
|
|
|
|
}
|
|
|
|
|
2021-11-26 11:09:27 +01:00
|
|
|
// Create a prefix
|
|
|
|
func (s *PrefixesService) Create(ctx context.Context, c *NewPrefix) error {
|
|
|
|
var err error
|
|
|
|
var req *http.Request
|
|
|
|
|
|
|
|
req, err = s.client.newRequest(ctx, "POST", prefixesPath, "", 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 prefix
|
|
|
|
// func (s *PrefixesService) Update(ctx context.Context, id string, c *UpdatePrefix) error {
|
|
|
|
// var req *http.Request
|
|
|
|
// var err error
|
|
|
|
|
|
|
|
// path := fmt.Sprintf("%s/%s", prefixesPath, 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
|
|
|
|
// }
|
|
|
|
// // ListPrefixes returns Netbox ipam_prefixes_list
|
|
|
|
// func (n *NetBox) ListPrefixes(i *Prefixes, f *PrefixFilter) error {
|
|
|
|
// 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/prefixes/?"+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
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // CreatePrefixes creates interfaces via Netbox API ipam_prefixes_create
|
|
|
|
// func (n *NetBox) CreatePrefixes(i *NewPrefix) 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/prefixes/", 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)
|
|
|
|
// }
|