generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 6
/
dashboard.go
162 lines (133 loc) · 4.07 KB
/
dashboard.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"context"
"errors"
"fmt"
"regexp"
"github.com/flovouin/terraform-provider-metabase/metabase"
)
// Returns the collection ID as a string, possibly converting it from an integer.
func collectionIdAsString(id metabase.Collection_Id) (*string, error) {
idStr, err := id.AsCollectionId0()
if err == nil {
return &idStr, nil
}
idInt, err := id.AsCollectionId1()
if err != nil {
return nil, err
}
idStr = fmt.Sprintf("%d", idInt)
return &idStr, nil
}
// Returns whether the given collection matches any of the definitions.
func isCollectionInDefinitions(c metabase.Collection, definitions []collectionDefinition) (bool, error) {
collectionId, err := collectionIdAsString(c.Id)
if err != nil {
// An error is not returned because we assume that the conversion failed because the ID is a string.
return false, nil
}
for _, d := range definitions {
if d.Id != "" && *collectionId == d.Id {
return true, nil
}
if len(d.Name) > 0 {
matchName, err := regexp.MatchString(d.Name, c.Name)
if err != nil {
return false, err
}
if matchName {
return true, nil
}
}
}
return false, nil
}
// Returns the list of collections for which dashboards should be imported.
func listCollectionsToImport(ctx context.Context, config dashboardFilterConfig, client metabase.ClientWithResponses) ([]string, error) {
listResp, err := client.ListCollectionsWithResponse(ctx, &metabase.ListCollectionsParams{})
if err != nil {
return nil, err
}
if listResp.JSON200 == nil {
return nil, errors.New("received unexpected response when listing collections")
}
emptyIncludedCollectionsList := len(config.IncludedCollections) == 0
collectionIds := make([]string, 0, len(*listResp.JSON200))
for _, c := range *listResp.JSON200 {
id, err := collectionIdAsString(c.Id)
if err != nil {
continue
}
// Excluded collections take precedence over inclusion.
isExcluded, err := isCollectionInDefinitions(c, config.ExcludedCollections)
if err != nil {
return nil, err
}
if isExcluded {
continue
}
isIncluded := true
if !emptyIncludedCollectionsList {
isIncluded, err = isCollectionInDefinitions(c, config.IncludedCollections)
if err != nil {
return nil, err
}
}
if isIncluded {
collectionIds = append(collectionIds, *id)
}
}
return collectionIds, nil
}
// Fetches all dashboards from Metabase and returns the list of IDs of dashboards that should be imported.
func listDashboardsToImport(ctx context.Context, config dashboardFilterConfig, client metabase.ClientWithResponses) ([]int, error) {
if len(config.DashboardIds) > 0 {
return config.DashboardIds, nil
}
collectionIds, err := listCollectionsToImport(ctx, config, client)
if err != nil {
return nil, err
}
var nameRegexp *regexp.Regexp
if len(config.DashboardName) > 0 {
r, err := regexp.Compile(config.DashboardName)
if err != nil {
return nil, err
}
nameRegexp = r
}
var descriptionRegexp *regexp.Regexp
if len(config.DashboardDescription) > 0 {
r, err := regexp.Compile(config.DashboardDescription)
if err != nil {
return nil, err
}
descriptionRegexp = r
}
dashboardIds := make([]int, 0)
for _, collectionId := range collectionIds {
listResp, err := client.ListCollectionItemsWithResponse(ctx, collectionId, &metabase.ListCollectionItemsParams{
Models: &[]metabase.CollectionItemModel{metabase.CollectionItemModelDashboard},
})
if err != nil {
return nil, err
}
if listResp.JSON200 == nil {
return nil, errors.New("received unexpected response when listing dashboards")
}
if listResp.JSON200.Total != len(listResp.JSON200.Data) {
return nil, errors.New("received unexpected response when listing dashboards: pagination is not supported")
}
for _, dashboard := range listResp.JSON200.Data {
if nameRegexp != nil && !nameRegexp.MatchString(dashboard.Name) {
continue
}
if descriptionRegexp != nil &&
(dashboard.Description == nil || !descriptionRegexp.MatchString(*dashboard.Description)) {
continue
}
dashboardIds = append(dashboardIds, dashboard.Id)
}
}
return dashboardIds, nil
}