From bf376105e76344def152c09c8ceaa64dfbad510c Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Mon, 16 Dec 2019 23:03:09 +0100 Subject: [PATCH] Add virtualization cluster list. --- netbox_virtualization.go | 154 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 netbox_virtualization.go diff --git a/netbox_virtualization.go b/netbox_virtualization.go new file mode 100644 index 0000000..3594fe0 --- /dev/null +++ b/netbox_virtualization.go @@ -0,0 +1,154 @@ +package netboxgo + +import ( + "crypto/tls" + "encoding/json" + + "io/ioutil" + "net/http" + "net/url" + + "time" + + "github.com/gorilla/schema" + "github.com/pkg/errors" +) + +type VirtualizationClustersCreate struct { + ID int `json:"id"` + Name string `json:"name"` + Type struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + Slug string `json:"slug"` + ClusterCount int `json:"cluster_count"` + } `json:"type"` + Group struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + Slug string `json:"slug"` + ClusterCount int `json:"cluster_count"` + } `json:"group"` + Site struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + Slug string `json:"slug"` + } `json:"site"` + Comments string `json:"comments"` + Tags []string `json:"tags"` + CustomFields struct { + } `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` + DeviceCount int `json:"device_count"` + VirtualmachineCount int `json:"virtualmachine_count"` +} + +type VirtualizationClustersList struct { + Count int `json:"count"` + Next string `json:"next"` + Previous string `json:"previous"` + Results []struct { + ID int `json:"id"` + Name string `json:"name"` + Type struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + Slug string `json:"slug"` + ClusterCount int `json:"cluster_count"` + } `json:"type"` + Group struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + Slug string `json:"slug"` + ClusterCount int `json:"cluster_count"` + } `json:"group"` + Site struct { + ID int `json:"id"` + URL string `json:"url"` + Name string `json:"name"` + Slug string `json:"slug"` + } `json:"site"` + Comments string `json:"comments"` + Tags []string `json:"tags"` + CustomFields struct { + } `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` + DeviceCount int `json:"device_count"` + VirtualmachineCount int `json:"virtualmachine_count"` + } `json:"results"` +} + +type ClusterFilter struct { + Offset int64 `schema:"offset,omitempty"` + Limit int64 `schema:"limit,omitempty"` + + //User specific filters + Name string `schema:"name,omitempty"` + GroupID string `schema:"group_id,omitempty"` + TypeID string `schema:"type_id,omitempty"` + Type string `schema:"type,omitempty"` + SiteID string `schema:"site_id,omitempty"` + Site string `schema:"site,omitempty"` + Tenant string `schema:"tenant,omitempty"` + IDIn string `schema:"id__in,omitempty"` + Q string `schema:"q,omitempty"` + Tag string `schema:"tag,omitempty"` +} + +// ListVirtualizationClusters returns Netbox virtualization_clusters +func (n *NetBox) ListClusters(i *VirtualizationClustersList, f *ClusterFilter) error { + var 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/ipam/vrfs/?"+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 +}