Skip to content

Commit c21638e

Browse files
allmightyspiffGitHub Enterprise
authored andcommitted
Merge pull request #860 from SoftLayer/issues851
Fixed user detail not showing JSON output
2 parents d68fb6b + b0466f0 commit c21638e

6 files changed

Lines changed: 355 additions & 453 deletions

File tree

README.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ Check testhelpers/fake_softlayer_session.go for all the fields that get recorded
157157

158158

159159

160-
### Fake Managers
160+
### Test Fakes
161+
161162

162163
CLI calls to manager functions need an entry in `plugin\testhelpers\fake_manager.go`
163164
Managers have a fake/test interface that is autogenerate with a program called [couterfieter](https://github.com/maxbrunsfeld/counterfeiter)
@@ -177,24 +178,37 @@ each manager and defined interface should have this line in it to be automatical
177178

178179
If you want to use the real manager but fixture API data, just initialize the manager like this in the CLI test
179180

180-
(filenames here is optional of course)
181+
This example is from `plugin\commands\account\invoice-detail_test.go`
181182
```go
182-
BeforeEach(func() {
183-
184-
filenames := []string{"getDatacenters_1",}
185-
fakeSLSession = testhelpers.NewFakeSoftlayerSession(filenames)
186-
OrderManager = managers.NewOrderManager(fakeSLSession)
187-
fakeUI = terminal.NewFakeUI()
188-
cmd = order.NewPlaceCommand(fakeUI, OrderManager, nil)
189-
cliCommand = cli.Command{
190-
Name: metadata.OrderPlaceMetaData().Name,
191-
Description: metadata.OrderPlaceMetaData().Description,
192-
Usage: metadata.OrderPlaceMetaData().Usage,
193-
Flags: metadata.OrderPlaceMetaData().Flags,
194-
Action: cmd.Run,
195-
}
196-
})
183+
var _ = Describe("<COMMAND> Tests", func() {
184+
var (
185+
fakeUI *terminal.FakeUI
186+
cliCommand *account.InvoiceDetailCommand
187+
fakeSession *session.Session
188+
slCommand *metadata.SoftlayerCommand
189+
fakeHandler *testhelpers.FakeTransportHandler
190+
)
191+
BeforeEach(func() {
192+
// Fake UI to capture output of comamnds
193+
fakeUI = terminal.NewFakeUI()
194+
// Fake session to handle loading data from testfixtures
195+
fakeSession = testhelpers.NewFakeSoftlayerSession(nil)
196+
// Fake handler to control error generation
197+
fakeHandler = testhelpers.GetSessionHandler(fakeSession)
198+
// Real parent command, with fake UI and Fake Session being passed in
199+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
200+
// Real actual command
201+
cliCommand = account.NewInvoiceDetailCommand(slCommand)
202+
// Need to set output flag since its set manually in the parent command normally.
203+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
204+
})
205+
AfterEach(func() {
206+
// Clear API call logs and any errors that might have been set after every test
207+
fakeHandler.ClearApiCallLogs()
208+
fakeHandler.ClearErrors()
209+
})
197210
```
211+
`plugin\commands\user\details_test.go` is also a good example test file for CLI commands.
198212
199213
### `[no tests to run]`
200214
New commands needs a `command_test.go` file in the CLI directory.
@@ -224,8 +238,7 @@ fakeHandler = testhelpers.GetSessionHandler(fakeSession)
224238

225239
// Then in a BeforeEach for the specific test...
226240
BeforeEach(func() {
227-
fakeHandler.AddApiError("SoftLayer_User_Customer", "getObject",
228-
500, "Internal Server Error")
241+
fakeHandler.AddApiError("SoftLayer_User_Customer", "getObject", 500, "Internal Server Error")
229242
})
230243
```
231244

plugin/commands/user/details.go

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,25 @@ func NewDetailsCommand(sl *metadata.SoftlayerCommand) (cmd *DetailsCommand) {
5252
return thisCmd
5353
}
5454

55+
type UserInformation struct {
56+
User datatypes.User_Customer
57+
Hardware []datatypes.Hardware
58+
Virtual []datatypes.Virtual_Guest
59+
Events []datatypes.Event_Log
60+
Permissions []datatypes.User_Customer_CustomerPermission_Permission
61+
Logins []datatypes.User_Customer_Access_Authentication
62+
DedicatedHosts []datatypes.Virtual_DedicatedHost
63+
}
64+
5565
func (cmd *DetailsCommand) Run(args []string) error {
5666
userId := args[0]
5767
id, err := strconv.Atoi(userId)
5868
if err != nil {
5969
return errors.NewInvalidUsageError(T("User ID should be a number."))
6070
}
6171

72+
outputFormat := cmd.GetOutputFlag()
73+
userInfo := UserInformation{}
6274
keys := cmd.Keys
6375
permissions := cmd.Permissions
6476
hardware := cmd.Hardware
@@ -68,11 +80,14 @@ func (cmd *DetailsCommand) Run(args []string) error {
6880

6981
object_mask := "userStatus[name],parent[id,username],apiAuthenticationKeys[authenticationKey]"
7082
user, err := cmd.UserManager.GetUser(id, object_mask)
83+
userInfo.User = user
7184
if err != nil {
7285
return errors.NewAPIError(T("Failed to show user detail.\n"), err.Error(), 2)
7386
}
7487

75-
baseUserPrint(user, keys, cmd.UI)
88+
if outputFormat != "JSON" {
89+
baseUserPrint(user, keys, cmd.UI)
90+
}
7691

7792
if permissions {
7893
perms, err := cmd.UserManager.GetUserPermissions(id)
@@ -84,7 +99,10 @@ func (cmd *DetailsCommand) Run(args []string) error {
8499
table.Add(utils.FormatStringPointer(perm.KeyName), utils.FormatStringPointer(perm.Name))
85100
}
86101
table.Add("", "")
87-
table.Print()
102+
if outputFormat != "JSON" {
103+
table.Print()
104+
}
105+
userInfo.Permissions = perms
88106
}
89107

90108
if hardware {
@@ -105,7 +123,6 @@ func (cmd *DetailsCommand) Run(args []string) error {
105123
table.Add(hostId, hostFqdn, hostCpu, hostMem, hostDisk, hostCreated)
106124
}
107125
table.Add("", "")
108-
table.Print()
109126

110127
tableAccess := cmd.UI.Table([]string{T("ID"), T("Hostname"), T("Primary Public IP"), T("Primary Private IP"), T("Created")})
111128
for _, host := range access.Hardware {
@@ -117,7 +134,12 @@ func (cmd *DetailsCommand) Run(args []string) error {
117134
tableAccess.Add(hostId, hostFqdn, hostPrimary, hostPrivate, hostCreated)
118135
}
119136
tableAccess.Add("", "")
120-
tableAccess.Print()
137+
if outputFormat != "JSON" {
138+
table.Print()
139+
tableAccess.Print()
140+
}
141+
userInfo.Hardware = access.Hardware
142+
userInfo.DedicatedHosts = access.DedicatedHosts
121143
}
122144

123145
if virtual {
@@ -137,7 +159,10 @@ func (cmd *DetailsCommand) Run(args []string) error {
137159
tableAccess.Add(hostId, hostFqdn, hostPrimary, hostPrivate, hostCreated)
138160
}
139161
tableAccess.Add("", "")
140-
tableAccess.Print()
162+
if outputFormat != "JSON" {
163+
tableAccess.Print()
164+
}
165+
userInfo.Virtual = access.VirtualGuests
141166
}
142167

143168
if logins {
@@ -156,7 +181,11 @@ func (cmd *DetailsCommand) Run(args []string) error {
156181
table.Add(loginData, loginIp, loginSucc)
157182
}
158183
table.Add("", "")
159-
table.Print()
184+
if outputFormat != "JSON" {
185+
table.Print()
186+
}
187+
userInfo.Logins = loginLog
188+
160189
}
161190

162191
if events {
@@ -176,9 +205,16 @@ func (cmd *DetailsCommand) Run(args []string) error {
176205
table.Add(eventData, eventName, eventIp, eventLabel, eventUsername)
177206
}
178207
table.Add("", "")
179-
table.Print()
208+
if outputFormat != "JSON" {
209+
table.Print()
210+
}
211+
userInfo.Events = events
180212
}
181213

214+
if outputFormat == "JSON" {
215+
err := utils.PrintPrettyJSON(cmd.UI, userInfo)
216+
return err
217+
}
182218
return nil
183219

184220
}

0 commit comments

Comments
 (0)