File: labels.go

package info (click to toggle)
golang-github-hetznercloud-hcloud-go 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,072 kB
  • sloc: sh: 5; makefile: 2
file content (23 lines) | stat: -rw-r--r-- 712 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package hcloud

import (
	"fmt"
	"regexp"
)

var keyRegexp = regexp.MustCompile(
	`^([a-z0-9A-Z]((?:[\-_.]|[a-z0-9A-Z]){0,253}[a-z0-9A-Z])?/)?[a-z0-9A-Z]((?:[\-_.]|[a-z0-9A-Z]|){0,61}[a-z0-9A-Z])?$`)
var valueRegexp = regexp.MustCompile(`^(([a-z0-9A-Z](?:[\-_.]|[a-z0-9A-Z]){0,61})?[a-z0-9A-Z]$|$)`)

func ValidateResourceLabels(labels map[string]interface{}) (bool, error) {
	for k, v := range labels {
		if match := keyRegexp.MatchString(k); !match {
			return false, fmt.Errorf("label key '%s' is not correctly formatted", k)
		}

		if match := valueRegexp.MatchString(v.(string)); !match {
			return false, fmt.Errorf("label value '%s' (key: %s) is not correctly formatted", v, k)
		}
	}
	return true, nil
}