Skip to content

Commit

Permalink
Merge pull request #85 from phoenixnap/feature/add-paymentsapi-v2
Browse files Browse the repository at this point in the history
Feature/add paymentsapi v2
  • Loading branch information
valerioponte authored Feb 9, 2024
2 parents dac78e3 + e355db6 commit b302c2b
Show file tree
Hide file tree
Showing 17 changed files with 426 additions and 12 deletions.
1 change: 1 addition & 0 deletions component-tests/support/constants/help-outputs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Available Commands:
ssh-key Retrieve one or all ssh-keys for your account.
storage-network Retrieve one or all storage networks.
tag Retrieve one or all tags.
transaction Retrieve one or all transactions for your account.
Flags:
-h, --help help for get
Expand Down
2 changes: 1 addition & 1 deletion docs/generation/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ require (
github.com/phoenixnap/go-sdk-bmc/locationapi v1.0.0 // indirect
github.com/phoenixnap/go-sdk-bmc/networkapi/v2 v2.3.1 // indirect
github.com/phoenixnap/go-sdk-bmc/networkstorageapi v1.4.0 // indirect
github.com/phoenixnap/go-sdk-bmc/paymentsapi v1.0.1 // indirect
github.com/phoenixnap/go-sdk-bmc/ranchersolutionapi/v2 v2.0.5 // indirect
github.com/phoenixnap/go-sdk-bmc/tagapi/v2 v2.0.5 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rs/zerolog v1.31.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions docs/generation/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ github.com/phoenixnap/go-sdk-bmc/networkapi/v2 v2.3.1 h1:HFRYgoCTBvYu6j0smwK+0QB
github.com/phoenixnap/go-sdk-bmc/networkapi/v2 v2.3.1/go.mod h1:zkZLpUrrpt8XdQZnrLus+UhapP6dlbeGGtGTkmgUJts=
github.com/phoenixnap/go-sdk-bmc/networkstorageapi v1.4.0 h1:C47dgp+VUCUds1wWfTHo/uxNcuqlFYzPByamiG/NyXw=
github.com/phoenixnap/go-sdk-bmc/networkstorageapi v1.4.0/go.mod h1:IV5yA1XAOYNlO1esYGX4jLmbrKOjQ0jbXv5lRl2V18Q=
github.com/phoenixnap/go-sdk-bmc/paymentsapi v1.0.1 h1:1l33Tmf2T/KlhtF9z8Eowv+QY+7XH6XcjsjOGF1s15A=
github.com/phoenixnap/go-sdk-bmc/paymentsapi v1.0.1/go.mod h1:ZplGK88bXlz04t2O/nUrjej/3rDw4kCNmAkifrKUL4A=
github.com/phoenixnap/go-sdk-bmc/ranchersolutionapi/v2 v2.0.5 h1:rQKWPjb1J4F7FegeoV03seuPrGEJtbPyOeYFc5vgWKs=
github.com/phoenixnap/go-sdk-bmc/ranchersolutionapi/v2 v2.0.5/go.mod h1://8OW8iHrlF1kWpwcigZ0fFZpgit4t9I4cemZhonq40=
github.com/phoenixnap/go-sdk-bmc/tagapi/v2 v2.0.5 h1:qsLlQncNX2bfJBYyM9wQl+CAyD2YbyMCzrhZD5dSB7A=
Expand All @@ -80,8 +82,6 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
Expand All @@ -97,7 +97,6 @@ github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMV
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand All @@ -123,7 +122,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
1 change: 1 addition & 0 deletions docs/pnapctl_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ pnapctl get [flags]
* [pnapctl get ssh-key](pnapctl_get_ssh-key.md) - Retrieve one or all ssh-keys for your account.
* [pnapctl get storage-network](pnapctl_get_storage-network.md) - Retrieve one or all storage networks.
* [pnapctl get tag](pnapctl_get_tag.md) - Retrieve one or all tags.
* [pnapctl get transaction](pnapctl_get_transaction.md) - Retrieve one or all transactions for your account.

54 changes: 54 additions & 0 deletions docs/pnapctl_get_transaction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## pnapctl get transaction

Retrieve one or all transactions for your account.

### Synopsis

Retrieve one or all transactions for your account.

Prints all information about the transactions assigned to your account.
By default, the data is printed in json format.

Table format isn't supported for this command.

To print a specific transaction, a transaction ID needs to be passed as an argument.

```
pnapctl get transaction [TRANSACTION_ID] [flags]
```

### Examples

```
# List all transactions in.
pnapctl get transactions [--limit <LIMIT>] [--offset <OFFSET>] [--sortdirection <SORTDIRECTION>] [--sortfield <SORTFIELD>] [--from <FROM>] [--to <TO>] [--output <OUTPUT_TYPE>]
# List a specific transaction.
pnapctl get transactions <TRANSACTION_ID> [--output <OUTPUT_TYPE>]
```

### Options

```
--from string A 'from' filter. Needs to be in the following format: '2021-04-27T16:24:57.123Z'
-h, --help help for transaction
--limit int Limit the number of records returned.
--offset int The number of items to skip in the results.
-o, --output string Define the output format. Possible values: table, json, yaml (default "table")
--sortDirection string Ordering of the event's time. Must be 'ASC' or 'DESC'
--sortField string If a sortField is requested, pagination will be done after sorting. Default sorting is by date.
--to string A 'to' filter. Needs to be in the following format: '2021-04-27T16:24:57.123Z'
```

### Options inherited from parent commands

```
--config string config file defaults to the environment variable "PNAPCTL_HOME" or "pnap.yaml" in the home directory.
-v, --verbose change log level from Warn (default) to Debug.
```

### SEE ALSO

* [pnapctl get](pnapctl_get.md) - Display one or many resources.

2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ generate-mock-util: $(MOCKGEN) ; $(info $(M) generating mock util $(UTIL)...) @
$Q $(MOCKGEN) --source=common/$(UTIL)/$(UTIL).go --destination=testsupport/mocks/mock_$(UTIL).go --package=mocks

# All items to generate mocks for
gen-clients = bmcapi audit ip networks rancher tags billing networkstorage locations
gen-clients = bmcapi audit ip networks rancher tags billing networkstorage locations payments
gen-utils = printer fileprocessor
gen-all = $(gen-clients) $(gen-utils)

Expand Down
5 changes: 4 additions & 1 deletion src/apps/pnapctl/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
"phoenixnap.com/pnapctl/common/client/ip"
"phoenixnap.com/pnapctl/common/client/locations"
"phoenixnap.com/pnapctl/common/client/networkstorage"
"phoenixnap.com/pnapctl/common/client/payments"

"github.com/mitchellh/go-homedir"
"github.com/rs/zerolog"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"

Expand Down Expand Up @@ -166,6 +167,7 @@ func initConfig() {
customBillingHostname := viper.GetString("billingHostname")
customNetworkStorageHostname := viper.GetString("networkStorageHostname")
customLocationHostname := viper.GetString("locationHostname")
customPaymentsHostname := viper.GetString("paymentsHostname")
customTokenUrl := viper.GetString("tokenURL")

bmcapi.Client = bmcapi.NewMainClient(clientId, clientSecret, customBmcApiHostname, customTokenUrl)
Expand All @@ -177,6 +179,7 @@ func initConfig() {
billing.Client = billing.NewMainClient(clientId, clientSecret, customBillingHostname, customTokenUrl)
networkstorage.Client = networkstorage.NewMainClient(clientId, clientSecret, customNetworkStorageHostname, customTokenUrl)
locations.Client = locations.NewMainClient(clientId, clientSecret, customLocationHostname, customTokenUrl)
payments.Client = payments.NewMainClient(clientId, clientSecret, customPaymentsHostname, customTokenUrl)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/commands/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"phoenixnap.com/pnapctl/commands/get/sshkeys"
storagenetworks "phoenixnap.com/pnapctl/commands/get/storage-networks"
"phoenixnap.com/pnapctl/commands/get/tags"
"phoenixnap.com/pnapctl/commands/get/transactions"
)

var GetCmd = &cobra.Command{
Expand Down Expand Up @@ -49,4 +50,5 @@ func init() {
GetCmd.AddCommand(account_billing_configuration.GetAccountBillingConfigurationCmd)
GetCmd.AddCommand(storagenetworks.GetStorageNetworksCmd)
GetCmd.AddCommand(locations.GetLocationsCmd)
GetCmd.AddCommand(transactions.GetTransactionsCmd)
}
45 changes: 45 additions & 0 deletions src/commands/get/transactions/get_all_transactions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package transactions

import (
"testing"

paymentsSdk "github.com/phoenixnap/go-sdk-bmc/paymentsapi"
"github.com/stretchr/testify/assert"
"phoenixnap.com/pnapctl/common/models/generators"
. "phoenixnap.com/pnapctl/testsupport/mockhelp"
)

func getRequestParams() (int, int, string, string, string, string) {
return Limit, Offset, SortDirection, SortField, From, To
}

func TestGetAllTransactionsSuccess(test_framework *testing.T) {
paginatedTransactions := generators.Generate[paymentsSdk.PaginatedTransactions]()

// Mocking
PreparePaymentsApiMockClient(test_framework).
TransactionsGet(getRequestParams()).
Return(&paginatedTransactions, nil)

ExpectToPrintSuccess(test_framework, &paginatedTransactions)

err := GetTransactionsCmd.RunE(GetTransactionsCmd, []string{})

// Assertions
assert.NoError(test_framework, err)
}

func TestGetAllTransactionsPrinterFailure(test_framework *testing.T) {
paginatedTransactions := generators.Generate[paymentsSdk.PaginatedTransactions]()

PreparePaymentsApiMockClient(test_framework).
TransactionsGet(getRequestParams()).
Return(&paginatedTransactions, nil)

expectedErr := ExpectToPrintFailure(test_framework, &paginatedTransactions)

err := GetTransactionsCmd.RunE(GetTransactionsCmd, []string{})

// Assertions
assert.EqualError(test_framework, err, expectedErr.Error())
}
56 changes: 56 additions & 0 deletions src/commands/get/transactions/get_one_transaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package transactions

import (
"testing"

"github.com/phoenixnap/go-sdk-bmc/paymentsapi"
"github.com/stretchr/testify/assert"
"phoenixnap.com/pnapctl/common/ctlerrors"
"phoenixnap.com/pnapctl/common/models/generators"
. "phoenixnap.com/pnapctl/testsupport/mockhelp"
"phoenixnap.com/pnapctl/testsupport/testutil"
)

func TestGetTransactionSuccess(test_framework *testing.T) {
transaction := generators.Generate[paymentsapi.Transaction]()

PreparePaymentsApiMockClient(test_framework).
TransactionGetById(RESOURCEID).
Return(&transaction, nil)

ExpectToPrintSuccess(test_framework, &transaction)

err := GetTransactionsCmd.RunE(GetTransactionsCmd, []string{RESOURCEID})

// Assertions
assert.NoError(test_framework, err)
}

func TestGetTransactionClientFailure(test_framework *testing.T) {
PreparePaymentsApiMockClient(test_framework).
TransactionGetById(RESOURCEID).
Return(nil, testutil.TestError)

err := GetTransactionsCmd.RunE(GetTransactionsCmd, []string{RESOURCEID})

// Expected error
expectedErr := ctlerrors.GenericFailedRequestError(err, ctlerrors.ErrorSendingRequest)

// Assertions
assert.EqualError(test_framework, err, expectedErr.Error())
}

func TestGetTransactionPrinterFailure(test_framework *testing.T) {
transaction := generators.Generate[paymentsapi.Transaction]()

PreparePaymentsApiMockClient(test_framework).
TransactionGetById(RESOURCEID).
Return(&transaction, nil)

expectedErr := ExpectToPrintFailure(test_framework, &transaction)

err := GetTransactionsCmd.RunE(GetTransactionsCmd, []string{RESOURCEID})

// Assertions
assert.EqualError(test_framework, err, expectedErr.Error())
}
81 changes: 81 additions & 0 deletions src/commands/get/transactions/gettransactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package transactions

import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

payments "phoenixnap.com/pnapctl/common/client/payments"
"phoenixnap.com/pnapctl/common/printer"
"phoenixnap.com/pnapctl/common/utils"
"phoenixnap.com/pnapctl/common/utils/cmdname"
)

var Limit int
var Offset int
var SortDirection string
var SortField string
var From string
var To string

func init() {
utils.SetupOutputFlag(GetTransactionsCmd)

GetTransactionsCmd.PersistentFlags().IntVar(&Limit, "limit", 0, "Limit the number of records returned.")
GetTransactionsCmd.PersistentFlags().IntVar(&Offset, "offset", 0, "The number of items to skip in the results.")
GetTransactionsCmd.PersistentFlags().StringVar(&SortDirection, "sortDirection", "", "Ordering of the event's time. Must be 'ASC' or 'DESC'")
GetTransactionsCmd.PersistentFlags().StringVar(&SortField, "sortField", "", "If a sortField is requested, pagination will be done after sorting. Default sorting is by date.")
GetTransactionsCmd.PersistentFlags().StringVar(&From, "from", "", "A 'from' filter. Needs to be in the following format: '2021-04-27T16:24:57.123Z'")
GetTransactionsCmd.PersistentFlags().StringVar(&To, "to", "", "A 'to' filter. Needs to be in the following format: '2021-04-27T16:24:57.123Z'")
}

var GetTransactionsCmd = &cobra.Command{
Use: "transaction [TRANSACTION_ID]",
Short: "Retrieve one or all transactions for your account.",
Aliases: []string{"transactions"},
SilenceUsage: true,
Args: cobra.MaximumNArgs(1),
Long: `Retrieve one or all transactions for your account.
Prints all information about the transactions assigned to your account.
By default, the data is printed in json format.
Table format isn't supported for this command.
To print a specific transaction, a transaction ID needs to be passed as an argument.`,
Example: `
# List all transactions in.
pnapctl get transactions [--limit <LIMIT>] [--offset <OFFSET>] [--sortdirection <SORTDIRECTION>] [--sortfield <SORTFIELD>] [--from <FROM>] [--to <TO>] [--output <OUTPUT_TYPE>]
# List a specific transaction.
pnapctl get transactions <TRANSACTION_ID> [--output <OUTPUT_TYPE>]`,
RunE: func(cmd *cobra.Command, args []string) error {
cmdname.SetCommandName(cmd)
printer.OutputFormat = "json"
if len(args) >= 1 {
return getTransactionById(args[0])
}
return getTransactions()
},
}

func getTransactions() error {
log.Info().Msg("Retrieving list of Transactions...")
results, err := payments.Client.TransactionsGet(Limit, Offset, SortDirection, SortField, From, To)

if err != nil {
return err
} else {
return printer.MainPrinter.PrintOutput(results)
}
}

func getTransactionById(id string) error {
log.Info().Msgf("Retrieving Transaction with ID [%s].", id)
results, err := payments.Client.TransactionGetById(id)

if err != nil {
return err
} else {
return printer.MainPrinter.PrintOutput(results)
}
}
Loading

0 comments on commit b302c2b

Please sign in to comment.