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 eCloud VPC VPN Gateway management commands #185

Merged
merged 1 commit into from
Nov 11, 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 @@ -69,6 +69,7 @@
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 Expand Up @@ -111,18 +112,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 115 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 120 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 123 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 126 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 @@ -137,7 +138,7 @@
return (exists == false), nil
}

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

Check notice on line 141 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 @@ -153,7 +154,7 @@
return (exists == false), nil
}

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

Check notice on line 157 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 @@ -166,10 +167,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 170 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 173 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 @@ -183,10 +184,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 187 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 190 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
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
Loading