Migrate to new client style

This commit is contained in:
Kalle Carlbark 2021-11-26 11:09:27 +01:00
parent 3bec026387
commit 89a5d96e64
No known key found for this signature in database
23 changed files with 1961 additions and 1880 deletions

183
clusters.go Normal file
View file

@ -0,0 +1,183 @@
package netboxgo
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
)
type ClustersService service
// NewCluster is used to create new VirtualizationClusters
type NewCluster struct {
ID int `json:"id"`
Name string `json:"name"`
Type struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ClusterCount int `json:"cluster_count"`
} `json:"type"`
Group struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ClusterCount int `json:"cluster_count"`
} `json:"group"`
Site struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"site"`
Comments string `json:"comments"`
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"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
}
// Clusters is used to list VirtualizationClusters
type Clusters struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []struct {
ID int `json:"id"`
Name string `json:"name"`
Type struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ClusterCount int `json:"cluster_count"`
} `json:"type"`
Group struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ClusterCount int `json:"cluster_count"`
} `json:"group"`
Site struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"site"`
Comments string `json:"comments"`
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"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"results"`
}
// ClusterFilter is used to filter out VirtualizationClusters
type ClusterFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
// User specific filters
Name string `schema:"name,omitempty"`
Group string `schema:"group,omitempty"`
GroupID string `schema:"group_id,omitempty"`
TypeID string `schema:"type_id,omitempty"`
Type string `schema:"type,omitempty"`
SiteID string `schema:"site_id,omitempty"`
Site string `schema:"site,omitempty"`
Tenant string `schema:"tenant,omitempty"`
IDIn string `schema:"id__in,omitempty"`
Q string `schema:"q,omitempty"`
Tag string `schema:"tag,omitempty"`
}
const clustersPath = "/virtualization/clusters"
// List clusters. ClusterFilter is used to list based on filter queries.
func (s *ClustersService) List(ctx context.Context, f *ClusterFilter) (*Clusters, error) {
var clusters Clusters
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 &clusters, err
}
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", clustersPath, query, nil)
if err != nil {
return &clusters, err
}
_, err = s.client.do(req, &clusters)
if err != nil {
return &clusters, err
}
return &clusters, nil
}
// Create a cluster
func (s *ClustersService) Create(ctx context.Context, c *NewCluster) error {
var err error
var req *http.Request
req, err = s.client.newRequest(ctx, "POST", clustersPath, "", 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 cluster
// func (s *ClustersService) Update(ctx context.Context, id string, c *UpdateCluster) error {
// var req *http.Request
// var err error
// path := fmt.Sprintf("%s/%s", clustersPath, 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

View file

@ -1,18 +1,16 @@
package netboxgo
import (
"crypto/tls"
"encoding/json"
"io/ioutil"
"context"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
type ListDeviceRoles struct {
type DeviceRolesService service
type DeviceRoles struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
@ -45,58 +43,69 @@ type DeviceRoleFilter struct {
Q string `schema:"q,omitempty"`
}
// ListDeviceTypes method returns dcim_device_roles from Netbox API
func (n *NetBox) ListDeviceRoles(d *ListDeviceRoles, f *DeviceRoleFilter) error {
const deviceRolesPath = "/dcim/device-roles"
// List deviceroles. DeviceRoleFilter is used to list based on filter queries.
func (s *DeviceRolesService) List(ctx context.Context, f *DeviceRoleFilter) (*DeviceRoles, error) {
var deviceroles DeviceRoles
var query string
var req *http.Request
var err error
encoder := schema.NewEncoder()
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
}
timeout := time.Duration(60 * time.Second)
client := &http.Client{
Timeout: timeout,
Transport: transport,
}
deviceurl := n.RootURL + "/api/dcim/device-roles/"
if f != nil {
form := url.Values{}
err := encoder.Encode(f, form)
err = encoder.Encode(f, form)
if err != nil {
return err
return &deviceroles, err
}
query := form.Encode()
deviceurl = deviceurl + "?" + query
}
var request *http.Request
var err error
request, err = http.NewRequest("GET", deviceurl, nil)
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", deviceRolesPath, 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
return &deviceroles, err
}
if response.StatusCode != http.StatusOK {
return errors.Errorf("Error: response was: %d should be %d (%s)\n", response.StatusCode, http.StatusOK, deviceurl)
_, err = s.client.do(req, &deviceroles)
if err != nil {
return &deviceroles, err
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
return &deviceroles, nil
}
err = response.Body.Close()
if err != nil {
return err
}
// var err error
// var req *http.Request
err = json.Unmarshal(data, &d)
if err != nil {
return err
}
return nil
}
// req, err = s.client.newRequest(ctx, "POST", deviceRolesPath, "", 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-role
// func (s *DeviceRolesService) Update(ctx context.Context, id string, c *UpdateDevice) error {
// var req *http.Request
// var err error
// path := fmt.Sprintf("%s/%s", deviceRolesPath, 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
// }

View file

@ -1,18 +1,17 @@
package netboxgo
import (
"crypto/tls"
"encoding/json"
"io/ioutil"
"context"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
type ListDeviceTypes struct {
type DeviceTypesService service
type DeviceTypes struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
@ -49,8 +48,7 @@ type DeviceType struct {
Slug string `json:"slug"`
Color string `json:"color"`
} `json:"tags"`
CustomFields struct {
} `json:"custom_fields"`
CustomFields struct{} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
DeviceCount int `json:"device_count"`
@ -72,58 +70,126 @@ type DeviceTypeFilter struct {
Tag string `schema:"tag,omitempty"`
}
// ListDeviceTypes method returns dcim_device_types_list from Netbox API
func (n *NetBox) ListDeviceTypes(d *ListDeviceTypes, f *DeviceTypeFilter) error {
const deviceTypesPath = "/dcim/device-types"
// List device-types. DeviceTypeFilter is used to list based on filter queries.
func (s *DeviceTypesService) List(ctx context.Context, f *DeviceTypeFilter) (*DeviceTypes, error) {
var devicetypes DeviceTypes
var query string
var req *http.Request
var err error
encoder := schema.NewEncoder()
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
}
timeout := time.Duration(60 * time.Second)
client := &http.Client{
Timeout: timeout,
Transport: transport,
}
deviceurl := n.RootURL + "/api/dcim/device-types/"
if f != nil {
form := url.Values{}
err := encoder.Encode(f, form)
err = encoder.Encode(f, form)
if err != nil {
return err
return &devicetypes, err
}
query := form.Encode()
deviceurl = deviceurl + "?" + query
}
var request *http.Request
var err error
request, err = http.NewRequest("GET", deviceurl, nil)
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", deviceTypesPath, 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
return &devicetypes, err
}
if response.StatusCode != http.StatusOK {
return errors.Errorf("Error: response was: %d should be %d (%s)\n", response.StatusCode, http.StatusOK, deviceurl)
_, err = s.client.do(req, &devicetypes)
if err != nil {
return &devicetypes, err
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
return &devicetypes, nil
}
err = response.Body.Close()
if err != nil {
return err
}
// // Create a device
// func (s *DeviceTypesService) Create(ctx context.Context, c *NewDeviceType) error {
// var err error
// var req *http.Request
err = json.Unmarshal(data, &d)
if err != nil {
return err
}
return nil
}
// req, err = s.client.newRequest(ctx, "POST", deviceTypesPath, "", 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 *DeviceTypesService) Update(ctx context.Context, id string, c *UpdateDevice) error {
// var req *http.Request
// var err error
// path := fmt.Sprintf("%s/%s", deviceTypesPath, 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
// }
// // ListDeviceTypes method returns dcim_device_types_list from Netbox API
// func (n *NetBox) ListDeviceTypes(d *DeviceTypes, f *DeviceTypeFilter) error {
// encoder := schema.NewEncoder()
// transport := &http.Transport{
// TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
// }
// timeout := time.Duration(60 * time.Second)
// client := &http.Client{
// Timeout: timeout,
// Transport: transport,
// }
// deviceurl := n.RootURL + "/api/dcim/device-types/"
// if f != nil {
// form := url.Values{}
// err := encoder.Encode(f, form)
// if err != nil {
// return err
// }
// query := form.Encode()
// deviceurl = deviceurl + "?" + query
// }
// var request *http.Request
// var err error
// request, err = http.NewRequest("GET", deviceurl, 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 (%s)\n", response.StatusCode, http.StatusOK, deviceurl)
// }
// data, err := ioutil.ReadAll(response.Body)
// if err != nil {
// return err
// }
// err = response.Body.Close()
// if err != nil {
// return err
// }
// err = json.Unmarshal(data, &d)
// if err != nil {
// return err
// }
// return nil
// }

View file

@ -1,21 +1,19 @@
package netboxgo
import (
"bytes"
"crypto/tls"
"encoding/json"
"io/ioutil"
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
// DcimDevicesList is used for Netbox dcim_device_list return struct
type DcimDevicesList struct {
type DevicesService service
// Devices is used for Netbox dcim_device_list return struct
type Devices struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
@ -172,7 +170,7 @@ type Device struct {
LastUpdated time.Time `json:"last_updated"`
}
type DcimCreateDevice struct {
type NewDevice struct {
Name string `json:"name,omitempty,omitempty"`
DeviceType int `json:"device_type,omitempty,omitempty"`
DeviceRole int `json:"device_role,omitempty,omitempty"`
@ -230,7 +228,7 @@ type DcimCreateDevice struct {
} `json:"custom_fields,omitempty"`
}
type DcimUpdateDevice struct {
type UpdateDevice struct {
Name string `json:"name,omitempty"`
DeviceType int `json:"device_type,omitempty"`
DeviceRole int `json:"device_role,omitempty"`
@ -312,121 +310,70 @@ type DeviceFilter struct {
Tag string `schema:"tag,omitempty"`
}
// ListDevices method returns dcim_device_list from Netbox API
func (n *NetBox) ListDevices(d *DcimDevicesList, f *DeviceFilter) error {
const devicesPath = "/dcim/devices"
// List devices. DeviceFilter is used to list based on filter queries.
func (s *DevicesService) List(ctx context.Context, f *DeviceFilter) (*Devices, error) {
var devices Devices
var query string
var req *http.Request
var err error
encoder := schema.NewEncoder()
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
}
timeout := time.Duration(60 * time.Second)
client := &http.Client{
Timeout: timeout,
Transport: transport,
}
deviceurl := n.RootURL + "/api/dcim/devices/"
if f != nil {
form := url.Values{}
err := encoder.Encode(f, form)
err = encoder.Encode(f, form)
if err != nil {
return err
return &devices, err
}
query := form.Encode()
deviceurl = deviceurl + "?" + query
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", devicesPath, query, nil)
if err != nil {
return &devices, err
}
var request *http.Request
_, err = s.client.do(req, &devices)
if err != nil {
return &devices, err
}
return &devices, nil
}
// Create a device
func (s *DevicesService) Create(ctx context.Context, c *NewDevice) error {
var err error
request, err = http.NewRequest("GET", deviceurl, nil)
var req *http.Request
req, err = s.client.newRequest(ctx, "POST", devicesPath, "", c)
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
return fmt.Errorf("unable to create request: %w", err)
}
if response.StatusCode != http.StatusOK {
return errors.Errorf("Error: response was: %d should be %d (%s)\n", response.StatusCode, http.StatusOK, deviceurl)
}
data, err := ioutil.ReadAll(response.Body)
_, err = s.client.do(req, nil)
if err != nil {
return err
}
err = response.Body.Close()
if err != nil {
return err
}
err = json.Unmarshal(data, &d)
if err != nil {
return err
}
return nil
}
func (n *NetBox) CreateDevice(v *DcimCreateDevice) error {
data, err := json.Marshal(v)
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/dcim/devices/", bytes.NewBuffer(data))
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)
}
func (n *NetBox) UpdateDevice(v *DcimUpdateDevice, id int) error {
data, err := json.Marshal(v)
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("PATCH", n.RootURL+"/api/dcim/devices/"+strconv.Itoa(id)+"/", bytes.NewBuffer(data))
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)
_, err = client.Do(request)
if err != nil {
return err
return fmt.Errorf("unable to do request: %w", err)
}
return nil
}
// Update a device
func (s *DevicesService) Update(ctx context.Context, id string, c *UpdateDevice) error {
var req *http.Request
var err error
path := fmt.Sprintf("%s/%s", devicesPath, 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

View file

@ -1,22 +1,20 @@
package netboxgo
import (
"bytes"
"crypto/tls"
"encoding/json"
// "fmt"
"io/ioutil"
"context"
"fmt"
"net/http"
"net/url"
"time"
// "fmt"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
// DcimInterfacesCreate is used for the return values from Netbox API dcim_interfaces_create
type DcimInterfacesCreate struct {
type InterfacesService service
// NewInterface is used for the return values from Netbox API dcim_interfaces_create
type NewInterface struct {
Device int `json:"device"`
Name string `json:"name"`
Type int `json:"type,omitempty"`
@ -36,8 +34,8 @@ type DcimInterfacesCreate struct {
Tags []string `json:"tags,omitempty"`
}
// DcimInterfacesList is used for the return value from NetBox API dcim_interfaces_list
type DcimInterfacesList struct {
// Interfaces is used for the return value from NetBox API dcim_interfaces_list
type Interfaces struct {
Count int `json:"count,omitempty"`
Next interface{} `json:"next,omitempty"`
Previous interface{} `json:"previous,omitempty"`
@ -114,89 +112,51 @@ type InterfaceFilter struct {
VLAN string `schema:"vlan,omitempty"`
}
// ListInterfaces returns Netbox dcim_interfaces_list
func (n *NetBox) ListInterfaces(i *DcimInterfacesList, f *InterfaceFilter) error {
var encoder = schema.NewEncoder()
const interfacesPath = "/dcim/interfaces"
// List devices. DeviceFilter is used to list based on filter queries.
func (s *InterfacesService) List(ctx context.Context, f *InterfaceFilter) (*Interfaces, error) {
var interfaces Interfaces
var query string
var req *http.Request
var err error
encoder := schema.NewEncoder()
form := url.Values{}
err := encoder.Encode(f, form)
err = encoder.Encode(f, form)
if err != nil {
return err
return &interfaces, err
}
query := form.Encode()
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/dcim/interfaces/?"+query, nil)
req, err = s.client.newRequest(ctx, "GET", interfacesPath, 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
return &interfaces, err
}
if response.StatusCode != http.StatusOK {
return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusOK)
_, err = s.client.do(req, &interfaces)
if err != nil {
return &interfaces, err
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
return &interfaces, nil
}
err = response.Body.Close()
// Create a interfaces
func (s *InterfacesService) Create(ctx context.Context, c *NewInterface) error {
var err error
var req *http.Request
req, err = s.client.newRequest(ctx, "POST", interfacesPath, "", c)
if err != nil {
return err
return fmt.Errorf("unable to create request: %w", err)
}
err = json.Unmarshal(data, &i)
_, err = s.client.do(req, nil)
if err != nil {
return err
return fmt.Errorf("unable to do request: %w", err)
}
return nil
}
// CreateInterfaces creates interfaces via Netbox API dcim_interfaces_create
func (n *NetBox) CreateInterfaces(i *DcimInterfacesCreate) error {
interfaceData, 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/dcim/interfaces/", bytes.NewBuffer(interfaceData))
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)
}

108
inventory.go Normal file
View file

@ -0,0 +1,108 @@
package netboxgo
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/gorilla/schema"
)
type InventoryItemsService service
type NewInventoryItem struct {
Device int `json:"device"`
Parent int `json:"parent"`
Name string `json:"name"`
Manufacturer int `json:"manufacturer"`
PartID string `json:"part_id"`
Serial string `json:"serial"`
AssetTag string `json:"asset_tag"`
Discovered bool `json:"discovered"`
Description string `json:"description"`
Tags []string `json:"tags"`
}
type InventoryItems struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []struct {
ID int `json:"id"`
Device struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
} `json:"device"`
Parent int `json:"parent"`
Name string `json:"name"`
Manufacturer struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
DevicetypeCount int `json:"devicetype_count"`
} `json:"manufacturer"`
PartID string `json:"part_id"`
Serial string `json:"serial"`
AssetTag string `json:"asset_tag"`
Discovered bool `json:"discovered"`
Description string `json:"description"`
Tags []string `json:"tags"`
} `json:"results"`
}
type InventoryItemFilter struct {
Device string `schema:"device"`
}
const inventoryItemsPath = "/dcim/inventory-items"
// List inventory-items. InventoryItemFilter is used to list based on filter queries.
func (s *InventoryItemsService) List(ctx context.Context, f *InventoryItemFilter) (*InventoryItems, error) {
var inventoryitems InventoryItems
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 &inventoryitems, err
}
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", inventoryItemsPath, query, nil)
if err != nil {
return &inventoryitems, err
}
_, err = s.client.do(req, &inventoryitems)
if err != nil {
return &inventoryitems, err
}
return &inventoryitems, nil
}
// Create a inventory-item
func (s *InventoryItemsService) Create(ctx context.Context, c *NewInventoryItem) error {
var err error
var req *http.Request
req, err = s.client.newRequest(ctx, "POST", inventoryItemsPath, "", 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
}

266
netbox.go
View file

@ -1,9 +1,14 @@
package netboxgo
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
@ -12,13 +17,89 @@ import (
"github.com/pkg/errors"
)
// NetBox settings struct
type NetBox struct {
RootURL string
type DCIM struct {
Devices *DevicesService
DeviceRoles *DeviceRolesService
DeviceTypes *DeviceTypesService
InventoryItems *InventoryItemsService
RearPorts *RearPortsService
}
type Virtualization struct {
Clusters *ClustersService
VirtualMachines *VirtualMachinesService
}
type IPAM struct {
VLANs *VLANsService
VRFs *VRFsService
Prefixes *PrefixesService
}
type Secret struct {
Secrets *SecretsService
}
type Tenancy struct {
Tenants *TenantsService
Sites *SitesService
SiteGroups *SiteGroupsService
}
// Client struct is used to create a new NetBox endpoint
type Client struct {
DCIM *DCIM
Tenancy *Tenancy
IPAM *IPAM
Virtualization *Virtualization
Secret *Secret
Tenants *TenantsService
Sites *SitesService
SiteGroups *SiteGroupsService
VirtualMachines *VirtualMachinesService
Clusters *ClustersService
VLANs *VLANsService
Secrets *SecretsService
InventoryItems *InventoryItemsService
Devices *DevicesService
DeviceRoles *DeviceRolesService
DeviceTypes *DeviceTypesService
Interfaces *InterfacesService
Prefixes *PrefixesService
VRFs *VRFsService
RearPorts *RearPortsService
// baseURL is the URL used for the base URL of the API
baseURL *url.URL
// httpClient is used for HTTP requests
httpClient *http.Client
// Reuse a single struct instead of allocating one for each service on the heap.
common service
// UserAgent is used to set the UserAgent on the HTTP requests
UserAgent string
// Token is set for authentication of the API
Token string
SessionKey string
FetchMode string
// Debug enables verbose debugging messages to console.
Debug bool
// InsecureSkipVerify is used to selectively skip InsecureVerifications
InsecureSkipVerify bool
// SessionKey is used to read authentication data
SessionKey string
// Used by golang wasm
FetchMode string
}
type service struct {
client *Client
}
// NetBoxSessionKey sets the session key for secrets retrieval
@ -26,15 +107,79 @@ type NetBoxSessionKey struct {
XSessionKey string `json:"session_key"`
}
// New populates the NetBox settings struct
func (n *NetBox) New(root, token string, TLSSkipVerify bool) {
n.RootURL = root
n.Token = token
n.InsecureSkipVerify = TLSSkipVerify
// NewClient returns a new NetBox API client
func NewClient(apiurl string, httpClient *http.Client) (*Client, error) {
c := &Client{}
api, err := url.Parse(apiurl)
if err != nil {
return c, fmt.Errorf("unable to parse url %s: %w", apiurl, err)
}
c.baseURL = api
c.SetDebug(false)
c.SetUserAgent("netboxgo/0.0.6")
c.httpClient = httpClient
if httpClient == nil {
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: false,
CipherSuites: []uint16{
tls.TLS_AES_128_GCM_SHA256,
tls.TLS_AES_256_GCM_SHA384,
tls.TLS_CHACHA20_POLY1305_SHA256,
},
MinVersion: tls.VersionTLS12,
},
Proxy: http.ProxyFromEnvironment,
}
timeout := time.Duration(60 * time.Second)
c.httpClient = &http.Client{
Transport: transport,
Jar: nil,
Timeout: timeout,
}
}
c.common.client = c
c.Clusters = (*ClustersService)(&c.common)
c.Devices = (*DevicesService)(&c.common)
c.DeviceRoles = (*DeviceRolesService)(&c.common)
c.DeviceTypes = (*DeviceTypesService)(&c.common)
c.Interfaces = (*InterfacesService)(&c.common)
c.InventoryItems = (*InventoryItemsServices)(&c.common)
c.Prefixes = (*PrefixesService)(&c.common)
c.RearPorts = (*RearPortsService)(&c.common)
c.Tenants = (*TenantsService)(&c.common)
c.Secrets = (*SecretsService)(&c.common)
c.Sites = (*SitesService)(&c.common)
c.SiteGroups = (*SiteGroupsService)(&c.common)
c.VirtualMachines = (*VirtualMachinesService)(&c.common)
c.VLANs = (*VLANsService)(&c.common)
c.VRFs = (*VRFsService)(&c.common)
return c, nil
}
// SetUserAgent is used to set the user agent for interaction with the API
func (c *Client) SetUserAgent(u string) {
c.UserAgent = u
}
// SetDebug is used to toggle debugging, set to true for debug
func (c *Client) SetDebug(b bool) {
c.Debug = b
}
// SetToken is used to set the http token bearer for interaction with the API
func (c *Client) SetToken(token string) {
c.Token = token
}
// FetchSessionKey fetches sessionkey
func (n *NetBox) FetchSessionKey(privatekey string) error {
func (n *Client) FetchSessionKey(privatekey string) error {
form := url.Values{}
form.Add("private_key", privatekey)
query := form.Encode()
@ -47,10 +192,12 @@ func (n *NetBox) FetchSessionKey(privatekey string) error {
Timeout: timeout,
Transport: transport,
}
request, err := http.NewRequest("POST", n.RootURL+"/api/secrets/get-session-key/", strings.NewReader(query))
const secretsPath = "/secrets"
request, err := http.NewRequest("POST", secretsPath+"/get-session-key/", strings.NewReader(query))
if err != nil {
return err
}
request.Header.Add("Accept", "application/json")
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
@ -65,7 +212,7 @@ func (n *NetBox) FetchSessionKey(privatekey string) error {
}
if response.StatusCode != http.StatusOK {
return errors.Errorf("Error: response was: %d should be %d\n%s\n", response.StatusCode, http.StatusOK, response.Header)
return errors.Errorf("response was: %d should be %d\n%s\n", response.StatusCode, http.StatusOK, response.Header)
}
data, err := ioutil.ReadAll(response.Body)
@ -87,3 +234,96 @@ func (n *NetBox) FetchSessionKey(privatekey string) error {
return nil
}
// newRequest is used to make new request to endpoints
func (c *Client) newRequest(ctx context.Context, method, path string, query string, body interface{}) (*http.Request, error) {
var err error
var buf io.ReadWriter
u := &url.URL{
Scheme: c.baseURL.Scheme,
Host: c.baseURL.Host,
Path: c.baseURL.Path + path,
RawQuery: query,
}
if body != nil {
buf = new(bytes.Buffer)
err = json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
readBuf, _ := json.Marshal(body)
c.debugf("debug: method:%s url:%s body:%s", method, u.String(), string(readBuf))
req, err := http.NewRequestWithContext(ctx, method, u.String(), buf)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
if c.SessionKey != "" {
req.Header.Add("X-Session-Key", c.SessionKey)
}
req.Header.Set("Accept", "application/json")
if c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}
if c.Token != "" {
req.Header.Add("Authorization", "Bearer "+c.Token)
}
if c.SessionKey != "" {
req.Header.Add("X-Session-Key", c.SessionKey)
}
return req, nil
}
// do method is used to decode request response to proper struct
func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
var err error
var resp *http.Response
resp, err = c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("client unable to do request: %w", err)
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
var data []byte
data, err = io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("unable to read body response: %w", err)
}
return nil, fmt.Errorf("request failed: body=%s", string(data))
}
if v != nil {
err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
return resp, fmt.Errorf("unable to decode response body: %w", err)
}
}
return resp, nil
}
// debugf is used as a formtter for debugging information
func (c *Client) debugf(fmt string, v ...interface{}) {
if c.Debug {
log.Printf("cendot client: "+fmt, v...)
}
}

View file

@ -1,152 +0,0 @@
package netboxgo
import (
"bytes"
"crypto/tls"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
type AddInventory struct {
Device int `json:"device"`
Parent int `json:"parent"`
Name string `json:"name"`
Manufacturer int `json:"manufacturer"`
PartID string `json:"part_id"`
Serial string `json:"serial"`
AssetTag string `json:"asset_tag"`
Discovered bool `json:"discovered"`
Description string `json:"description"`
Tags []string `json:"tags"`
}
type DcimInventoryItemsList struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []struct {
ID int `json:"id"`
Device struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
} `json:"device"`
Parent int `json:"parent"`
Name string `json:"name"`
Manufacturer struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
DevicetypeCount int `json:"devicetype_count"`
} `json:"manufacturer"`
PartID string `json:"part_id"`
Serial string `json:"serial"`
AssetTag string `json:"asset_tag"`
Discovered bool `json:"discovered"`
Description string `json:"description"`
Tags []string `json:"tags"`
} `json:"results"`
}
type InventoryFilter struct {
Device string `schema:"device"`
}
// ListDevices method returns dcim_device_list from Netbox API
func (n *NetBox) ListInventory(d *DcimInventoryItemsList, f *InventoryFilter) error {
encoder := schema.NewEncoder()
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
}
timeout := time.Duration(60 * time.Second)
client := &http.Client{
Timeout: timeout,
Transport: transport,
}
deviceurl := n.RootURL + "/api/dcim/inventory-items/"
if f != nil {
form := url.Values{}
err := encoder.Encode(f, form)
if err != nil {
return err
}
query := form.Encode()
deviceurl = deviceurl + "?" + query
}
var request *http.Request
var err error
request, err = http.NewRequest("GET", deviceurl, 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 (%s)\n", response.StatusCode, http.StatusOK, deviceurl)
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
err = response.Body.Close()
if err != nil {
return err
}
err = json.Unmarshal(data, &d)
if err != nil {
return err
}
return nil
}
func (n *NetBox) AddInventory(v *AddInventory) error {
data, err := json.Marshal(v)
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/dcim/inventory-items/", bytes.NewBuffer(data))
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)
}

View file

@ -1,217 +0,0 @@
package netboxgo
import (
"bytes"
"crypto/tls"
"encoding/json"
// "fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
// IpamPrefixesCreate is used for the return values from Netbox API ipam_prefixes_create
type IpamPrefixesCreate 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"`
}
// IpamPrefixesList is used for the return value from NetBox API ipam_prefixes_list
type IpamPrefixesList 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"`
}
// ListPrefixes returns Netbox ipam_prefixes_list
func (n *NetBox) ListPrefixes(i *IpamPrefixesList, 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 *IpamPrefixesCreate) 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)
}

View file

@ -1,157 +0,0 @@
package netboxgo
import (
"crypto/tls"
"encoding/json"
// "fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
type SiteGroupsList struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []SiteGroupList `json:"results"`
}
type SiteGroupList struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
Parent struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
SiteCount int `json:"site_count"`
Depth int `json:"_depth"`
} `json:"parent"`
Description string `json:"description"`
CustomFields struct {
} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
SiteCount int `json:"site_count"`
Depth int `json:"_depth"`
}
// SiteFilter is used to filter out returned object from Netbox API tenancy_tenants_list
type SiteGroupFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
// User specific filters
Name string `schema:"name,omitempty"`
ID string `schema:"id,omitempty"`
Slug string `schema:"slug,omitempty"`
Description string `schema:"description,omitempty"`
Group string `schema:"group,omitempty"`
GroupID string `schema:"group_id,omitempty"`
Parent string `schema:"parent,omitempty"`
IDIn string `schema:"id__in,omitempty"`
Q string `schema:"q,omitempty"`
Tag string `schema:"tag,omitempty"`
}
// ListSitegroups returns Netbox tenancy_tenants_list
func (n *NetBox) ListSiteGroups(i *SiteGroupsList, f *SiteFilter) 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/dcim/sites/?"+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
}
// GetSiteGroup returns a Netbox site-group
func (n *NetBox) GetSiteGroup(i *SiteGroupList, id string) error {
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/dcim/site-groups/"+id, 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
}

View file

@ -1,167 +0,0 @@
package netboxgo
import (
"bytes"
"crypto/tls"
"encoding/json"
// "fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
// TenancyTenantsCreate is used for the return values from Netbox API tenancy_tenants_create
type TenancyTenantsCreate struct {
Name string `json:"name"`
Slug string `json:"slug"`
Group int `json:"group"`
Description string `json:"description"`
Comments string `json:"comments"`
Tags []string `json:"tags"`
CustomFields struct {
} `json:"custom_fields"`
}
// TenancyTenantsList is used for the return value from NetBox API tenancy_tenants_list
type TenancyTenantsList struct {
Count int `json:"count"`
Next string `json:"next"`
Previous interface{} `json:"previous"`
Results []struct {
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Group struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"group"`
Description string `json:"description"`
Comments string `json:"comments"`
Tags []interface{} `json:"tags"`
CustomFields struct {
AccountNumber string `json:"account_number"`
AtlasCustNumber interface{} `json:"atlas_cust_number"`
TextField interface{} `json:"text_field"`
} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
CircuitCount interface{} `json:"circuit_count"`
DeviceCount int `json:"device_count"`
IpaddressCount interface{} `json:"ipaddress_count"`
PrefixCount int `json:"prefix_count"`
RackCount interface{} `json:"rack_count"`
SiteCount int `json:"site_count"`
VirtualmachineCount interface{} `json:"virtualmachine_count"`
VlanCount int `json:"vlan_count"`
VrfCount interface{} `json:"vrf_count"`
} `json:"results"`
}
// TenantFilter is used to filter out returned object from Netbox API tenancy_tenants_list
type TenantFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
// User specific filters
Name string `schema:"name,omitempty"`
Slug string `schema:"slug,omitempty"`
Group string `schema:"group,omitempty"`
GroupID string `schema:"group_id,omitempty"`
IDIn string `schema:"id__in,omitempty"`
Q string `schema:"q,omitempty"`
Tag string `schema:"tag,omitempty"`
}
// ListTenants returns Netbox tenancy_tenants_list
func (n *NetBox) ListTenants(i *TenancyTenantsList, f *TenantFilter) 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/tenancy/tenants/?"+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
}
// CreateTenants creates interfaces via Netbox API tenancy_tenants_create
func (n *NetBox) CreateTenants(i *TenancyTenantsCreate) 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/tenancy/tenants/", 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)
}

View file

@ -1,284 +0,0 @@
package netboxgo
import (
"bytes"
"crypto/tls"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
//VirtualizationVirtualMachinesCreate is used to create new VirtualizationVirtualMachines
type VirtualizationVirtualMachinesCreate struct {
ID int `json:"id"`
Name string `json:"name"`
Status struct {
Label string `json:"label"`
Value int `json:"value"`
} `json:"status"`
Site struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"site"`
Cluster struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"cluster"`
Role struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"role"`
Tenant struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"tenant"`
Platform struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"platform"`
PrimaryIP struct {
ID int `json:"id"`
URL string `json:"url"`
Family int `json:"family"`
Address string `json:"address"`
} `json:"primary_ip"`
PrimaryIP4 struct {
ID int `json:"id"`
URL string `json:"url"`
Family int `json:"family"`
Address string `json:"address"`
} `json:"primary_ip4"`
PrimaryIP6 struct {
ID int `json:"id"`
URL string `json:"url"`
Family int `json:"family"`
Address string `json:"address"`
} `json:"primary_ip6"`
Vcpus int `json:"vcpus"`
Memory int `json:"memory"`
Disk int `json:"disk"`
Comments string `json:"comments"`
LocalContextData string `json:"local_context_data"`
Tags []string `json:"tags"`
CustomFields struct {
} `json:"custom_fields"`
ConfigContext struct {
AdditionalProp1 string `json:"additionalProp1"`
AdditionalProp2 string `json:"additionalProp2"`
AdditionalProp3 string `json:"additionalProp3"`
} `json:"config_context"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
}
//VirtualizationClustersCreate is used to create new VirtualizationClusters
type VirtualizationClustersCreate struct {
ID int `json:"id"`
Name string `json:"name"`
Type struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ClusterCount int `json:"cluster_count"`
} `json:"type"`
Group struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ClusterCount int `json:"cluster_count"`
} `json:"group"`
Site struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"site"`
Comments string `json:"comments"`
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"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
}
// VirtualizationClustersList is used to list VirtualizationClusters
type VirtualizationClustersList struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []struct {
ID int `json:"id"`
Name string `json:"name"`
Type struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ClusterCount int `json:"cluster_count"`
} `json:"type"`
Group struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ClusterCount int `json:"cluster_count"`
} `json:"group"`
Site struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"site"`
Comments string `json:"comments"`
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"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"results"`
}
// ClusterFilter is used to filter out VirtualizationClusters
type ClusterFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
//User specific filters
Name string `schema:"name,omitempty"`
Group string `schema:"group,omitempty"`
GroupID string `schema:"group_id,omitempty"`
TypeID string `schema:"type_id,omitempty"`
Type string `schema:"type,omitempty"`
SiteID string `schema:"site_id,omitempty"`
Site string `schema:"site,omitempty"`
Tenant string `schema:"tenant,omitempty"`
IDIn string `schema:"id__in,omitempty"`
Q string `schema:"q,omitempty"`
Tag string `schema:"tag,omitempty"`
}
// ListClusters returns Netbox virtualization_clusters
func (n *NetBox) ListClusters(i *VirtualizationClustersList, f *ClusterFilter) error {
var 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/virtualization/clusters/?"+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
}
// CreateVirtualMachine creates VirtualMachines via Netbox API dcim_interfaces_create
func (n *NetBox) CreateVirtualMachine(v *VirtualizationClustersCreate) error {
data, err := json.Marshal(v)
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/virtualization/virtual-machines/", bytes.NewBuffer(data))
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)
}

