forked from raystack/guardian
-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource.go
67 lines (57 loc) · 1.23 KB
/
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
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
package grafana
import (
"strconv"
"github.com/goto/guardian/domain"
)
const (
ResourceTypeFolder = "folder"
ResourceTypeDashboard = "dashboard"
)
type Folder struct {
ID int `json:"id"`
UID string `json:"uid"`
Title string `json:"title"`
}
type Dashboard struct {
ID int `json:"id"`
UID string `json:"uid"`
Title string `json:"title"`
Slug string `json:"slug"`
FolderID int `json:"folderId"`
FolderUID string `json:"folderUid"`
FolderTitle string `json:"folderTitle"`
}
func (d *Dashboard) FromDomain(r *domain.Resource) error {
if r.Type != ResourceTypeDashboard {
return ErrInvalidResourceType
}
id, err := strconv.Atoi(r.URN)
if err != nil {
return err
}
d.ID = id
d.Title = r.Name
return nil
}
func (d *Dashboard) ToDomain() *domain.Resource {
details := map[string]interface{}{}
id := strconv.Itoa(d.ID)
if d.FolderID != 0 {
details["folder_id"] = d.FolderID
}
if d.FolderUID != "" {
details["folder_uid"] = d.FolderUID
}
if d.FolderTitle != "" {
details["folder_title"] = d.FolderTitle
}
if d.UID != "" {
details["uid"] = d.UID
}
return &domain.Resource{
Type: ResourceTypeDashboard,
Name: d.Title,
URN: id,
Details: details,
}
}