Skip to content

Commit

Permalink
Add eCloud VPC VPN Gateway management commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiol committed Nov 11, 2024
1 parent 9d681d5 commit 02f1801
Show file tree
Hide file tree
Showing 11 changed files with 1,906 additions and 60 deletions.
1 change: 1 addition & 0 deletions cmd/ecloud/ecloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func ECloudRootCmd(f factory.ClientFactory, fs afero.Fs) *cobra.Command {
cmd.AddCommand(ecloudVPNProfileGroupRootCmd(f))
cmd.AddCommand(ecloudVPNServiceRootCmd(f))
cmd.AddCommand(ecloudVPNSessionRootCmd(f))
cmd.AddCommand(ecloudVPNGatewayRootCmd(f))
cmd.AddCommand(ecloudVolumeGroupRootCmd(f))
cmd.AddCommand(ecloudAffinityRuleRootCmd(f))
cmd.AddCommand(ecloudAffinityRuleMemberRootCmd(f))
Expand Down
234 changes: 234 additions & 0 deletions cmd/ecloud/ecloud_vpngateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
package ecloud

import (
"errors"
"fmt"

"github.com/ans-group/cli/internal/pkg/factory"
"github.com/ans-group/cli/internal/pkg/helper"
"github.com/ans-group/cli/internal/pkg/output"
"github.com/ans-group/sdk-go/pkg/service/ecloud"
"github.com/spf13/cobra"
)

func ecloudVPNGatewayRootCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "vpngateway",
Short: "sub-commands relating to VPN gateways",
}

// Child commands
cmd.AddCommand(ecloudVPNGatewayUserRootCmd(f))
cmd.AddCommand(ecloudVPNGatewaySpecificationRootCmd(f))
cmd.AddCommand(ecloudVPNGatewayListCmd(f))
cmd.AddCommand(ecloudVPNGatewayShowCmd(f))
cmd.AddCommand(ecloudVPNGatewayCreateCmd(f))
cmd.AddCommand(ecloudVPNGatewayUpdateCmd(f))
cmd.AddCommand(ecloudVPNGatewayDeleteCmd(f))

return cmd
}

func ecloudVPNGatewayListCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Lists VPN gateways",
Example: "ans ecloud vpngateway list",
RunE: ecloudCobraRunEFunc(f, ecloudVPNGatewayList),
}

cmd.Flags().String("name", "", "VPN gateway name for filtering")

return cmd
}

func ecloudVPNGatewayList(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
params, err := helper.GetAPIRequestParametersFromFlags(cmd,
helper.NewStringFilterFlagOption("name", "name"),
)
if err != nil {
return err
}

gateways, err := service.GetVPNGateways(params)
if err != nil {
return fmt.Errorf("Error retrieving VPN gateways: %s", err)

Check notice on line 55 in cmd/ecloud/ecloud_vpngateway.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Error string should not be capitalized or end with punctuation

Error string should not be capitalized or end with punctuation mark
}

return output.CommandOutput(cmd, OutputECloudVPNGatewaysProvider(gateways))
}

func ecloudVPNGatewayShowCmd(f factory.ClientFactory) *cobra.Command {
return &cobra.Command{
Use: "show <gateway: id>...",
Short: "Show details of a VPN gateway",
Example: "ans ecloud vpngateway show vpng-abcdef12",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("Missing VPN gateway")

Check notice on line 68 in cmd/ecloud/ecloud_vpngateway.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Error string should not be capitalized or end with punctuation

Error string should not be capitalized or end with punctuation mark
}

return nil
},
RunE: ecloudCobraRunEFunc(f, ecloudVPNGatewayShow),
}
}

func ecloudVPNGatewayShow(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
var vpnGateways []ecloud.VPNGateway
for _, arg := range args {
vpnGateway, err := service.GetVPNGateway(arg)
if err != nil {
output.OutputWithErrorLevelf("Error retrieving VPN gateway [%s]: %s", arg, err)
continue
}

vpnGateways = append(vpnGateways, vpnGateway)
}

return output.CommandOutput(cmd, OutputECloudVPNGatewaysProvider(vpnGateways))
}

func ecloudVPNGatewayCreateCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates a VPN gateway",
Example: "ans ecloud vpngateway create --router rtr-abcdef12 --specification vpngs-abcdef12",
RunE: ecloudCobraRunEFunc(f, ecloudVPNGatewayCreate),
}

// Setup flags
cmd.Flags().String("name", "", "Name of gateway")
cmd.Flags().String("router", "", "ID of router")
cmd.MarkFlagRequired("router")
cmd.Flags().String("specification", "", "ID of VPN gateway specification")
cmd.MarkFlagRequired("specification")
cmd.Flags().Bool("wait", false, "Specifies that the command should wait until the VPN gateway has been completely created")

return cmd
}

func ecloudVPNGatewayCreate(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
createRequest := ecloud.CreateVPNGatewayRequest{}
createRequest.Name, _ = cmd.Flags().GetString("name")
createRequest.RouterID, _ = cmd.Flags().GetString("router")
createRequest.SpecificationID, _ = cmd.Flags().GetString("specification")

taskRef, err := service.CreateVPNGateway(createRequest)
if err != nil {
return fmt.Errorf("Error creating VPN gateway: %s", err)

Check notice on line 119 in cmd/ecloud/ecloud_vpngateway.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Error string should not be capitalized or end with punctuation

Error string should not be capitalized or end with punctuation mark
}

waitFlag, _ := cmd.Flags().GetBool("wait")
if waitFlag {
err := helper.WaitForCommand(TaskStatusWaitFunc(service, taskRef.TaskID, ecloud.TaskStatusComplete))
if err != nil {
return fmt.Errorf("Error waiting for VPN gateway task to complete: %s", err)

Check notice on line 126 in cmd/ecloud/ecloud_vpngateway.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Error string should not be capitalized or end with punctuation

Error string should not be capitalized or end with punctuation mark
}
}

vpnGateway, err := service.GetVPNGateway(taskRef.ResourceID)
if err != nil {
return fmt.Errorf("Error retrieving new VPN gateway: %s", err)

Check notice on line 132 in cmd/ecloud/ecloud_vpngateway.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Error string should not be capitalized or end with punctuation

Error string should not be capitalized or end with punctuation mark
}

return output.CommandOutput(cmd, OutputECloudVPNGatewaysProvider([]ecloud.VPNGateway{vpnGateway}))
}

func ecloudVPNGatewayUpdateCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "update <gateway: id>...",
Short: "Updates a VPN gateway",
Long: "Update the name of a VPN gateway",
Example: "ans ecloud vpngateway update vpng-abcdef12 --name \"my gateway\"",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("Missing VPN gateway")

Check notice on line 146 in cmd/ecloud/ecloud_vpngateway.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Error string should not be capitalized or end with punctuation

Error string should not be capitalized or end with punctuation mark
}

return nil
},
RunE: ecloudCobraRunEFunc(f, ecloudVPNGatewayUpdate),
}

cmd.Flags().String("name", "", "Name of gateway")
cmd.Flags().Bool("wait", false, "Specifies that the command should wait until the VPN gateway has been completely updated")

return cmd
}

func ecloudVPNGatewayUpdate(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
patchRequest := ecloud.PatchVPNGatewayRequest{}

if cmd.Flags().Changed("name") {
patchRequest.Name, _ = cmd.Flags().GetString("name")
}

var vpnGateways []ecloud.VPNGateway
for _, arg := range args {
task, err := service.PatchVPNGateway(arg, patchRequest)
if err != nil {
output.OutputWithErrorLevelf("Error updating VPN gateway [%s]: %s", arg, err)
continue
}

waitFlag, _ := cmd.Flags().GetBool("wait")
if waitFlag {
err := helper.WaitForCommand(TaskStatusWaitFunc(service, task.TaskID, ecloud.TaskStatusComplete))
if err != nil {
output.OutputWithErrorLevelf("Error waiting for task to complete for VPN gateway [%s]: %s", arg, err)
continue
}
}

vpnGateway, err := service.GetVPNGateway(arg)
if err != nil {
output.OutputWithErrorLevelf("Error retrieving updated VPN gateway [%s]: %s", arg, err)
continue
}

vpnGateways = append(vpnGateways, vpnGateway)
}

return output.CommandOutput(cmd, OutputECloudVPNGatewaysProvider(vpnGateways))
}

func ecloudVPNGatewayDeleteCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "delete <gateway: id>...",
Short: "Removes a VPN gateway",
Example: "ans ecloud vpngateway delete vpng-abcdef12",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("Missing VPN gateway")

Check notice on line 203 in cmd/ecloud/ecloud_vpngateway.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Error string should not be capitalized or end with punctuation

Error string should not be capitalized or end with punctuation mark
}

return nil
},
RunE: ecloudCobraRunEFunc(f, ecloudVPNGatewayDelete),
}

cmd.Flags().Bool("wait", false, "Specifies that the command should wait until the VPN gateway has been completely removed")

return cmd
}

func ecloudVPNGatewayDelete(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
for _, arg := range args {
taskID, err := service.DeleteVPNGateway(arg)
if err != nil {
output.OutputWithErrorLevelf("Error removing VPN gateway [%s]: %s", arg, err)
continue
}

waitFlag, _ := cmd.Flags().GetBool("wait")
if waitFlag {
err := helper.WaitForCommand(TaskStatusWaitFunc(service, taskID, ecloud.TaskStatusComplete))
if err != nil {
output.OutputWithErrorLevelf("Error waiting for task to complete for VPN gateway [%s]: %s", arg, err)
continue
}
}
}
return nil
}
85 changes: 85 additions & 0 deletions cmd/ecloud/ecloud_vpngateway_specs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package ecloud

import (
"errors"
"fmt"

"github.com/ans-group/cli/internal/pkg/factory"
"github.com/ans-group/cli/internal/pkg/helper"
"github.com/ans-group/cli/internal/pkg/output"
"github.com/ans-group/sdk-go/pkg/service/ecloud"
"github.com/spf13/cobra"
)

func ecloudVPNGatewaySpecificationRootCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "spec",
Short: "sub-commands relating to VPN gateway specifications",
}

// Child commands
cmd.AddCommand(ecloudVPNGatewaySpecificationListCmd(f))
cmd.AddCommand(ecloudVPNGatewaySpecificationShowCmd(f))

return cmd
}

func ecloudVPNGatewaySpecificationListCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Lists VPN gateway specifications",
Example: "ans ecloud vpngateway spec list",
RunE: ecloudCobraRunEFunc(f, ecloudVPNGatewaySpecificationList),
}

cmd.Flags().String("name", "", "VPN gateway specification name for filtering")

return cmd
}

func ecloudVPNGatewaySpecificationList(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
params, err := helper.GetAPIRequestParametersFromFlags(cmd,
helper.NewStringFilterFlagOption("name", "name"),
)
if err != nil {
return err
}

specs, err := service.GetVPNGatewaySpecifications(params)
if err != nil {
return fmt.Errorf("Error retrieving VPN gateway specifications: %s", err)

Check notice on line 50 in cmd/ecloud/ecloud_vpngateway_specs.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Error string should not be capitalized or end with punctuation

Error string should not be capitalized or end with punctuation mark
}

return output.CommandOutput(cmd, OutputECloudVPNGatewaySpecificationsProvider(specs))
}

func ecloudVPNGatewaySpecificationShowCmd(f factory.ClientFactory) *cobra.Command {
return &cobra.Command{
Use: "show <specification: id>...",
Short: "Show details of a VPN gateway specification",
Example: "ans ecloud vpngateway spec show vpngs-abcdef12",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("Missing VPN gateway specification")

Check notice on line 63 in cmd/ecloud/ecloud_vpngateway_specs.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Error string should not be capitalized or end with punctuation

Error string should not be capitalized or end with punctuation mark
}

return nil
},
RunE: ecloudCobraRunEFunc(f, ecloudVPNGatewaySpecificationShow),
}
}

func ecloudVPNGatewaySpecificationShow(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
var specs []ecloud.VPNGatewaySpecification
for _, arg := range args {
spec, err := service.GetVPNGatewaySpecification(arg)
if err != nil {
output.OutputWithErrorLevelf("Error retrieving VPN gateway specification [%s]: %s", arg, err)
continue
}

specs = append(specs, spec)
}

return output.CommandOutput(cmd, OutputECloudVPNGatewaySpecificationsProvider(specs))
}
Loading

0 comments on commit 02f1801

Please sign in to comment.