Skip to content

Commit

Permalink
[CHANGE] IP UUID as positional argument
Browse files Browse the repository at this point in the history
  • Loading branch information
eijei521 committed Aug 29, 2022
1 parent 34f3efb commit 441873e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 35 deletions.
25 changes: 13 additions & 12 deletions internal/ips/assign.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ips

import (
"fmt"

"github.com/cherryservers/cherryctl/internal/utils"
"github.com/cherryservers/cherrygo/v3"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -17,18 +18,26 @@ func (c *Client) Assign() *cobra.Command {
targetHostname string
)
ipAssignCmd := &cobra.Command{
Use: `assign -i <ip_address_id> {--target-hostname | --target-id | --target-ip-id} [-p <project_id>]`,
Use: `assign UUID {--target-hostname <hostname> | --target-id <server_id> | --target-ip-id <ip_id>} [-p <project_id>]`,
Args: cobra.ExactArgs(1),
Aliases: []string{"attach"},
Short: "Assign an IP address to a specified server or other IP address.",
Long: "Assign an IP address to a specified server or another IP address. IP address assignment to another IP is possible only if routed IP type is floating and target IP is subnet or primary-ip type.",
Example: ` # Assign an IP address to a server:
cherryctl ip assign -i 30c15082-a06e-4c43-bfc3-252616b46eba --server-id 12345`,
cherryctl ip assign 30c15082-a06e-4c43-bfc3-252616b46eba --server-id 12345`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
request := &cherrygo.AssignIPAddress{}

if targetIPID != "" && IsValidUUID(targetIPID) {
if utils.IsValidUUID(args[0]) {
ipID = args[0]
} else {
fmt.Println("IP address with ID %s was not found.", args[0])
return nil
}

if targetIPID != "" && utils.IsValidUUID(targetIPID) {
request.IpID = targetIPID
} else if targetHostname != "" {
srvID, err := utils.ServerHostnameToID(targetHostname, projectID, c.ServerService)
Expand Down Expand Up @@ -57,20 +66,12 @@ func (c *Client) Assign() *cobra.Command {
},
}

ipAssignCmd.Flags().StringVarP(&ipID, "ip-address-id", "i", "", "The ID of a IP address.")
ipAssignCmd.Flags().IntVarP(&projectID, "project-id", "p", 0, "The project's ID.")

ipAssignCmd.Flags().StringVarP(&targetHostname, "target-hostname", "", "", "The hostname of the server to assign IP to.")
ipAssignCmd.Flags().IntVarP(&targetID, "target-id", "", 0, "The ID of the server to assign IP to.")
ipAssignCmd.Flags().StringVarP(&targetIPID, "target-ip-id", "", "", "Subnet or primary-ip type IP ID to route IP to.")

ipAssignCmd.MarkFlagsMutuallyExclusive("target-hostname", "target-id", "target-ip-id")
ipAssignCmd.MarkFlagRequired("ip-address-id")

return ipAssignCmd
}

func IsValidUUID(u string) bool {
_, err := uuid.Parse(u)
return err == nil
}
2 changes: 1 addition & 1 deletion internal/ips/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (c *Client) Create() *cobra.Command {
tags []string
)
ipCreateCmd := &cobra.Command{
Use: `create [-p <project_id>] --region <region_slug> [--target-hostname | --target-id | --target-ip-id] [--ptr-record] [--a-record] [--tags]`,
Use: `create [-p <project_id>] --region <region_slug> [--target-hostname <hostname> | --target-id <server_id> | --target-ip-id <ip_uuid>] [--ptr-record <ptr>] [--a-record <a>] [--tags <tags>]`,
Short: "Create floating IP address.",
Long: "Create floating IP address in speficied project.",
Example: ` # Create a floating IP address in EU-Nord-1 location:
Expand Down
18 changes: 12 additions & 6 deletions internal/ips/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ips
import (
"fmt"

"github.com/cherryservers/cherryctl/internal/utils"
"github.com/manifoldco/promptui"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -12,19 +13,27 @@ func (c *Client) Delete() *cobra.Command {
var ipID string
var force bool
deleteIpCmd := &cobra.Command{
Use: `delete -i <ip_address_id>`,
Use: `delete UUID`,
Args: cobra.ExactArgs(1),
Short: "Delete an IP address.",
Long: "Deletes the specified IP address with a confirmation prompt. To skip the confirmation use --force.",
Example: ` # Deletes the specified IP:
cherryctl ip delete -i 30c15082-a06e-4c43-bfc3-252616b46eba
cherryctl ip delete 30c15082-a06e-4c43-bfc3-252616b46eba
>
✔ Are you sure you want to delete IP address 30c15082-a06e-4c43-bfc3-252616b46eba: y
# Deletes a server, skipping confirmation:
cherryctl ip delete -f -i 30c15082-a06e-4c43-bfc3-252616b46eba`,
cherryctl ip delete 30c15082-a06e-4c43-bfc3-252616b46eba -f`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if utils.IsValidUUID(args[0]) {
ipID = args[0]
} else {
fmt.Println("IP address with ID %s was not found.", args[0])
return nil
}

if !force {
prompt := promptui.Prompt{
Label: fmt.Sprintf("Are you sure you want to delete IP address %s: ", ipID),
Expand All @@ -46,10 +55,7 @@ func (c *Client) Delete() *cobra.Command {
},
}

deleteIpCmd.Flags().StringVarP(&ipID, "ip-address-id", "i", "", "The ID of a IP address.")
deleteIpCmd.Flags().BoolVarP(&force, "force", "f", false, "Skips confirmation for the server deletion.")

_ = deleteIpCmd.MarkFlagRequired("ip-address-id")

return deleteIpCmd
}
16 changes: 11 additions & 5 deletions internal/ips/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@ package ips
import (
"fmt"

"github.com/cherryservers/cherryctl/internal/utils"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func (c *Client) Get() *cobra.Command {
var ipID string
ipGetCmd := &cobra.Command{
Use: `get [-i <ip_address_id>]`,
Use: `get UUID`,
Args: cobra.ExactArgs(1),
Short: "Get an IP address details.",
Long: "Get the details of the specified IP address.",
Example: ` # Gets the details of the specified IP address:
cherryctl ip get -i 12345`,
cherryctl ip get 30c15082-a06e-4c43-bfc3-252616b46eba`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if utils.IsValidUUID(args[0]) {
ipID = args[0]
} else {
fmt.Println("IP address with ID %s was not found.", args[0])
return nil
}

getOptions := c.Servicer.GetOptions()
getOptions.Fields = []string{"ip", "region", "hostname"}
i, _, err := c.Service.Get(ipID, getOptions)
Expand All @@ -33,8 +42,5 @@ func (c *Client) Get() *cobra.Command {
},
}

ipGetCmd.Flags().StringVarP(&ipID, "ip-address-id", "i", "", "The ID of a IP address.")
_ = ipGetCmd.MarkFlagRequired("ip-address-id")

return ipGetCmd
}
16 changes: 10 additions & 6 deletions internal/ips/unassign.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ips
import (
"fmt"

"github.com/cherryservers/cherryctl/internal/utils"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -12,15 +13,22 @@ func (c *Client) Unassign() *cobra.Command {
ipID string
)
ipDetachCmd := &cobra.Command{
Use: `unassign -i <ip_address_id>`,
Use: `unassign UUID`,
Args: cobra.ExactArgs(1),
Aliases: []string{"detach", "unasign"},
Short: "Unassign an IP address.",
Long: "Unassign an IP address.",
Example: ` # Unassign an IP address:
cherryctl ip unassign -i 12345`,
cherryctl ip unassign 30c15082-a06e-4c43-bfc3-252616b46eba`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if utils.IsValidUUID(args[0]) {
ipID = args[0]
} else {
fmt.Println("IP address with ID %s was not found.", args[0])
return nil
}

_, err := c.Service.Unassign(ipID)
if err != nil {
Expand All @@ -32,9 +40,5 @@ func (c *Client) Unassign() *cobra.Command {
},
}

ipDetachCmd.Flags().StringVarP(&ipID, "ip-address-id", "i", "", "The ID of an IP address.")

ipDetachCmd.MarkFlagRequired("ip-address-id")

return ipDetachCmd
}
15 changes: 10 additions & 5 deletions internal/ips/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/cherryservers/cherryctl/internal/utils"
"github.com/cherryservers/cherrygo/v3"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -17,14 +18,21 @@ func (c *Client) Update() *cobra.Command {
tags []string
)
ipUpdateCmd := &cobra.Command{
Use: `update -i <ip_address_id> [--ptr-record] [--a-record] [--tags]`,
Use: `update UUID [--ptr-record <ptr>] [--a-record <a>] [--tags <tags>]`,
Args: cobra.ExactArgs(1),
Short: "Update IP address.",
Long: "Update tags, ptr record, a record or target server of a IP address.",
Example: ` # Updates a record and tags:
cherryctl ip update -i 30c15082-a06e-4c43-bfc3-252616b46eba --a-record stage --tags="env=stage"`,
cherryctl ip update 30c15082-a06e-4c43-bfc3-252616b46eba --a-record stage --tags="env=stage"`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if utils.IsValidUUID(args[0]) {
ipID = args[0]
} else {
fmt.Println("IP address with ID %s was not found.", args[0])
return nil
}

tagsArr := make(map[string]string)
for _, kv := range tags {
Expand Down Expand Up @@ -60,12 +68,9 @@ func (c *Client) Update() *cobra.Command {
},
}

ipUpdateCmd.Flags().StringVarP(&ipID, "ip-address-id", "i", "", "The ID of a IP address.")
ipUpdateCmd.Flags().StringVarP(&ptrRecord, "ptr-record", "", "", "Slug of the region from where IP address will requested.")
ipUpdateCmd.Flags().StringVarP(&aRecord, "a-record", "", "", "Slug of the region from where IP address will requested.")
ipUpdateCmd.Flags().StringSliceVarP(&tags, "tags", "", []string{}, `Tag or list of tags for the server: --tags="key=value,env=prod".`)

ipUpdateCmd.MarkFlagRequired("ip-address-id")

return ipUpdateCmd
}
6 changes: 6 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/cherryservers/cherrygo/v3"
"github.com/google/uuid"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -39,3 +40,8 @@ func serverList(projectID int, ServerService cherrygo.ServersService) ([]cherryg

return serverList, err
}

func IsValidUUID(u string) bool {
_, err := uuid.Parse(u)
return err == nil
}

0 comments on commit 441873e

Please sign in to comment.