282 lines
7.8 KiB
Go
282 lines
7.8 KiB
Go
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 {
|
|
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"`
|
|
}
|
|
|
|
// Prefixes is used for the return value from NetBox API ipam_prefixes_list
|
|
type Prefixes struct {
|
|
Count int `json:"count"`
|
|
Next string `json:"next"`
|
|
Previous string `json:"previous"`
|
|
Results []struct {
|
|
ID int `json:"id"`
|
|
Family struct {
|
|
Label string `json:"label"`
|
|
Value int `json:"value"`
|
|
} `json:"family"`
|
|
Prefix string `json:"prefix"`
|
|
Site struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
} `json:"site"`
|
|
Vrf struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
Rd string `json:"rd"`
|
|
PrefixCount int `json:"prefix_count"`
|
|
} `json:"vrf"`
|
|
Tenant struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
} `json:"tenant"`
|
|
VLAN struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Vid int `json:"vid"`
|
|
Name string `json:"name"`
|
|
DisplayName string `json:"display_name"`
|
|
} `json:"vlan"`
|
|
Status struct {
|
|
Label string `json:"label"`
|
|
Value string `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"`
|
|
IsPool bool `json:"is_pool"`
|
|
Description string `json:"description"`
|
|
Tags []struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
Color string `json:"color"`
|
|
} `json:"tags"`
|
|
CustomFields struct{} `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"`
|
|
}
|
|
|
|
const prefixesPath = "/ipam/prefixes"
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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)
|
|
// }
|