Fix virtualization and DCIM interfaces
This commit is contained in:
parent
232b80583a
commit
551409f431
3 changed files with 250 additions and 42 deletions
|
@ -12,10 +12,10 @@ import (
|
||||||
"github.com/gorilla/schema"
|
"github.com/gorilla/schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InterfacesService service
|
type DCIMInterfacesService service
|
||||||
|
|
||||||
// NewInterface is used for creating a new interface
|
// NewDCIMInterface is used for creating a new interface
|
||||||
type NewInterface struct {
|
type NewDCIMInterface struct {
|
||||||
CustomFields interface{} `json:"custom_fields"`
|
CustomFields interface{} `json:"custom_fields"`
|
||||||
WWN string `json:"wwn"`
|
WWN string `json:"wwn"`
|
||||||
Label string `json:"label"`
|
Label string `json:"label"`
|
||||||
|
@ -51,16 +51,16 @@ type NewInterface struct {
|
||||||
MGMTOnly bool `json:"mgmt_only"`
|
MGMTOnly bool `json:"mgmt_only"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interfaces is a list of interfaces
|
// DCIMInterfaces is a list of interfaces
|
||||||
type Interfaces struct {
|
type DCIMInterfaces struct {
|
||||||
Next interface{} `json:"next,omitempty"`
|
Next interface{} `json:"next,omitempty"`
|
||||||
Previous interface{} `json:"previous,omitempty"`
|
Previous interface{} `json:"previous,omitempty"`
|
||||||
Results []Interface `json:"results,omitempty"`
|
Results []DCIMInterface `json:"results,omitempty"`
|
||||||
Count int `json:"count,omitempty"`
|
Count int `json:"count,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interface is one interface
|
// DCIMInterface is one interface
|
||||||
type Interface struct {
|
type DCIMInterface struct {
|
||||||
CustomFields struct{} `json:"custom_fields"`
|
CustomFields struct{} `json:"custom_fields"`
|
||||||
LastUpdated time.Time `json:"last_updated"`
|
LastUpdated time.Time `json:"last_updated"`
|
||||||
LinkPeer interface{} `json:"link_peer"`
|
LinkPeer interface{} `json:"link_peer"`
|
||||||
|
@ -193,8 +193,8 @@ type Interface struct {
|
||||||
Occupied bool `json:"_occupied"`
|
Occupied bool `json:"_occupied"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// InterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list
|
// DCIMInterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list
|
||||||
type InterfaceFilter struct {
|
type DCIMInterfaceFilter struct {
|
||||||
// User specific filters
|
// User specific filters
|
||||||
ID string `schema:"id,omitempty"`
|
ID string `schema:"id,omitempty"`
|
||||||
Name string `schema:"name,omitempty"`
|
Name string `schema:"name,omitempty"`
|
||||||
|
@ -222,8 +222,8 @@ type InterfaceFilter struct {
|
||||||
const interfacesPath = dcimPath + "/interfaces"
|
const interfacesPath = dcimPath + "/interfaces"
|
||||||
|
|
||||||
// List devices. DeviceFilter is used to list based on filter queries.
|
// List devices. DeviceFilter is used to list based on filter queries.
|
||||||
func (s *InterfacesService) List(ctx context.Context, f *InterfaceFilter) (*Interfaces, error) {
|
func (s *DCIMInterfacesService) List(ctx context.Context, f *DCIMInterfaceFilter) (*DCIMInterfaces, error) {
|
||||||
var interfaces Interfaces
|
var interfaces DCIMInterfaces
|
||||||
var query string
|
var query string
|
||||||
var req *http.Request
|
var req *http.Request
|
||||||
var err error
|
var err error
|
||||||
|
@ -251,10 +251,10 @@ func (s *InterfacesService) List(ctx context.Context, f *InterfaceFilter) (*Inte
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a interfaces
|
// Create a interfaces
|
||||||
func (s *InterfacesService) Create(ctx context.Context, c *NewInterface) (*Interface, error) {
|
func (s *DCIMInterfacesService) Create(ctx context.Context, c *NewDCIMInterface) (*DCIMInterface, error) {
|
||||||
var err error
|
var err error
|
||||||
var req *http.Request
|
var req *http.Request
|
||||||
var nic Interface
|
var nic DCIMInterface
|
||||||
|
|
||||||
req, err = s.client.newRequest(ctx, "POST", interfacesPath, "", c)
|
req, err = s.client.newRequest(ctx, "POST", interfacesPath, "", c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -270,7 +270,7 @@ func (s *InterfacesService) Create(ctx context.Context, c *NewInterface) (*Inter
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete a interface
|
// Delete a interface
|
||||||
func (s *InterfacesService) Delete(ctx context.Context, i string) error {
|
func (s *DCIMInterfacesService) Delete(ctx context.Context, i string) error {
|
||||||
var err error
|
var err error
|
||||||
var req *http.Request
|
var req *http.Request
|
||||||
|
|
44
netbox.go
44
netbox.go
|
@ -15,29 +15,24 @@ import (
|
||||||
|
|
||||||
// Client struct is used to create a new NetBox endpoint
|
// Client struct is used to create a new NetBox endpoint
|
||||||
type Client struct {
|
type Client struct {
|
||||||
// DCIM *DCIM
|
Clusters *ClustersService
|
||||||
// Tenancy *Tenancy
|
ClusterTypes *ClusterTypesService
|
||||||
// IPAM *IPAM
|
Devices *DevicesService
|
||||||
// Virtualization *Virtualization
|
DeviceRoles *DeviceRolesService
|
||||||
// Secret *Secret
|
DeviceTypes *DeviceTypesService
|
||||||
|
DCIMInterfaces *DCIMInterfacesService
|
||||||
Clusters *ClustersService
|
InventoryItems *InventoryItemsService
|
||||||
ClusterTypes *ClusterTypesService
|
Secrets *SecretsService
|
||||||
Devices *DevicesService
|
Sites *SitesService
|
||||||
DeviceRoles *DeviceRolesService
|
SiteGroups *SiteGroupsService
|
||||||
DeviceTypes *DeviceTypesService
|
Platforms *PlatformsService
|
||||||
Interfaces *InterfacesService
|
Prefixes *PrefixesService
|
||||||
InventoryItems *InventoryItemsService
|
RearPorts *RearPortsService
|
||||||
Secrets *SecretsService
|
Tenants *TenantsService
|
||||||
Sites *SitesService
|
VirtualMachines *VirtualMachinesService
|
||||||
SiteGroups *SiteGroupsService
|
VirtualizationInterfaces *VirtualizationInterfacesService
|
||||||
Platforms *PlatformsService
|
VRFs *VRFsService
|
||||||
Prefixes *PrefixesService
|
VLANs *VLANsService
|
||||||
RearPorts *RearPortsService
|
|
||||||
Tenants *TenantsService
|
|
||||||
VirtualMachines *VirtualMachinesService
|
|
||||||
VRFs *VRFsService
|
|
||||||
VLANs *VLANsService
|
|
||||||
|
|
||||||
// baseURL is the URL used for the base URL of the API
|
// baseURL is the URL used for the base URL of the API
|
||||||
baseURL *url.URL
|
baseURL *url.URL
|
||||||
|
@ -132,7 +127,8 @@ func NewClient(apiurl string, httpClient *http.Client) (*Client, error) {
|
||||||
c.Devices = (*DevicesService)(&c.common)
|
c.Devices = (*DevicesService)(&c.common)
|
||||||
c.DeviceRoles = (*DeviceRolesService)(&c.common)
|
c.DeviceRoles = (*DeviceRolesService)(&c.common)
|
||||||
c.DeviceTypes = (*DeviceTypesService)(&c.common)
|
c.DeviceTypes = (*DeviceTypesService)(&c.common)
|
||||||
c.Interfaces = (*InterfacesService)(&c.common)
|
c.DCIMInterfaces = (*DCIMInterfacesService)(&c.common)
|
||||||
|
c.VirtualizationInterfaces = (*VirtualizationInterfacesService)(&c.common)
|
||||||
c.InventoryItems = (*InventoryItemsService)(&c.common)
|
c.InventoryItems = (*InventoryItemsService)(&c.common)
|
||||||
c.Prefixes = (*PrefixesService)(&c.common)
|
c.Prefixes = (*PrefixesService)(&c.common)
|
||||||
c.Platforms = (*PlatformsService)(&c.common)
|
c.Platforms = (*PlatformsService)(&c.common)
|
||||||
|
|
212
virtualization_interfaces.go
Normal file
212
virtualization_interfaces.go
Normal file
|
@ -0,0 +1,212 @@
|
||||||
|
package netboxgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
type VirtualizationInterfacesService service
|
||||||
|
|
||||||
|
// NewVirtualizationInterface is used for creating a new virtualization interface
|
||||||
|
type NewVirtualizationInterface struct {
|
||||||
|
VirtualMachine int `json:"virtual_machine"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
Parent int `json:"parent"`
|
||||||
|
Bridge int `json:"bridge"`
|
||||||
|
MTU int `json:"mtu"`
|
||||||
|
MACAddress string `json:"mac_address"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Mode string `json:"mode"`
|
||||||
|
UntaggedVLAN int `json:"untagged_vlan"`
|
||||||
|
TaggedVLANs []int `json:"tagged_vlans"`
|
||||||
|
Tags []struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Slug string `json:"slug"`
|
||||||
|
Color string `json:"color"`
|
||||||
|
} `json:"tags"`
|
||||||
|
CustomFields interface{} `json:"custom_fields"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// VirtualizationInterfaces is a list of interfaces
|
||||||
|
type VirtualizationInterfaces struct {
|
||||||
|
Next string `json:"next,omitempty"`
|
||||||
|
Previous string `json:"previous,omitempty"`
|
||||||
|
Results []VirtualizationInterface `json:"results,omitempty"`
|
||||||
|
Count int `json:"count,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// VirtualizationInterface is one virtualization interface
|
||||||
|
type VirtualizationInterface struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Display string `json:"display"`
|
||||||
|
VirtualMachine struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Display string `json:"display"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
} `json:"virtual_machine"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
Parent struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Display string `json:"display"`
|
||||||
|
VirtualMachine struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Display string `json:"display"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
} `json:"virtual_machine"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
} `json:"parent"`
|
||||||
|
Bridge struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Display string `json:"display"`
|
||||||
|
VirtualMachine struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Display string `json:"display"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
} `json:"virtual_machine"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
} `json:"bridge"`
|
||||||
|
MTU int `json:"mtu"`
|
||||||
|
MACAddress string `json:"mac_address"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Mode struct {
|
||||||
|
Label string `json:"label"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
} `json:"mode"`
|
||||||
|
UntaggedVLAN struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Display string `json:"display"`
|
||||||
|
Vid int `json:"vid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
} `json:"untagged_vlan"`
|
||||||
|
TaggedVLANs []struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Display string `json:"display"`
|
||||||
|
Vid int `json:"vid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
} `json:"tagged_vlans"`
|
||||||
|
Tags []struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Display string `json:"display"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Slug string `json:"slug"`
|
||||||
|
Color string `json:"color"`
|
||||||
|
} `json:"tags"`
|
||||||
|
CustomFields interface{} `json:"custom_fields"`
|
||||||
|
Created string `json:"created"`
|
||||||
|
LastUpdated time.Time `json:"last_updated"`
|
||||||
|
CountIPAddresses int `json:"count_ipaddresses"`
|
||||||
|
CountFHRPGroups int `json:"count_fhrp_groups"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// VirtualizationInterfaceFilter is used to filter out virtualization interfaces
|
||||||
|
type VirtualizationInterfaceFilter struct {
|
||||||
|
// User specific filters
|
||||||
|
ID string `schema:"id,omitempty"`
|
||||||
|
Name string `schema:"name,omitempty"`
|
||||||
|
ConnectionStatus string `schema:"connection_status,omitempty"`
|
||||||
|
Type string `schema:"type,omitempty"`
|
||||||
|
MTU string `schema:"mtu,omitempty"`
|
||||||
|
MGMTOnly string `schema:"mgmt_only,omitempty"`
|
||||||
|
Mode string `schema:"mode,omitempty"`
|
||||||
|
Description string `schema:"description,omitempty"`
|
||||||
|
Q string `schema:"q,omitempty"`
|
||||||
|
VirtualMachine string `schema:"device,omitempty"`
|
||||||
|
ClusterID string `schema:"cluster_id,omitempty"`
|
||||||
|
VirtualMachineID string `schema:"virtual_machine_id,omitempty"`
|
||||||
|
Cabled string `schema:"cabled,omitempty"`
|
||||||
|
Kind string `schema:"kind,omitempty"`
|
||||||
|
LagID string `schema:"lag_id,omitempty"`
|
||||||
|
MACAddress string `schema:"mac_address,omitempty"`
|
||||||
|
Tag string `schema:"tag,omitempty"`
|
||||||
|
VLANID string `schema:"vlan_id,omitempty"`
|
||||||
|
VLAN string `schema:"vlan,omitempty"`
|
||||||
|
ParentID string `schema:"parent_id,omitempty"`
|
||||||
|
BridgeID string `schema:"bridge_id,omitempty"`
|
||||||
|
|
||||||
|
Offset int64 `schema:"offset,omitempty"`
|
||||||
|
Limit int64 `schema:"limit,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
const virtualizationInterfacesPath = virtualizationPath + "/interfaces"
|
||||||
|
|
||||||
|
// List devices. DeviceFilter is used to list based on filter queries.
|
||||||
|
func (s *VirtualizationInterfacesService) List(ctx context.Context, f *VirtualizationInterfaceFilter) (*VirtualizationInterfaces, error) {
|
||||||
|
var interfaces VirtualizationInterfaces
|
||||||
|
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 &interfaces, err
|
||||||
|
}
|
||||||
|
query = form.Encode()
|
||||||
|
|
||||||
|
req, err = s.client.newRequest(ctx, "GET", interfacesPath, query, nil)
|
||||||
|
if err != nil {
|
||||||
|
return &interfaces, err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = s.client.do(req, &interfaces)
|
||||||
|
if err != nil {
|
||||||
|
return &interfaces, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &interfaces, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a interfaces
|
||||||
|
func (s *VirtualizationInterfacesService) Create(ctx context.Context, c *NewVirtualizationInterface) (*VirtualizationInterface, error) {
|
||||||
|
var err error
|
||||||
|
var req *http.Request
|
||||||
|
var nic VirtualizationInterface
|
||||||
|
|
||||||
|
req, err = s.client.newRequest(ctx, "POST", interfacesPath, "", c)
|
||||||
|
if err != nil {
|
||||||
|
return &nic, fmt.Errorf("unable to create request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = s.client.do(req, &nic)
|
||||||
|
if err != nil {
|
||||||
|
return &nic, fmt.Errorf("unable to do request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &nic, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete a interface
|
||||||
|
func (s *VirtualizationInterfacesService) Delete(ctx context.Context, i string) error {
|
||||||
|
var err error
|
||||||
|
var req *http.Request
|
||||||
|
|
||||||
|
req, err = s.client.newRequest(ctx, "DELETE", virtualizationInterfacesPath+"/"+i+"/", "", nil)
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in a new issue