From fc390abb4e4edff592e88d953ba3ab5f79a0e78d Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Fri, 19 Aug 2022 12:35:38 +0200 Subject: [PATCH 1/4] Add: acmeaccount commands --- cli/command/create/create-acmeaccount.go | 31 ++++++++++++++++++++++++ cli/command/delete/delete-acmeaccount.go | 17 +++++++++++++ cli/command/delete/delete.go | 20 ++++++++++----- cli/command/get/get-acmeaccount.go | 17 +++++++++++++ cli/command/get/get.go | 12 +++++---- 5 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 cli/command/create/create-acmeaccount.go create mode 100644 cli/command/delete/delete-acmeaccount.go create mode 100644 cli/command/get/get-acmeaccount.go diff --git a/cli/command/create/create-acmeaccount.go b/cli/command/create/create-acmeaccount.go new file mode 100644 index 00000000..ffc25a01 --- /dev/null +++ b/cli/command/create/create-acmeaccount.go @@ -0,0 +1,31 @@ +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", + Short: "Placeholder", + Long: `A longer Placeholder`, + 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) +} diff --git a/cli/command/delete/delete-acmeaccount.go b/cli/command/delete/delete-acmeaccount.go new file mode 100644 index 00000000..fb5a9777 --- /dev/null +++ b/cli/command/delete/delete-acmeaccount.go @@ -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) +} diff --git a/cli/command/delete/delete.go b/cli/command/delete/delete.go index 0f5af319..8cc90c49 100644 --- a/cli/command/delete/delete.go +++ b/cli/command/delete/delete.go @@ -1,6 +1,8 @@ package delete import ( + "fmt" + "github.com/Telmate/proxmox-api-go/cli" "github.com/spf13/cobra" ) @@ -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 -} \ No newline at end of file +} diff --git a/cli/command/get/get-acmeaccount.go b/cli/command/get/get-acmeaccount.go new file mode 100644 index 00000000..29ce51a8 --- /dev/null +++ b/cli/command/get/get-acmeaccount.go @@ -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) +} diff --git a/cli/command/get/get.go b/cli/command/get/get.go index e910f904..6f5b5e70 100644 --- a/cli/command/get/get.go +++ b/cli/command/get/get.go @@ -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" ) @@ -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 } From 31fd660fb2b71b60eee2bb626c74dc319cceae95 Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Fri, 19 Aug 2022 12:38:33 +0200 Subject: [PATCH 2/4] Update: usage description --- cli/command/create/create-acmeaccount.go | 8 +++++--- cli/command/delete/delete-acmeaccount.go | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cli/command/create/create-acmeaccount.go b/cli/command/create/create-acmeaccount.go index ffc25a01..79e69aa9 100644 --- a/cli/command/create/create-acmeaccount.go +++ b/cli/command/create/create-acmeaccount.go @@ -7,9 +7,11 @@ import ( ) var create_acmeaccountCmd = &cobra.Command{ - Use: "acmeaccount", - Short: "Placeholder", - Long: `A longer Placeholder`, + 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()) diff --git a/cli/command/delete/delete-acmeaccount.go b/cli/command/delete/delete-acmeaccount.go index fb5a9777..f35f4101 100644 --- a/cli/command/delete/delete-acmeaccount.go +++ b/cli/command/delete/delete-acmeaccount.go @@ -6,7 +6,7 @@ import ( var delete_acmeaccountCmd = &cobra.Command{ Use: "acmeaccount ACMEACCOUNTID", - Short: "Deletes the Speciefied acmeaccount", + Short: "Deletes the Speciefied AcmeAccount", RunE: func(cmd *cobra.Command, args []string) error { return DeleteID(args, "AcmeAccount") }, From 5fee9d857eb828f453a4f5f543c6cb3ea9732d9e Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Fri, 19 Aug 2022 12:50:41 +0200 Subject: [PATCH 3/4] Tests for acmeaccount --- test/cli/AcmeAccount/AcmeAccount_0_test.go | 65 ++++++++++++++++++++++ test/cli/AcmeAccount/AcmeAccount_1_test.go | 59 ++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 test/cli/AcmeAccount/AcmeAccount_0_test.go create mode 100644 test/cli/AcmeAccount/AcmeAccount_1_test.go diff --git a/test/cli/AcmeAccount/AcmeAccount_0_test.go b/test/cli/AcmeAccount/AcmeAccount_0_test.go new file mode 100644 index 00000000..2154d964 --- /dev/null +++ b/test/cli/AcmeAccount/AcmeAccount_0_test.go @@ -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": [ + "a@nonexistantdomain.com", + "b@nonexistantdomain.com", + "c@nonexistantdomain.com", + "d@nonexistantdomain.com" + ], + "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": [ + "a@nonexistantdomain.com", + "b@nonexistantdomain.com", + "c@nonexistantdomain.com", + "d@nonexistantdomain.com" + ], + "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) +} diff --git a/test/cli/AcmeAccount/AcmeAccount_1_test.go b/test/cli/AcmeAccount/AcmeAccount_1_test.go new file mode 100644 index 00000000..90231d9c --- /dev/null +++ b/test/cli/AcmeAccount/AcmeAccount_1_test.go @@ -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": [ + "a@nonexistantdomain.com" + ], + "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": [ + "a@nonexistantdomain.com" + ], + "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) +} From 56181cf0ec4dcf148ed65c0786738c4719c02c7f Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Fri, 19 Aug 2022 13:05:25 +0200 Subject: [PATCH 4/4] Return err when error occures --- proxmox/client.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/proxmox/client.go b/proxmox/client.go index a504daf2..2dff5a78 100644 --- a/proxmox/client.go +++ b/proxmox/client.go @@ -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) } @@ -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) } @@ -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) } @@ -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