forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
40 lines (35 loc) · 792 Bytes
/
resource.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
package suggest
import (
"path"
"regexp"
)
var (
nsRe = regexp.MustCompile("{namespace}")
)
// Resource represents a Sensu resource
type Resource struct {
Group string
Name string
Path string
Fields []Field
}
// URIPath given a namespace returns the API path used to get/list/put/delete
// the resource.
func (r *Resource) URIPath(ns string) string {
if r.Path != "" {
return nsRe.ReplaceAllString(r.Path, ns)
}
if r.Group == "core/v2" {
return path.Join("/", "api", r.Group, "namespaces", ns, r.Name)
}
return path.Join("/", "api", r.Group, r.Name)
}
// LookupField uses given ref to find the appropriate field.
func (r *Resource) LookupField(ref RefComponents) Field {
for _, f := range r.Fields {
if f.Matches(ref.FieldPath) {
return f
}
}
return nil
}