Skip to content

Commit

Permalink
Merge pull request #178 from Tinyblargon/CLI-Overhaul
Browse files Browse the repository at this point in the history
Cli overhaul: MetricServer
  • Loading branch information
mleone87 authored May 10, 2022
2 parents 69c923c + ca862cc commit 91eff17
Show file tree
Hide file tree
Showing 10 changed files with 830 additions and 11 deletions.
17 changes: 17 additions & 0 deletions cli/command/get/get-metricserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package get

import (
"github.com/spf13/cobra"
)

var get_metricserverCmd = &cobra.Command{
Use: "metricserver METRICSID",
Short: "Gets the configuration of the specified MetricServer",
RunE: func(cmd *cobra.Command, args []string) error {
return GetConfig(args, "MetricServer")
},
}

func init() {
getCmd.AddCommand(get_metricserverCmd)
}
2 changes: 2 additions & 0 deletions cli/command/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func GetConfig(args []string, IDtype string) (err error) {
c := cli.NewClient()
var config interface{}
switch IDtype {
case "MetricServer" :
config, err = proxmox.NewConfigMetricsFromApi(id, c)
case "User" :
config, err = proxmox.NewConfigUserFromApi(id, c)
}
Expand Down
34 changes: 34 additions & 0 deletions cli/command/set/set-metricserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package set

import (
"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

var set_metricserverCmd = &cobra.Command{
Use: "metricserver METRICSID",
Short: "Sets the current state of a MetricServer",
Long: `Sets the current state of a MetricServer.
Depending on the current state of the MetricServer, the MetricServer will be created or updated.
The config can be set with the --file flag or piped from stdin.
For config examples see "example metricserver"`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
id := cli.ValidateIDset(args, 0,"MetricServerID")
config, err := proxmox.NewConfigMetricsFromJson(cli.NewConfig())
if err != nil {
return
}
c := cli.NewClient()
err = config.SetMetrics(id, c)
if err != nil {
return
}
cli.PrintItemSet(setCmd.OutOrStdout() ,id ,"MericServer")
return
},
}

func init() {
setCmd.AddCommand(set_metricserverCmd)
}
30 changes: 19 additions & 11 deletions proxmox/config_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
)

type ConfigMetricsGraphite struct {
Protocol string `json:"protocol,omitempty"`
Protocol string `json:"protocol"`
Path string `json:"path,omitempty"`
}

type ConfigMetricsInfluxDB struct {
ApiPathPrefix string `json:"api-path-prefix,omitempty"`
Bucket string `json:"bucket,omitempty"`
Protocol string `json:"protocol,omitempty"`
Protocol string `json:"protocol"`
MaxBodySize int `json:"max-body-size,omitempty"`
Organization string `json:"organization,omitempty"`
Token string `json:"token,omitempty"`//token key is never returned from api
VerifyCertificate bool `json:"verify-certificate,omitempty"`
VerifyCertificate bool `json:"verify-certificate"`
}

// Metrics options for the Proxmox API
Expand All @@ -27,30 +27,35 @@ type ConfigMetrics struct {
Port int `json:"port"`
Server string `json:"server"`
Type string `json:"type"`//type key is only used on create
Enable bool `json:"enable,omitempty"`
MTU int `json:"mtu,omitempty"`
Enable bool `json:"enable"`
MTU int `json:"mtu"`
Timeout int `json:"timeout,omitempty"`
Graphite *ConfigMetricsGraphite `json:"graphite,omitempty"`
InfluxDB *ConfigMetricsInfluxDB `json:"influxdb,omitempty"`
}

