From 236a9bb23e4af554261daa1ebf801b5cd088639a Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Mon, 14 Feb 2022 11:09:50 +0100 Subject: [PATCH] When creating objects return the created object TODO: Do this for all objects --- interfaces.go | 234 ++++++++++++++++++++++++++++++-------------- virtual_machines.go | 191 +++++++++++++++++++++--------------- 2 files changed, 276 insertions(+), 149 deletions(-) diff --git a/interfaces.go b/interfaces.go index c29f434..f0f15cb 100644 --- a/interfaces.go +++ b/interfaces.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "net/url" + "time" // "fmt" @@ -13,84 +14,171 @@ import ( 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 { - 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 { + MacAddress string `json:"mac_address,omitempty"` + Name string `json:"name"` + 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"` + Description string `json:"description,omitempty"` + TaggedVLANs []int `json:"tagged_vlans,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 { - 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"` + Results []Interface `json:"results,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 { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + Name string `json:"name"` + } `json:"device"` + Name string `json:"name"` + Label string `json:"label"` + Type struct { + Label string `json:"label"` + Value string `json:"value"` + } `json:"type"` + Enabled bool `json:"enabled"` + Parent 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:"parent"` + Bridge 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:"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 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"` @@ -110,6 +198,9 @@ type InterfaceFilter struct { 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 interfacesPath = dcimPath + "/interfaces" @@ -144,19 +235,20 @@ func (s *InterfacesService) List(ctx context.Context, f *InterfaceFilter) (*Inte } // 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 req *http.Request + var nic Interface req, err = s.client.newRequest(ctx, "POST", interfacesPath, "", c) 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 { - return fmt.Errorf("unable to do request: %w", err) + return &nic, fmt.Errorf("unable to do request: %w", err) } - return nil + return &nic, nil } diff --git a/virtual_machines.go b/virtual_machines.go index c1a587e..b304401 100644 --- a/virtual_machines.go +++ b/virtual_machines.go @@ -12,90 +12,124 @@ import ( type VirtualMachinesService service +// VirtualMachines is a list of virtual-machines 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"` + 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 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"` } // NewVirtualMachine is used to create new VirtualizationVirtualMachines type NewVirtualMachine struct { - CustomFields *struct{} `json:"custom_fields,omitempty"` - Status string `json:"status"` LocalContextData interface{} `json:"local_context_data,omitempty"` - Comments string `json:"comments,omitempty"` - Name string `json:"name"` + 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"` - Platform int `json:"platform,omitempty"` - PrimaryIP4 int `json:"primary_ip4,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"` + 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 @@ -177,21 +211,22 @@ func (s *VirtualMachinesService) List(ctx context.Context, f *VirtualMachineFilt } // 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 req *http.Request + var virtualMachine VirtualMachine req, err = s.client.newRequest(ctx, "POST", virtualMachinesPath, "", c) 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 { - 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