451 lines
17 KiB
Go
451 lines
17 KiB
Go
package netboxgo
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gorilla/schema"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// DcimDevicesList is used for Netbox dcim_device_list return struct
|
|
type DcimDevicesList struct {
|
|
Count int `json:"count"`
|
|
Next string `json:"next"`
|
|
Previous string `json:"previous"`
|
|
Results []struct {
|
|
ID int `json:"id"`
|
|
Name string `json:"name"`
|
|
DisplayName string `json:"display_name"`
|
|
DeviceType struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Manufacturer struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
DevicetypeCount int `json:"devicetype_count"`
|
|
} `json:"manufacturer"`
|
|
Model string `json:"model"`
|
|
Slug string `json:"slug"`
|
|
DisplayName string `json:"display_name"`
|
|
DeviceCount int `json:"device_count"`
|
|
} `json:"device_type"`
|
|
DeviceRole 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:"device_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"`
|
|
DeviceCount int `json:"device_count"`
|
|
VirtualmachineCount int `json:"virtualmachine_count"`
|
|
} `json:"platform"`
|
|
Serial string `json:"serial"`
|
|
AssetTag string `json:"asset_tag"`
|
|
Site struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
} `json:"site"`
|
|
Rack struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
DisplayName string `json:"display_name"`
|
|
DeviceCount int `json:"device_count"`
|
|
} `json:"rack"`
|
|
Position int `json:"position"`
|
|
Face struct {
|
|
Label string `json:"label"`
|
|
Value string `json:"value"`
|
|
} `json:"face"`
|
|
ParentDevice struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
DisplayName string `json:"display_name"`
|
|
} `json:"parent_device"`
|
|
Status struct {
|
|
Label string `json:"label"`
|
|
Value string `json:"value"`
|
|
} `json:"status"`
|
|
PrimaryIP struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Family int `json:"family"`
|
|
Address string `json:"address"`
|
|
} `json:"primary_ip"`
|
|
PrimaryIP4 struct {
|
|
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"`
|
|
Cluster struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
VirtualmachineCount int `json:"virtualmachine_count"`
|
|
} `json:"cluster"`
|
|
VirtualChassis struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Master struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
DisplayName string `json:"display_name"`
|
|
} `json:"master"`
|
|
MemberCount int `json:"member_count"`
|
|
} `json:"virtual_chassis"`
|
|
VcPosition int `json:"vc_position"`
|
|
VcPriority int `json:"vc_priority"`
|
|
Comments string `json:"comments"`
|
|
// LocalContextData string `json:"local_context_data"`
|
|
Tags []struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
Color string `json:"color"`
|
|
} `json:"tags"`
|
|
CustomFields struct {
|
|
DcimDeviceBelongsToService struct {
|
|
Value int `json:"value"`
|
|
Label string `json:"label"`
|
|
} `json:"dcim_device_belongs_to_service"`
|
|
DcimDeviceCendotid string `json:"dcim_device_cendotid"`
|
|
DcimDeviceExposedToInternet struct {
|
|
Value int `json:"value"`
|
|
Label string `json:"label"`
|
|
} `json:"dcim_device_exposed_to_internet"`
|
|
DcimDeviceImportOsVersion bool `json:"dcim_device_import_os_version"`
|
|
DcimDevicePod struct {
|
|
Value int `json:"value"`
|
|
Label string `json:"label"`
|
|
} `json:"dcim_device_pod"`
|
|
DcimDeviceOsVersion interface{} `json:"dcim_device_os_version"`
|
|
DcimDevicesImportInterfaces interface{} `json:"dcim_devices_import_interfaces"`
|
|
DcimDeviceImportvlan bool `json:"dcim_device_importvlan"`
|
|
DcimDeviceL2Domain interface{} `json:"dcim_device_l2domain"`
|
|
DcimDeviceCountTrunkVlans interface{} `json:"dcim_device_count_trunk_vlans"`
|
|
DcimDeviceLogicalportsDeviceLimit interface{} `json:"dcim_device_logicalports_device_limit"`
|
|
DcimDeviceLogicalportsActiveNum interface{} `json:"dcim_device_logicalports_active_num"`
|
|
DcimDeviceLogicalportsConfiguredNum interface{} `json:"dcim_device_logicalports_configured_num"`
|
|
DcimDeviceCountTimesLastRun interface{} `json:"dcim_device_count_times_last_run"`
|
|
DcimDeviceImportVrfs interface{} `json:"dcim_device_import_vrfs"`
|
|
DcimDeviceVrfTag interface{} `json:"dcim_device_vrf_tag"`
|
|
} `json:"custom_fields"`
|
|
ConfigContext struct {
|
|
AdditionalProp1 string `json:"additionalProp1"`
|
|
AdditionalProp2 string `json:"additionalProp2"`
|
|
AdditionalProp3 string `json:"additionalProp3"`
|
|
} `json:"config_context"`
|
|
Created string `json:"created"`
|
|
LastUpdated time.Time `json:"last_updated"`
|
|
} `json:"results"`
|
|
}
|
|
|
|
type DcimCreateDevice struct {
|
|
Name string `json:"name,omitempty,omitempty"`
|
|
DeviceType int `json:"device_type,omitempty,omitempty"`
|
|
DeviceRole int `json:"device_role,omitempty,omitempty"`
|
|
Tenant int `json:"tenant,omitempty,omitempty"`
|
|
Platform int `json:"platform,omitempty,omitempty"`
|
|
Serial string `json:"serial,omitempty"`
|
|
AssetTag string `json:"asset_tag,omitempty"`
|
|
Site int `json:"site,omitempty"`
|
|
Rack int `json:"rack,omitempty"`
|
|
Position int `json:"position,omitempty"`
|
|
Face string `json:"face,omitempty"`
|
|
ParentDevice struct {
|
|
Name string `json:"name,omitempty"`
|
|
} `json:"parent_device,omitempty"`
|
|
Status string `json:"status,omitempty"`
|
|
PrimaryIP4 int `json:"primary_ip4,omitempty"`
|
|
PrimaryIP6 int `json:"primary_ip6,omitempty"`
|
|
Cluster int `json:"cluster,omitempty"`
|
|
VirtualChassis int `json:"virtual_chassis,omitempty"`
|
|
VcPosition int `json:"vc_position,omitempty"`
|
|
VcPriority int `json:"vc_priority,omitempty"`
|
|
Comments string `json:"comments,omitempty"`
|
|
LocalContextData string `json:"local_context_data,omitempty"`
|
|
Tags []struct {
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
Color string `json:"color"`
|
|
} `json:"tags"`
|
|
CustomFields struct {
|
|
DcimDeviceBelongsToService struct {
|
|
Value int `json:"value,omitempty"`
|
|
Label string `json:"label,omitempty"`
|
|
} `json:"dcim_device_belongs_to_service,omitempty"`
|
|
DcimDeviceCendotid string `json:"dcim_device_cendotid,omitempty"`
|
|
DcimDeviceExposedToInternet struct {
|
|
Value int `json:"value,omitempty"`
|
|
Label string `json:"label,omitempty"`
|
|
} `json:"dcim_device_exposed_to_internet,omitempty"`
|
|
DcimDeviceImportOsVersion bool `json:"dcim_device_import_os_version,omitempty"`
|
|
DcimDevicePod struct {
|
|
Value int `json:"value,omitempty"`
|
|
Label string `json:"label,omitempty"`
|
|
} `json:"dcim_device_pod,omitempty"`
|
|
DcimDeviceOsVersion interface{} `json:"dcim_device_os_version,omitempty"`
|
|
DcimDevicesImportInterfaces interface{} `json:"dcim_devices_import_interfaces,omitempty"`
|
|
DcimDeviceImportvlan bool `json:"dcim_device_importvlan,omitempty"`
|
|
DcimDeviceL2Domain interface{} `json:"dcim_device_l2domain,omitempty"`
|
|
DcimDeviceCountTrunkVlans interface{} `json:"dcim_device_count_trunk_vlans,omitempty"`
|
|
DcimDeviceLogicalportsDeviceLimit interface{} `json:"dcim_device_logicalports_device_limit,omitempty"`
|
|
DcimDeviceLogicalportsActiveNum interface{} `json:"dcim_device_logicalports_active_num,omitempty"`
|
|
DcimDeviceLogicalportsConfiguredNum interface{} `json:"dcim_device_logicalports_configured_num,omitempty"`
|
|
DcimDeviceCountTimesLastRun interface{} `json:"dcim_device_count_times_last_run,omitempty"`
|
|
DcimDeviceImportVrfs interface{} `json:"dcim_device_import_vrfs,omitempty"`
|
|
DcimDeviceVrfTag interface{} `json:"dcim_device_vrf_tag,omitempty"`
|
|
} `json:"custom_fields,omitempty"`
|
|
}
|
|
|
|
type DcimUpdateDevice struct {
|
|
Name string `json:"name,omitempty"`
|
|
DeviceType int `json:"device_type,omitempty"`
|
|
DeviceRole int `json:"device_role,omitempty"`
|
|
Tenant int `json:"tenant,omitempty"`
|
|
Platform int `json:"platform,omitempty"`
|
|
Serial string `json:"serial,omitempty"`
|
|
AssetTag string `json:"asset_tag,omitempty"`
|
|
Site int `json:"site,omitempty"`
|
|
Rack int `json:"rack,omitempty"`
|
|
Position int `json:"position,omitempty"`
|
|
Face string `json:"face,omitempty"`
|
|
ParentDevice *struct {
|
|
Name string `json:"name,omitempty"`
|
|
} `json:"parent_device,omitempty"`
|
|
Status string `json:"status,omitempty"`
|
|
PrimaryIP4 int `json:"primary_ip4,omitempty"`
|
|
PrimaryIP6 int `json:"primary_ip6,omitempty"`
|
|
Cluster int `json:"cluster,omitempty"`
|
|
VirtualChassis int `json:"virtual_chassis,omitempty"`
|
|
VcPosition int `json:"vc_position,omitempty"`
|
|
VcPriority int `json:"vc_priority,omitempty"`
|
|
Comments string `json:"comments,omitempty"`
|
|
LocalContextData string `json:"local_context_data,omitempty"`
|
|
Tags []string `json:"tags,omitempty"`
|
|
CustomFields struct {
|
|
DcimDeviceBelongsToService int `json:"dcim_device_belongs_to_service,omitempty"`
|
|
DcimDeviceCendotid string `json:"dcim_device_cendotid,omitempty"`
|
|
DcimDeviceExposedToInternet int `json:"dcim_device_exposed_to_internet,omitempty"`
|
|
DcimDeviceImportOsVersion bool `json:"dcim_device_import_os_version,omitempty,omitempty"`
|
|
DcimDevicePod int `json:"dcim_device_pod,omitempty"`
|
|
} `json:"custom_fields,omitempty"`
|
|
}
|
|
|
|
// DeviceFilter is used to filter dcim_device_list query to the Netbox API
|
|
type DeviceFilter struct {
|
|
Offset int64 `schema:"offset,omitempty"`
|
|
Limit int64 `schema:"limit,omitempty"`
|
|
|
|
// 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"`
|
|
}
|
|
|
|
// ListDevices method returns dcim_device_list from Netbox API
|
|
func (n *NetBox) ListDevices(d *DcimDevicesList, f *DeviceFilter) error {
|
|
encoder := schema.NewEncoder()
|
|
|
|
transport := &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
|
|
}
|
|
timeout := time.Duration(60 * time.Second)
|
|
client := &http.Client{
|
|
Timeout: timeout,
|
|
Transport: transport,
|
|
}
|
|
deviceurl := n.RootURL + "/api/dcim/devices/"
|
|
if f != nil {
|
|
form := url.Values{}
|
|
err := encoder.Encode(f, form)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
query := form.Encode()
|
|
deviceurl = deviceurl + "?" + query
|
|
}
|
|
var request *http.Request
|
|
var err error
|
|
request, err = http.NewRequest("GET", deviceurl, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
request.Header.Add("Accept", "application/json")
|
|
request.Header.Add("Authorization", " Token "+n.Token)
|
|
response, err := client.Do(request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
return errors.Errorf("Error: response was: %d should be %d (%s)\n", response.StatusCode, http.StatusOK, deviceurl)
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(response.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = response.Body.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = json.Unmarshal(data, &d)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (n *NetBox) CreateDevice(v *DcimCreateDevice) error {
|
|
data, err := json.Marshal(v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
transport := &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
|
|
}
|
|
timeout := time.Duration(60 * time.Second)
|
|
client := &http.Client{
|
|
Timeout: timeout,
|
|
Transport: transport,
|
|
}
|
|
request, err := http.NewRequest("POST", n.RootURL+"/api/dcim/devices/", bytes.NewBuffer(data))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
request.Header.Add("Accept", "application/json")
|
|
request.Header.Add("Content-Type", "application/json")
|
|
request.Header.Add("Authorization", " Token "+n.Token)
|
|
response, err := client.Do(request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = response.Body.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if response.StatusCode == http.StatusCreated {
|
|
return nil
|
|
}
|
|
return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusCreated)
|
|
}
|
|
|
|
func (n *NetBox) UpdateDevice(v *DcimUpdateDevice, id int) error {
|
|
data, err := json.Marshal(v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
transport := &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
|
|
}
|
|
timeout := time.Duration(60 * time.Second)
|
|
client := &http.Client{
|
|
Timeout: timeout,
|
|
Transport: transport,
|
|
}
|
|
fmt.Println("DEBUG DATA == RESPONSE FROM CLIENT")
|
|
fmt.Printf("%+v\n", string(data))
|
|
fmt.Println("DEBUG DATA == RESPONSE FROM CLIENT")
|
|
|
|
request, err := http.NewRequest("PATCH", n.RootURL+"/api/dcim/devices/"+strconv.Itoa(id)+"/", bytes.NewBuffer(data))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
request.Header.Add("Accept", "application/json")
|
|
request.Header.Add("Content-Type", "application/json")
|
|
request.Header.Add("Authorization", " Token "+n.Token)
|
|
response, err := client.Do(request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
koko, _ := ioutil.ReadAll(response.Body)
|
|
fmt.Println("DEBUG DATA === response from server")
|
|
fmt.Printf("%+v\n", string(koko))
|
|
fmt.Println("DEBUG DATA === response from server")
|
|
|
|
err = response.Body.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if response.StatusCode == http.StatusOK {
|
|
return nil
|
|
}
|
|
return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusCreated)
|
|
}
|