Skip to content

Commit

Permalink
fix(ipaddress): show full API response in marshaled outputs (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
kangasta authored Oct 18, 2023
1 parent 4c2024c commit 0b98d35
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 130 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- In JSON and YAML output of `kubernetes list`: display full API response.
- **Breaking**: In JSON and YAML output of `database list`: display the full API response. Value of `title` is not replaced with value from `name`, if `title` is empty.
- **Breaking**: In JSON and YAML output of `database types`: display the full API response. This changes the top level datatype from list to object, where keys are the available database type, e.g., `pg` and `mysql`.
- **Breaking**: In JSON and YAML output of `ip-address list`: display full API response. This changes `partofplan` key to `part_of_plan` and `ptrrecord` key to `ptr_record`.
- In human readable output of `kubernetes show` command, show node-groups as table. Node-group details are available with `kubernetes nodegroup show` command.

## Fixed
- **Breaking**: In JSON and YAML output of `ip-address show`: use same JSON keys as in API documentation. This removes `credits` key that was used in place of `floating`.

## Removed
- **Breaking**: Remove `database connection list` and `database connection cancel` commands in favor of `database session` counterparts
- **Breaking**: In JSON and YAML output of `database properties * show`: pass-through the API response. This removes `key` field from the output.
Expand Down
6 changes: 3 additions & 3 deletions internal/commands/account/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (s *showCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Ou
return nil, err
}

details, err := output.Details{
details := output.Details{
Sections: []output.DetailSection{
{
Rows: []output.DetailRow{
Expand Down Expand Up @@ -80,12 +80,12 @@ func (s *showCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Ou
},
},
},
}, nil
}

return output.MarshaledWithHumanOutput{
Value: account,
Output: details,
}, err
}, nil
}

