File: cloud.go

package info (click to toggle)
golang-github-juju-names 4.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 340 kB
  • sloc: makefile: 14
file content (51 lines) | stat: -rw-r--r-- 1,183 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
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
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package names

import (
	"fmt"
	"regexp"
)

const CloudTagKind = "cloud"

var (
	cloudSnippet = "[a-zA-Z0-9][a-zA-Z0-9._-]*"
	validCloud   = regexp.MustCompile("^" + cloudSnippet + "$")
)

type CloudTag struct {
	id string
}

func (t CloudTag) String() string { return t.Kind() + "-" + t.id }
func (t CloudTag) Kind() string   { return CloudTagKind }
func (t CloudTag) Id() string     { return t.id }

// NewCloudTag returns the tag for the cloud with the given ID.
// It will panic if the given cloud ID is not valid.
func NewCloudTag(id string) CloudTag {
	if !IsValidCloud(id) {
		panic(fmt.Sprintf("%q is not a valid cloud ID", id))
	}
	return CloudTag{id}
}

// ParseCloudTag parses a cloud tag string.
func ParseCloudTag(cloudTag string) (CloudTag, error) {
	tag, err := ParseTag(cloudTag)
	if err != nil {
		return CloudTag{}, err
	}
	dt, ok := tag.(CloudTag)
	if !ok {
		return CloudTag{}, invalidTagError(cloudTag, CloudTagKind)
	}
	return dt, nil
}

// IsValidCloud returns whether id is a valid cloud ID.
func IsValidCloud(id string) bool {
	return validCloud.MatchString(id)
}