Skip to content

Commit 306d233

Browse files
allmightyspiffGitHub Enterprise
authored andcommitted
Merge pull request #779 from Ramkishor-Chaladi/newfeature_775
added new feature for vpn disable command to user
2 parents bdf70bd + 7453487 commit 306d233

4 files changed

Lines changed: 135 additions & 0 deletions

File tree

plugin/commands/user/user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
2929
cobraCmd.AddCommand(NewVpnSubnetCommand(sl).Command)
3030
cobraCmd.AddCommand(NewVpnManualCommand(sl).Command)
3131
cobraCmd.AddCommand(NewVpnPasswordCommand(sl).Command)
32+
cobraCmd.AddCommand(NewVpnDisableCommand(sl).Command)
3233
cobraCmd.AddCommand(NewVpnEnableCommand(sl).Command)
3334
cobraCmd.AddCommand(NewApikeyCommand(sl).Command)
3435
return cobraCmd

plugin/commands/user/user_suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var availableCommands = []string{
3535
"vpn-manual",
3636
"vpn-password",
3737
"vpn-subnet",
38+
"vpn-disable",
3839
"vpn-enable",
3940
}
4041

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package user
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/softlayer/softlayer-go/datatypes"
7+
"github.com/softlayer/softlayer-go/sl"
8+
"github.com/spf13/cobra"
9+
10+
slErrors "github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
11+
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
14+
)
15+
16+
type VpnDisableCommand struct {
17+
*metadata.SoftlayerCommand
18+
UserManager managers.UserManager
19+
Command *cobra.Command
20+
}
21+
22+
func NewVpnDisableCommand(sl *metadata.SoftlayerCommand) (cmd *VpnDisableCommand) {
23+
thisCmd := &VpnDisableCommand{
24+
SoftlayerCommand: sl,
25+
UserManager: managers.NewUserManager(sl.Session),
26+
}
27+
28+
cobraCmd := &cobra.Command{
29+
Use: "vpn-disable " + T("USER_ID"),
30+
Short: T("Disable vpn for a user."),
31+
Args: metadata.OneArgs,
32+
RunE: func(cmd *cobra.Command, args []string) error {
33+
return thisCmd.Run(args)
34+
},
35+
}
36+
37+
thisCmd.Command = cobraCmd
38+
return thisCmd
39+
}
40+
41+
func (cmd *VpnDisableCommand) Run(args []string) error {
42+
userID, err := strconv.Atoi(args[0])
43+
if err != nil {
44+
return slErrors.NewInvalidSoftlayerIdInputError("User ID")
45+
}
46+
userTemplate := datatypes.User_Customer{
47+
SslVpnAllowedFlag: sl.Bool(false),
48+
}
49+
success, err := cmd.UserManager.EditUser(userTemplate, userID)
50+
if err != nil {
51+
return slErrors.NewAPIError(T(""), err.Error(), 2)
52+
}
53+
if success {
54+
cmd.UI.Ok()
55+
}
56+
return nil
57+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package user_test
2+
3+
import (
4+
"errors"
5+
6+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
7+
. "github.com/onsi/ginkgo"
8+
. "github.com/onsi/gomega"
9+
"github.com/softlayer/softlayer-go/datatypes"
10+
"github.com/softlayer/softlayer-go/session"
11+
"github.com/softlayer/softlayer-go/sl"
12+
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/user"
14+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
15+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
16+
)
17+
18+
var _ = Describe("user vpn-enable", func() {
19+
var (
20+
fakeUI *terminal.FakeUI
21+
cliCommand *user.VpnDisableCommand
22+
fakeSession *session.Session
23+
slCommand *metadata.SoftlayerCommand
24+
fakeUserManager *testhelpers.FakeUserManager
25+
)
26+
BeforeEach(func() {
27+
fakeUI = terminal.NewFakeUI()
28+
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
29+
fakeUserManager = new(testhelpers.FakeUserManager)
30+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
31+
cliCommand = user.NewVpnDisableCommand(slCommand)
32+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
33+
cliCommand.UserManager = fakeUserManager
34+
35+
testUser = datatypes.User_Customer{
36+
SslVpnAllowedFlag: sl.Bool(false),
37+
}
38+
})
39+
40+
Describe("user vpn-enable", func() {
41+
42+
Context("Return error", func() {
43+
It("Set command without Argument", func() {
44+
err := testhelpers.RunCobraCommand(cliCommand.Command)
45+
Expect(err).To(HaveOccurred())
46+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: This command requires one argument."))
47+
})
48+
49+
It("Set command with an invalid user Id", func() {
50+
err := testhelpers.RunCobraCommand(cliCommand.Command, "abcde")
51+
Expect(err).To(HaveOccurred())
52+
Expect(err.Error()).To(ContainSubstring("Invalid input for 'User ID'. It must be a positive integer."))
53+
})
54+
})
55+
56+
Context("Account cancel-item, softlayer errors", func() {
57+
It("Set command with unknow item ID", func() {
58+
fakeUserManager.EditUserReturns(true, errors.New("SoftLayer_Exception_ObjectNotFound: Unable to find object with id of '123'. (HTTP 404)"))
59+
err := testhelpers.RunCobraCommand(cliCommand.Command, "123")
60+
Expect(err).To(HaveOccurred())
61+
Expect(err.Error()).To(ContainSubstring("SoftLayer_Exception_ObjectNotFound: Unable to find object with id of '123'. (HTTP 404)"))
62+
})
63+
})
64+
65+
Context("Return no error", func() {
66+
BeforeEach(func() {
67+
fakeUserManager.EditUserReturns(true, nil)
68+
})
69+
It("enable", func() {
70+
err := testhelpers.RunCobraCommand(cliCommand.Command, "111111")
71+
Expect(err).NotTo(HaveOccurred())
72+
Expect(fakeUI.Outputs()).To(ContainSubstring("OK"))
73+
})
74+
})
75+
})
76+
})

0 commit comments

Comments
 (0)