Add ability to list virtual machines

This commit is contained in:
Kalle Carlbark 2022-02-07 22:01:45 +01:00
parent 2bca52c094
commit 8a12a2668e
No known key found for this signature in database

View file

@ -4,85 +4,228 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
)
type VirtualMachinesService service
type VirtualMachines struct {
Next interface{} `json:"next"`
Previous interface{} `json:"previous"`
Results []struct {
LastUpdated time.Time `json:"last_updated"`
Vcpus interface{} `json:"vcpus"`
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"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
} `json:"platform"`
Role struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
} `json:"role"`
Cluster 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"`
Display string `json:"display"`
Address string `json:"address"`
ID int `json:"id"`
Family int `json:"family"`
} `json:"primary_ip4"`
PrimaryIP struct {
URL string `json:"url"`
Display string `json:"display"`
Address string `json:"address"`
ID int `json:"id"`
Family int `json:"family"`
} `json:"primary_ip"`
ID int `json:"id"`
} `json:"results"`
Count int `json:"count"`
}
// NewVirtualMachine is used to create new VirtualizationVirtualMachines
type NewVirtualMachine struct {
ID int `json:"id"`
LastUpdated time.Time `json:"last_updated"`
ConfigContext interface{} `json:"config_context"`
CustomFields interface{} `json:"custom_fields"`
Tenant struct {
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
} `json:"tenant"`
Site struct {
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
} `json:"site"`
PrimaryIP4 struct {
URL string `json:"url"`
Address string `json:"address"`
ID int `json:"id"`
Family int `json:"family"`
} `json:"primary_ip4"`
PrimaryIP struct {
URL string `json:"url"`
Address string `json:"address"`
ID int `json:"id"`
Family int `json:"family"`
} `json:"primary_ip"`
PrimaryIP6 struct {
URL string `json:"url"`
Address string `json:"address"`
ID int `json:"id"`
Family int `json:"family"`
} `json:"primary_ip6"`
LocalContextData string `json:"local_context_data"`
Created string `json:"created"`
Name string `json:"name"`
Comments string `json:"comments"`
Cluster struct {
URL string `json:"url"`
Name string `json:"name"`
ID int `json:"id"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"cluster"`
Tags []string `json:"tags"`
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"`
ID int `json:"id"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"platform"`
PrimaryIP struct {
ID int `json:"id"`
Role struct {
URL string `json:"url"`
Family int `json:"family"`
Address string `json:"address"`
} `json:"primary_ip"`
PrimaryIP4 struct {
Name string `json:"name"`
Slug string `json:"slug"`
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"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"role"`
Disk int `json:"disk"`
Comments string `json:"comments"`
LocalContextData string `json:"local_context_data"`
Tags []string `json:"tags"`
CustomFields interface{} `json:"custom_fields"`
ConfigContext interface{} `json:"config_context"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
Memory int `json:"memory"`
Vcpus int `json:"vcpus"`
ID int `json:"id"`
}
// VirtualMachineFilter is used to filter dcim_device_list query to the Netbox API
type VirtualMachineFilter struct {
// User specific filters
ID string `schema:"id,omitempty"`
Name string `schema:"name,omitempty"`
AssetTag string `schema:"asset_tag,omitempty"`
Face string `schema:"face,omitempty"`
Position string `schema:"position,omitempty"`
VcPosition string `schema:"vc_position,omitempty"`
VcPriority string `schema:"vc_priority,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"`
ManufacturerID string `schema:"manufacturer_id,omitempty"`
Manufacturer string `schema:"manufacturer,omitempty"`
DeviceTypeID string `schema:"device_type_id,omitempty"`
RoleID string `schema:"role_id,omitempty"`
Role string `schema:"role,omitempty"`
RegionID string `schema:"region_id,omitempty"`
Region string `schema:"region,omitempty"`
SiteID string `schema:"site_id,omitempty"`
Site string `schema:"site,omitempty"`
RackGroupID string `schema:"rack_group_id,omitempty"`
RackID string `schema:"rack_id,omitempty"`
ClusterID string `schema:"cluster_id,omitempty"`
Model string `schema:"model,omitempty"`
Status string `schema:"status,omitempty"`
IsfullDepth string `schema:"is_full_depth,omitempty"`
MacAddress string `schema:"mac_address,omitempty"`
Serial string `schema:"serial,omitempty"`
HasPrimaryIP string `schema:"has_primary_ip,omitempty"`
VirtualChassiID string `schema:"virtual_chassis_id,omitempty"`
VirtualChassiMember string `schema:"virtual_chassis_member,omitempty"`
ConsolePorts string `schema:"console_ports,omitempty"`
ConsoleServerPorts string `schema:"console_server_ports,omitempty"`
PowerPorts string `schema:"power_ports,omitempty"`
PowerOutlets string `schema:"power_outlets,omitempty"`
Interfaces string `schema:"interfaces,omitempty"`
PassThroughPorts string `schema:"pass_through_ports,omitempty"`
Tag string `schema:"tag,omitempty"`
Offset int `schema:"offset,omitempty"`
Limit int `schema:"limit,omitempty"`
}
const virtualMachinesPath = virtualizationPath + "/virtual-machines"
// List secrets. SecretsFilter is used to list based on filter queries.
func (s *VirtualMachinesService) List(ctx context.Context, f *VirtualMachineFilter) (*VirtualMachines, error) {
var virtualMachines VirtualMachines
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 &virtualMachines, err
}
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", virtualMachinesPath, query, nil)
if err != nil {
return &virtualMachines, err
}
_, err = s.client.do(req, &virtualMachines)
if err != nil {
return &virtualMachines, err
}
return &virtualMachines, nil
}
// Create a virtual-machine
func (s *VirtualMachinesService) Create(ctx context.Context, c *NewVirtualMachine) error {
var err error