Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ generate: deps
mockgen --destination ./internal/mocks/load_balancers_service.go --package=mocks --source ./vendor/github.com/serverscom/serverscom-go-client/pkg/load_balancers.go
mockgen --destination ./internal/mocks/racks_service.go --package=mocks --source ./vendor/github.com/serverscom/serverscom-go-client/pkg/racks.go
mockgen --destination ./internal/mocks/invoices_service.go --package=mocks --source ./vendor/github.com/serverscom/serverscom-go-client/pkg/invoices.go
mockgen --destination ./internal/mocks/account_service.go --package=mocks --source ./vendor/github.com/serverscom/serverscom-go-client/pkg/accounts.go
sed -i '' 's|github.com/serverscom/srvctl/vendor/github.com/serverscom/serverscom-go-client/pkg|github.com/serverscom/serverscom-go-client/pkg|g' \
./internal/mocks/ssh_service.go \
./internal/mocks/hosts_service.go \
./internal/mocks/ssl_service.go \
./internal/mocks/load_balancers_service.go \
./internal/mocks/racks_service.go \
./internal/mocks/invoices_service.go \
./internal/mocks/account_service.go \
./internal/mocks/collection.go

37 changes: 37 additions & 0 deletions cmd/entities/account/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package account

import (
"log"

serverscom "github.com/serverscom/serverscom-go-client/pkg"
"github.com/serverscom/srvctl/cmd/base"
"github.com/serverscom/srvctl/internal/output/entities"
"github.com/spf13/cobra"
)

func NewCmd(cmdContext *base.CmdContext) *cobra.Command {
accountBalanceEntity, err := entities.Registry.GetEntityFromValue(serverscom.AccountBalance{})
if err != nil {
log.Fatal(err)
}
entitiesMap := make(map[string]entities.EntityInterface)
entitiesMap["account"] = accountBalanceEntity
cmd := &cobra.Command{
Use: "account",
Short: "Manage account operations",
PersistentPreRunE: base.CombinePreRunE(
base.CheckFormatterFlags(cmdContext, entitiesMap),
base.CheckEmptyContexts(cmdContext),
),
Args: base.NoArgs,
Run: base.UsageRun,
}

cmd.AddCommand(
newGetBalanceCmd(cmdContext),
)

base.AddFormatFlags(cmd)

return cmd
}
96 changes: 96 additions & 0 deletions cmd/entities/account/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package account

import (
"errors"
"path/filepath"
"testing"

. "github.com/onsi/gomega"
serverscom "github.com/serverscom/serverscom-go-client/pkg"
"github.com/serverscom/srvctl/cmd/testutils"
"github.com/serverscom/srvctl/internal/mocks"
"go.uber.org/mock/gomock"
)

var (
fixtureBasePath = filepath.Join("..", "..", "..", "testdata", "entities", "account")
testAccountBalance = serverscom.AccountBalance{
CurrentBalance: 100.0,
NextInvoiceTotalDue: 0.0,
Currency: "EUR",
}
)

func TestGetAccountBalanceCmd(t *testing.T) {
testCases := []struct {
name string
output string
expectedOutput []byte
expectError bool
}{
{
name: "get account balance in default format",
output: "",
expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get_balance.txt")),
},
{
name: "get account balance in JSON format",
output: "json",
expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get_balance.json")),
},
{
name: "get account balance in YAML format",
output: "yaml",
expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get_balance.yaml")),
},
{
name: "get account balance with error",
expectError: true,
},
}

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

accountServiceHandler := mocks.NewMockAccountService(mockCtrl)

scClient := serverscom.NewClientWithEndpoint("", "")
scClient.Account = accountServiceHandler

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

var err error
if tc.expectError {
err = errors.New("some error")
}
accountServiceHandler.EXPECT().
GetBalance(gomock.Any()).
Return(&testAccountBalance, err)

testCmdContext := testutils.NewTestCmdContext(scClient)
accountCmd := NewCmd(testCmdContext)

args := []string{"account", "balance"}
if tc.output != "" {
args = append(args, "--output", tc.output)
}

builder := testutils.NewTestCommandBuilder().
WithCommand(accountCmd).
WithArgs(args)

cmd := builder.Build()

err = cmd.Execute()

if tc.expectError {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).To(BeNil())
g.Expect(builder.GetOutput()).To(BeEquivalentTo(string(tc.expectedOutput)))
}
})
}
}
37 changes: 37 additions & 0 deletions cmd/entities/account/get_balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package account

