284 lines
8.1 KiB
Go
284 lines
8.1 KiB
Go
package netboxgo
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
"github.com/gorilla/schema"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
//VirtualizationVirtualMachinesCreate is used to create new VirtualizationVirtualMachines
|
|
type VirtualizationVirtualMachinesCreate struct {
|
|
ID int `json:"id"`
|
|
Name string `json:"name"`
|
|
Status struct {
|
|
Label string `json:"label"`
|
|
Value int `json:"value"`
|
|
} `json:"status"`
|
|
Site struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
} `json:"site"`
|
|
Cluster struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
VirtualmachineCount int `json:"virtualmachine_count"`
|
|
} `json:"cluster"`
|
|
Role struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
DeviceCount int `json:"device_count"`
|
|
VirtualmachineCount int `json:"virtualmachine_count"`
|
|
} `json:"role"`
|
|
Tenant struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
} `json:"tenant"`
|
|
Platform struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Name string `json:"name"`
|
|
Slug string `json:"slug"`
|
|
DeviceCount int `json:"device_count"`
|
|
VirtualmachineCount int `json:"virtualmachine_count"`
|
|
} `json:"platform"`
|
|
PrimaryIP struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Family int `json:"family"`
|
|
Address string `json:"address"`
|
|
} `json:"primary_ip"`
|
|
PrimaryIP4 struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Family int `json:"family"`
|
|
Address string `json:"address"`
|
|
} `json:"primary_ip4"`
|
|
PrimaryIP6 struct {
|
|
ID int `json:"id"`
|
|
URL string `json:"url"`
|
|
Family int `json:"family"`
|
|
Address string `json:"address"`
|
|
} `json:"primary_ip6"`
|
|
Vcpus int `json:"vcpus"`
|
|
Memory int `json:"memory"`
|
|
Disk int `json:"disk"`
|
|
Comments string `json:"comments"`
|
|
LocalContextData string `json:"local_context_data"`
|
|
Tags []string `json:"tags"`
|
|
CustomFields struct {
|
|
} `json:"custom_fields"`
|
|
ConfigContext struct {
|
|
AdditionalProp1 string `json:"additionalProp1"`
|
|
AdditionalProp2 string `json:"additionalProp2"`
|
|
AdditionalProp3 string `json:"additionalProp3"`
|
|
} `json:"config_context"`
|
|
Created string `json:"created"`
|
|
LastUpdated time.Time `json:"last_updated"`
|
|
}
|
|
|
|
//VirtualizationClustersCreate is used to create new VirtualizationClusters
|
|
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 []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"`
|
|
}
|
|
|
|
// VirtualizationClustersList is used to list VirtualizationClusters
|
|
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 []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"`
|
|
}
|
|
|
|
// ListClusters 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/virtualization/clusters/?"+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
|
|
}
|
|
|
|
// CreateVirtualMachine creates VirtualMachines via Netbox API dcim_interfaces_create
|
|
func (n *NetBox) CreateVirtualMachine(v *VirtualizationClustersCreate) 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/virtualization/virtual-machines/", 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)
|
|
}
|