File: controlleragent.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 (62 lines) | stat: -rw-r--r-- 1,757 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
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2019 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package names

import (
	"fmt"
	"regexp"
	"strconv"
)

// ControllerAgentTagKind indicates that a tag belongs to a controller agent.
const ControllerAgentTagKind = "controller"

var validControllerAgentId = regexp.MustCompile("^" + NumberSnippet + "$")

// ControllerAgentTag represents a tag used to describe a controller.
type ControllerAgentTag struct {
	id string
}

// NewControllerAgentTag returns the tag of an controller agent with the given id.
func NewControllerAgentTag(id string) ControllerAgentTag {
	_, err := strconv.Atoi(id)
	if err != nil {
		panic(fmt.Sprintf("%q is not a valid controller agent id", id))
	}
	return ControllerAgentTag{id: id}
}

// ParseControllerAgentTag parses a controller agent tag string.
func ParseControllerAgentTag(controllerAgentTag string) (ControllerAgentTag, error) {
	tag, err := ParseTag(controllerAgentTag)
	if err != nil {
		return ControllerAgentTag{}, err
	}
	et, ok := tag.(ControllerAgentTag)
	if !ok {
		return ControllerAgentTag{}, invalidTagError(controllerAgentTag, ControllerAgentTagKind)
	}
	return et, nil
}

// Number returns the controller agent number.
func (t ControllerAgentTag) Number() int {
	n, _ := strconv.Atoi(t.Id())
	return n
}

// String implements Tag.
func (t ControllerAgentTag) String() string { return t.Kind() + "-" + t.Id() }

// Kind implements Tag.
func (t ControllerAgentTag) Kind() string { return ControllerAgentTagKind }

// Id implements Tag.
func (t ControllerAgentTag) Id() string { return t.id }

// IsValidControllerAgent returns whether id is a valid controller agent id.
func IsValidControllerAgent(id string) bool {
	return validControllerAgentId.MatchString(id)
}