File: labels.go

package info (click to toggle)
hcloud-cli 1.39.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,628 kB
  • sloc: sh: 36; makefile: 7
file content (35 lines) | stat: -rw-r--r-- 1,234 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package floatingip

import (
	"context"
	"fmt"

	"github.com/hetznercloud/cli/internal/cmd/base"
	"github.com/hetznercloud/cli/internal/hcapi2"
	"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var labelCmds = base.LabelCmds{
	ResourceNameSingular:   "Floating IP",
	ShortDescriptionAdd:    "Add a label to an Floating IP",
	ShortDescriptionRemove: "Remove a label from an Floating IP",
	NameSuggestions:        func(c hcapi2.Client) func() []string { return c.FloatingIP().Names },
	LabelKeySuggestions:    func(c hcapi2.Client) func(idOrName string) []string { return c.FloatingIP().LabelKeys },
	FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) {
		floatingIP, _, err := client.FloatingIP().Get(ctx, idOrName)
		if err != nil {
			return nil, 0, err
		}
		if floatingIP == nil {
			return nil, 0, fmt.Errorf("floating IP not found: %s", idOrName)
		}
		return floatingIP.Labels, floatingIP.ID, nil
	},
	SetLabels: func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error {
		opts := hcloud.FloatingIPUpdateOpts{
			Labels: labels,
		}
		_, _, err := client.FloatingIP().Update(ctx, &hcloud.FloatingIP{ID: id}, opts)
		return err
	},
}