248 lines
8.4 KiB
Go
248 lines
8.4 KiB
Go
package netboxgo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/gorilla/schema"
|
|
)
|
|
|
|
type VirtualMachinesService service
|
|
|
|
// VirtualMachines is a list of virtual-machines
|
|
type VirtualMachines struct {
|
|
Next interface{} `json:"next"`
|
|
Previous interface{} `json:"previous"`
|
|
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"`
|
|
Tenant struct {
|
|
URL string `json:"url"`
|
|
Display string `json:"display"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
ID int `json:"id"`
|
|
} `json:"tenant"`
|
|
Site struct {
|
|
URL string `json:"url"`
|
|
Display string `json:"display"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
ID int `json:"id"`
|
|
} `json:"site"`
|
|
PrimaryIP6 struct {
|
|
URL string `json:"url"`
|
|
Display string `json:"display"`
|
|
Address string `json:"address"`
|
|
ID int `json:"id"`
|
|
Family int `json:"family"`
|
|
} `json:"primary_ip6"`
|
|
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"`
|
|
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"`
|
|
}
|
|
|
|
// NewVirtualMachine is used to create new VirtualizationVirtualMachines
|
|
type NewVirtualMachine struct {
|
|
LocalContextData interface{} `json:"local_context_data,omitempty"`
|
|
CustomFields *struct{} `json:"custom_fields,omitempty"`
|
|
Tags *[]struct {
|
|
Name string `json:"name,omitempty"`
|
|
Slug string `json:"slug,omitempty"`
|
|
Color string `json:"color,omitempty"`
|
|
} `json:"tags,omitempty"`
|
|
Status string `json:"status"`
|
|
Comments string `json:"comments,omitempty"`
|
|
Name string `json:"name"`
|
|
PrimaryIP4 int `json:"primary_ip4,omitempty"`
|
|
Platform int `json:"platform,omitempty"`
|
|
VCPUs int `json:"vcpus,omitempty"`
|
|
Memory int `json:"memory,omitempty"`
|
|
Disk int `json:"disk,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
|
|
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) (*VirtualMachine, error) {
|
|
var err error
|
|
var req *http.Request
|
|
var virtualMachine VirtualMachine
|
|
|
|
req, err = s.client.newRequest(ctx, "POST", virtualMachinesPath, "", c)
|
|
if err != nil {
|
|
return &virtualMachine, fmt.Errorf("unable to create request: %w", err)
|
|
}
|
|
|
|
_, err = s.client.do(req, &virtualMachine)
|
|
if err != nil {
|
|
return &virtualMachine, fmt.Errorf("unable to do request: %w", err)
|
|
}
|
|
|
|
return &virtualMachine, nil
|
|
}
|
|
|
|
// Delete a virtual-machine
|
|
func (s *VirtualMachinesService) Delete(ctx context.Context, i string) error {
|
|
var err error
|
|
var req *http.Request
|
|
|
|
req, err = s.client.newRequest(ctx, "DELETE", virtualMachinesPath+"/"+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
|
|
}
|