Fix virtualization and DCIM interfaces

This commit is contained in:
Kalle Carlbark 2022-02-14 14:12:46 +01:00
parent 232b80583a
commit 551409f431
No known key found for this signature in database
3 changed files with 250 additions and 42 deletions

View file

@ -12,10 +12,10 @@ import (
"github.com/gorilla/schema"
)
type InterfacesService service
type DCIMInterfacesService service
// NewInterface is used for creating a new interface
type NewInterface struct {
// NewDCIMInterface is used for creating a new interface
type NewDCIMInterface struct {
CustomFields interface{} `json:"custom_fields"`
WWN string `json:"wwn"`
Label string `json:"label"`
@ -51,16 +51,16 @@ type NewInterface struct {
MGMTOnly bool `json:"mgmt_only"`
}
// Interfaces is a list of interfaces
type Interfaces struct {
// DCIMInterfaces is a list of interfaces
type DCIMInterfaces struct {
Next interface{} `json:"next,omitempty"`
Previous interface{} `json:"previous,omitempty"`
Results []Interface `json:"results,omitempty"`
Results []DCIMInterface `json:"results,omitempty"`
Count int `json:"count,omitempty"`
}
// Interface is one interface
type Interface struct {
// DCIMInterface is one interface
type DCIMInterface struct {
CustomFields struct{} `json:"custom_fields"`
LastUpdated time.Time `json:"last_updated"`
LinkPeer interface{} `json:"link_peer"`
@ -193,8 +193,8 @@ type Interface struct {
Occupied bool `json:"_occupied"`
}
// InterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list
type InterfaceFilter struct {
// DCIMInterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list
type DCIMInterfaceFilter struct {
// User specific filters
ID string `schema:"id,omitempty"`
Name string `schema:"name,omitempty"`
@ -222,8 +222,8 @@ type InterfaceFilter struct {
const interfacesPath = dcimPath + "/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
func (s *DCIMInterfacesService) List(ctx context.Context, f *DCIMInterfaceFilter) (*DCIMInterfaces, error) {
var interfaces DCIMInterfaces
var query string
var req *http.Request
var err error
@ -251,10 +251,10 @@ func (s *InterfacesService) List(ctx context.Context, f *InterfaceFilter) (*Inte
}
// 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 req *http.Request
var nic Interface
var nic DCIMInterface
req, err = s.client.newRequest(ctx, "POST", interfacesPath, "", c)
if err != nil {
@ -270,7 +270,7 @@ func (s *InterfacesService) Create(ctx context.Context, c *NewInterface) (*Inter
}
// 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 req *http.Request

View file

@ -15,18 +15,12 @@ import (
// Client struct is used to create a new NetBox endpoint
type Client struct {
// DCIM *DCIM
// Tenancy *Tenancy
// IPAM *IPAM
// Virtualization *Virtualization
// Secret *Secret
Clusters *ClustersService
ClusterTypes *ClusterTypesService
Devices *DevicesService
DeviceRoles *DeviceRolesService
DeviceTypes *DeviceTypesService
Interfaces *InterfacesService
DCIMInterfaces *DCIMInterfacesService
InventoryItems *InventoryItemsService
Secrets *SecretsService
Sites *SitesService
@ -36,6 +30,7 @@ type Client struct {
RearPorts *RearPortsService
Tenants *TenantsService
VirtualMachines *VirtualMachinesService
VirtualizationInterfaces *VirtualizationInterfacesService
VRFs *VRFsService
VLANs *VLANsService
@ -132,7 +127,8 @@ func NewClient(apiurl string, httpClient *http.Client) (*Client, error) {
c.Devices = (*DevicesService)(&c.common)
c.DeviceRoles = (*DeviceRolesService)(&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.Prefixes = (*PrefixesService)(&c.common)
c.Platforms = (*PlatformsService)(&c.common)

View 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
}