View file

@ -1,240 +0,0 @@
package netboxgo
import (
"bytes"
"crypto/tls"
"encoding/json"
// "fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
// IpamVLANsCreate is used for the return values from Netbox API ipam_vlans_create
type IpamVLANsCreate 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 IpamVLAN 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 IpamVLANsList struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []IpamVLAN `json:"results"`
}
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"`
}
// 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"`
}
// ListVLANs returns Netbox ipam_vlans_list
func (n *NetBox) ListVLANs(i *IpamVLANsList, f *VLANFilter) 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/vlans/?"+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
}
// CreateVLANs creates interfaces via Netbox API ipam_vlans_create
func (n *NetBox) CreateVLANs(i *IpamVLANsCreate) 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/vlans/", 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)
}

View file

@ -1,161 +0,0 @@
package netboxgo
import (
"bytes"
"crypto/tls"
"encoding/json"
// "fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
// IpamVrfsCreate is used for the return values from Netbox API ipam_vrfs_create
type IpamVrfsCreate 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"`
}
// IpamVrfsList is used for the return value from NetBox API ipam_vrfs_list
type IpamVrfsList 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"`
}
// ListVrfs returns Netbox ipam_vrfs_list
func (n *NetBox) ListVrfs(i *IpamVrfsList, f *VrfFilter) error {
var 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/vrfs/?"+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
}
// CreateVrfs creates interfaces via Netbox API ipam_vrfs_create
func (n *NetBox) CreateVrfs(i *IpamVrfsCreate) 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/vrfs/", 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)
}

