-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcomponents.go
131 lines (111 loc) · 3.97 KB
/
components.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
package cachet
import (
"fmt"
)
const (
// Docs: https://docs.cachethq.io/docs/component-statuses
// ComponentStatusUnknown means "The component's status is not known."
ComponentStatusUnknown = 0
// 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
}
// Tag ...
type Tag struct {
Tag string `json:"tag,omitempty"`
}
// 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,omitempty"`
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"`
//Tags Tag `json:"tags,omitempty"`
}
// ComponentResponse reflects the response of /components call
type ComponentResponse struct {
Meta Meta `json:"meta,omitempty"`
Components []Component `json:"data,omitempty"`
}
// ComponentsQueryParams contains fields to filter returned results
type ComponentsQueryParams struct {
ID int `url:"id,omitempty"`
Name string `url:"name,omitempty"`
Status int `url:"status,omitempty"`
Order int `url:"order,omitempty"`
Enabled bool `url:"enabled,omitempty"`
GroupID int `url:"group_id,omitempty"`
QueryOptions
}
// 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"`
}
// GetAll return all components that have been created.
//
// Docs: https://docs.cachethq.io/reference#get-components
func (s *ComponentsService) GetAll(filter *ComponentsQueryParams) (*ComponentResponse, *Response, error) {
u := "api/v1/components"
v := new(ComponentResponse)
u, err := addOptions(u, filter)
if err != nil {
return nil, nil, err
}
resp, err := s.client.Call("GET", u, nil, v)
return v, resp, err
}
// Get return a single component.
//
// Docs: https://docs.cachethq.io/reference#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/reference#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
}