139 lines
3.5 KiB
Go
139 lines
3.5 KiB
Go
|
package netboxgo
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"time"
|
||
|
|
||
|
"github.com/gorilla/schema"
|
||
|
)
|
||
|
|
||
|
type VRFsService service
|
||
|
|
||
|
// NewVRF is used for the return values from Netbox API ipam_vrfs_create
|
||
|
type NewVRF struct {
|
||
|
Name string `json:"name"`
|
||
|
Rd string `json:"rd"`
|
||
|
Tenant int `json:"tenant"`
|
||
|
EnforceUnique bool `json:"enforce_unique"`
|
||
|
Description string `json:"description"`
|
||
|
Tags []string `json:"tags"`
|
||
|
CustomFields struct{} `json:"custom_fields"`
|
||
|
}
|
||
|
|
||
|
// VRFs is used for the return value from NetBox API ipam_vrfs_list
|
||
|
type VRFs struct {
|
||
|
Count int `json:"count"`
|
||
|
Next string `json:"next"`
|
||
|
Previous string `json:"previous"`
|
||
|
Results []struct {
|
||
|
ID int `json:"id"`
|
||
|
Name string `json:"name"`
|
||
|
Rd string `json:"rd"`
|
||
|
Tenant struct {
|
||
|
ID int `json:"id"`
|
||
|
URL string `json:"url"`
|
||
|
Name string `json:"name"`
|
||
|
Slug string `json:"slug"`
|
||
|
} `json:"tenant"`
|
||
|
EnforceUnique bool `json:"enforce_unique"`
|
||
|
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"`
|
||
|
IpaddressCount int `json:"ipaddress_count"`
|
||
|
PrefixCount int `json:"prefix_count"`
|
||
|
} `json:"results"`
|
||
|
}
|
||
|
|
||
|
// VRFFilter is used to filter out returned object from Netbox API ipam_vrfs_list
|
||
|
type VRFFilter struct {
|
||
|
Offset int64 `schema:"offset,omitempty"`
|
||
|
Limit int64 `schema:"limit,omitempty"`
|
||
|
|
||
|
// User specific filters
|
||
|
Name string `schema:"name,omitempty"`
|
||
|
RD string `schema:"rd,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"`
|
||
|
}
|
||
|
|
||
|
const vrfsPath = "/ipam/vrfs"
|
||
|
|
||
|
// List vrfs. VRFFilter is used to list based on filter queries.
|
||
|
func (s *VRFsService) List(ctx context.Context, f *VRFFilter) (*VRFs, error) {
|
||
|
var vrfs VRFs
|
||
|
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 &vrfs, err
|
||
|
}
|
||
|
query = form.Encode()
|
||
|
|
||
|
req, err = s.client.newRequest(ctx, "GET", vrfsPath, query, nil)
|
||
|
if err != nil {
|
||
|
return &vrfs, err
|
||
|
}
|
||
|
|
||
|
_, err = s.client.do(req, &vrfs)
|
||
|
if err != nil {
|
||
|
return &vrfs, err
|
||
|
}
|
||
|
|
||
|
return &vrfs, nil
|
||
|
}
|
||
|
|
||
|
// Create a device
|
||
|
func (s *VRFsService) Create(ctx context.Context, c *NewVRF) error {
|
||
|
var err error
|
||
|
var req *http.Request
|
||
|
|
||
|
req, err = s.client.newRequest(ctx, "POST", vrfsPath, "", 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 *VRFsService) Update(ctx context.Context, id string, c *UpdateVRF) error {
|
||
|
// var req *http.Request
|
||
|
// var err error
|
||
|
|
||
|
// path := fmt.Sprintf("%s/%s", vrfsPath, 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
|
||
|
// }
|