File: volume.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 (53 lines) | stat: -rw-r--r-- 1,249 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package hcapi2

import (
	"context"
	"strconv"
)

// VolumeClient embeds the Hetzner Cloud Volume client and provides some additional
// helper functions.
type VolumeClient interface {
	VolumeClientBase
	Names() []string
	LabelKeys(idOrName string) []string
}

func NewVolumeClient(client VolumeClientBase) VolumeClient {
	return &volumeClient{
		VolumeClientBase: client,
	}
}

type volumeClient struct {
	VolumeClientBase
}

// Names obtains a list of available volumes for the current account. It
// returns nil if the current project has no volumes or the volume names could
// not be fetched.
func (c *volumeClient) Names() []string {
	vols, err := c.All(context.Background())
	if err != nil || len(vols) == 0 {
		return nil
	}
	names := make([]string, len(vols))
	for i, vol := range vols {
		name := vol.Name
		if name == "" {
			name = strconv.FormatInt(vol.ID, 10)
		}
		names[i] = name
	}
	return names
}

// LabelKeys returns a slice containing the keys of all labels assigned
// to the Volume with the passed idOrName.
func (c *volumeClient) LabelKeys(idOrName string) []string {
	vol, _, err := c.Get(context.Background(), idOrName)
	if err != nil || vol == nil || len(vol.Labels) == 0 {
		return nil
	}
	return labelKeys(vol.Labels)
}