func formatCredits(credits float64) string {
Expand Down
80 changes: 42 additions & 38 deletions internal/commands/ipaddress/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,49 @@ func (s *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Ou
for i, ipAddress := range ips.IPAddresses {
rows[i] = output.TableRow{ipAddress.Address, ipAddress.Access, ipAddress.Family, ipAddress.PartOfPlan, ipAddress.PTRRecord, ipAddress.ServerUUID, ipAddress.Floating, ipAddress.Zone}
}
return output.Table{
Columns: []output.TableColumn{
{
Header: "Address",
Key: "address",
Colour: ui.DefaultAddressColours,
},
{
Header: "Access",
Key: "access",
},
{
Header: "Family",
Key: "family",
},
{
Header: "Part of Plan",
Key: "partofplan",
Format: format.Boolean,
},
{
Header: "PTR Record",
Key: "ptrrecord",
},
{
Header: "Server",
Key: "server",
Colour: ui.DefaultUUUIDColours,
},
{
Header: "Floating",
Key: "floating",
Format: format.Boolean,
},
{
Header: "Zone",
Key: "zone",

return output.MarshaledWithHumanOutput{
Value: ips,
Output: output.Table{
Columns: []output.TableColumn{
{
Header: "Address",
Key: "address",
Colour: ui.DefaultAddressColours,
},
{
Header: "Access",
Key: "access",
},
{
Header: "Family",
Key: "family",
},
{
Header: "Part of Plan",
Key: "part_of_plan",
Format: format.Boolean,
},
{
Header: "PTR Record",
Key: "ptr_record",
},
{
Header: "Server",
Key: "server",
Colour: ui.DefaultUUUIDColours,
},
{
Header: "Floating",
Key: "floating",
Format: format.Boolean,
},
{
Header: "Zone",
Key: "zone",
},
},
Rows: rows,
},
Rows: rows,
}, nil
}
46 changes: 46 additions & 0 deletions internal/commands/ipaddress/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ipaddress

import (
"testing"

"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/config"
smock "github.com/UpCloudLtd/upcloud-cli/v2/internal/mock"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/mockexecute"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/testutils"
"github.com/stretchr/testify/assert"

"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
)

func TestList_MarshaledOutput(t *testing.T) {
ipAddress := upcloud.IPAddress{
Address: "94.237.117.150",
Access: "public",
Family: "IPv4",
PartOfPlan: upcloud.FromBool(true),
PTRRecord: "94-237-117-150.fi-hel1.upcloud.host",
ServerUUID: "005ab220-7ff6-42c9-8615-e4c02eb4104e",
MAC: "ee:1b:db:ca:6b:80",
Floating: upcloud.FromBool(false),
Zone: "fi-hel1",
}
ipAddresses := upcloud.IPAddresses{
IPAddresses: []upcloud.IPAddress{
ipAddress,
},
}

mService := smock.Service{}
mService.On("GetIPAddresses").Return(&ipAddresses, nil)

conf := config.New()
conf.Viper().Set(config.KeyOutput, config.ValueOutputJSON)

command := commands.BuildCommand(ListCommand(), nil, conf)

output, err := mockexecute.MockExecute(command, &mService, conf)

assert.Nil(t, err)
testutils.AssertOutputHasType(t, output, &upcloud.IPAddressSlice{})
}
20 changes: 13 additions & 7 deletions internal/commands/ipaddress/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,31 @@ func (s *showCommand) Execute(exec commands.Executor, arg string) (output.Output
if err != nil {
return nil, err
}
return output.Details{

details := output.Details{
Sections: []output.DetailSection{
{
Rows: []output.DetailRow{
{Title: "Address:", Key: "address", Value: ipAddress.Address, Colour: ui.DefaultAddressColours},
{Title: "Access:", Key: "access", Value: ipAddress.Access},
{Title: "Family:", Key: "access", Value: ipAddress.Family},
{Title: "Part of Plan:", Key: "access", Value: ipAddress.PartOfPlan, Format: format.Boolean},
{Title: "PTR Record:", Key: "access", Value: ipAddress.PTRRecord},
{Title: "Family:", Key: "family", Value: ipAddress.Family},
{Title: "Part of Plan:", Key: "part_of_plan", Value: ipAddress.PartOfPlan, Format: format.Boolean},
{Title: "PTR Record:", Key: "ptr_record", Value: ipAddress.PTRRecord},
{
Title: "Server UUID:",
Key: "access", Value: ipAddress.ServerUUID,
Key: "server", Value: ipAddress.ServerUUID,
Colour: ui.DefaultUUUIDColours,
},
{Title: "MAC:", Key: "credits", Value: ipAddress.MAC},
{Title: "Floating:", Key: "credits", Value: ipAddress.Floating, Format: format.Boolean},
{Title: "MAC:", Key: "mac", Value: ipAddress.MAC},
{Title: "Floating:", Key: "floating", Value: ipAddress.Floating, Format: format.Boolean},
{Title: "Zone:", Key: "zone", Value: ipAddress.Zone},
},
},
},
}

return output.MarshaledWithHumanOutput{
Value: ipAddress,
Output: details,
}, nil
}
30 changes: 30 additions & 0 deletions internal/commands/kubernetes/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package kubernetes

import (
"testing"

"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/config"
smock "github.com/UpCloudLtd/upcloud-cli/v2/internal/mock"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/mockexecute"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/testutils"

"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestList_MarshaledOutput(t *testing.T) {
mService := smock.Service{}
mService.On("GetKubernetesClusters", mock.Anything).Return([]upcloud.KubernetesCluster{testCluster}, nil)

conf := config.New()
conf.Viper().Set(config.KeyOutput, config.ValueOutputJSON)

command := commands.BuildCommand(ListCommand(), nil, conf)

output, err := mockexecute.MockExecute(command, &mService, conf)

assert.Nil(t, err)
testutils.AssertOutputIsList(t, output)
}
Loading

0 comments on commit 0b98d35

Please sign in to comment.