|
| 1 | +package ecloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/ans-group/cli/internal/pkg/factory" |
| 8 | + "github.com/ans-group/cli/internal/pkg/helper" |
| 9 | + "github.com/ans-group/cli/internal/pkg/output" |
| 10 | + "github.com/ans-group/sdk-go/pkg/service/ecloud" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +func ecloudVPNGatewayRootCmd(f factory.ClientFactory) *cobra.Command { |
| 15 | + cmd := &cobra.Command{ |
| 16 | + Use: "vpngateway", |
| 17 | + Short: "sub-commands relating to VPN gateways", |
| 18 | + } |
| 19 | + |
| 20 | + // Child commands |
| 21 | + cmd.AddCommand(ecloudVPNGatewayUserRootCmd(f)) |
| 22 | + cmd.AddCommand(ecloudVPNGatewaySpecificationRootCmd(f)) |
| 23 | + cmd.AddCommand(ecloudVPNGatewayListCmd(f)) |
| 24 | + cmd.AddCommand(ecloudVPNGatewayShowCmd(f)) |
| 25 | + cmd.AddCommand(ecloudVPNGatewayCreateCmd(f)) |
| 26 | + cmd.AddCommand(ecloudVPNGatewayUpdateCmd(f)) |
| 27 | + cmd.AddCommand(ecloudVPNGatewayDeleteCmd(f)) |
| 28 | + |
| 29 | + return cmd |
| 30 | +} |
| 31 | + |
| 32 | +func ecloudVPNGatewayListCmd(f factory.ClientFactory) *cobra.Command { |
| 33 | + cmd := &cobra.Command{ |
| 34 | + Use: "list", |
| 35 | + Short: "Lists VPN gateways", |
| 36 | + Example: "ans ecloud vpngateway list", |
| 37 | + RunE: ecloudCobraRunEFunc(f, ecloudVPNGatewayList), |
| 38 | + } |
| 39 | + |
| 40 | + cmd.Flags().String("name", "", "VPN gateway name for filtering") |
| 41 | + |
| 42 | + return cmd |
| 43 | +} |
| 44 | + |
| 45 | +func ecloudVPNGatewayList(service ecloud.ECloudService, cmd *cobra.Command, args []string) error { |
| 46 | + params, err := helper.GetAPIRequestParametersFromFlags(cmd, |
| 47 | + helper.NewStringFilterFlagOption("name", "name"), |
| 48 | + ) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + gateways, err := service.GetVPNGateways(params) |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("Error retrieving VPN gateways: %s", err) |
| 56 | + } |
| 57 | + |
| 58 | + return output.CommandOutput(cmd, OutputECloudVPNGatewaysProvider(gateways)) |
| 59 | +} |
| 60 | + |
| 61 | +func ecloudVPNGatewayShowCmd(f factory.ClientFactory) *cobra.Command { |
| 62 | + return &cobra.Command{ |
| 63 | + Use: "show <gateway: id>...", |
| 64 | + Short: "Show details of a VPN gateway", |
| 65 | + Example: "ans ecloud vpngateway show vpng-abcdef12", |
| 66 | + Args: func(cmd *cobra.Command, args []string) error { |
| 67 | + if len(args) < 1 { |
| 68 | + return errors.New("Missing VPN gateway") |
| 69 | + } |
| 70 | + |
| 71 | + return nil |
| 72 | + }, |
| 73 | + RunE: ecloudCobraRunEFunc(f, ecloudVPNGatewayShow), |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func ecloudVPNGatewayShow(service ecloud.ECloudService, cmd *cobra.Command, args []string) error { |
| 78 | + var vpnGateways []ecloud.VPNGateway |
| 79 | + for _, arg := range args { |
| 80 | + vpnGateway, err := service.GetVPNGateway(arg) |
| 81 | + if err != nil { |
| 82 | + output.OutputWithErrorLevelf("Error retrieving VPN gateway [%s]: %s", arg, err) |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + vpnGateways = append(vpnGateways, vpnGateway) |
| 87 | + } |
| 88 | + |
| 89 | + return output.CommandOutput(cmd, OutputECloudVPNGatewaysProvider(vpnGateways)) |
| 90 | +} |
| 91 | + |
| 92 | +func ecloudVPNGatewayCreateCmd(f factory.ClientFactory) *cobra.Command { |
| 93 | + cmd := &cobra.Command{ |
| 94 | + Use: "create", |
| 95 | + Short: "Creates a VPN gateway", |
| 96 | + Example: "ans ecloud vpngateway create --router rtr-abcdef12 --specification vpngs-abcdef12", |
| 97 | + RunE: ecloudCobraRunEFunc(f, ecloudVPNGatewayCreate), |
| 98 | + } |
| 99 | + |
| 100 | + // Setup flags |
| 101 | + cmd.Flags().String("name", "", "Name of gateway") |
| 102 | + cmd.Flags().String("router", "", "ID of router") |
| 103 | + cmd.MarkFlagRequired("router") |
| 104 | + cmd.Flags().String("specification", "", "ID of VPN gateway specification") |
| 105 | + cmd.MarkFlagRequired("specification") |
| 106 | + cmd.Flags().Bool("wait", false, "Specifies that the command should wait until the VPN gateway has been completely created") |
| 107 | + |
| 108 | + return cmd |
| 109 | +} |
| 110 | + |
| 111 | +func ecloudVPNGatewayCreate(service ecloud.ECloudService, cmd *cobra.Command, args []string) error { |
| 112 | + createRequest := ecloud.CreateVPNGatewayRequest{} |
| 113 | + createRequest.Name, _ = cmd.Flags().GetString("name") |
| 114 | + createRequest.RouterID, _ = cmd.Flags().GetString("router") |
| 115 | + createRequest.SpecificationID, _ = cmd.Flags().GetString("specification") |
| 116 | + |
| 117 | + taskRef, err := service.CreateVPNGateway(createRequest) |
| 118 | + if err != nil { |
| 119 | + return fmt.Errorf("Error creating VPN gateway: %s", err) |
| 120 | + } |
| 121 | + |
| 122 | + waitFlag, _ := cmd.Flags().GetBool("wait") |
| 123 | + if waitFlag { |
| 124 | + err := helper.WaitForCommand(TaskStatusWaitFunc(service, taskRef.TaskID, ecloud.TaskStatusComplete)) |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("Error waiting for VPN gateway task to complete: %s", err) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + vpnGateway, err := service.GetVPNGateway(taskRef.ResourceID) |
| 131 | + if err != nil { |
| 132 | + return fmt.Errorf("Error retrieving new VPN gateway: %s", err) |
| 133 | + } |
| 134 | + |
| 135 | + return output.CommandOutput(cmd, OutputECloudVPNGatewaysProvider([]ecloud.VPNGateway{vpnGateway})) |
| 136 | +} |
| 137 | + |
| 138 | +func ecloudVPNGatewayUpdateCmd(f factory.ClientFactory) *cobra.Command { |
| 139 | + cmd := &cobra.Command{ |
| 140 | + Use: "update <gateway: id>...", |
| 141 | + Short: "Updates a VPN gateway", |
| 142 | + Long: "Update the name of a VPN gateway", |
| 143 | + Example: "ans ecloud vpngateway update vpng-abcdef12 --name \"my gateway\"", |
| 144 | + Args: func(cmd *cobra.Command, args []string) error { |
| 145 | + if len(args) < 1 { |
| 146 | + return errors.New("Missing VPN gateway") |
| 147 | + } |
| 148 | + |
| 149 | + return nil |
| 150 | + }, |
| 151 | + RunE: ecloudCobraRunEFunc(f, ecloudVPNGatewayUpdate), |
| 152 | + } |
| 153 | + |
| 154 | + cmd.Flags().String("name", "", "Name of gateway") |
| 155 | + cmd.Flags().Bool("wait", false, "Specifies that the command should wait until the VPN gateway has been completely updated") |
| 156 | + |
| 157 | + return cmd |
| 158 | +} |
| 159 | + |
| 160 | +func ecloudVPNGatewayUpdate(service ecloud.ECloudService, cmd *cobra.Command, args []string) error { |
| 161 | + patchRequest := ecloud.PatchVPNGatewayRequest{} |
| 162 | + |
| 163 | + if cmd.Flags().Changed("name") { |
| 164 | + patchRequest.Name, _ = cmd.Flags().GetString("name") |
| 165 | + } |
| 166 | + |
| 167 | + var vpnGateways []ecloud.VPNGateway |
| 168 | + for _, arg := range args { |
| 169 | + task, err := service.PatchVPNGateway(arg, patchRequest) |
| 170 | + if err != nil { |
| 171 | + output.OutputWithErrorLevelf("Error updating VPN gateway [%s]: %s", arg, err) |
| 172 | + continue |
| 173 | + } |
| 174 | + |
| 175 | + waitFlag, _ := cmd.Flags().GetBool("wait") |
| 176 | + if waitFlag { |
| 177 | + err := helper.WaitForCommand(TaskStatusWaitFunc(service, task.TaskID, ecloud.TaskStatusComplete)) |
| 178 | + if err != nil { |
| 179 | + output.OutputWithErrorLevelf("Error waiting for task to complete for VPN gateway [%s]: %s", arg, err) |
| 180 | + continue |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + vpnGateway, err := service.GetVPNGateway(arg) |
| 185 | + if err != nil { |
| 186 | + output.OutputWithErrorLevelf("Error retrieving updated VPN gateway [%s]: %s", arg, err) |
| 187 | + continue |
| 188 | + } |
| 189 | + |
| 190 | + vpnGateways = append(vpnGateways, vpnGateway) |
| 191 | + } |
| 192 | + |
| 193 | + return output.CommandOutput(cmd, OutputECloudVPNGatewaysProvider(vpnGateways)) |
| 194 | +} |
| 195 | + |
| 196 | +func ecloudVPNGatewayDeleteCmd(f factory.ClientFactory) *cobra.Command { |
| 197 | + cmd := &cobra.Command{ |
| 198 | + Use: "delete <gateway: id>...", |
| 199 | + Short: "Removes a VPN gateway", |
| 200 | + Example: "ans ecloud vpngateway delete vpng-abcdef12", |
| 201 | + Args: func(cmd *cobra.Command, args []string) error { |
| 202 | + if len(args) < 1 { |
| 203 | + return errors.New("Missing VPN gateway") |
| 204 | + } |
| 205 | + |
| 206 | + return nil |
| 207 | + }, |
| 208 | + RunE: ecloudCobraRunEFunc(f, ecloudVPNGatewayDelete), |
| 209 | + } |
| 210 | + |
| 211 | + cmd.Flags().Bool("wait", false, "Specifies that the command should wait until the VPN gateway has been completely removed") |
| 212 | + |
| 213 | + return cmd |
| 214 | +} |
| 215 | + |
| 216 | +func ecloudVPNGatewayDelete(service ecloud.ECloudService, cmd *cobra.Command, args []string) error { |
| 217 | + for _, arg := range args { |
| 218 | + taskID, err := service.DeleteVPNGateway(arg) |
| 219 | + if err != nil { |
| 220 | + output.OutputWithErrorLevelf("Error removing VPN gateway [%s]: %s", arg, err) |
| 221 | + continue |
| 222 | + } |
| 223 | + |
| 224 | + waitFlag, _ := cmd.Flags().GetBool("wait") |
| 225 | + if waitFlag { |
| 226 | + err := helper.WaitForCommand(TaskStatusWaitFunc(service, taskID, ecloud.TaskStatusComplete)) |
| 227 | + if err != nil { |
| 228 | + output.OutputWithErrorLevelf("Error waiting for task to complete for VPN gateway [%s]: %s", arg, err) |
| 229 | + continue |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + return nil |
| 234 | +} |
0 commit comments