282
prefixes.go Normal file
View file

@ -0,0 +1,282 @@
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)
// }

View file

@ -1,18 +1,16 @@
package netboxgo
import (
"crypto/tls"
"encoding/json"
"io/ioutil"
"context"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
type RearPortList struct {
type RearPortsService service
type RearPorts struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
@ -54,58 +52,33 @@ type RearPortFilter struct {
Device string `schema:"device,omitempty"`
}
// ListRearPorts method returns dcim_device_list from Netbox API
func (n *NetBox) ListRearPorts(d *RearPortList, f *RearPortFilter) error {
const rearPortsPath = "/dcim/rear-ports"
// List rearports. RearPortFilter is used to list based on filter queries.
func (s *RearPortsService) List(ctx context.Context, f *RearPortFilter) (*RearPorts, error) {
var rearports RearPorts
var query string
var req *http.Request
var err error
encoder := schema.NewEncoder()
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
}
timeout := time.Duration(60 * time.Second)
client := &http.Client{
Timeout: timeout,
Transport: transport,
}
deviceurl := n.RootURL + "/api/dcim/rear-ports/"
if f != nil {
form := url.Values{}
err := encoder.Encode(f, form)
err = encoder.Encode(f, form)
if err != nil {
return err
return &rearports, err
}
query := form.Encode()
deviceurl = deviceurl + "?" + query
}
var request *http.Request
var err error
request, err = http.NewRequest("GET", deviceurl, nil)
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", rearPortsPath, 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
return &rearports, err
}
if response.StatusCode != http.StatusOK {
return errors.Errorf("Error: response was: %d should be %d (%s)\n", response.StatusCode, http.StatusOK, deviceurl)
_, err = s.client.do(req, &rearports)
if err != nil {
return &rearports, err
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
err = response.Body.Close()
if err != nil {
return err
}
err = json.Unmarshal(data, &d)
if err != nil {
return err
}
return nil
return &rearports, nil
}

View file

@ -1,19 +1,19 @@
package netboxgo
import (
"crypto/tls"
"encoding/json"
"io/ioutil"
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
// SecretsList contains secrets
type SecretsList struct {
type SecretsService service
// Secrets contains secrets
type Secrets struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
@ -42,8 +42,7 @@ type SecretsList struct {
Slug string `json:"slug"`
Color string `json:"color"`
} `json:"tags"`
CustomFields struct {
} `json:"custom_fields"`
CustomFields struct{} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
} `json:"results"`
@ -66,54 +65,40 @@ type SecretFilter struct {
Tag string `schema:"tag,omitempty"`
}
// ListSecrets returns Netbox Secrets
func (n *NetBox) ListSecrets(i *SecretsList, f *SecretFilter) error {
const secretsPath = "/secrets/secrets"
// List secrets. SecretsFilter is used to list based on filter queries.
func (s *SecretsService) List(ctx context.Context, f *SecretFilter) (*Secrets, error) {
var secrets Secrets
var query string
var req *http.Request
var err error
if s.client.SessionKey == "" {
return &secrets, fmt.Errorf("session key needed when interacting with secrets. no session key found: you need to fetch a session key with FetchSessionKey()")
}
encoder := schema.NewEncoder()
form := url.Values{}
err := encoder.Encode(f, form)
err = encoder.Encode(f, form)
if err != nil {
return err
return &secrets, err
}
query := form.Encode()
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/secrets/secrets/?"+query, nil)
req, err = s.client.newRequest(ctx, "GET", devicesPath, query, nil)
if err != nil {
return err
}
request.Header.Add("Accept", "application/json")
request.Header.Add("Authorization", " Token "+n.Token)
request.Header.Add("X-Session-Key", n.SessionKey)
response, err := client.Do(request)
if err != nil {
return err
return &secrets, err
}
if response.StatusCode != http.StatusOK {
return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusOK)
_, err = s.client.do(req, &secrets)
if err != nil {
return &secrets, err
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
// Clean c.client.SessionKey from memory
s.client.SessionKey = ""
err = response.Body.Close()
if err != nil {
return err
}
err = json.Unmarshal(data, &i)
if err != nil {
return err
}
return nil
return &secrets, nil
}

151
site_groups.go Normal file
View file

@ -0,0 +1,151 @@
package netboxgo
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
)
type SiteGroupsService service
type SiteGroups struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []SiteGroup `json:"results"`
}
type SiteGroup struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
Parent struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
SiteCount int `json:"site_count"`
Depth int `json:"_depth"`
} `json:"parent"`
Description string `json:"description"`
CustomFields struct{} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
SiteCount int `json:"site_count"`
Depth int `json:"_depth"`
}
// SiteFilter is used to filter out returned object from Netbox API tenancy_tenants_list
type SiteGroupFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
// User specific filters
Name string `schema:"name,omitempty"`
ID string `schema:"id,omitempty"`
Slug string `schema:"slug,omitempty"`
Description string `schema:"description,omitempty"`
Group string `schema:"group,omitempty"`
GroupID string `schema:"group_id,omitempty"`
Parent string `schema:"parent,omitempty"`
IDIn string `schema:"id__in,omitempty"`
Q string `schema:"q,omitempty"`
Tag string `schema:"tag,omitempty"`
}
const siteGroupsPath = "/dcim/site-groups"
// List sitegroups. SiteGroupFilter is used to list based on filter queries.
func (s *SiteGroupsService) List(ctx context.Context, f *SiteGroupFilter) (*SiteGroups, error) {
var sitegroups SiteGroups
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 &sitegroups, err
}
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", siteGroupsPath, query, nil)
if err != nil {
return &sitegroups, err
}
_, err = s.client.do(req, &sitegroups)
if err != nil {
return &sitegroups, err
}
return &sitegroups, nil
}
// Create a sitegroup
// func (s *SiteGroupsService) Create(ctx context.Context, c *NewSiteGroup) error {
// var err error
// var req *http.Request
//
// req, err = s.client.newRequest(ctx, "POST", siteGroupsPath, "", 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
// }
// Get a SiteGroup based on id
func (s *SiteGroupsService) Get(ctx context.Context, id string) (*SiteGroup, error) {
var sitegroup SiteGroup
var err error
path := fmt.Sprintf("%s/%s", siteGroupsPath, id)
req, err := s.client.newRequest(ctx, "GET", path, "", nil)
if err != nil {
return &sitegroup, err
}
_, err = s.client.do(req, &sitegroup)
if err != nil {
return &sitegroup, err
}
return &sitegroup, err
}
// // Update a device
// func (s *SiteGroupsService) Update(ctx context.Context, id string, c *UpdateDevice) error {
// var req *http.Request
// var err error
// path := fmt.Sprintf("%s/%s", siteGroupsPath, 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
// }

