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,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)

View file

@ -12,90 +12,124 @@ 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"`
LastUpdated time.Time `json:"last_updated"` Count int `json:"count"`
Vcpus interface{} `json:"vcpus"` }
CustomFields interface{} `json:"custom_fields,omitempty"`
LocalContextData interface{} `json:"local_context_data"` // VirtualMachine is one virtual-machine
Disk interface{} `json:"disk"` type VirtualMachine struct {
Site interface{} `json:"site"` CustomFields interface{} `json:"custom_fields"`
Memory interface{} `json:"memory"` LastUpdated time.Time `json:"last_updated"`
ConfigContext interface{} `json:"config_context,omitempty"` Tenant struct {
Tenant interface{} `json:"tenant"` URL string `json:"url"`
PrimaryIP6 interface{} `json:"primary_ip6"` Display string `json:"display"`
Status struct { Name string `json:"name"`
Value string `json:"value"` Slug string `json:"slug"`
Label string `json:"label"` ID int `json:"id"`
} `json:"status"` } `json:"tenant"`
Name string `json:"name"` Site struct {
URL string `json:"url"` URL string `json:"url"`
Display string `json:"display"` Display string `json:"display"`
Created string `json:"created"` Name string `json:"name"`
Comments string `json:"comments"` Slug string `json:"slug"`
Platform struct { ID int `json:"id"`
URL string `json:"url"` } `json:"site"`
Display string `json:"display"` PrimaryIP6 struct {
Name string `json:"name"` URL string `json:"url"`
Slug string `json:"slug"` Display string `json:"display"`
ID int `json:"id"` Address string `json:"address"`
} `json:"platform"` ID int `json:"id"`
Role struct { Family int `json:"family"`
URL string `json:"url"` } `json:"primary_ip6"`
Display string `json:"display"` PrimaryIP struct {
Name string `json:"name"` URL string `json:"url"`
Slug string `json:"slug"` Display string `json:"display"`
ID int `json:"id"` Address string `json:"address"`
} `json:"role"` ID int `json:"id"`
Cluster struct { Family int `json:"family"`
URL string `json:"url"` } `json:"primary_ip"`
Display string `json:"display"` PrimaryIP4 struct {
Name string `json:"name"` URL string `json:"url"`
ID int `json:"id"` Display string `json:"display"`
} `json:"cluster"` Address string `json:"address"`
Tags []interface{} `json:"tags"` ID int `json:"id"`
PrimaryIP4 struct { Family int `json:"family"`
URL string `json:"url"` } `json:"primary_ip4"`
Display string `json:"display"` ConfigContext interface{} `json:"config_context"`
Address string `json:"address"` Status struct {
ID int `json:"id"` Label string `json:"label"`
Family int `json:"family"` Value string `json:"value"`
} `json:"primary_ip4"` } `json:"status"`
PrimaryIP struct { URL string `json:"url"`
URL string `json:"url"` Name string `json:"name"`
Display string `json:"display"` Display string `json:"display"`
Address string `json:"address"` Created string `json:"created"`
ID int `json:"id"` Comments string `json:"comments"`
Family int `json:"family"` LocalContextData interface{} `json:"local_context_data"`
} `json:"primary_ip"` Cluster struct {
ID int `json:"id"` URL string `json:"url"`
} `json:"results"` Display string `json:"display"`
Count int `json:"count"` 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"`
} }
// 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"`
PrimaryIP4 int `json:"primary_ip4,omitempty"` Comments string `json:"comments,omitempty"`
VCPUs int `json:"vcpus,omitempty"` Name string `json:"name"`
Memory int `json:"memory,omitempty"` PrimaryIP4 int `json:"primary_ip4,omitempty"`
Disk int `json:"disk,omitempty"` Platform int `json:"platform,omitempty"`
Role int `json:"role,omitempty"` VCPUs int `json:"vcpus,omitempty"`
Cluster int `json:"cluster,omitempty"` Memory int `json:"memory,omitempty"`
Tenant int `json:"tenant,omitempty"` Disk int `json:"disk,omitempty"`
PrimaryIP6 int `json:"primary_ip6,omitempty"` Role int `json:"role,omitempty"`
Cluster int `json:"cluster,omitempty"`
Tenant int `json:"tenant,omitempty"`
PrimaryIP6 int `json:"primary_ip6,omitempty"`
} }
// VirtualMachineFilter is used to filter dcim_device_list query to the Netbox API // VirtualMachineFilter is used to filter dcim_device_list query to the Netbox API
@ -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
}