When creating objects return the created object

TODO: Do this for all objects
This commit is contained in:
Kalle Carlbark 2022-02-14 11:09:50 +01:00
parent f5bf955b0e
commit 236a9bb23e
No known key found for this signature in database
2 changed files with 276 additions and 149 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"time"
// "fmt" // "fmt"
@ -13,84 +14,171 @@ import (
type InterfacesService service type InterfacesService service
// NewInterface is used for the return values from Netbox API dcim_interfaces_create // NewInterface is used for creating a new interface
type NewInterface struct { 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"` MacAddress string `json:"mac_address,omitempty"`
MgmtOnly bool `json:"mgmt_only,omitempty"` Name string `json:"name"`
Description string `json:"description,omitempty"`
ConnectionStatus bool `json:"connection_status,omitempty"`
Cable struct { Cable struct {
Label string `json:"label,omitempty"` Label string `json:"label,omitempty"`
} `json:"cable,omitempty"` } `json:"cable,omitempty"`
Mode int `json:"mode,omitempty"` Description string `json:"description,omitempty"`
UntaggedVLAN int `json:"untagged_vlan,omitempty"`
TaggedVLANs []int `json:"tagged_vlans,omitempty"` TaggedVLANs []int `json:"tagged_vlans,omitempty"`
Tags []string `json:"tags,omitempty"` Tags []string `json:"tags,omitempty"`
Device int `json:"device"`
Mtu int `json:"mtu,omitempty"`
Lag int `json:"lag,omitempty"`
UntaggedVLAN int `json:"untagged_vlan,omitempty"`
Type int `json:"type,omitempty"`
Mode int `json:"mode,omitempty"`
ConnectionStatus bool `json:"connection_status,omitempty"`
Enabled bool `json:"enabled,omitempty"`
MgmtOnly bool `json:"mgmt_only,omitempty"`
} }
// Interfaces is used for the return value from NetBox API dcim_interfaces_list // Interfaces is a list of interfaces
type Interfaces struct { type Interfaces struct {
Count int `json:"count,omitempty"`
Next interface{} `json:"next,omitempty"` Next interface{} `json:"next,omitempty"`
Previous interface{} `json:"previous,omitempty"` Previous interface{} `json:"previous,omitempty"`
Results []struct { Results []Interface `json:"results,omitempty"`
ID int `json:"id,omitempty"` Count int `json:"count,omitempty"`
}
// Interface is one interface
type Interface struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Device struct { Device struct {
ID int `json:"id,omitempty"` ID int `json:"id"`
URL string `json:"url,omitempty"` URL string `json:"url"`
Name string `json:"name,omitempty"` Display string `json:"display"`
DisplayName string `json:"display_name,omitempty"` Name string `json:"name"`
} `json:"device,omitempty"` } `json:"device"`
Name string `json:"name,omitempty"` Name string `json:"name"`
Label string `json:"label"`
Type struct { Type struct {
Value int `json:"value,omitempty"` Label string `json:"label"`
Label string `json:"label,omitempty"` Value string `json:"value"`
} `json:"type,omitempty"` } `json:"type"`
FormFactor struct { Enabled bool `json:"enabled"`
Value int `json:"value,omitempty"` Parent struct {
Label string `json:"label,omitempty"` ID int `json:"id"`
} `json:"form_factor,omitempty"` URL string `json:"url"`
Enabled bool `json:"enabled,omitempty"` Display string `json:"display"`
Lag struct {
ID int `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Device struct { Device struct {
ID int `json:"id,omitempty"` ID int `json:"id"`
URL string `json:"url,omitempty"` URL string `json:"url"`
Name string `json:"name,omitempty"` Display string `json:"display"`
DisplayName string `json:"display_name,omitempty"` Name string `json:"name"`
} `json:"device,omitempty"` } `json:"device"`
Name string `json:"name,omitempty"` Name string `json:"name"`
Cable interface{} `json:"cable,omitempty"` Cable int `json:"cable"`
ConnectionStatus interface{} `json:"connection_status,omitempty"` Occupied string `json:"_occupied"`
} `json:"lag,omitempty"` } `json:"parent"`
Mtu interface{} `json:"mtu,omitempty"` Bridge struct {
MacAddress interface{} `json:"mac_address,omitempty"` ID int `json:"id"`
MgmtOnly bool `json:"mgmt_only,omitempty"` URL string `json:"url"`
Description string `json:"description,omitempty"` Display string `json:"display"`
ConnectedEndpointType interface{} `json:"connected_endpoint_type,omitempty"` Device struct {
ConnectedEndpoint interface{} `json:"connected_endpoint,omitempty"` ID int `json:"id"`
ConnectionStatus interface{} `json:"connection_status,omitempty"` URL string `json:"url"`
Cable interface{} `json:"cable,omitempty"` Display string `json:"display"`
Mode interface{} `json:"mode,omitempty"` Name string `json:"name"`
UntaggedVLAN interface{} `json:"untagged_vlan,omitempty"` } `json:"device"`
TaggedVLANs []interface{} `json:"tagged_vlans,omitempty"` Name string `json:"name"`
Tags []interface{} `json:"tags,omitempty"` Cable int `json:"cable"`
CountIpaddresses int `json:"count_ipaddresses,omitempty"` Occupied string `json:"_occupied"`
} `json:"results,omitempty"` } `json:"bridge"`
Lag struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Device struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
} `json:"device"`
Name string `json:"name"`
Cable int `json:"cable"`
Occupied string `json:"_occupied"`
} `json:"lag"`
Mtu int `json:"mtu"`
MacAddress string `json:"mac_address"`
Wwn string `json:"wwn"`
MgmtOnly bool `json:"mgmt_only"`
Description string `json:"description"`
Mode struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"mode"`
RFRole struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"rf_role"`
RFChannel struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"rf_channel"`
RFChannelFrequency int `json:"rf_channel_frequency"`
RFChannelWidth int `json:"rf_channel_width"`
TXPower int `json:"tx_power"`
UntaggedVlan struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Vid int `json:"vid"`
Name string `json:"name"`
} `json:"untagged_vlan"`
TaggedVlans []struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Vid int `json:"vid"`
Name string `json:"name"`
} `json:"tagged_vlans"`
MarkConnected bool `json:"mark_connected"`
Cable struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Label string `json:"label"`
} `json:"cable"`
WirelessLink struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Ssid string `json:"ssid"`
} `json:"wireless_link"`
LinkPeer interface{} `json:"link_peer"`
LinkPeerType string `json:"link_peer_type"`
WirelessLans []struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Ssid string `json:"ssid"`
} `json:"wireless_lans"`
ConnectedEndpoint interface{} `json:"connected_endpoint"`
ConnectedEndpointType string `json:"connected_endpoint_type"`
ConnectedEndpointReachable bool `json:"connected_endpoint_reachable"`
Tags []struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
} `json:"tags"`
CustomFields struct{} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
CountIpaddresses int `json:"count_ipaddresses"`
CountFhrpGroups int `json:"count_fhrp_groups"`
Occupied bool `json:"_occupied"`
} }
// InterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list // InterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list
type InterfaceFilter struct { type InterfaceFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
// User specific filters // User specific filters
ID string `schema:"id,omitempty"` ID string `schema:"id,omitempty"`
Name string `schema:"name,omitempty"` Name string `schema:"name,omitempty"`
@ -110,6 +198,9 @@ type InterfaceFilter struct {
Tag string `schema:"tag,omitempty"` Tag string `schema:"tag,omitempty"`
VLANID string `schema:"vlan_id,omitempty"` VLANID string `schema:"vlan_id,omitempty"`
VLAN string `schema:"vlan,omitempty"` VLAN string `schema:"vlan,omitempty"`
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
} }
const interfacesPath = dcimPath + "/interfaces" const interfacesPath = dcimPath + "/interfaces"
@ -144,19 +235,20 @@ func (s *InterfacesService) List(ctx context.Context, f *InterfaceFilter) (*Inte
} }
// Create a interfaces // Create a interfaces
func (s *InterfacesService) Create(ctx context.Context, c *NewInterface) error { func (s *InterfacesService) Create(ctx context.Context, c *NewInterface) (*Interface, error) {
var err error var err error
var req *http.Request var req *http.Request
var nic Interface
req, err = s.client.newRequest(ctx, "POST", interfacesPath, "", c) req, err = s.client.newRequest(ctx, "POST", interfacesPath, "", c)
if err != nil { if err != nil {
return fmt.Errorf("unable to create request: %w", err) return &nic, fmt.Errorf("unable to create request: %w", err)
} }
_, err = s.client.do(req, nil) _, err = s.client.do(req, &nic)
if err != nil { if err != nil {
return fmt.Errorf("unable to do request: %w", err) return &nic, fmt.Errorf("unable to do request: %w", err)
} }
return nil return &nic, nil
} }

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 string `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