forked from andygrunwald/cachet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.go
More file actions
183 lines (153 loc) · 5.77 KB
/
Copy pathcomponents.go
File metadata and controls
183 lines (153 loc) · 5.77 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package cachet
import (
"fmt"
)
const (
// Docs: https://docs.cachethq.io/docs/component-statuses
// ComponentStatusOperational means "The component is working."
ComponentStatusOperational = 1
// ComponentStatusPerformanceIssues means "The component is experiencing some slowness."
ComponentStatusPerformanceIssues = 2
// ComponentStatusPartialOutage means "The component may not be working for everybody."
// This could be a geographical issue for example.
ComponentStatusPartialOutage = 3
// ComponentStatusMajorOutage means "The component is not working for anybody."
ComponentStatusMajorOutage = 4
)
// ComponentsService contains REST endpoints that belongs to cachet components.
type ComponentsService struct {
client *Client
}
// Component entity reflects one single component
type Component struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Link string `json:"link,omitempty"`
Status int `json:"status,omitempty"`
Order int `json:"order,omitempty"`
Enabled bool `json:"enabled"`
GroupID int `json:"group_id,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
DeletedAt string `json:"deleted_at,omitempty"`
StatusName string `json:"status_name,omitempty"`
}
// ComponentGroup entity reflects one single component group
type ComponentGroup struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Order int `json:"order,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}
// ComponentResponse reflects the response of /components call
type ComponentResponse struct {
Meta Meta `json:"meta,omitempty"`
Components []Component `json:"data,omitempty"`
}
// ComponentGroupResponse reflects the response of /components/groups call
type ComponentGroupResponse struct {
Meta Meta `json:"meta,omitempty"`
ComponentGroups []ComponentGroup `json:"data,omitempty"`
}
// componentApiResponse is an internal type to hide
// some the "data" nested level from the API.
// Some calls (e.g. Get or Create) return the component in the "data" key.
type componentAPIResponse struct {
Data *Component `json:"data"`
}
// componentGroupAPIResponse is an internal type to hide
// some the "data" nested level from the API.
// Some calls (e.g. Get or Create) return the component group in the "data" key.
type componentGroupAPIResponse struct {
Data *ComponentGroup `json:"data"`
}
// GetAll return all components that have been created.
//
// Docs: https://docs.cachethq.io/docs/get-components
func (s *ComponentsService) GetAll() (*ComponentResponse, *Response, error) {
u := "api/v1/components"
v := new(ComponentResponse)
resp, err := s.client.Call("GET", u, nil, v)
return v, resp, err
}
// Get return a single component.
//
// Docs: https://docs.cachethq.io/docs/get-a-component
func (s *ComponentsService) Get(id int) (*Component, *Response, error) {
u := fmt.Sprintf("api/v1/components/%d", id)
v := new(componentAPIResponse)
resp, err := s.client.Call("GET", u, nil, v)
return v.Data, resp, err
}
// Create a new component.
//
// Docs: https://docs.cachethq.io/docs/components
func (s *ComponentsService) Create(c *Component) (*Component, *Response, error) {
u := "api/v1/components"
v := new(componentAPIResponse)
resp, err := s.client.Call("POST", u, c, v)
return v.Data, resp, err
}
// Update updates a component.
//
// Docs: https://docs.cachethq.io/docs/update-a-component
func (s *ComponentsService) Update(id int, c *Component) (*Component, *Response, error) {
u := fmt.Sprintf("api/v1/components/%d", id)
v := new(componentAPIResponse)
resp, err := s.client.Call("PUT", u, c, v)
return v.Data, resp, err
}
// Delete deletes a component.
//
// Docs: https://docs.cachethq.io/docs/delete-a-component
func (s *ComponentsService) Delete(id int) (*Response, error) {
u := fmt.Sprintf("api/v1/components/%d", id)
resp, err := s.client.Call("DELETE", u, nil, nil)
return resp, err
}
// GetAllGroups return all component groups that have been created.
//
// Docs: https://docs.cachethq.io/docs/get-componentgroups
func (s *ComponentsService) GetAllGroups() (*ComponentGroupResponse, *Response, error) {
u := "api/v1/components/groups"
v := new(ComponentGroupResponse)
resp, err := s.client.Call("GET", u, nil, v)
return v, resp, err
}
// GetGroup return a single component group.
//
// Docs: https://docs.cachethq.io/docs/get-a-component-group
func (s *ComponentsService) GetGroup(id int) (*ComponentGroup, *Response, error) {
u := fmt.Sprintf("api/v1/components/groups/%d", id)
v := new(componentGroupAPIResponse)
resp, err := s.client.Call("GET", u, nil, v)
return v.Data, resp, err
}
// CreateGroup creates a new component group.
//
// Docs: https://docs.cachethq.io/docs/post-componentgroups
func (s *ComponentsService) CreateGroup(c *ComponentGroup) (*ComponentGroup, *Response, error) {
u := "api/v1/components/groups"
v := new(componentGroupAPIResponse)
resp, err := s.client.Call("POST", u, c, v)
return v.Data, resp, err
}
// UpdateGroup updates a component group.
//
// Docs: https://docs.cachethq.io/docs/put-component-group
func (s *ComponentsService) UpdateGroup(id int, c *ComponentGroup) (*ComponentGroup, *Response, error) {
u := fmt.Sprintf("api/v1/components/groups/%d", id)
v := new(componentGroupAPIResponse)
resp, err := s.client.Call("PUT", u, c, v)
return v.Data, resp, err
}
// DeleteGroup deletes a component group.
//
// Docs: https://docs.cachethq.io/docs/delete-component-group
func (s *ComponentsService) DeleteGroup(id int) (*Response, error) {
u := fmt.Sprintf("api/v1/components/groups/%d", id)
resp, err := s.client.Call("DELETE", u, nil, nil)
return resp, err
}