import (
"github.com/serverscom/srvctl/cmd/base"
"github.com/spf13/cobra"
)

func newGetBalanceCmd(cmdContext *base.CmdContext) *cobra.Command {
cmd := &cobra.Command{
Use: "balance",
Short: "Get an account balance info",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
manager := cmdContext.GetManager()

ctx, cancel := base.SetupContext(cmd, manager)
defer cancel()

base.SetupProxy(cmd, manager)

scClient := cmdContext.GetClient().SetVerbose(manager.GetVerbose(cmd)).GetScClient()

balance, err := scClient.Account.GetBalance(ctx)
if err != nil {
return err
}

if balance != nil {
formatter := cmdContext.GetOrCreateFormatter(cmd)
return formatter.Format(balance)
}
return nil
},
}

return cmd
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/serverscom/srvctl/cmd/base"
"github.com/serverscom/srvctl/cmd/config"
"github.com/serverscom/srvctl/cmd/context"
"github.com/serverscom/srvctl/cmd/entities/account"
"github.com/serverscom/srvctl/cmd/entities/hosts"
"github.com/serverscom/srvctl/cmd/entities/invoices"
loadbalancers "github.com/serverscom/srvctl/cmd/entities/load_balancers"
Expand Down Expand Up @@ -46,6 +47,7 @@ func NewRootCmd(version string) *cobra.Command {
cmd.AddCommand(loadbalancers.NewCmd(cmdContext))
cmd.AddCommand(racks.NewCmd(cmdContext))
cmd.AddCommand(invoices.NewCmd(cmdContext))
cmd.AddCommand(account.NewCmd(cmdContext))

return cmd
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/creack/pty v1.1.24
github.com/jmespath/go-jmespath v0.4.0
github.com/onsi/gomega v1.36.2
github.com/serverscom/serverscom-go-client v1.0.13
github.com/serverscom/serverscom-go-client v1.0.14
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ 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/serverscom/serverscom-go-client v1.0.13 h1:HjcODZl8M8SEwhbeBGX2D/M3ZZM7I0JGQq717rW792Y=
github.com/serverscom/serverscom-go-client v1.0.13/go.mod h1:o4lNYX+shv5TZ6miuGAaMDJP8y7Z7TdPEhMsCcL9PrU=
github.com/serverscom/serverscom-go-client v1.0.14 h1:/SR4moqSL6MqW+gt6wtF9Wl5KfckP4RcqeS0AECwwAs=
github.com/serverscom/serverscom-go-client v1.0.14/go.mod h1:o4lNYX+shv5TZ6miuGAaMDJP8y7Z7TdPEhMsCcL9PrU=
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 Down
57 changes: 57 additions & 0 deletions internal/mocks/account_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions internal/output/entities/accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package entities

import (
"log"
"reflect"

serverscom "github.com/serverscom/serverscom-go-client/pkg"
)

var (
AccountBalanceType = reflect.TypeOf(serverscom.AccountBalance{})
)

func RegisterAccountDefinition() {
balanceEntity := &Entity{
fields: []Field{
{ID: "CurrentBalance", Name: "CurrentBalance", Path: "CurrentBalance", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true},
{ID: "NextInvoiceTotalDue", Name: "NextInvoiceTotalDue", Path: "NextInvoiceTotalDue", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true},
{ID: "Currency", Name: "Currency", Path: "Currency", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true},
},
eType: AccountBalanceType,
}
if err := Registry.Register(balanceEntity); err != nil {
log.Fatal(err)
}
}
1 change: 1 addition & 0 deletions internal/output/entities/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ func init() {
RegisterLoadBalancerDefinitions()
RegisterRackDefinition()
RegisterInvoiceDefinition()
RegisterAccountDefinition()
}
5 changes: 5 additions & 0 deletions testdata/entities/account/get_balance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"current_balance": 100,
"next_invoice_total_due": 0,
"currency": "EUR"
}
2 changes: 2 additions & 0 deletions testdata/entities/account/get_balance.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CurrentBalance NextInvoiceTotalDue Currency
100 0 EUR
3 changes: 3 additions & 0 deletions testdata/entities/account/get_balance.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
currentbalance: 100
nextinvoicetotaldue: 0
currency: EUR
Loading