netboxgo/vlans.go

202 lines
5.2 KiB
Go
Raw Normal View History

2021-11-26 11:09:27 +01:00
package netboxgo
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
)
type VLANsService service
// NewVLAN is used for the return values from Netbox API ipam_vlans_create
type NewVLAN struct {
CustomFields interface{} `json:"custom_fields"`
Description string `json:"description"`
2021-11-29 11:38:01 +01:00
Name string `json:"name"`
Tags []string `json:"tags"`
Site int `json:"site"`
2021-11-29 11:38:01 +01:00
Tenant int `json:"tenant"`
Status int `json:"status"`
Role int `json:"role"`
Group int `json:"group"`
Vid int `json:"vid"`
2021-11-26 11:09:27 +01:00
}
type VLAN struct {
LastUpdated time.Time `json:"last_updated"`
CustomFields interface{} `json:"custom_fields"`
Site struct {
2021-11-26 11:09:27 +01:00
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
2021-11-26 11:09:27 +01:00
} `json:"site"`
Tenant struct {
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
2021-11-26 11:09:27 +01:00
} `json:"tenant"`
Status struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"status"`
Description string `json:"description"`
Name string `json:"name"`
Created string `json:"created"`
DisplayName string `json:"display_name"`
Group struct {
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
VLANCount int `json:"vlan_count"`
} `json:"group"`
Tags []string `json:"tags"`
2021-11-26 11:09:27 +01:00
Role struct {
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
2021-11-26 11:09:27 +01:00
PrefixCount int `json:"prefix_count"`
VLANCount int `json:"vlan_count"`
} `json:"role"`
ID int `json:"id"`
Vid int `json:"vid"`
PrefixCount int `json:"prefix_count"`
2021-11-26 11:09:27 +01:00
}
// IpamVLANsList is used for the return value from NetBox API ipam_vlans_list
type VLANs struct {
Next string `json:"next"`
Previous string `json:"previous"`
Results []VLAN `json:"results"`
Count int `json:"count"`
2021-11-26 11:09:27 +01:00
}
// VLANFilter is used to filter out returned object from Netbox API ipam_vlans_list
type VLANFilter struct {
// 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"`
Offset int `schema:"offset,omitempty"`
Limit int `schema:"limit,omitempty"`
2021-11-26 11:09:27 +01:00
}
const vlansPath = ipamPath + "/vlans"
2021-11-26 11:09:27 +01:00
// List vlans. VLANFilter is used to list based on filter queries.
func (s *VLANsService) List(ctx context.Context, f *VLANFilter) (*VLANs, error) {
var vlans VLANs
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 &vlans, err
}
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", vlansPath, query, nil)
if err != nil {
return &vlans, err
}
_, err = s.client.do(req, &vlans)
if err != nil {
return &vlans, err
}
return &vlans, nil
}
// ListAll vlans. VLANFilter is used to filter list all based on filters
func (s *VLANsService) ListAll(ctx context.Context, f *VLANFilter) (*VLANs, error) {
var all VLANs
var v *VLANs
var numVlans int
var err error
f.Limit = 1
v, err = s.List(ctx, f)
if err != nil {
return &all, fmt.Errorf("unable to count vlans with filter %+v: %w", f, err)
}
numVlans = v.Count
for count := 0; count < numVlans; count += 1000 {
f.Limit = 1000
f.Offset = count
v, err = s.List(ctx, f)
if err != nil {
return &all, fmt.Errorf("unable to list vlans with filter %+v: %w", f, err)
}
all.Results = append(all.Results, v.Results...)
}
return &all, nil
}
2021-11-26 11:09:27 +01:00
// Create a device
func (s *VLANsService) Create(ctx context.Context, c *NewDevice) error {
var err error
var req *http.Request
req, err = s.client.newRequest(ctx, "POST", vlansPath, "", 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 device
// func (s *VLANsService) Update(ctx context.Context, id string, c *UpdateVLAN) error {
// var req *http.Request
// var err error
// path := fmt.Sprintf("%s/%s", vlansPath, 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
// }