Skip to content

Commit 252e6b9

Browse files
Ramkishor ChaladiRamkishor Chaladi
authored andcommitted
added new feature vpn enable command to user
1 parent f2953c7 commit 252e6b9

4 files changed

Lines changed: 158 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(NewVpnEnableCommand(sl).Command)
3233
cobraCmd.AddCommand(NewApikeyCommand(sl).Command)
3334
return cobraCmd
3435
}

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-enable",
3839
}
3940

4041
// This test suite exists to make sure commands don't get accidently removed from the SetupCobraCommands

plugin/commands/user/vpn_enable.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 VpnEnableCommand struct {
17+
*metadata.SoftlayerCommand
18+
UserManager managers.UserManager
19+
Command *cobra.Command
20+
}
21+
22+
func NewVpnEnableCommand(sl *metadata.SoftlayerCommand) (cmd *VpnEnableCommand) {
23+
thisCmd := &VpnEnableCommand{
24+
SoftlayerCommand: sl,
25+
UserManager: managers.NewUserManager(sl.Session),
26+
}
27+
28+
cobraCmd := &cobra.Command{
29+
Use: "vpn-enable " + T("USER_ID"),
30+
Short: T("Enable 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 *VpnEnableCommand) Run(args []string) error {
42+
43+
userID, err := strconv.Atoi(args[0])
44+
if err != nil {
45+
return slErrors.NewInvalidSoftlayerIdInputError("User ID")
46+
}
47+
48+
userTemplate := datatypes.User_Customer{
49+
SslVpnAllowedFlag: sl.Bool(true),
50+
}
51+
52+
subs := map[string]interface{}{"UserId": userID}
53+
confirm, err := cmd.UI.Confirm(T("This will Enable vpn for the user id: {{.UserId}}. Continue?", subs))
54+
if err != nil {
55+
return err
56+
}
57+
if !confirm {
58+
cmd.UI.Print(T("Aborted."))
59+
return nil
60+
}
61+
62+
success, err := cmd.UserManager.EditUser(userTemplate, userID)
63+
if err != nil {
64+
return slErrors.NewAPIError(T("Failed to enable vpn for the 'User ID' \n", subs), err.Error(), 2)
65+
}
66+
67+
if success {
68+
cmd.UI.Ok()
69+
cmd.UI.Print(T("{{.UserId}} is successfully enabled.", subs))
70+
}
71+
return nil
72+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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/session"
10+
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/user"
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
14+
)
15+
16+
var _ = Describe("user vpn-enable", func() {
17+
var (
18+
fakeUI *terminal.FakeUI
19+
cliCommand *user.VpnEnableCommand
20+
fakeSession *session.Session
21+
slCommand *metadata.SoftlayerCommand
22+
fakeUserManager *testhelpers.FakeUserManager
23+
)
24+
BeforeEach(func() {
25+
fakeUI = terminal.NewFakeUI()
26+
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
27+
fakeUserManager = new(testhelpers.FakeUserManager)
28+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
29+
cliCommand = user.NewVpnEnableCommand(slCommand)
30+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
31+
cliCommand.UserManager = fakeUserManager
32+
})
33+
34+
Describe("user vpn-enable", func() {
35+
36+
Context("Return error", func() {
37+
It("Set command without Argument", func() {
38+
err := testhelpers.RunCobraCommand(cliCommand.Command)
39+
Expect(err).To(HaveOccurred())
40+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: This command requires one argument."))
41+
})
42+
43+
It("Set command with an invalid user Id", func() {
44+
err := testhelpers.RunCobraCommand(cliCommand.Command, "abcde")
45+
Expect(err).To(HaveOccurred())
46+
Expect(err.Error()).To(ContainSubstring("Invalid input for 'User ID'. It must be a positive integer."))
47+
})
48+
})
49+
50+
Context("Vpn enable with correct vpn ID but continue", func() {
51+
It("return no error", func() {
52+
fakeUI.Inputs("No")
53+
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234")
54+
Expect(err).NotTo(HaveOccurred())
55+
Expect(fakeUI.Outputs()).To(ContainSubstring("This will Enable vpn for the user id: 1234. Continue?"))
56+
Expect(fakeUI.Outputs()).To(ContainSubstring("Aborted."))
57+
})
58+
})
59+
60+
Context("Return error", func() {
61+
BeforeEach(func() {
62+
fakeUserManager.EditUserReturns(false, errors.New("Failed"))
63+
})
64+
It("Failed edit user", func() {
65+
err := testhelpers.RunCobraCommand(cliCommand.Command, "111111")
66+
Expect(err).To(HaveOccurred())
67+
Expect(err.Error()).To(ContainSubstring("Failed to enable vpn for the 'User ID' "))
68+
Expect(err.Error()).To(ContainSubstring("SoftLayer_Exception_ObjectNotFound: Unable to find object with id of '123'. (HTTP 404)"))
69+
})
70+
})
71+
72+
Context("Return no error", func() {
73+
BeforeEach(func() {
74+
fakeUserManager.EditUserReturns(true, nil)
75+
})
76+
It("vpn enable", func() {
77+
err := testhelpers.RunCobraCommand(cliCommand.Command, "111111")
78+
Expect(err).NotTo(HaveOccurred())
79+
Expect(fakeUI.Outputs()).To(ContainSubstring("OK"))
80+
Expect(fakeUI.Outputs()).To(ContainSubstring("1234 is successfully enabled."))
81+
})
82+
})
83+
})
84+
})

0 commit comments

Comments
 (0)