Skip to content

Commit

Permalink
Merge pull request #188 from Tinyblargon/CLI-Overhaul
Browse files Browse the repository at this point in the history
Cli overhaul: acmeaccount
  • Loading branch information
mleone87 authored Aug 31, 2022
2 parents 0129fa9 + 56181cf commit f6b9051
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 15 deletions.
33 changes: 33 additions & 0 deletions cli/command/create/create-acmeaccount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package create

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

var create_acmeaccountCmd = &cobra.Command{
Use: "acmeaccount ACMEACCOUNTID",
Short: "Creates a new AcmeAccount",
Long: `Creates a new AcmeAccount.
The config can be set with the --file flag or piped from stdin.
For config examples see "example acmeaccount"`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
id := cli.ValidateIDset(args, 0, "AcmeAccountID")
config, err := proxmox.NewConfigAcmeAccountFromJson(cli.NewConfig())
if err != nil {
return
}
c := cli.NewClient()
err = config.CreateAcmeAccount(id, c)
if err != nil {
return
}
cli.PrintItemCreated(createCmd.OutOrStdout(), id, "AcmeAccount")
return
},
}

func init() {
createCmd.AddCommand(create_acmeaccountCmd)
}
17 changes: 17 additions & 0 deletions cli/command/delete/delete-acmeaccount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package delete

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

var delete_acmeaccountCmd = &cobra.Command{
Use: "acmeaccount ACMEACCOUNTID",
Short: "Deletes the Speciefied AcmeAccount",
RunE: func(cmd *cobra.Command, args []string) error {
return DeleteID(args, "AcmeAccount")
},
}

func init() {
deleteCmd.AddCommand(delete_acmeaccountCmd)
}
20 changes: 14 additions & 6 deletions cli/command/delete/delete.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package delete

import (
"fmt"

"github.com/Telmate/proxmox-api-go/cli"
"github.com/spf13/cobra"
)
Expand All @@ -14,22 +16,28 @@ func init() {
cli.RootCmd.AddCommand(deleteCmd)
}

func DeleteID(args []string, IDtype string) (err error){
func DeleteID(args []string, IDtype string) (err error) {
var exitStatus string
id := cli.ValidateIDset(args, 0, IDtype+"ID")
c := cli.NewClient()
switch IDtype {
case "MetricServer" :
case "AcmeAccount":
exitStatus, err = c.DeleteAcmeAccount(id)
case "MetricServer":
err = c.DeleteMetricServer(id)
case "Pool" :
case "Pool":
err = c.DeletePool(id)
case "Storage" :
case "Storage":
err = c.DeleteStorage(id)
case "User" :
case "User":
err = c.DeleteUser(id)
}
if err != nil {
if exitStatus != "" {
err = fmt.Errorf("error deleting %s (%s): %v, error status: %s ", IDtype, id, err, exitStatus)
}
return
}
cli.PrintItemDeleted(deleteCmd.OutOrStdout(), id, IDtype)
return
}
}
17 changes: 17 additions & 0 deletions cli/command/get/get-acmeaccount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package get

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

var get_acmeaccountCmd = &cobra.Command{
Use: "acmeaccount ACMEACCOUNTID",
Short: "Gets the configuration of the specified AcmeAccount",
RunE: func(cmd *cobra.Command, args []string) (err error) {
return GetConfig(args, "AcmeAccount")
},
}

func init() {
getCmd.AddCommand(get_acmeaccountCmd)
}
12 changes: 7 additions & 5 deletions cli/command/get/get.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package get

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

