-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from phoenixnap/feature/add-paymentsapi-v2
Feature/add paymentsapi v2
- Loading branch information
Showing
17 changed files
with
426 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/commands/get/transactions/get_all_transactions_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.