netboxgo/vlans.go
2021-11-26 11:09:27 +01:00

172 lines
4.5 KiB
Go

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 {
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"`
}
type VLAN 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 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"`
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"`
}
// IpamVLANsList is used for the return value from NetBox API ipam_vlans_list
type VLANs struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []VLAN `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"`
}
const vlansPath = "/ipam/vlans"
// 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
}
// 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
// }