|
| 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 | + 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(true), |
| 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 | +} |
0 commit comments