Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for backup gateways and agent-level backups #187

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/ecloud/ecloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"github.com/spf13/cobra"
)

func ECloudRootCmd(f factory.ClientFactory, fs afero.Fs) *cobra.Command {

Check notice on line 16 in cmd/ecloud/ecloud.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Name starts with a package name

Name starts with the package name
cmd := &cobra.Command{
Use: "ecloud",
Short: "Commands relating to eCloud service",
Expand Down Expand Up @@ -74,6 +74,7 @@
cmd.AddCommand(ecloudAffinityRuleRootCmd(f))
cmd.AddCommand(ecloudAffinityRuleMemberRootCmd(f))
cmd.AddCommand(ecloudResourceTierRootCmd(f))
cmd.AddCommand(ecloudBackupGatewayRootCmd(f))
}

return cmd
Expand Down Expand Up @@ -112,18 +113,18 @@
// GetKeyValueFromStringFlag returns a string map from given string flag. Expects format 'key=value'
func GetKeyValueFromStringFlag(flag string) (key, value string, err error) {
if flag == "" {
return key, value, errors.New("Missing key/value")

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

parts := strings.Split(flag, "=")
if len(parts) < 2 || len(parts) > 2 {
return key, value, errors.New("Invalid format, expecting: key=value")

Check notice on line 121 in cmd/ecloud/ecloud.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
}
if parts[0] == "" {
return key, value, errors.New("Missing key")

Check notice on line 124 in cmd/ecloud/ecloud.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
}
if parts[1] == "" {
return key, value, errors.New("Missing value")

Check notice on line 127 in cmd/ecloud/ecloud.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 parts[0], parts[1], nil
Expand All @@ -138,7 +139,7 @@
return (exists == false), nil
}

return false, fmt.Errorf("Failed to retrieve solution template [%s]: %s", templateName, err.Error())

Check notice on line 142 in cmd/ecloud/ecloud.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 (exists == true), nil
Expand All @@ -154,7 +155,7 @@
return (exists == false), nil
}

return false, fmt.Errorf("Failed to retrieve pod template [%s]: %s", templateName, err.Error())

Check notice on line 158 in cmd/ecloud/ecloud.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 (exists == true), nil
Expand All @@ -167,10 +168,10 @@
return func() (finished bool, err error) {
status, err := fn()
if err != nil {
return false, fmt.Errorf("Failed to retrieve status for resource: %s", err)

Check notice on line 171 in cmd/ecloud/ecloud.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
}
if status == ecloud.SyncStatusFailed {
return false, fmt.Errorf("Resource in [%s] state", ecloud.SyncStatusFailed.String())

Check notice on line 174 in cmd/ecloud/ecloud.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
}
if status == expectedStatus {
return true, nil
Expand All @@ -184,10 +185,10 @@
return func() (finished bool, err error) {
task, err := service.GetTask(taskID)
if err != nil {
return false, fmt.Errorf("Failed to retrieve task status: %s", err)

Check notice on line 188 in cmd/ecloud/ecloud.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
}
if task.Status == ecloud.TaskStatusFailed {
return false, fmt.Errorf("Task in [%s] state", ecloud.TaskStatusFailed)

Check notice on line 191 in cmd/ecloud/ecloud.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
}
if task.Status == expectedStatus {
return true, nil
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
Loading