forked from OpsLevel/opslevel-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtools.go
130 lines (115 loc) · 3.63 KB
/
tools.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
package opslevel
import (
"github.com/shurcooL/graphql"
)
type ToolCategory string
const (
ToolCategoryAdmin ToolCategory = "admin"
ToolCategoryCode ToolCategory = "code"
ToolCategoryContinuousIntegration ToolCategory = "continuous_integration"
ToolCategoryDeployment ToolCategory = "deployment"
ToolCategoryErrors ToolCategory = "errors"
ToolCategoryFeatureFlag ToolCategory = "feature_flag"
ToolCategoryHealthChecks ToolCategory = "health_checks"
ToolCategoryIncidents ToolCategory = "incidents"
ToolCategoryLogs ToolCategory = "logs"
ToolCategoryMetrics ToolCategory = "metrics"
ToolCategoryOrchestrator ToolCategory = "orchestrator"
ToolCategoryRunbooks ToolCategory = "runbooks"
ToolCategoryStatusPage ToolCategory = "status_page"
ToolCategoryWiki ToolCategory = "wiki"
ToolCategoryOther ToolCategory = "other"
)
type Tool struct {
Category ToolCategory
CategoryAlias graphql.String `json:",omitempty"`
DisplayName graphql.String
Environment graphql.String `json:",omitempty"`
Id graphql.ID `json:",omitempty"`
Service Service `json:",omitempty"`
Url graphql.String
// TODO: Not sure why these fields don't work during ToolCreateInput
//DisplayCategory graphql.String `json:",omitempty"`
//Locked graphql.Boolean `json:",omitempty"`
//PlainId graphql.Int `json:",omitempty"`
}
type ToolCreateInput struct {
Category ToolCategory `json:"category"`
DisplayName string `json:"displayName"`
Url string `json:"url"`
Environment string `json:"environment,omitempty"`
ServiceId graphql.ID `json:"serviceId,omitempty"`
ServiceAlias string `json:"serviceAlias,omitempty"`
}
//#region Create
func (client *Client) CreateTool(input ToolCreateInput) (*Tool, error) {
// TODO: validate - Category, DisplayName & Url are non nil - or throw err
var m struct {
Payload struct {
Tool Tool
Errors []OpsLevelErrors
} `graphql:"toolCreate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
if err := client.Mutate(&m, v); err != nil {
return nil, err
}
return &m.Payload.Tool, FormatErrors(m.Payload.Errors)
}
//#endregion
//#region Retrieve
type ListToolQuery struct {
Account struct {
Tools struct {
Nodes []Tool
PageInfo PageInfo
TotalCount graphql.Int
} `graphql:"tools(after: $after, first: $first, service: $service)"`
}
}
func (q *ListToolQuery) Query(client *Client, service graphql.ID) error {
var subQ ListToolQuery
v := PayloadVariables{
"after": q.Account.Tools.PageInfo.End,
"first": graphql.Int(100),
"service": service,
}
if err := client.Query(&subQ, v); err != nil {
return err
}
if subQ.Account.Tools.PageInfo.HasNextPage {
subQ.Query(client, service)
}
for _, tool := range subQ.Account.Tools.Nodes {
q.Account.Tools.Nodes = append(q.Account.Tools.Nodes, tool)
}
return nil
}
func (client *Client) ListTools(service graphql.ID) ([]Tool, error) {
q := ListToolQuery{}
if err := q.Query(client, service); err != nil {
return []Tool{}, err
}
return q.Account.Tools.Nodes, nil
}
func (client *Client) GetToolCount(service graphql.ID) (int, error) {
var q struct {
Account struct {
Tools struct {
Nodes []Tool
PageInfo PageInfo
TotalCount graphql.Int
} `graphql:"tools(service: $service)"`
}
}
v := PayloadVariables{
"service": service,
}
if err := client.Query(&q, v); err != nil {
return 0, err
}
return int(q.Account.Tools.TotalCount), nil
}
//#endregion