10 "github.com/google/go-querystring/query"
13 //Ssl represent the data received by the API with GET
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"`
39 //PartialSsl represent a ssl test creation or modification
40 type PartialSsl struct {
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"`
64 type updateSsl struct {
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"`
77 type sslUpdateResponse struct {
78 Success bool `json:"Success"`
79 Message interface{} `json:"Message"`
82 type sslCreateResponse struct {
83 Success bool `json:"Success"`
84 Message interface{} `json:"Message"`
85 Input createSsl `json:"Input"`
88 //Ssls represent the actions done wit the API
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)
100 func consolidateSsl(s *Ssl) {
101 (*s).ContactGroupsC = strings.Trim(strings.Join(strings.Fields(fmt.Sprint((*s).ContactGroups)), ","), "[]")
104 func findSsl(responses []*Ssl, id string) (*Ssl, error) {
106 for _, elem := range responses {
107 if (*elem).ID == id {
111 return response, fmt.Errorf("%s Not found", id)
114 func (tt *ssls) completeSsl(s *PartialSsl) (*Ssl, error) {
115 full, err := tt.Detail(strconv.Itoa((*s).ID))
119 (*full).ContactGroups = strings.Split((*s).ContactGroupsC,",")
123 //Partial return a PartialSsl corresponding to the Ssl
124 func Partial(s *Ssl) (*PartialSsl,error) {
126 return nil,fmt.Errorf("s is nil")
128 id,err:=strconv.Atoi(s.ID)
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,
150 //NewSsls return a new ssls
151 func NewSsls(c apiClient) Ssls {
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)
161 return nil, fmt.Errorf("Error getting StatusCake Ssl: %s", err.Error())
163 var getResponse []*Ssl
164 err = json.NewDecoder(rawResponse.Body).Decode(&getResponse)
169 for ssl := range getResponse {
170 consolidateSsl(getResponse[ssl])
173 return getResponse, err
176 //Detail return the ssl corresponding to the id
177 func (tt *ssls) Detail(id string) (*Ssl, error) {
178 responses, err := tt.All()
182 mySsl, errF := findSsl(responses, id)
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) {
192 s, err = tt.UpdatePartial(s)
196 return tt.completeSsl(s)
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) {
203 return tt.CreatePartial(s)
207 v, _ = query.Values(updateSsl(*s))
209 rawResponse, err := tt.client.put("/SSL/Update", v)
211 return nil, fmt.Errorf("Error creating StatusCake Ssl: %s", err.Error())
214 var updateResponse sslUpdateResponse
215 err = json.NewDecoder(rawResponse.Body).Decode(&updateResponse)
220 if !updateResponse.Success {
221 return nil, fmt.Errorf("%s", updateResponse.Message.(string))
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)}})
238 //Create create the ssl whith the data in s and return the Ssl created
239 func (tt *ssls) Create(s *PartialSsl) (*Ssl, error) {
241 s, err = tt.CreatePartial(s)
245 return tt.completeSsl(s)
248 //CreatePartial create the ssl whith the data in s and return the PartialSsl created
249 func (tt *ssls) CreatePartial(s *PartialSsl) (*PartialSsl, error) {
252 v, _ = query.Values(createSsl(*s))
254 rawResponse, err := tt.client.put("/SSL/Update", v)
256 return nil, fmt.Errorf("Error creating StatusCake Ssl: %s", err.Error())
259 var createResponse sslCreateResponse
260 err = json.NewDecoder(rawResponse.Body).Decode(&createResponse)
265 if !createResponse.Success {
266 return nil, fmt.Errorf("%s", createResponse.Message.(string))
268 *s = PartialSsl(createResponse.Input)
269 (*s).ID = int(createResponse.Message.(float64))