netboxgo/clusters.go

184 lines
4.8 KiB
Go
Raw Normal View History

2021-11-26 11:09:27 +01:00
package netboxgo
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
)
type ClustersService service
// NewCluster is used to create new VirtualizationClusters
type NewCluster 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 []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{} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
}
// Clusters is used to list VirtualizationClusters
type Clusters 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 []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{} `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"`
}
// ClusterFilter is used to filter out VirtualizationClusters
type ClusterFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
// User specific filters
Name string `schema:"name,omitempty"`
Group string `schema:"group,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"`
}
const clustersPath = "/virtualization/clusters"
// List clusters. ClusterFilter is used to list based on filter queries.
func (s *ClustersService) List(ctx context.Context, f *ClusterFilter) (*Clusters, error) {
var clusters Clusters
var query string
var req *http.Request
var err error
encoder := schema.NewEncoder()
form := url.Values{}
err = encoder.Encode(f, form)
if err != nil {
return &clusters, err
}
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", clustersPath, query, nil)
if err != nil {
return &clusters, err
}
_, err = s.client.do(req, &clusters)
if err != nil {
return &clusters, err
}
return &clusters, nil
}
// Create a cluster
func (s *ClustersService) Create(ctx context.Context, c *NewCluster) error {
var err error
var req *http.Request
req, err = s.client.newRequest(ctx, "POST", clustersPath, "", c)
if err != nil {
return fmt.Errorf("unable to create request: %w", err)
}
_, err = s.client.do(req, nil)
if err != nil {
return fmt.Errorf("unable to do request: %w", err)
}
return nil
}
// // Update a cluster
// func (s *ClustersService) Update(ctx context.Context, id string, c *UpdateCluster) error {
// var req *http.Request
// var err error
// path := fmt.Sprintf("%s/%s", clustersPath, id)
// req, err = s.client.newRequest(ctx, "PATCH", path, "", c)
// if err != nil {
// return fmt.Errorf("unable to create request: %w", err)
// }
// _, err = s.client.do(req, nil)
// if err != nil {
// return fmt.Errorf("unable to do request: %w", err)
// }
// return nil