From 3bec02638733751c9efbef70bba570553b486d0e Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Tue, 1 Jun 2021 22:53:19 +0200 Subject: [PATCH] features o Sites o Sitegroups --- netbox_devices.go | 23 +---- netbox_site_groups.go | 157 +++++++++++++++++++++++++++++++++ netbox_sites.go | 198 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 358 insertions(+), 20 deletions(-) create mode 100644 netbox_site_groups.go create mode 100644 netbox_sites.go diff --git a/netbox_devices.go b/netbox_devices.go index f5cf7ed..795392d 100644 --- a/netbox_devices.go +++ b/netbox_devices.go @@ -4,7 +4,6 @@ import ( "bytes" "crypto/tls" "encoding/json" - "fmt" "io/ioutil" "net/http" "net/url" @@ -417,10 +416,6 @@ func (n *NetBox) UpdateDevice(v *DcimUpdateDevice, id int) error { 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 @@ -428,23 +423,11 @@ func (n *NetBox) UpdateDevice(v *DcimUpdateDevice, id int) error { 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) + + _, 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) + return nil } diff --git a/netbox_site_groups.go b/netbox_site_groups.go new file mode 100644 index 0000000..5b16497 --- /dev/null +++ b/netbox_site_groups.go @@ -0,0 +1,157 @@ +package netboxgo + +import ( + "crypto/tls" + "encoding/json" + + // "fmt" + "io/ioutil" + "net/http" + "net/url" + "time" + + "github.com/gorilla/schema" + "github.com/pkg/errors" +) + +type SiteGroupsList struct { + Count int `json:"count"` + Next string `json:"next"` + Previous string `json:"previous"` + Results []SiteGroupList `json:"results"` +} + +type SiteGroupList struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + Name string `json:"name"` + Slug string `json:"slug"` + Parent struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + Name string `json:"name"` + Slug string `json:"slug"` + SiteCount int `json:"site_count"` + Depth int `json:"_depth"` + } `json:"parent"` + Description string `json:"description"` + CustomFields struct { + } `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` + SiteCount int `json:"site_count"` + Depth int `json:"_depth"` +} + +// SiteFilter is used to filter out returned object from Netbox API tenancy_tenants_list +type SiteGroupFilter struct { + Offset int64 `schema:"offset,omitempty"` + Limit int64 `schema:"limit,omitempty"` + + // User specific filters + Name string `schema:"name,omitempty"` + ID string `schema:"id,omitempty"` + Slug string `schema:"slug,omitempty"` + Description string `schema:"description,omitempty"` + Group string `schema:"group,omitempty"` + GroupID string `schema:"group_id,omitempty"` + Parent string `schema:"parent,omitempty"` + IDIn string `schema:"id__in,omitempty"` + Q string `schema:"q,omitempty"` + Tag string `schema:"tag,omitempty"` +} + +// ListSitegroups returns Netbox tenancy_tenants_list +func (n *NetBox) ListSiteGroups(i *SiteGroupsList, f *SiteFilter) error { + encoder := schema.NewEncoder() + + form := url.Values{} + err := encoder.Encode(f, form) + if err != nil { + return err + } + query := form.Encode() + + 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("GET", n.RootURL+"/api/dcim/sites/?"+query, 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\n", response.StatusCode, http.StatusOK) + } + + data, err := ioutil.ReadAll(response.Body) + if err != nil { + return err + } + + err = response.Body.Close() + if err != nil { + return err + } + + err = json.Unmarshal(data, &i) + if err != nil { + return err + } + return nil +} + +// GetSiteGroup returns a Netbox site-group +func (n *NetBox) GetSiteGroup(i *SiteGroupList, id string) error { + 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("GET", n.RootURL+"/api/dcim/site-groups/"+id, 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\n", response.StatusCode, http.StatusOK) + } + + data, err := ioutil.ReadAll(response.Body) + if err != nil { + return err + } + + err = response.Body.Close() + if err != nil { + return err + } + + err = json.Unmarshal(data, &i) + if err != nil { + return err + } + return nil +} diff --git a/netbox_sites.go b/netbox_sites.go new file mode 100644 index 0000000..033799d --- /dev/null +++ b/netbox_sites.go @@ -0,0 +1,198 @@ +package netboxgo + +import ( + "crypto/tls" + "encoding/json" + + // "fmt" + "io/ioutil" + "net/http" + "net/url" + "time" + + "github.com/gorilla/schema" + "github.com/pkg/errors" +) + +type SitesList struct { + Count int `json:"count"` + Next string `json:"next"` + Previous string `json:"previous"` + Results []SiteList `json:"results"` +} + +type SiteList struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + Name string `json:"name"` + Slug string `json:"slug"` + Status struct { + Label string `json:"label"` + Value string `json:"value"` + } `json:"status"` + Region struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + Name string `json:"name"` + Slug string `json:"slug"` + SiteCount int `json:"site_count"` + Depth int `json:"_depth"` + } `json:"region"` + Group struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + Name string `json:"name"` + Slug string `json:"slug"` + SiteCount int `json:"site_count"` + Depth int `json:"_depth"` + } `json:"group"` + Tenant struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + Name string `json:"name"` + Slug string `json:"slug"` + } `json:"tenant"` + Facility string `json:"facility"` + Asn int `json:"asn"` + TimeZone string `json:"time_zone"` + Description string `json:"description"` + PhysicalAddress string `json:"physical_address"` + ShippingAddress string `json:"shipping_address"` + Latitude string `json:"latitude"` + Longitude string `json:"longitude"` + ContactName string `json:"contact_name"` + ContactPhone string `json:"contact_phone"` + ContactEmail string `json:"contact_email"` + Comments string `json:"comments"` + 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"` + CircuitCount int `json:"circuit_count"` + DeviceCount int `json:"device_count"` + PrefixCount int `json:"prefix_count"` + RackCount int `json:"rack_count"` + VirtualmachineCount int `json:"virtualmachine_count"` + VlanCount int `json:"vlan_count"` +} + +// SiteFilter is used to filter out returned object from Netbox API tenancy_tenants_list +type SiteFilter struct { + Offset int64 `schema:"offset,omitempty"` + Limit int64 `schema:"limit,omitempty"` + + // User specific filters + Name string `schema:"name,omitempty"` + ID string `schema:"id,omitempty"` + Slug string `schema:"slug,omitempty"` + Group string `schema:"group,omitempty"` + GroupID string `schema:"group_id,omitempty"` + IDIn string `schema:"id__in,omitempty"` + Q string `schema:"q,omitempty"` + Tag string `schema:"tag,omitempty"` +} + +// ListTenants returns Netbox tenancy_tenants_list +func (n *NetBox) ListSites(i *SitesList, f *SiteFilter) error { + encoder := schema.NewEncoder() + + form := url.Values{} + err := encoder.Encode(f, form) + if err != nil { + return err + } + query := form.Encode() + + 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("GET", n.RootURL+"/api/dcim/sites/?"+query, 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\n", response.StatusCode, http.StatusOK) + } + + data, err := ioutil.ReadAll(response.Body) + if err != nil { + return err + } + + err = response.Body.Close() + if err != nil { + return err + } + + err = json.Unmarshal(data, &i) + if err != nil { + return err + } + return nil +} + +// GetSite returns Netbox tenancy_tenants_list +func (n *NetBox) GetSite(i *SiteList, id string) error { + 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("GET", n.RootURL+"/api/dcim/sites/"+id, 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\n", response.StatusCode, http.StatusOK) + } + + data, err := ioutil.ReadAll(response.Body) + if err != nil { + return err + } + + err = response.Body.Close() + if err != nil { + return err + } + + err = json.Unmarshal(data, &i) + if err != nil { + return err + } + return nil +}