Compare commits

...

10 commits

Author SHA1 Message Date
836b8e717f
Slash needed? 2022-02-14 15:41:38 +01:00
d9a9af4eae
Omit ALL optional fields.. 2022-02-14 15:39:18 +01:00
63c2a86871
Omit empty fields 2022-02-14 15:35:08 +01:00
b3138a42e5
Fix interfaces path for virtualization and DCIM 2022-02-14 14:35:21 +01:00
Kalle Carlbark
551409f431
Fix virtualization and DCIM interfaces 2022-02-14 14:12:46 +01:00
Kalle Carlbark
232b80583a
Occupied is a bool, not string 2022-02-14 13:33:39 +01:00
Kalle Carlbark
c8c7c4edfc
Recommig f5bf955b0e 2022-02-14 13:29:23 +01:00
Kalle Carlbark
170fde8a31
Add ability to remove interfaces 2022-02-14 12:19:02 +01:00
Kalle Carlbark
2bd3658b60
Update NewInterface
To match Netbox version
2022-02-14 11:21:57 +01:00
Kalle Carlbark
236a9bb23e
When creating objects return the created object
TODO: Do this for all objects
2022-02-14 11:09:50 +01:00
5 changed files with 633 additions and 264 deletions

288
dcim_interfaces.go Normal file
View file

