Skip to content
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## master

* Add type filter flag `-t` / `--type` to `image list` command
* Expose labels of servers, Floating IPs, images, and SSH Keys
* Add `hcloud {server|ssh-key|image|floating-ip} {add-label|remove-label}` commands

## v1.6.1

Expand Down
48 changes: 42 additions & 6 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[[constraint]]
name = "github.com/hetznercloud/hcloud-go"
version = "v1.6.0"
version = "1.9.0"

[[constraint]]
name = "github.com/pelletier/go-toml"
Expand Down
2 changes: 2 additions & 0 deletions cli/floatingip.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func newFloatingIPCommand(cli *CLI) *cobra.Command {
newFloatingIPDeleteCommand(cli),
newFloatingIPEnableProtectionCommand(cli),
newFloatingIPDisableProtectionCommand(cli),
newFloatingIPAddLabelCommand(cli),
newFloatingIPRemoveLabelCommand(cli),
)
return cmd
}
Expand Down
66 changes: 66 additions & 0 deletions cli/floatingip_add_label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cli

import (
"errors"
"fmt"
"strconv"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/spf13/cobra"
)

func newFloatingIPAddLabelCommand(cli *CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "add-label [FLAGS] FLOATINGIP LABEL",
Short: "Add a label to a Floating IP",
Args: cobra.ExactArgs(2),
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: chainRunE(validateFloatingIPAddLabel, cli.ensureToken),
RunE: cli.wrap(runFloatingIPAddLabel),
}

cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already")
return cmd
}

func validateFloatingIPAddLabel(cmd *cobra.Command, args []string) error {
label := splitLabel(args[1])
if len(label) != 2 {
return fmt.Errorf("invalid label: %s", args[1])
}

return nil
}

func runFloatingIPAddLabel(cli *CLI, cmd *cobra.Command, args []string) error {
overwrite, _ := cmd.Flags().GetBool("overwrite")
id, err := strconv.Atoi(args[0])
if err != nil {
return errors.New("invalid Floating IP ID")
}
floatingIP, _, err := cli.Client().FloatingIP.GetByID(cli.Context, id)
if err != nil {
return err
}
if floatingIP == nil {
return fmt.Errorf("Floating IP not found: %d", id)
}
label := splitLabel(args[1])

if _, ok := floatingIP.Labels[label[0]]; ok && !overwrite {
return fmt.Errorf("label %s on Floating IP %d already exists", label[0], floatingIP.ID)
}
labels := floatingIP.Labels
labels[label[0]] = label[1]
opts := hcloud.FloatingIPUpdateOpts{
Labels: labels,
}
_, _, err = cli.Client().FloatingIP.Update(cli.Context, floatingIP, opts)
if err != nil {
return err
}
fmt.Printf("Label %s added to Floating IP %d\n", label[0], floatingIP.ID)

return nil
}
8 changes: 8 additions & 0 deletions cli/floatingip_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,13 @@ func runFloatingIPDescribe(cli *CLI, cmd *cobra.Command, args []string) error {
fmt.Printf("Protection:\n")
fmt.Printf(" Delete:\t%s\n", yesno(floatingIP.Protection.Delete))

fmt.Print("Labels:\n")
if len(floatingIP.Labels) == 0 {
fmt.Print(" No labels\n")
} else {
for key, value := range floatingIP.Labels {
fmt.Printf(" %s: %s\n", key, value)
}
}
return nil
}
10 changes: 9 additions & 1 deletion cli/floatingip_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func newFloatingIPListCommand(cli *CLI) *cobra.Command {
RunE: cli.wrap(runFloatingIPList),
}
addListOutputFlag(cmd, floatingIPListTableOutput.Columns())
cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels")
return cmd
}

Expand All @@ -80,7 +81,14 @@ func runFloatingIPList(cli *CLI, cmd *cobra.Command, args []string) error {
return err
}

floatingIPs, err := cli.Client().FloatingIP.All(cli.Context)
labelSelector, _ := cmd.Flags().GetString("selector")
opts := hcloud.FloatingIPListOpts{
ListOpts: hcloud.ListOpts{
LabelSelector: labelSelector,
PerPage: 50,
},
}
floatingIPs, err := cli.Client().FloatingIP.AllWithOpts(cli.Context, opts)
if err != nil {
return err
}
Expand Down
80 changes: 80 additions & 0 deletions cli/floatingip_remove_label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cli

import (
"errors"
"fmt"
"strconv"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/spf13/cobra"
)

func newFloatingIPRemoveLabelCommand(cli *CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "remove-label [FLAGS] FLOATINGIP LABELKEY",
Short: "Remove a label from a Floating IP",
Args: cobra.RangeArgs(1, 2),
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: chainRunE(validateFloatingIPRemoveLabel, cli.ensureToken),
RunE: cli.wrap(runFloatingIPRemoveLabel),
}

cmd.Flags().BoolP("all", "a", false, "Remove all labels")
return cmd
}

func validateFloatingIPRemoveLabel(cmd *cobra.Command, args []string) error {
all, _ := cmd.Flags().GetBool("all")

if all && len(args) == 2 {
return errors.New("must not specify a label key when using --all/-a")
}
if !all && len(args) != 2 {
return errors.New("must specify a label key when not using --all/-a")
}

return nil
}

func runFloatingIPRemoveLabel(cli *CLI, cmd *cobra.Command, args []string) error {
all, _ := cmd.Flags().GetBool("all")
id, err := strconv.Atoi(args[0])
if err != nil {
return errors.New("invalid Floating IP ID")
}
floatingIP, _, err := cli.Client().FloatingIP.GetByID(cli.Context, id)
if err != nil {
return err
}
if floatingIP == nil {
return fmt.Errorf("Floating IP not found: %d", id)
}

labels := floatingIP.Labels
if all {
labels = make(map[string]string)
} else {
label := args[1]
if _, ok := floatingIP.Labels[label]; !ok {
return fmt.Errorf("label %s on Floating IP %d does not exist", label, floatingIP.ID)
}
delete(labels, label)
}

opts := hcloud.FloatingIPUpdateOpts{
Labels: labels,
}
_, _, err = cli.Client().FloatingIP.Update(cli.Context, floatingIP, opts)
if err != nil {
return err
}

if all {
fmt.Printf("All labels removed from Floating IP %d\n", floatingIP.ID)
} else {
fmt.Printf("Label %s removed from Floating IP %d\n", args[1], floatingIP.ID)
}

return nil
}
2 changes: 2 additions & 0 deletions cli/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func newImageCommand(cli *CLI) *cobra.Command {
newImageUpdateCommand(cli),
newImageEnableProtectionCommand(cli),
newImageDisableProtectionCommand(cli),
newImageAddLabelCommand(cli),
newImageRemoveLabelCommand(cli),
)
return cmd
}
Expand Down
Loading