]>
Commit | Line | Data |
---|---|---|
2ad41f52 AG |
1 | package statuscake |
2 | ||
3 | import ( | |
4 | "encoding/json" | |
5 | "fmt" | |
6 | "net/url" | |
7 | "strings" | |
8 | "strconv" | |
9 | ||
10 | "github.com/google/go-querystring/query" | |
11 | ) | |
12 | ||
13 | //Ssl represent the data received by the API with GET | |
14 | type Ssl struct { | |
15 | ID string `json:"id" url:"id,omitempty"` | |
16 | Domain string `json:"domain" url:"domain,omitempty"` | |
17 | Checkrate int `json:"checkrate" url:"checkrate,omitempty"` | |
18 | ContactGroupsC string ` url:"contact_groups,omitempty"` | |
19 | AlertAt string `json:"alert_at" url:"alert_at,omitempty"` | |
20 | AlertReminder bool `json:"alert_reminder" url:"alert_expiry,omitempty"` | |
21 | AlertExpiry bool `json:"alert_expiry" url:"alert_reminder,omitempty"` | |
22 | AlertBroken bool `json:"alert_broken" url:"alert_broken,omitempty"` | |
23 | AlertMixed bool `json:"alert_mixed" url:"alert_mixed,omitempty"` | |
24 | Paused bool `json:"paused"` | |
25 | IssuerCn string `json:"issuer_cn"` | |
26 | CertScore string `json:"cert_score"` | |
27 | CipherScore string `json:"cipher_score"` | |
28 | CertStatus string `json:"cert_status"` | |
29 | Cipher string `json:"cipher"` | |
30 | ValidFromUtc string `json:"valid_from_utc"` | |
31 | ValidUntilUtc string `json:"valid_until_utc"` | |
32 | MixedContent []map[string]string `json:"mixed_content"` | |
33 | Flags map[string]bool `json:"flags"` | |
34 | ContactGroups []string `json:"contact_groups"` | |
35 | LastReminder int `json:"last_reminder"` | |
36 | LastUpdatedUtc string `json:"last_updated_utc"` | |
37 | } | |
38 | ||
39 | //PartialSsl represent a ssl test creation or modification | |
40 | type PartialSsl struct { | |
41 | ID int | |
42 | Domain string | |
43 | Checkrate string | |
44 | ContactGroupsC string | |
45 | AlertAt string | |
46 | AlertExpiry bool | |
47 | AlertReminder bool | |
48 | AlertBroken bool | |
49 | AlertMixed bool | |
50 | } | |
51 | ||
52 | type createSsl struct { | |
53 | ID int `url:"id,omitempty"` | |
54 | Domain string `url:"domain" json:"domain"` | |
55 | Checkrate string `url:"checkrate" json:"checkrate"` | |
56 | ContactGroupsC string `url:"contact_groups" json:"contact_groups"` | |
57 | AlertAt string `url:"alert_at" json:"alert_at"` | |
58 | AlertExpiry bool `url:"alert_expiry" json:"alert_expiry"` | |
59 | AlertReminder bool `url:"alert_reminder" json:"alert_reminder"` | |
60 | AlertBroken bool `url:"alert_broken" json:"alert_broken"` | |
61 | AlertMixed bool `url:"alert_mixed" json:"alert_mixed"` | |
62 | } | |
63 | ||
64 | type updateSsl struct { | |
65 | ID int `url:"id"` | |
66 | Domain string `url:"domain" json:"domain"` | |
67 | Checkrate string `url:"checkrate" json:"checkrate"` | |
68 | ContactGroupsC string `url:"contact_groups" json:"contact_groups"` | |
69 | AlertAt string `url:"alert_at" json:"alert_at"` | |
70 | AlertExpiry bool `url:"alert_expiry" json:"alert_expiry"` | |
71 | AlertReminder bool `url:"alert_reminder" json:"alert_reminder"` | |
72 | AlertBroken bool `url:"alert_broken" json:"alert_broken"` | |
73 | AlertMixed bool `url:"alert_mixed" json:"alert_mixed"` | |
74 | } | |
75 | ||
76 | ||
77 | type sslUpdateResponse struct { | |
78 | Success bool `json:"Success"` | |
79 | Message interface{} `json:"Message"` | |
80 | } | |
81 | ||
82 | type sslCreateResponse struct { | |
83 | Success bool `json:"Success"` | |
84 | Message interface{} `json:"Message"` | |
85 | Input createSsl `json:"Input"` | |
86 | } | |
87 | ||
88 | //Ssls represent the actions done wit the API | |
89 | type Ssls interface { | |
90 | All() ([]*Ssl, error) | |
91 | completeSsl(*PartialSsl) (*Ssl, error) | |
92 | Detail(string) (*Ssl, error) | |
93 | Update(*PartialSsl) (*Ssl, error) | |
94 | UpdatePartial(*PartialSsl) (*PartialSsl, error) | |
95 | Delete(ID string) error | |
96 | CreatePartial(*PartialSsl) (*PartialSsl, error) | |
97 | Create(*PartialSsl) (*Ssl, error) | |
98 | } | |
99 | ||
100 | func consolidateSsl(s *Ssl) { | |
101 | (*s).ContactGroupsC = strings.Trim(strings.Join(strings.Fields(fmt.Sprint((*s).ContactGroups)), ","), "[]") | |
102 | } | |
103 | ||
104 | func findSsl(responses []*Ssl, id string) (*Ssl, error) { | |
105 | var response *Ssl | |
106 | for _, elem := range responses { | |
107 | if (*elem).ID == id { | |
108 | return elem, nil | |
109 | } | |
110 | } | |
111 | return response, fmt.Errorf("%s Not found", id) | |
112 | } | |
113 | ||
114 | func (tt *ssls) completeSsl(s *PartialSsl) (*Ssl, error) { | |
115 | full, err := tt.Detail(strconv.Itoa((*s).ID)) | |
116 | if err != nil { | |
117 | return nil, err | |
118 | } | |
119 | (*full).ContactGroups = strings.Split((*s).ContactGroupsC,",") | |
120 | return full, nil | |
121 | } | |
122 | ||
123 | //Partial return a PartialSsl corresponding to the Ssl | |
124 | func Partial(s *Ssl) (*PartialSsl,error) { | |
125 | if s==nil { | |
126 | return nil,fmt.Errorf("s is nil") | |
127 | } | |
128 | id,err:=strconv.Atoi(s.ID) | |
129 | if(err!=nil){ | |
130 | return nil,err | |
131 | } | |
132 | return &PartialSsl{ | |
133 | ID: id, | |
134 | Domain: s.Domain, | |
135 | Checkrate: strconv.Itoa(s.Checkrate), | |
136 | ContactGroupsC: s.ContactGroupsC, | |
137 | AlertReminder: s.AlertReminder, | |
138 | AlertExpiry: s.AlertExpiry, | |
139 | AlertBroken: s.AlertBroken, | |
140 | AlertMixed: s.AlertMixed, | |
141 | AlertAt: s.AlertAt, | |
142 | },nil | |
143 | ||
144 | } | |
145 | ||
146 | type ssls struct { | |
147 | client apiClient | |
148 | } | |
149 | ||
150 | //NewSsls return a new ssls | |
151 | func NewSsls(c apiClient) Ssls { | |
152 | return &ssls{ | |
153 | client: c, | |
154 | } | |
155 | } | |
156 | ||
157 | //All return a list of all the ssl from the API | |
158 | func (tt *ssls) All() ([]*Ssl, error) { | |
159 | rawResponse, err := tt.client.get("/SSL", nil) | |
160 | if err != nil { | |
161 | return nil, fmt.Errorf("Error getting StatusCake Ssl: %s", err.Error()) | |
162 | } | |
163 | var getResponse []*Ssl | |
164 | err = json.NewDecoder(rawResponse.Body).Decode(&getResponse) | |
165 | if err != nil { | |
166 | return nil, err | |
167 | } | |
168 | ||
169 | for ssl := range getResponse { | |
170 | consolidateSsl(getResponse[ssl]) | |
171 | } | |
172 | ||
173 | return getResponse, err | |
174 | } | |
175 | ||
176 | //Detail return the ssl corresponding to the id | |
177 | func (tt *ssls) Detail(id string) (*Ssl, error) { | |
178 | responses, err := tt.All() | |
179 | if err != nil { | |
180 | return nil, err | |
181 | } | |
182 | mySsl, errF := findSsl(responses, id) | |
183 | if errF != nil { | |
184 | return nil, errF | |
185 | } | |
186 | return mySsl, nil | |
187 | } | |
188 | ||
189 | //Update update the API with s and create one if s.ID=0 then return the corresponding Ssl | |
190 | func (tt *ssls) Update(s *PartialSsl) (*Ssl, error) { | |
191 | var err error | |
192 | s, err = tt.UpdatePartial(s) | |
193 | if err!= nil { | |
194 | return nil, err | |
195 | } | |
196 | return tt.completeSsl(s) | |
197 | } | |
198 | ||
199 | //UpdatePartial update the API with s and create one if s.ID=0 then return the corresponding PartialSsl | |
200 | func (tt *ssls) UpdatePartial(s *PartialSsl) (*PartialSsl, error) { | |
201 | ||
202 | if((*s).ID == 0){ | |
203 | return tt.CreatePartial(s) | |
204 | } | |
205 | var v url.Values | |
206 | ||
207 | v, _ = query.Values(updateSsl(*s)) | |
208 | ||
209 | rawResponse, err := tt.client.put("/SSL/Update", v) | |
210 | if err != nil { | |
211 | return nil, fmt.Errorf("Error creating StatusCake Ssl: %s", err.Error()) | |
212 | } | |
213 | ||
214 | var updateResponse sslUpdateResponse | |
215 | err = json.NewDecoder(rawResponse.Body).Decode(&updateResponse) | |
216 | if err != nil { | |
217 | return nil, err | |
218 | } | |
219 | ||
220 | if !updateResponse.Success { | |
221 | return nil, fmt.Errorf("%s", updateResponse.Message.(string)) | |
222 | } | |
223 | ||
224 | ||
225 | return s, nil | |
226 | } | |
227 | ||
228 | //Delete delete the ssl which ID is id | |
229 | func (tt *ssls) Delete(id string) error { | |
230 | _, err := tt.client.delete("/SSL/Update", url.Values{"id": {fmt.Sprint(id)}}) | |
231 | if err != nil { | |
232 | return err | |
233 | } | |
234 | ||
235 | return nil | |
236 | } | |
237 | ||
238 | //Create create the ssl whith the data in s and return the Ssl created | |
239 | func (tt *ssls) Create(s *PartialSsl) (*Ssl, error) { | |
240 | var err error | |
241 | s, err = tt.CreatePartial(s) | |
242 | if err!= nil { | |
243 | return nil, err | |
244 | } | |
245 | return tt.completeSsl(s) | |
246 | } | |
247 | ||
248 | //CreatePartial create the ssl whith the data in s and return the PartialSsl created | |
249 | func (tt *ssls) CreatePartial(s *PartialSsl) (*PartialSsl, error) { | |
250 | (*s).ID=0 | |
251 | var v url.Values | |
252 | v, _ = query.Values(createSsl(*s)) | |
253 | ||
254 | rawResponse, err := tt.client.put("/SSL/Update", v) | |
255 | if err != nil { | |
256 | return nil, fmt.Errorf("Error creating StatusCake Ssl: %s", err.Error()) | |
257 | } | |
258 | ||
259 | var createResponse sslCreateResponse | |
260 | err = json.NewDecoder(rawResponse.Body).Decode(&createResponse) | |
261 | if err != nil { | |
262 | return nil, err | |
263 | } | |
264 | ||
265 | if !createResponse.Success { | |
266 | return nil, fmt.Errorf("%s", createResponse.Message.(string)) | |
267 | } | |
268 | *s = PartialSsl(createResponse.Input) | |
269 | (*s).ID = int(createResponse.Message.(float64)) | |
270 | ||
271 | return s,nil | |
272 | } | |
273 |