-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
entities.go
144 lines (126 loc) · 5.66 KB
/
entities.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package cluster
import (
"fmt"
"net/url"
"strings"
"github.com/lxc/incus/internal/version"
)
// Numeric type codes identifying different kind of entities.
const (
TypeContainer = 0
TypeImage = 1
TypeProfile = 2
TypeProject = 3
TypeCertificate = 4
TypeInstance = 5
TypeInstanceBackup = 6
TypeInstanceSnapshot = 7
TypeNetwork = 8
TypeNetworkACL = 9
TypeNode = 10
TypeOperation = 11
TypeStoragePool = 12
TypeStorageVolume = 13
TypeStorageVolumeBackup = 14
TypeStorageVolumeSnapshot = 15
TypeWarning = 16
TypeClusterGroup = 17
TypeStorageBucket = 18
)
// EntityNames associates an entity code to its name.
var EntityNames = map[int]string{
TypeContainer: "container",
TypeImage: "image",
TypeProfile: "profile",
TypeProject: "project",
TypeCertificate: "certificate",
TypeInstance: "instance",
TypeInstanceBackup: "instance backup",
TypeInstanceSnapshot: "instance snapshot",
TypeNetwork: "network",
TypeNetworkACL: "network acl",
TypeNode: "node",
TypeOperation: "operation",
TypeStoragePool: "storage pool",
TypeStorageVolume: "storage volume",
TypeStorageVolumeBackup: "storage volume backup",
TypeStorageVolumeSnapshot: "storage volume snapshot",
TypeStorageBucket: "storage bucket",
TypeWarning: "warning",
TypeClusterGroup: "cluster group",
}
// EntityTypes associates an entity name to its type code.
var EntityTypes = map[string]int{}
// EntityURIs associates an entity code to its URI pattern.
var EntityURIs = map[int]string{
TypeContainer: "/" + version.APIVersion + "/containers/%s?project=%s",
TypeImage: "/" + version.APIVersion + "/images/%s?project=%s",
TypeProfile: "/" + version.APIVersion + "/profiles/%s?project=%s",
TypeProject: "/" + version.APIVersion + "/projects/%s",
TypeCertificate: "/" + version.APIVersion + "/certificates/%s",
TypeInstance: "/" + version.APIVersion + "/instances/%s?project=%s",
TypeInstanceBackup: "/" + version.APIVersion + "/instances/%s/backups/%s?project=%s",
TypeInstanceSnapshot: "/" + version.APIVersion + "/instances/%s/snapshots/%s?project=%s",
TypeNetwork: "/" + version.APIVersion + "/networks/%s?project=%s",
TypeNetworkACL: "/" + version.APIVersion + "/network-acls/%s?project=%s",
TypeNode: "/" + version.APIVersion + "/cluster/members/%s",
TypeOperation: "/" + version.APIVersion + "/operations/%s",
TypeStoragePool: "/" + version.APIVersion + "/storage-pools/%s",
TypeStorageVolume: "/" + version.APIVersion + "/storage-pools/%s/volumes/%s/%s?project=%s",
TypeStorageVolumeBackup: "/" + version.APIVersion + "/storage-pools/%s/volumes/%s/%s/backups/%s?project=%s",
TypeStorageVolumeSnapshot: "/" + version.APIVersion + "/storage-pools/%s/volumes/%s/%s/snapshots/%s?project=%s",
TypeStorageBucket: "/" + version.APIVersion + "/storage-pools/%s/buckets/%s?project=%s",
TypeWarning: "/" + version.APIVersion + "/warnings/%s",
TypeClusterGroup: "/" + version.APIVersion + "/cluster/groups/%s",
}
func init() {
for code, name := range EntityNames {
EntityTypes[name] = code
}
}
// URLToEntityType parses a raw URL string and returns the entity type, the project, the location and the path arguments. The
// returned project is set to "default" if it is not present (unless the entity type is TypeProject, in which case it is
// set to the value of the path parameter). An error is returned if the URL is not recognised.
func URLToEntityType(rawURL string) (int, string, string, []string, error) {
u, err := url.Parse(rawURL)
if err != nil {
return -1, "", "", nil, fmt.Errorf("Failed to parse url %q into an entity type: %w", rawURL, err)
}
// We need to space separate the path because fmt.Sscanf uses this as a delimiter.
spaceSeparatedURLPath := strings.Replace(u.Path, "/", " / ", -1)
for entityType, entityURI := range EntityURIs {
entityPath, _, _ := strings.Cut(entityURI, "?")
// Skip if we don't have the same number of slashes.
if strings.Count(entityPath, "/") != strings.Count(u.Path, "/") {
continue
}
spaceSeparatedEntityPath := strings.Replace(entityPath, "/", " / ", -1)
// Make an []any for the number of expected path arguments and set each value in the slice to a *string.
nPathArgs := strings.Count(spaceSeparatedEntityPath, "%s")
pathArgsAny := make([]any, 0, nPathArgs)
for i := 0; i < nPathArgs; i++ {
var pathComponentStr string
pathArgsAny = append(pathArgsAny, &pathComponentStr)
}
// Scan the given URL into the entity URL. If we found all the expected path arguments and there
// are no errors we have a match.
nFound, err := fmt.Sscanf(spaceSeparatedURLPath, spaceSeparatedEntityPath, pathArgsAny...)
if nFound == nPathArgs && err == nil {
pathArgs := make([]string, 0, nPathArgs)
for _, pathArgAny := range pathArgsAny {
pathArgPtr := pathArgAny.(*string)
pathArgs = append(pathArgs, *pathArgPtr)
}
projectName := u.Query().Get("project")
if projectName == "" {
projectName = "default"
}
location := u.Query().Get("target")
if entityType == TypeProject {
return TypeProject, pathArgs[0], location, pathArgs, nil
}
return entityType, projectName, location, pathArgs, nil
}
}
return -1, "", "", nil, fmt.Errorf("Unknown entity URL %q", u.String())
}