Expand All @@ -20,16 +20,18 @@ func GetConfig(args []string, IDtype string) (err error) {
c := cli.NewClient()
var config interface{}
switch IDtype {
case "MetricServer" :
case "AcmeAccount":
config, err = proxmox.NewConfigAcmeAccountFromApi(id, c)
case "MetricServer":
config, err = proxmox.NewConfigMetricsFromApi(id, c)
case "Storage" :
case "Storage":
config, err = proxmox.NewConfigStorageFromApi(id, c)
case "User" :
case "User":
config, err = proxmox.NewConfigUserFromApi(id, c)
}
if err != nil {
return
}
cli.PrintFormattedJson(getCmd.OutOrStdout(),config)
cli.PrintFormattedJson(getCmd.OutOrStdout(), config)
return
}
8 changes: 4 additions & 4 deletions proxmox/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,7 @@ func (c *Client) CreateItemWithTask(Params map[string]interface{}, url string) (
var resp *http.Response
resp, err = c.session.Post(url, nil, nil, &reqbody)
if err != nil {
return c.HandleTaskError(resp)
return c.HandleTaskError(resp), err
}
return c.CheckTask(resp)
}
Expand All @@ -1769,7 +1769,7 @@ func (c *Client) UpdateItemWithTask(Params map[string]interface{}, url string) (
var resp *http.Response
resp, err = c.session.Put(url, nil, nil, &reqbody)
if err != nil {
return c.HandleTaskError(resp)
return c.HandleTaskError(resp), err
}
return c.CheckTask(resp)
}
Expand All @@ -1784,7 +1784,7 @@ func (c *Client) DeleteUrlWithTask(url string) (exitStatus string, err error) {
var resp *http.Response
resp, err = c.session.Delete(url, nil, nil)
if err != nil {
return c.HandleTaskError(resp)
return c.HandleTaskError(resp), err
}
return c.CheckTask(resp)
}
Expand All @@ -1795,7 +1795,7 @@ func (c *Client) GetItemList(url string) (list map[string]interface{}, err error
}

// Close task responce
func (c *Client) HandleTaskError(resp *http.Response) (exitStatus string, err error) {
func (c *Client) HandleTaskError(resp *http.Response) (exitStatus string) {
defer resp.Body.Close()
// This might not work if we never got a body. We'll ignore errors in trying to read,
// but extract the body if possible to give any error information back in the exitStatus
Expand Down
65 changes: 65 additions & 0 deletions test/cli/AcmeAccount/AcmeAccount_0_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cli_acmeaccount_test

import (
"testing"

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

func Test_AcmeAccount_0_Cleanup(t *testing.T) {
Test := cliTest.Test{
ReqErr: true,
ErrContains: "test-0",
Args: []string{"-i", "delete", "acmeaccount", "test-0"},
}
Test.StandardTest(t)
}

func Test_AcmeAccount_0_Set(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"contact": [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
],
"directory": "https://acme-staging-v02.api.letsencrypt.org/directory",
"tos": true
}`,
Expected: "(test-0)",
Contains: true,
Args: []string{"-i", "create", "acmeaccount", "test-0"},
}
Test.StandardTest(t)
}

func Test_AcmeAccount_0_Get(t *testing.T) {
Test := cliTest.Test{
OutputJson: `
{
"name": "test-0",
"contact": [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
],
"directory": "https://acme-staging-v02.api.letsencrypt.org/directory",
"tos": true
}`,
Args: []string{"-i", "get", "acmeaccount", "test-0"},
}
Test.StandardTest(t)
}

func Test_AcmeAccount_0_Delete(t *testing.T) {
Test := cliTest.Test{
Expected: "",
ReqErr: false,
Args: []string{"-i", "delete", "acmeaccount", "test-0"},
}
Test.StandardTest(t)
}
59 changes: 59 additions & 0 deletions test/cli/AcmeAccount/AcmeAccount_1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cli_acmeaccount_test

import (
"testing"

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

func Test_AcmeAccount_1_Cleanup(t *testing.T) {
Test := cliTest.Test{
ReqErr: true,
ErrContains: "test-1",
Args: []string{"-i", "delete", "acmeaccount", "test-1"},
}
Test.StandardTest(t)
}

func Test_AcmeAccount_1_Set(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"contact": [
"[email protected]"
],
"directory": "https://acme-staging-v02.api.letsencrypt.org/directory",
"tos": true
}`,
Expected: "(test-1)",
Contains: true,
Args: []string{"-i", "create", "acmeaccount", "test-1"},
}
Test.StandardTest(t)
}

func Test_AcmeAccount_1_Get(t *testing.T) {
Test := cliTest.Test{
OutputJson: `
{
"name": "test-1",
"contact": [
"[email protected]"
],
"directory": "https://acme-staging-v02.api.letsencrypt.org/directory",
"tos": true
}`,
Args: []string{"-i", "get", "acmeaccount", "test-1"},
}
Test.StandardTest(t)
}

func Test_AcmeAccount_1_Delete(t *testing.T) {
Test := cliTest.Test{
Expected: "",
ReqErr: false,
Args: []string{"-i", "delete", "acmeaccount", "test-1"},
}
Test.StandardTest(t)
}

0 comments on commit f6b9051

Please sign in to comment.