@ -0,0 +1,288 @@
package netboxgo
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
// "fmt"
"github.com/gorilla/schema"
)
type DCIMInterfacesService service
// NewDCIMInterface is used for creating a new interface
type NewDCIMInterface struct {
CustomFields interface{} `json:"custom_fields"`
WWN string `json:"wwn"`
Label string `json:"label"`
Type string `json:"type"`
Cable struct {
Label string `json:"label"`
} `json:"cable"`
RFChannel string `json:"rf_channel"`
RFRole string `json:"rf_role"`
Name string `json:"name"`
Description string `json:"description"`
MACAddress string `json:"mac_address"`
Mode string `json:"mode"`
TaggedVLANs []int `json:"tagged_vlans"`
WirelessLANs []int `json:"wireless_lans"`
Tags []struct {
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
} `json:"tags"`
Device int `json:"device"`
Parent int `json:"parent"`
RFChannelFrequency int `json:"rf_channel_frequency"`
RFChannelWidth int `json:"rf_channel_width"`
TXPower int `json:"tx_power"`
UntaggedVLAN int `json:"untagged_vlan"`
Bridge int `json:"bridge"`
LAG int `json:"lag"`
MTU int `json:"mtu"`
WirelessLink int `json:"wireless_link"`
Enabled bool `json:"enabled"`
MarkConnected bool `json:"mark_connected"`
MGMTOnly bool `json:"mgmt_only"`
}
// DCIMInterfaces is a list of interfaces
type DCIMInterfaces struct {
Next interface{} `json:"next,omitempty"`
Previous interface{} `json:"previous,omitempty"`
Results []DCIMInterface `json:"results,omitempty"`
Count int `json:"count,omitempty"`
}
// DCIMInterface is one interface
type DCIMInterface struct {
CustomFields struct{} `json:"custom_fields"`
LastUpdated time.Time `json:"last_updated"`
LinkPeer interface{} `json:"link_peer"`
ConnectedEndpoint interface{} `json:"connected_endpoint"`
Parent struct {
Occupied string `json:"_occupied"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Device struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
} `json:"device"`
Cable int `json:"cable"`
ID int `json:"id"`
} `json:"parent"`
Device struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
} `json:"device"`
RFChannel struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"rf_channel"`
RFRole struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"rf_role"`
Type struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"type"`
Mode struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"mode"`
Display string `json:"display"`
LinkPeerType string `json:"link_peer_type"`
MACAddress string `json:"mac_address"`
WWN string `json:"wwn"`
URL string `json:"url"`
Description string `json:"description"`
ConnectedEndpointType string `json:"connected_endpoint_type"`
Label string `json:"label"`
Name string `json:"name"`
Created string `json:"created"`
Cable struct {
URL string `json:"url"`
Display string `json:"display"`
Label string `json:"label"`
ID int `json:"id"`
} `json:"cable"`
WirelessLink struct {
URL string `json:"url"`
Display string `json:"display"`
Ssid string `json:"ssid"`
ID int `json:"id"`
} `json:"wireless_link"`
WirelessLANs []struct {
URL string `json:"url"`
Display string `json:"display"`
Ssid string `json:"ssid"`
ID int `json:"id"`
} `json:"wireless_lans"`
TaggedVLANs []struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
Vid int `json:"vid"`
} `json:"tagged_vlans"`
Tags []struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
ID int `json:"id"`
} `json:"tags"`
UntaggedVLAN struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
Vid int `json:"vid"`
} `json:"untagged_vlan"`
LAG struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Device struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
} `json:"device"`
ID int `json:"id"`
Cable int `json:"cable"`
Occupied bool `json:"_occupied"`
} `json:"lag"`
Bridge struct {
Occupied string `json:"_occupied"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Device struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
} `json:"device"`
Cable int `json:"cable"`
ID int `json:"id"`
} `json:"bridge"`
CountIPAddresses int `json:"count_ipaddresses"`
MTU int `json:"mtu"`
TXPower int `json:"tx_power"`
CountFHRPGroups int `json:"count_fhrp_groups"`
RFChannelFrequency int `json:"rf_channel_frequency"`
ID int `json:"id"`
RFChannelWidth int `json:"rf_channel_width"`
ConnectedEndpointReachable bool `json:"connected_endpoint_reachable"`
MarkConnected bool `json:"mark_connected"`
MGMTOnly bool `json:"mgmt_only"`
Enabled bool `json:"enabled"`
Occupied bool `json:"_occupied"`
}
// 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"`
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"`
Device string `schema:"device,omitempty"`
DeviceID string `schema:"device_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"`
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
}
const dcimInterfacesPath = dcimPath + "/interfaces"
// List devices. DeviceFilter is used to list based on filter queries.
func (s *DCIMInterfacesService) List(ctx context.Context, f *DCIMInterfaceFilter) (*DCIMInterfaces, error) {
var interfaces DCIMInterfaces
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", dcimInterfacesPath, 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 *DCIMInterfacesService) Create(ctx context.Context, c *NewDCIMInterface) (*DCIMInterface, error) {
var err error
var req *http.Request
var nic DCIMInterface
req, err = s.client.newRequest(ctx, "POST", dcimInterfacesPath, "", 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 *DCIMInterfacesService) Delete(ctx context.Context, i string) error {
var err error
var req *http.Request
req, err = s.client.newRequest(ctx, "DELETE", dcimInterfacesPath+"/"+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
}

View file

@ -1,162 +0,0 @@
package netboxgo
import (
"context"
"fmt"
"net/http"
"net/url"
// "fmt"
"github.com/gorilla/schema"
)
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"`
Enabled bool `json:"enabled,omitempty"`
Lag int `json:"lag,omitempty"`
Mtu int `json:"mtu,omitempty"`
MacAddress string `json:"mac_address,omitempty"`
MgmtOnly bool `json:"mgmt_only,omitempty"`
Description string `json:"description,omitempty"`
ConnectionStatus bool `json:"connection_status,omitempty"`
Cable struct {
Label string `json:"label,omitempty"`
} `json:"cable,omitempty"`
Mode int `json:"mode,omitempty"`
UntaggedVLAN int `json:"untagged_vlan,omitempty"`
TaggedVLANs []int `json:"tagged_vlans,omitempty"`
Tags []string `json:"tags,omitempty"`
}
// 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"`
Results []struct {
ID int `json:"id,omitempty"`
Device struct {
ID int `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Name string `json:"name,omitempty"`
DisplayName string `json:"display_name,omitempty"`
} `json:"device,omitempty"`
Name string `json:"name,omitempty"`
Type struct {
Value int `json:"value,omitempty"`
Label string `json:"label,omitempty"`
} `json:"type,omitempty"`
FormFactor struct {
Value int `json:"value,omitempty"`
Label string `json:"label,omitempty"`
} `json:"form_factor,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Lag struct {
ID int `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Device struct {
ID int `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Name string `json:"name,omitempty"`
DisplayName string `json:"display_name,omitempty"`
} `json:"device,omitempty"`
Name string `json:"name,omitempty"`
Cable interface{} `json:"cable,omitempty"`
ConnectionStatus interface{} `json:"connection_status,omitempty"`
} `json:"lag,omitempty"`
Mtu interface{} `json:"mtu,omitempty"`
MacAddress interface{} `json:"mac_address,omitempty"`
MgmtOnly bool `json:"mgmt_only,omitempty"`
Description string `json:"description,omitempty"`
ConnectedEndpointType interface{} `json:"connected_endpoint_type,omitempty"`
ConnectedEndpoint interface{} `json:"connected_endpoint,omitempty"`
ConnectionStatus interface{} `json:"connection_status,omitempty"`
Cable interface{} `json:"cable,omitempty"`
Mode interface{} `json:"mode,omitempty"`
UntaggedVLAN interface{} `json:"untagged_vlan,omitempty"`
TaggedVLANs []interface{} `json:"tagged_vlans,omitempty"`
Tags []interface{} `json:"tags,omitempty"`
CountIpaddresses int `json:"count_ipaddresses,omitempty"`
} `json:"results,omitempty"`
}
// InterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list
type InterfaceFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
// 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"`
Device string `schema:"device,omitempty"`
DeviceID string `schema:"device_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"`
}
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
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 *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 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

