Add omitempty.
This commit is contained in:
parent
a27a5c67ea
commit
6f8127e2ed
1 changed files with 153 additions and 2 deletions
|
@ -1,11 +1,13 @@
|
||||||
package netboxgo
|
package netboxgo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/schema"
|
"github.com/gorilla/schema"
|
||||||
|
@ -129,6 +131,31 @@ type DcimDevicesList struct {
|
||||||
// LocalContextData string `json:"local_context_data"`
|
// LocalContextData string `json:"local_context_data"`
|
||||||
Tags []string `json:"tags"`
|
Tags []string `json:"tags"`
|
||||||
CustomFields struct {
|
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"`
|
} `json:"custom_fields"`
|
||||||
ConfigContext struct {
|
ConfigContext struct {
|
||||||
AdditionalProp1 string `json:"additionalProp1"`
|
AdditionalProp1 string `json:"additionalProp1"`
|
||||||
|
@ -140,12 +167,66 @@ type DcimDevicesList struct {
|
||||||
} `json:"results"`
|
} `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 []string `json:"tags,omitempty"`
|
||||||
|
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"`
|
||||||
|
}
|
||||||
|
|
||||||
// DeviceFilter is used to filter dcim_device_list query to the Netbox API
|
// DeviceFilter is used to filter dcim_device_list query to the Netbox API
|
||||||
type DeviceFilter struct {
|
type DeviceFilter struct {
|
||||||
Offset int64 `schema:"offset,omitempty"`
|
Offset int64 `schema:"offset,omitempty"`
|
||||||
Limit int64 `schema:"limit,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"`
|
||||||
AssetTag string `schema:"asset_tag,omitempty"`
|
AssetTag string `schema:"asset_tag,omitempty"`
|
||||||
|
@ -190,7 +271,7 @@ type DeviceFilter struct {
|
||||||
|
|
||||||
// ListDevices method returns dcim_device_list from Netbox API
|
// ListDevices method returns dcim_device_list from Netbox API
|
||||||
func (n *NetBox) ListDevices(d *DcimDevicesList, f *DeviceFilter) error {
|
func (n *NetBox) ListDevices(d *DcimDevicesList, f *DeviceFilter) error {
|
||||||
var encoder = schema.NewEncoder()
|
encoder := schema.NewEncoder()
|
||||||
|
|
||||||
transport := &http.Transport{
|
transport := &http.Transport{
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
|
||||||
|
@ -243,3 +324,73 @@ func (n *NetBox) ListDevices(d *DcimDevicesList, f *DeviceFilter) error {
|
||||||
}
|
}
|
||||||
return nil
|
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 *DcimCreateDevice, 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,
|
||||||
|
}
|
||||||
|
request, err := http.NewRequest("PUT", 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
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue