/
name.go
129 lines (117 loc) · 3.59 KB
/
name.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package lxdprofile
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/juju/errors"
)
// AppName here is used as the application prefix name. We can't use names.Juju
// as that changes depending on platform.
const AppName = "juju"
// Prefix is used to prefix all the lxd profile programmable profiles. If a
// profile doesn't have the prefix, then it will be removed when ensuring the
// the validity of the names (see LXDProfileNames)
var Prefix = fmt.Sprintf("%s-", AppName)
// Name returns a serialisable name that we can use to identify profiles
// juju-<model>-<application>-<charm-revision>
func Name(modelName, appName string, revision int) string {
return fmt.Sprintf("%s%s-%s-%d", Prefix, modelName, appName, revision)
}
// LXDProfileNames ensures that the LXD profile names are unique yet preserve
// the same order as the input. It removes certain profile names from the list,
// for example "default" profile name will be removed.
func LXDProfileNames(names []string) []string {
// ensure that the ones we have are unique
unique := make(map[string]int)
for k, v := range names {
if !IsValidName(v) {
continue
}
unique[v] = k
}
i := 0
unordered := make([]nameIndex, len(unique))
for k, v := range unique {
unordered[i] = nameIndex{
Name: k,
Index: v,
}
i++
}
sort.Slice(unordered, func(i, j int) bool {
return unordered[i].Index < unordered[j].Index
})
ordered := make([]string, len(unordered))
for k, v := range unordered {
ordered[k] = v.Name
}
return ordered
}
// IsValidName returns if the name of the lxd profile looks valid.
func IsValidName(name string) bool {
// doesn't contain the prefix
if !strings.HasPrefix(name, Prefix) {
return false
}
// it's required to have at least the following chars `x-x-0`
suffix := name[len(Prefix):]
if len(suffix) < 5 {
return false
}
// lastly check the last part is a number
lastHyphen := strings.LastIndex(suffix, "-")
revision := suffix[lastHyphen+1:]
_, err := strconv.Atoi(revision)
return err == nil
}
type nameIndex struct {
Name string
Index int
}
// ProfileRevision returns an int which is the charm revision of the given
// profile name.
func ProfileRevision(profile string) (int, error) {
if !IsValidName(profile) {
return 0, errors.BadRequestf("not a juju profile name: %q", profile)
}
split := strings.Split(profile, "-")
rev := split[len(split)-1:]
return strconv.Atoi(rev[0])
}
// ProfileReplaceRevision replaces the old revision with a new revision
// in the profile.
func ProfileReplaceRevision(profile string, rev int) (string, error) {
if !IsValidName(profile) {
return "", errors.BadRequestf("not a juju profile name: %q", profile)
}
split := strings.Split(profile, "-")
notRev := split[:len(split)-1]
return strings.Join(append(notRev, strconv.Itoa(rev)), "-"), nil
}
// MatchProfileNameByApp returns the first profile which matches the provided
// appName. No match returns an empty string.
// Assumes there is not more than one profile for the same application.
func MatchProfileNameByAppName(names []string, appName string) (string, error) {
if appName == "" {
return "", errors.BadRequestf("no application name specified")
}
var foundProfile string
for _, p := range LXDProfileNames(names) {
rev, err := ProfileRevision(p)
if err != nil {
// "Shouldn't" happen since we used LXDProfileNames...
if errors.IsBadRequest(err) {
continue
}
return "", err
}
if strings.HasSuffix(p, fmt.Sprintf("-%s-%d", appName, rev)) {
foundProfile = p
break
}
}
return foundProfile, nil
}