@ -15,18 +15,12 @@ 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
// Tenancy *Tenancy
// IPAM *IPAM
// Virtualization *Virtualization
// Secret *Secret
Clusters *ClustersService Clusters *ClustersService
ClusterTypes *ClusterTypesService ClusterTypes *ClusterTypesService
Devices *DevicesService Devices *DevicesService
DeviceRoles *DeviceRolesService DeviceRoles *DeviceRolesService
DeviceTypes *DeviceTypesService DeviceTypes *DeviceTypesService
Interfaces *InterfacesService DCIMInterfaces *DCIMInterfacesService
InventoryItems *InventoryItemsService InventoryItems *InventoryItemsService
Secrets *SecretsService Secrets *SecretsService
Sites *SitesService Sites *SitesService
@ -36,6 +30,7 @@ type Client struct {
RearPorts *RearPortsService RearPorts *RearPortsService
Tenants *TenantsService Tenants *TenantsService
VirtualMachines *VirtualMachinesService VirtualMachines *VirtualMachinesService
VirtualizationInterfaces *VirtualizationInterfacesService
VRFs *VRFsService VRFs *VRFsService
VLANs *VLANsService VLANs *VLANsService
@ -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)

View file

@ -12,57 +12,39 @@ import (
type VirtualMachinesService service type VirtualMachinesService service
// VirtualMachines is a list of virtual-machines
type VirtualMachines struct { type VirtualMachines struct {
Next interface{} `json:"next"` Next interface{} `json:"next"`
Previous interface{} `json:"previous"` Previous interface{} `json:"previous"`
Results []struct { Results []VirtualMachine `json:"results"`
Count int `json:"count"`
}
// VirtualMachine is one virtual-machine
type VirtualMachine struct {
CustomFields interface{} `json:"custom_fields"`
LastUpdated time.Time `json:"last_updated"` LastUpdated time.Time `json:"last_updated"`
Vcpus interface{} `json:"vcpus"` Tenant struct {
CustomFields interface{} `json:"custom_fields,omitempty"`
LocalContextData interface{} `json:"local_context_data"`
Disk interface{} `json:"disk"`
Site interface{} `json:"site"`
Memory interface{} `json:"memory"`
ConfigContext interface{} `json:"config_context,omitempty"`
Tenant interface{} `json:"tenant"`
PrimaryIP6 interface{} `json:"primary_ip6"`
Status struct {
Value string `json:"value"`
Label string `json:"label"`
} `json:"status"`
Name string `json:"name"`
URL string `json:"url"`
Display string `json:"display"`
Created string `json:"created"`
Comments string `json:"comments"`
Platform struct {
URL string `json:"url"` URL string `json:"url"`
Display string `json:"display"` Display string `json:"display"`
Name string `json:"name"` Name string `json:"name"`
Slug string `json:"slug"` Slug string `json:"slug"`
ID int `json:"id"` ID int `json:"id"`
} `json:"platform"` } `json:"tenant"`
Role struct { Site struct {
URL string `json:"url"` URL string `json:"url"`
Display string `json:"display"` Display string `json:"display"`
Name string `json:"name"` Name string `json:"name"`
Slug string `json:"slug"` Slug string `json:"slug"`
ID int `json:"id"` ID int `json:"id"`
} `json:"role"` } `json:"site"`
Cluster struct { PrimaryIP6 struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
} `json:"cluster"`
Tags []interface{} `json:"tags"`
PrimaryIP4 struct {
URL string `json:"url"` URL string `json:"url"`
Display string `json:"display"` Display string `json:"display"`
Address string `json:"address"` Address string `json:"address"`
ID int `json:"id"` ID int `json:"id"`
Family int `json:"family"` Family int `json:"family"`
} `json:"primary_ip4"` } `json:"primary_ip6"`
PrimaryIP struct { PrimaryIP struct {
URL string `json:"url"` URL string `json:"url"`
Display string `json:"display"` Display string `json:"display"`
@ -70,25 +52,77 @@ type VirtualMachines struct {
ID int `json:"id"` ID int `json:"id"`
Family int `json:"family"` Family int `json:"family"`
} `json:"primary_ip"` } `json:"primary_ip"`
PrimaryIP4 struct {
URL string `json:"url"`
Display string `json:"display"`
Address string `json:"address"`
ID int `json:"id"`
Family int `json:"family"`
} `json:"primary_ip4"`
ConfigContext interface{} `json:"config_context"`
Status struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"status"`
URL string `json:"url"`
Name string `json:"name"`
Display string `json:"display"`
Created string `json:"created"`
Comments string `json:"comments"`
LocalContextData interface{} `json:"local_context_data"`
Cluster struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"cluster"`
Tags []struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
ID int `json:"id"`
} `json:"tags"`
Platform struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"platform"`
Role struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"role"`
Disk int `json:"disk"`
Memory int `json:"memory"`
Vcpus int `json:"vcpus"`
ID int `json:"id"` ID int `json:"id"`
} `json:"results"`
Count int `json:"count"`
} }
// NewVirtualMachine is used to create new VirtualizationVirtualMachines // NewVirtualMachine is used to create new VirtualizationVirtualMachines
type NewVirtualMachine struct { type NewVirtualMachine struct {
CustomFields *struct{} `json:"custom_fields,omitempty"`
Status string `json:"status"`
LocalContextData interface{} `json:"local_context_data,omitempty"` LocalContextData interface{} `json:"local_context_data,omitempty"`
Comments string `json:"comments,omitempty"` CustomFields *struct{} `json:"custom_fields,omitempty"`
Name string `json:"name"`
Tags *[]struct { Tags *[]struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"` Slug string `json:"slug,omitempty"`
Color string `json:"color,omitempty"` Color string `json:"color,omitempty"`
} `json:"tags,omitempty"` } `json:"tags,omitempty"`
Platform int `json:"platform,omitempty"` Status string `json:"status"`
Comments string `json:"comments,omitempty"`
Name string `json:"name"`
PrimaryIP4 int `json:"primary_ip4,omitempty"` PrimaryIP4 int `json:"primary_ip4,omitempty"`
Platform int `json:"platform,omitempty"`
VCPUs int `json:"vcpus,omitempty"` VCPUs int `json:"vcpus,omitempty"`
Memory int `json:"memory,omitempty"` Memory int `json:"memory,omitempty"`
Disk int `json:"disk,omitempty"` Disk int `json:"disk,omitempty"`
@ -177,21 +211,22 @@ func (s *VirtualMachinesService) List(ctx context.Context, f *VirtualMachineFilt
} }
// Create a virtual-machine // Create a virtual-machine
func (s *VirtualMachinesService) Create(ctx context.Context, c *NewVirtualMachine) error { func (s *VirtualMachinesService) Create(ctx context.Context, c *NewVirtualMachine) (*VirtualMachine, error) {
var err error var err error
var req *http.Request var req *http.Request
var virtualMachine VirtualMachine
req, err = s.client.newRequest(ctx, "POST", virtualMachinesPath, "", c) req, err = s.client.newRequest(ctx, "POST", virtualMachinesPath, "", c)
if err != nil { if err != nil {
return fmt.Errorf("unable to create request: %w", err) return &virtualMachine, fmt.Errorf("unable to create request: %w", err)
} }
_, err = s.client.do(req, nil) _, err = s.client.do(req, &virtualMachine)
if err != nil { if err != nil {
return fmt.Errorf("unable to do request: %w", err) return &virtualMachine, fmt.Errorf("unable to do request: %w", err)
} }
return nil return &virtualMachine, nil
} }
// Delete a virtual-machine // Delete a virtual-machine

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 {
CustomFields *interface{} `json:"custom_fields,omitempty"`
Name string `json:"name"`
Mode string `json:"mode,omitempty"`
Description string `json:"description,omitempty"`
MACAddress string `json:"mac_address,omitempty"`
Tags *[]struct {
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
Color string `json:"color,omitempty"`
} `json:"tags,omitempty"`
TaggedVLANs *[]int `json:"tagged_vlans,omitempty"`
Bridge int `json:"bridge,omitempty"`
Parent int `json:"parent,omitempty"`
UntaggedVLAN int `json:"untagged_vlan,omitempty"`
MTU int `json:"mtu,omitempty"`
VirtualMachine int `json:"virtual_machine"`
Enabled bool `json:"enabled,omitempty"`
}
// 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 {
LastUpdated time.Time `json:"last_updated"`
CustomFields interface{} `json:"custom_fields"`
Mode struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"mode"`
Display string `json:"display"`
Description string `json:"description"`
Created string `json:"created"`
URL string `json:"url"`
Name string `json:"name"`
MACAddress string `json:"mac_address"`
VirtualMachine struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
} `json:"virtual_machine"`
TaggedVLANs []struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
Vid int `json:"vid"`
} `json:"tagged_vlans"`
Tags []struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
ID int `json:"id"`
} `json:"tags"`
Bridge struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
VirtualMachine struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
} `json:"virtual_machine"`
ID int `json:"id"`
} `json:"bridge"`
Parent struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
VirtualMachine struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
} `json:"virtual_machine"`
ID int `json:"id"`
} `json:"parent"`
UntaggedVLAN struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
Vid int `json:"vid"`
} `json:"untagged_vlan"`
ID int `json:"id"`
MTU int `json:"mtu"`
CountIPAddresses int `json:"count_ipaddresses"`
CountFHRPGroups int `json:"count_fhrp_groups"`
Enabled bool `json:"enabled"`
}
// 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", virtualizationInterfacesPath, 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", virtualizationInterfacesPath, "", 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
}