func (config *ConfigMetrics) MapMetricsToApiValues()(params map[string]interface{}) {
func (config *ConfigMetrics) MapMetricsToApiValues(create bool) (params map[string]interface{}) {
var deletions string
params = map[string]interface{}{
"port": config.Port,
"server": config.Server,
"disable": BoolInvert(config.Enable),
"mtu": config.MTU,
"timeout": config.Timeout,
}
if config.Type != "" {
if create {
params["type"] = config.Type
}
if config.Graphite != nil{
params["path"] = config.Graphite.Path
params["proto"] = config.Graphite.Protocol
}
if config.InfluxDB != nil{
params["api-path-prefix"] = config.InfluxDB.ApiPathPrefix
if config.InfluxDB.ApiPathPrefix != "" {
params["api-path-prefix"] = config.InfluxDB.ApiPathPrefix
} else {
deletions = AddToList(deletions, "api-path-prefix")
}
params["bucket"] = config.InfluxDB.Bucket
params["max-body-size"] = config.InfluxDB.MaxBodySize
params["organization"] = config.InfluxDB.Organization
Expand All @@ -60,6 +65,10 @@ func (config *ConfigMetrics) MapMetricsToApiValues()(params map[string]interface
}
params["verify-certificate"] = config.InfluxDB.VerifyCertificate
}

if !create && deletions != "" {
params["delete"] = deletions
}
return
}

Expand Down Expand Up @@ -127,7 +136,7 @@ func (config *ConfigMetrics) SetMetrics(metricsid string, client *Client) (err e

func (config *ConfigMetrics) CreateMetrics(client *Client) (err error) {
config.RemoveMetricsNestedStructs()
params := config.MapMetricsToApiValues()
params := config.MapMetricsToApiValues(true)
err = client.CreateMetricServer(config.Name, params)
if err != nil {
params, _ := json.Marshal(&params)
Expand All @@ -138,8 +147,7 @@ func (config *ConfigMetrics) CreateMetrics(client *Client) (err error) {

func (config *ConfigMetrics) UpdateMetrics(client *Client) (err error) {
config.RemoveMetricsNestedStructs()
config.Type = ""
params := config.MapMetricsToApiValues()
params := config.MapMetricsToApiValues(false)
err = client.UpdateMetricServer(config.Name, params)
if err != nil {
params, _ := json.Marshal(&params)
Expand Down
167 changes: 167 additions & 0 deletions test/cli/MetricServers/MetricServer_Errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package cli_metricservers_test

import (
"testing"
_ "github.com/Telmate/proxmox-api-go/cli/command/commands"
cliTest "github.com/Telmate/proxmox-api-go/test/cli"
)

func Test_MetricServer_Errors_Type(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"type": "this gives an error"
}`,
ReqErr: true,
ErrContains: "(type)",
Args: []string{"-i","set","metricserver","test-metricserver00"},
}
Test.StandardTest(t)
}

func Test_MetricServer_Errors_Server(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"type": "influxdb",
"server": ""
}`,
ReqErr: true,
ErrContains: "(server)",
Args: []string{"-i","set","metricserver","test-metricserver00"},
}
Test.StandardTest(t)
}

func Test_MetricServer_Errors_Port_Lower(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"type": "influxdb",
"server": "192.168.67.3",
"port": 0
}`,
ReqErr: true,
ErrContains: "(port)",
Args: []string{"-i","set","metricserver","test-metricserver00"},
}
Test.StandardTest(t)
}

func Test_MetricServer_Errors_Port_Upper(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"type": "influxdb",
"server": "192.168.67.3",
"port": 65537
}`,
ReqErr: true,
ErrContains: "(port)",
Args: []string{"-i","set","metricserver","test-metricserver00"},
}
Test.StandardTest(t)
}

func Test_MetricServer_Errors_MTU_Lower(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"type": "influxdb",
"server": "192.168.67.3",
"port": 65536,
"mtu": 511
}`,
ReqErr: true,
ErrContains: "(mtu)",
Args: []string{"-i","set","metricserver","test-metricserver00"},
}
Test.StandardTest(t)
}

func Test_MetricServer_Errors_MTU_Upper(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"type": "influxdb",
"server": "192.168.67.3",
"port": 1,
"mtu": 65537
}`,
ReqErr: true,
ErrContains: "(mtu)",
Args: []string{"-i","set","metricserver","test-metricserver00"},
}
Test.StandardTest(t)
}

func Test_MetricServer_Errors_Timeout(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"type": "influxdb",
"server": "192.168.67.3",
"port": 1,
"mtu": 512,
"timeout": -1
}`,
ReqErr: true,
ErrContains: "(timeout)",
Args: []string{"-i","set","metricserver","test-metricserver00"},
}
Test.StandardTest(t)
}

// Graphite
func Test_MetricServer_Errors_Graphite_Protocol(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"type": "graphite",
"server": "192.168.67.3",
"graphite": {
"protocol": "notvalid"
}
}`,
ReqErr: true,
ErrContains: "(graphite:{ protocol })",
Args: []string{"-i","set","metricserver","test-metricserver00"},
}
Test.StandardTest(t)
}

// InfluxDB
func Test_MetricServer_Errors_InfluxDB_Protocol(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"type": "influxdb",
"server": "192.168.67.3",
"influxdb": {
"protocol": "notvalid"
}
}`,
ReqErr: true,
ErrContains: "(influxdb:{ protocol })",
Args: []string{"-i","set","metricserver","test-metricserver00"},
}
Test.StandardTest(t)
}

func Test_MetricServer_Errors_InfluxDB_MaxBodySize(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"type": "influxdb",
"server": "192.168.67.3",
"port": 8089,
"influxdb": {
"max-body-size": 0
}
}`,
ReqErr: true,
ErrContains: "(influxdb:{ max-body-size })",
Args: []string{"-i","set","metricserver","test-metricserver00"},
}
Test.StandardTest(t)
}
Loading

0 comments on commit 91eff17

Please sign in to comment.