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" "context"
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"time" "time"
"github.com/gorilla/schema"
) )
type VirtualMachinesService service 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 // NewVirtualMachine is used to create new VirtualizationVirtualMachines
type NewVirtualMachine struct { type NewVirtualMachine struct {
ID int `json:"id"` LastUpdated time.Time `json:"last_updated"`
Name string `json:"name"` 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 { Status struct {
Label string `json:"label"` Label string `json:"label"`
Value int `json:"value"` Value int `json:"value"`
} `json:"status"` } `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 { Platform struct {
ID int `json:"id"`
URL string `json:"url"` URL string `json:"url"`
Name string `json:"name"` Name string `json:"name"`
Slug string `json:"slug"` Slug string `json:"slug"`
ID int `json:"id"`
DeviceCount int `json:"device_count"` DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"` VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"platform"` } `json:"platform"`
PrimaryIP struct { Role struct {
ID int `json:"id"` URL string `json:"url"`
URL string `json:"url"` Name string `json:"name"`
Family int `json:"family"` Slug string `json:"slug"`
Address string `json:"address"` ID int `json:"id"`
} `json:"primary_ip"` DeviceCount int `json:"device_count"`
PrimaryIP4 struct { VirtualmachineCount int `json:"virtualmachine_count"`
ID int `json:"id"` } `json:"role"`
URL string `json:"url"` Disk int `json:"disk"`
Family int `json:"family"` Memory int `json:"memory"`
Address string `json:"address"` Vcpus int `json:"vcpus"`
} `json:"primary_ip4"` ID int `json:"id"`
PrimaryIP6 struct { }
ID int `json:"id"`
URL string `json:"url"` // VirtualMachineFilter is used to filter dcim_device_list query to the Netbox API
Family int `json:"family"` type VirtualMachineFilter struct {
Address string `json:"address"` // User specific filters
} `json:"primary_ip6"` ID string `schema:"id,omitempty"`
Vcpus int `json:"vcpus"` Name string `schema:"name,omitempty"`
Memory int `json:"memory"` AssetTag string `schema:"asset_tag,omitempty"`
Disk int `json:"disk"` Face string `schema:"face,omitempty"`
Comments string `json:"comments"` Position string `schema:"position,omitempty"`
LocalContextData string `json:"local_context_data"` VcPosition string `schema:"vc_position,omitempty"`
Tags []string `json:"tags"` VcPriority string `schema:"vc_priority,omitempty"`
CustomFields interface{} `json:"custom_fields"` TenantGroupID string `schema:"tenant_group_id,omitempty"`
ConfigContext interface{} `json:"config_context"` TenantGroup string `schema:"tenant_group,omitempty"`
Created string `json:"created"` TenantID string `schema:"tenant_id,omitempty"`
LastUpdated time.Time `json:"last_updated"` 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" 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 // Create a virtual-machine
func (s *VirtualMachinesService) Create(ctx context.Context, c *NewVirtualMachine) error { func (s *VirtualMachinesService) Create(ctx context.Context, c *NewVirtualMachine) error {
var err error var err error