Skip to content

Commit

Permalink
Add support for backup gateways and agent-level backups
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiol committed Nov 13, 2024
1 parent d2a8ec3 commit e054dfb
Show file tree
Hide file tree
Showing 11 changed files with 948 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/ecloud/ecloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func ECloudRootCmd(f factory.ClientFactory, fs afero.Fs) *cobra.Command {
cmd.AddCommand(ecloudAffinityRuleRootCmd(f))
cmd.AddCommand(ecloudAffinityRuleMemberRootCmd(f))
cmd.AddCommand(ecloudResourceTierRootCmd(f))
cmd.AddCommand(ecloudBackupGatewayRootCmd(f))
}

return cmd
Expand Down
236 changes: 236 additions & 0 deletions cmd/ecloud/ecloud_backupgateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
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 ecloudBackupGatewayRootCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "backupgateway",
Short: "sub-commands relating to backup gateways",
}

// Child commands
cmd.AddCommand(ecloudBackupGatewaySpecificationRootCmd(f))
cmd.AddCommand(ecloudBackupGatewayListCmd(f))
cmd.AddCommand(ecloudBackupGatewayShowCmd(f))
cmd.AddCommand(ecloudBackupGatewayCreateCmd(f))
cmd.AddCommand(ecloudBackupGatewayUpdateCmd(f))
cmd.AddCommand(ecloudBackupGatewayDeleteCmd(f))

return cmd
}

func ecloudBackupGatewayListCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Lists backup gateways",
Example: "ans ecloud backupgateway list",
RunE: ecloudCobraRunEFunc(f, ecloudBackupGatewayList),
}

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

return cmd
}

func ecloudBackupGatewayList(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.GetBackupGateways(params)
if err != nil {
return fmt.Errorf("Error retrieving backup gateways: %s", err)

Check notice on line 54 in cmd/ecloud/ecloud_backupgateway.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, OutputECloudBackupGatewaysProvider(gateways))
}

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

Check notice on line 67 in cmd/ecloud/ecloud_backupgateway.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, ecloudBackupGatewayShow),
}
}

func ecloudBackupGatewayShow(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
var backupGateways []ecloud.BackupGateway
for _, arg := range args {
backupGateway, err := service.GetBackupGateway(arg)
if err != nil {
output.OutputWithErrorLevelf("Error retrieving backup gateway [%s]: %s", arg, err)
continue
}

backupGateways = append(backupGateways, backupGateway)
}

return output.CommandOutput(cmd, OutputECloudBackupGatewaysProvider(backupGateways))
}

func ecloudBackupGatewayCreateCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates a backup gateway",
Example: "ans ecloud backupgateway create --router rtr-abcdef12 --vpc vpc-abcd1234 --specification bgws-abcdef12",
RunE: ecloudCobraRunEFunc(f, ecloudBackupGatewayCreate),
}

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

return cmd
}

func ecloudBackupGatewayCreate(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
createRequest := ecloud.CreateBackupGatewayRequest{}
createRequest.Name, _ = cmd.Flags().GetString("name")
createRequest.VPCID, _ = cmd.Flags().GetString("vpc")
createRequest.RouterID, _ = cmd.Flags().GetString("router")
createRequest.GatewaySpecID, _ = cmd.Flags().GetString("specification")

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

Check notice on line 121 in cmd/ecloud/ecloud_backupgateway.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 backup gateway task to complete: %s", err)

Check notice on line 128 in cmd/ecloud/ecloud_backupgateway.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
}
}

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

Check notice on line 134 in cmd/ecloud/ecloud_backupgateway.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, OutputECloudBackupGatewaysProvider([]ecloud.BackupGateway{backupGateway}))
}

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

Check notice on line 148 in cmd/ecloud/ecloud_backupgateway.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, ecloudBackupGatewayUpdate),
}

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

return cmd
}

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

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

var backupGateways []ecloud.BackupGateway
for _, arg := range args {
task, err := service.PatchBackupGateway(arg, patchRequest)
if err != nil {
output.OutputWithErrorLevelf("Error updating backup 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 backup gateway [%s]: %s", arg, err)
continue
}
}

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

backupGateways = append(backupGateways, backupGateway)
}

return output.CommandOutput(cmd, OutputECloudBackupGatewaysProvider(backupGateways))
}

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

Check notice on line 205 in cmd/ecloud/ecloud_backupgateway.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, ecloudBackupGatewayDelete),
}

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

return cmd
}

func ecloudBackupGatewayDelete(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
for _, arg := range args {
taskID, err := service.DeleteBackupGateway(arg)
if err != nil {
output.OutputWithErrorLevelf("Error removing backup 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 backup gateway [%s]: %s", arg, err)
continue
}
}
}
return nil
}
85 changes: 85 additions & 0 deletions cmd/ecloud/ecloud_backupgateway_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 ecloudBackupGatewaySpecificationRootCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "spec",
Short: "sub-commands relating to backup gateway specifications",
}

// Child commands
cmd.AddCommand(ecloudBackupGatewaySpecificationListCmd(f))
cmd.AddCommand(ecloudBackupGatewaySpecificationShowCmd(f))

return cmd
}

func ecloudBackupGatewaySpecificationListCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Lists Backup gateway specifications",
Example: "ans ecloud backupgateway spec list",
RunE: ecloudCobraRunEFunc(f, ecloudBackupGatewaySpecificationList),
}

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

return cmd
}

func ecloudBackupGatewaySpecificationList(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.GetBackupGatewaySpecifications(params)
if err != nil {
return fmt.Errorf("Error retrieving backup gateway specifications: %s", err)

Check notice on line 50 in cmd/ecloud/ecloud_backupgateway_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, OutputECloudBackupGatewaySpecificationsProvider(specs))
}

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

Check notice on line 63 in cmd/ecloud/ecloud_backupgateway_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, ecloudBackupGatewaySpecificationShow),
}
}

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

specs = append(specs, spec)
}

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

0 comments on commit e054dfb

Please sign in to comment.