View file

@ -1,27 +1,25 @@
package netboxgo
import (
"crypto/tls"
"encoding/json"
// "fmt"
"io/ioutil"
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
type SitesList struct {
type SitesService service
type Sites struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []SiteList `json:"results"`
Results []Site `json:"results"`
}
type SiteList struct {
type Site struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
@ -76,8 +74,7 @@ type SiteList struct {
Slug string `json:"slug"`
Color string `json:"color"`
} `json:"tags"`
CustomFields struct {
} `json:"custom_fields"`
CustomFields struct{} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
CircuitCount int `json:"circuit_count"`
@ -104,95 +101,92 @@ type SiteFilter struct {
Tag string `schema:"tag,omitempty"`
}
// ListTenants returns Netbox tenancy_tenants_list
func (n *NetBox) ListSites(i *SitesList, f *SiteFilter) error {
const sitesPath = "/dcim/sites"
// List sites. SiteFilter is used to list based on filter queries.
func (s *SitesService) List(ctx context.Context, f *SiteFilter) (*Sites, error) {
var sites Sites
var query string
var req *http.Request
var err error
encoder := schema.NewEncoder()
form := url.Values{}
err := encoder.Encode(f, form)
err = encoder.Encode(f, form)
if err != nil {
return err
return &sites, err
}
query := form.Encode()
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/dcim/sites/?"+query, nil)
req, err = s.client.newRequest(ctx, "GET", sitesPath, 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
return &sites, err
}
if response.StatusCode != http.StatusOK {
return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusOK)
_, err = s.client.do(req, &sites)
if err != nil {
return &sites, err
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
return &sites, nil
}
err = response.Body.Close()
// Get a Site based on id
func (s *SitesService) Get(ctx context.Context, id string) (*Site, error) {
var site Site
var err error
path := fmt.Sprintf("%s/%s", sitesPath, id)
req, err := s.client.newRequest(ctx, "GET", path, "", nil)
if err != nil {
return err
return &site, err
}
err = json.Unmarshal(data, &i)
_, err = s.client.do(req, &site)
if err != nil {
return err
}
return nil
return &site, err
}
// GetSite returns Netbox tenancy_tenants_list
func (n *NetBox) GetSite(i *SiteList, id string) error {
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/dcim/sites/"+id, 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
return &site, err
}
if response.StatusCode != http.StatusOK {
return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusOK)
}
// // Create a site
// func (s *SitesService) Create(ctx context.Context, c *NewSite) error {
// var err error
// var req *http.Request
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
// req, err = s.client.newRequest(ctx, "POST", sitesPath, "", c)
// if err != nil {
// return fmt.Errorf("unable to create request: %w", err)
// }
err = response.Body.Close()
if err != nil {
return err
}
// _, err = s.client.do(req, nil)
// if err != nil {
// return fmt.Errorf("unable to do request: %w", err)
// }
err = json.Unmarshal(data, &i)
if err != nil {
return err
}
return nil
}
// return nil
// }
// // Update a device
// func (s *SitesService) Update(ctx context.Context, id string, c *UpdateDevice) error {
// var req *http.Request
// var err error
// path := fmt.Sprintf("%s/%s", sitesPath, 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
// }

145
tenants.go Normal file
View file

@ -0,0 +1,145 @@
package netboxgo
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
)
type TenantsService service
// NewTenant is used for the return values from Netbox API tenancy_tenants_create
type NewTenant struct {
Name string `json:"name"`
Slug string `json:"slug"`
Group int `json:"group"`
Description string `json:"description"`
Comments string `json:"comments"`
Tags []string `json:"tags"`
CustomFields struct{} `json:"custom_fields"`
}
// Tenants is used for the return value from NetBox API tenancy_tenants_list
type Tenants struct {
Count int `json:"count"`
Next string `json:"next"`
Previous interface{} `json:"previous"`
Results []struct {
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Group struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"group"`
Description string `json:"description"`
Comments string `json:"comments"`
Tags []interface{} `json:"tags"`
CustomFields struct {
AccountNumber string `json:"account_number"`
AtlasCustNumber interface{} `json:"atlas_cust_number"`
TextField interface{} `json:"text_field"`
} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
CircuitCount interface{} `json:"circuit_count"`
DeviceCount int `json:"device_count"`
IpaddressCount interface{} `json:"ipaddress_count"`
PrefixCount int `json:"prefix_count"`
RackCount interface{} `json:"rack_count"`
SiteCount int `json:"site_count"`
VirtualmachineCount interface{} `json:"virtualmachine_count"`
VlanCount int `json:"vlan_count"`
VrfCount interface{} `json:"vrf_count"`
} `json:"results"`
}
// TenantFilter is used to filter out returned object from Netbox API tenancy_tenants_list
type TenantFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
// User specific filters
Name string `schema:"name,omitempty"`
Slug string `schema:"slug,omitempty"`
Group string `schema:"group,omitempty"`
GroupID string `schema:"group_id,omitempty"`
IDIn string `schema:"id__in,omitempty"`
Q string `schema:"q,omitempty"`
Tag string `schema:"tag,omitempty"`
}
const tenantsPath = "/tenancy/tenants"
// List tenants. TenantFilter is used to list based on filter queries.
func (s *TenantsService) List(ctx context.Context, f *TenantFilter) (*Tenants, error) {
var tenants Tenants
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 &tenants, err
}
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", tenantsPath, query, nil)
if err != nil {
return &tenants, err
}
_, err = s.client.do(req, &tenants)
if err != nil {
return &tenants, err
}
return &tenants, nil
}
// Create a tenant
func (s *TenantsService) Create(ctx context.Context, c *NewTenant) error {
var err error
var req *http.Request
req, err = s.client.newRequest(ctx, "POST", tenantsPath, "", 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 *TenantsService) Update(ctx context.Context, id string, c *UpdateDevice) error {
// var req *http.Request
// var err error
// path := fmt.Sprintf("%s/%s", tenantsPath, 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
// }

106
virtual_machines.go Normal file
View file

@ -0,0 +1,106 @@
package netboxgo
import (
"context"
"fmt"
"net/http"
"time"
)
type VirtualMachinesService service
// NewVirtualMachine is used to create new VirtualizationVirtualMachines
type NewVirtualMachine struct {
ID int `json:"id"`
Name string `json:"name"`
Status struct {
Label string `json:"label"`
Value int `json:"value"`
} `json:"status"`
Site struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"site"`
Cluster struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"cluster"`
Role struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"role"`
Tenant struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"tenant"`
Platform struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"platform"`
PrimaryIP struct {
ID int `json:"id"`
URL string `json:"url"`
Family int `json:"family"`
Address string `json:"address"`
} `json:"primary_ip"`
PrimaryIP4 struct {
ID int `json:"id"`
URL string `json:"url"`
Family int `json:"family"`
Address string `json:"address"`
} `json:"primary_ip4"`
PrimaryIP6 struct {
ID int `json:"id"`
URL string `json:"url"`
Family int `json:"family"`
Address string `json:"address"`
} `json:"primary_ip6"`
Vcpus int `json:"vcpus"`
Memory int `json:"memory"`
Disk int `json:"disk"`
Comments string `json:"comments"`
LocalContextData string `json:"local_context_data"`
Tags []string `json:"tags"`
CustomFields struct{} `json:"custom_fields"`
ConfigContext struct {
AdditionalProp1 string `json:"additionalProp1"`
AdditionalProp2 string `json:"additionalProp2"`
AdditionalProp3 string `json:"additionalProp3"`
} `json:"config_context"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
}
const virtualMachinesPath = "/virtualization/virtual-machines"
// Create a virtual-machine
func (s *VirtualMachinesService) Create(ctx context.Context, c *NewVirtualMachine) error {
var err error
var req *http.Request
req, err = s.client.newRequest(ctx, "POST", virtualMachinesPath, "", 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
}

172
vlans.go Normal file
View file

@ -0,0 +1,172 @@
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
// }

138
vrfs.go Normal file
View file

@ -0,0 +1,138 @@
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
// }