diff --git a/circuits.go b/circuits.go new file mode 100644 index 0000000..4411387 --- /dev/null +++ b/circuits.go @@ -0,0 +1,106 @@ +package netboxgo + +import ( + "time" +) + +type Circuits struct { + Count int `json:"count"` + Next string `json:"next"` + Previous string `json:"previous"` + Results []struct { + ID int `json:"id"` + Cid string `json:"cid"` + Provider struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + Slug string `json:"slug"` + CircuitCount int `json:"circuit_count"` + } `json:"provider"` + Type struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + Slug string `json:"slug"` + CircuitCount int `json:"circuit_count"` + } `json:"type"` + Status struct { + Label string `json:"label"` + Value string `json:"value"` + } `json:"status"` + Tenant struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + Slug string `json:"slug"` + } `json:"tenant"` + InstallDate string `json:"install_date"` + CommitRate int `json:"commit_rate"` + Description string `json:"description"` + TerminationA struct { + ID int `json:"id"` + URL string `json:"url"` + Site struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + Slug string `json:"slug"` + } `json:"site"` + ConnectedEndpoint struct { + ID int `json:"id"` + URL string `json:"url"` + Device struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + DisplayName string `json:"display_name"` + } `json:"device"` + Name string `json:"name"` + Cable int `json:"cable"` + ConnectionStatus struct { + Label string `json:"label"` + Value bool `json:"value"` + } `json:"connection_status"` + } `json:"connected_endpoint"` + PortSpeed int `json:"port_speed"` + UpstreamSpeed int `json:"upstream_speed"` + XconnectID string `json:"xconnect_id"` + } `json:"termination_a"` + TerminationZ struct { + ID int `json:"id"` + URL string `json:"url"` + Site struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + Slug string `json:"slug"` + } `json:"site"` + ConnectedEndpoint struct { + ID int `json:"id"` + URL string `json:"url"` + Device struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + DisplayName string `json:"display_name"` + } `json:"device"` + Name string `json:"name"` + Cable int `json:"cable"` + ConnectionStatus struct { + Label string `json:"label"` + Value bool `json:"value"` + } `json:"connection_status"` + } `json:"connected_endpoint"` + PortSpeed int `json:"port_speed"` + UpstreamSpeed int `json:"upstream_speed"` + XconnectID string `json:"xconnect_id"` + } `json:"termination_z"` + Comments string `json:"comments"` + Tags []string `json:"tags"` + CustomFields struct { + } `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` + } `json:"results"` +} diff --git a/netbox.go b/netbox.go index 9285d79..358a66b 100644 --- a/netbox.go +++ b/netbox.go @@ -17,6 +17,7 @@ type NetBox struct { RootURL string Token string SessionKey string + FetchMode string InsecureSkipVerify bool } @@ -34,7 +35,6 @@ func (n *NetBox) New(root, token string, TLSSkipVerify bool) { // FetchSessionKey fetches sessionkey func (n *NetBox) FetchSessionKey(privatekey string) error { - form := url.Values{} form.Add("private_key", privatekey) query := form.Encode() @@ -53,7 +53,11 @@ func (n *NetBox) FetchSessionKey(privatekey string) error { } request.Header.Add("Accept", "application/json") request.Header.Add("Content-Type", "application/x-www-form-urlencoded") - request.Header.Add("js.fetch:mode", "no-cors") + + if n.FetchMode != "" { + request.Header.Add("js.fetch:mode", "no-cors") + } + request.Header.Add("Authorization", " Token "+n.Token) response, err := client.Do(request) if err != nil { diff --git a/netbox_devices.go b/netbox_devices.go index b9bb5d3..65d8947 100644 --- a/netbox_devices.go +++ b/netbox_devices.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/tls" "encoding/json" + "fmt" "io/ioutil" "net/http" "net/url" @@ -221,6 +222,40 @@ type DcimCreateDevice struct { } `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"` @@ -360,7 +395,7 @@ func (n *NetBox) CreateDevice(v *DcimCreateDevice) error { return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusCreated) } -func (n *NetBox) UpdateDevice(v *DcimCreateDevice, id int) error { +func (n *NetBox) UpdateDevice(v *DcimUpdateDevice, id int) error { data, err := json.Marshal(v) if err != nil { return err @@ -373,7 +408,11 @@ func (n *NetBox) UpdateDevice(v *DcimCreateDevice, id int) error { Timeout: timeout, Transport: transport, } - request, err := http.NewRequest("PUT", n.RootURL+"/api/dcim/devices/"+strconv.Itoa(id)+"/", bytes.NewBuffer(data)) + 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 } @@ -384,12 +423,18 @@ func (n *NetBox) UpdateDevice(v *DcimCreateDevice, id int) error { 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.StatusCreated { + if response.StatusCode == http.StatusOK { return nil } return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusCreated) diff --git a/rear_ports.go b/rear_ports.go new file mode 100644 index 0000000..fef1b03 --- /dev/null +++ b/rear_ports.go @@ -0,0 +1,111 @@ +package netboxgo + +import ( + "crypto/tls" + "encoding/json" + "io/ioutil" + "net/http" + "net/url" + "time" + + "github.com/gorilla/schema" + "github.com/pkg/errors" +) + +type RearPortList struct { + Count int `json:"count"` + Next string `json:"next"` + Previous string `json:"previous"` + Results []struct { + ID int `json:"id"` + Device struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + DisplayName string `json:"display_name"` + } `json:"device"` + Name string `json:"name"` + Type struct { + Label string `json:"label"` + Value string `json:"value"` + } `json:"type"` + Positions int `json:"positions"` + Description string `json:"description"` + Cable struct { + ID int `json:"id"` + URL string `json:"url"` + Label string `json:"label"` + } `json:"cable"` + Tags []string `json:"tags"` + } `json:"results"` +} + +// RearPortFilter is used to filter dcim_rearport_query to the Netbox API +type RearPortFilter struct { + Offset int64 `schema:"offset,omitempty"` + Limit int64 `schema:"limit,omitempty"` + + // User specific filters + ID string `schema:"id,omitempty"` + Name string `schema:"name,omitempty"` + Type string `schema:"type,omitempty"` + Positions string `schema:"positions,omitempty"` + Description string `schema:"description,omitempty"` + Device string `schema:"device,omitempty"` +} + +// ListRearPorts method returns dcim_device_list from Netbox API +func (n *NetBox) ListRearPorts(d *RearPortList, f *RearPortFilter) 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/rear-ports/" + 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 +}