cloudstack/AlertService.go (727 lines of code) (raw):

// // Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you under the Apache License, Version 2.0 (the // "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, // software distributed under the License is distributed on an // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. // package cloudstack import ( "encoding/json" "fmt" "net/url" "strconv" "strings" ) type AlertServiceIface interface { ArchiveAlerts(p *ArchiveAlertsParams) (*ArchiveAlertsResponse, error) NewArchiveAlertsParams() *ArchiveAlertsParams DeleteAlerts(p *DeleteAlertsParams) (*DeleteAlertsResponse, error) NewDeleteAlertsParams() *DeleteAlertsParams GenerateAlert(p *GenerateAlertParams) (*GenerateAlertResponse, error) NewGenerateAlertParams(description string, name string, alertType int) *GenerateAlertParams ListAlerts(p *ListAlertsParams) (*ListAlertsResponse, error) NewListAlertsParams() *ListAlertsParams GetAlertID(name string, opts ...OptionFunc) (string, int, error) GetAlertByName(name string, opts ...OptionFunc) (*Alert, int, error) GetAlertByID(id string, opts ...OptionFunc) (*Alert, int, error) ListAlertTypes(p *ListAlertTypesParams) (*ListAlertTypesResponse, error) NewListAlertTypesParams() *ListAlertTypesParams } type ArchiveAlertsParams struct { p map[string]interface{} } func (p *ArchiveAlertsParams) toURLValues() url.Values { u := url.Values{} if p.p == nil { return u } if v, found := p.p["enddate"]; found { u.Set("enddate", v.(string)) } if v, found := p.p["ids"]; found { vv := strings.Join(v.([]string), ",") u.Set("ids", vv) } if v, found := p.p["startdate"]; found { u.Set("startdate", v.(string)) } if v, found := p.p["type"]; found { u.Set("type", v.(string)) } return u } func (p *ArchiveAlertsParams) SetEnddate(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["enddate"] = v } func (p *ArchiveAlertsParams) ResetEnddate() { if p.p != nil && p.p["enddate"] != nil { delete(p.p, "enddate") } } func (p *ArchiveAlertsParams) GetEnddate() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["enddate"].(string) return value, ok } func (p *ArchiveAlertsParams) SetIds(v []string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["ids"] = v } func (p *ArchiveAlertsParams) ResetIds() { if p.p != nil && p.p["ids"] != nil { delete(p.p, "ids") } } func (p *ArchiveAlertsParams) GetIds() ([]string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["ids"].([]string) return value, ok } func (p *ArchiveAlertsParams) SetStartdate(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["startdate"] = v } func (p *ArchiveAlertsParams) ResetStartdate() { if p.p != nil && p.p["startdate"] != nil { delete(p.p, "startdate") } } func (p *ArchiveAlertsParams) GetStartdate() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["startdate"].(string) return value, ok } func (p *ArchiveAlertsParams) SetType(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["type"] = v } func (p *ArchiveAlertsParams) ResetType() { if p.p != nil && p.p["type"] != nil { delete(p.p, "type") } } func (p *ArchiveAlertsParams) GetType() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["type"].(string) return value, ok } // You should always use this function to get a new ArchiveAlertsParams instance, // as then you are sure you have configured all required params func (s *AlertService) NewArchiveAlertsParams() *ArchiveAlertsParams { p := &ArchiveAlertsParams{} p.p = make(map[string]interface{}) return p } // Archive one or more alerts. func (s *AlertService) ArchiveAlerts(p *ArchiveAlertsParams) (*ArchiveAlertsResponse, error) { resp, err := s.cs.newRequest("archiveAlerts", p.toURLValues()) if err != nil { return nil, err } var r ArchiveAlertsResponse if err := json.Unmarshal(resp, &r); err != nil { return nil, err } return &r, nil } type ArchiveAlertsResponse struct { Displaytext string `json:"displaytext"` JobID string `json:"jobid"` Jobstatus int `json:"jobstatus"` Success bool `json:"success"` } func (r *ArchiveAlertsResponse) UnmarshalJSON(b []byte) error { var m map[string]interface{} err := json.Unmarshal(b, &m) if err != nil { return err } if success, ok := m["success"].(string); ok { m["success"] = success == "true" b, err = json.Marshal(m) if err != nil { return err } } if ostypeid, ok := m["ostypeid"].(float64); ok { m["ostypeid"] = strconv.Itoa(int(ostypeid)) b, err = json.Marshal(m) if err != nil { return err } } type alias ArchiveAlertsResponse return json.Unmarshal(b, (*alias)(r)) } type DeleteAlertsParams struct { p map[string]interface{} } func (p *DeleteAlertsParams) toURLValues() url.Values { u := url.Values{} if p.p == nil { return u } if v, found := p.p["enddate"]; found { u.Set("enddate", v.(string)) } if v, found := p.p["ids"]; found { vv := strings.Join(v.([]string), ",") u.Set("ids", vv) } if v, found := p.p["startdate"]; found { u.Set("startdate", v.(string)) } if v, found := p.p["type"]; found { u.Set("type", v.(string)) } return u } func (p *DeleteAlertsParams) SetEnddate(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["enddate"] = v } func (p *DeleteAlertsParams) ResetEnddate() { if p.p != nil && p.p["enddate"] != nil { delete(p.p, "enddate") } } func (p *DeleteAlertsParams) GetEnddate() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["enddate"].(string) return value, ok } func (p *DeleteAlertsParams) SetIds(v []string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["ids"] = v } func (p *DeleteAlertsParams) ResetIds() { if p.p != nil && p.p["ids"] != nil { delete(p.p, "ids") } } func (p *DeleteAlertsParams) GetIds() ([]string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["ids"].([]string) return value, ok } func (p *DeleteAlertsParams) SetStartdate(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["startdate"] = v } func (p *DeleteAlertsParams) ResetStartdate() { if p.p != nil && p.p["startdate"] != nil { delete(p.p, "startdate") } } func (p *DeleteAlertsParams) GetStartdate() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["startdate"].(string) return value, ok } func (p *DeleteAlertsParams) SetType(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["type"] = v } func (p *DeleteAlertsParams) ResetType() { if p.p != nil && p.p["type"] != nil { delete(p.p, "type") } } func (p *DeleteAlertsParams) GetType() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["type"].(string) return value, ok } // You should always use this function to get a new DeleteAlertsParams instance, // as then you are sure you have configured all required params func (s *AlertService) NewDeleteAlertsParams() *DeleteAlertsParams { p := &DeleteAlertsParams{} p.p = make(map[string]interface{}) return p } // Delete one or more alerts. func (s *AlertService) DeleteAlerts(p *DeleteAlertsParams) (*DeleteAlertsResponse, error) { resp, err := s.cs.newRequest("deleteAlerts", p.toURLValues()) if err != nil { return nil, err } var r DeleteAlertsResponse if err := json.Unmarshal(resp, &r); err != nil { return nil, err } return &r, nil } type DeleteAlertsResponse struct { Displaytext string `json:"displaytext"` JobID string `json:"jobid"` Jobstatus int `json:"jobstatus"` Success bool `json:"success"` } func (r *DeleteAlertsResponse) UnmarshalJSON(b []byte) error { var m map[string]interface{} err := json.Unmarshal(b, &m) if err != nil { return err } if success, ok := m["success"].(string); ok { m["success"] = success == "true" b, err = json.Marshal(m) if err != nil { return err } } if ostypeid, ok := m["ostypeid"].(float64); ok { m["ostypeid"] = strconv.Itoa(int(ostypeid)) b, err = json.Marshal(m) if err != nil { return err } } type alias DeleteAlertsResponse return json.Unmarshal(b, (*alias)(r)) } type GenerateAlertParams struct { p map[string]interface{} } func (p *GenerateAlertParams) toURLValues() url.Values { u := url.Values{} if p.p == nil { return u } if v, found := p.p["description"]; found { u.Set("description", v.(string)) } if v, found := p.p["name"]; found { u.Set("name", v.(string)) } if v, found := p.p["podid"]; found { u.Set("podid", v.(string)) } if v, found := p.p["type"]; found { vv := strconv.Itoa(v.(int)) u.Set("type", vv) } if v, found := p.p["zoneid"]; found { u.Set("zoneid", v.(string)) } return u } func (p *GenerateAlertParams) SetDescription(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["description"] = v } func (p *GenerateAlertParams) ResetDescription() { if p.p != nil && p.p["description"] != nil { delete(p.p, "description") } } func (p *GenerateAlertParams) GetDescription() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["description"].(string) return value, ok } func (p *GenerateAlertParams) SetName(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["name"] = v } func (p *GenerateAlertParams) ResetName() { if p.p != nil && p.p["name"] != nil { delete(p.p, "name") } } func (p *GenerateAlertParams) GetName() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["name"].(string) return value, ok } func (p *GenerateAlertParams) SetPodid(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["podid"] = v } func (p *GenerateAlertParams) ResetPodid() { if p.p != nil && p.p["podid"] != nil { delete(p.p, "podid") } } func (p *GenerateAlertParams) GetPodid() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["podid"].(string) return value, ok } func (p *GenerateAlertParams) SetType(v int) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["type"] = v } func (p *GenerateAlertParams) ResetType() { if p.p != nil && p.p["type"] != nil { delete(p.p, "type") } } func (p *GenerateAlertParams) GetType() (int, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["type"].(int) return value, ok } func (p *GenerateAlertParams) SetZoneid(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["zoneid"] = v } func (p *GenerateAlertParams) ResetZoneid() { if p.p != nil && p.p["zoneid"] != nil { delete(p.p, "zoneid") } } func (p *GenerateAlertParams) GetZoneid() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["zoneid"].(string) return value, ok } // You should always use this function to get a new GenerateAlertParams instance, // as then you are sure you have configured all required params func (s *AlertService) NewGenerateAlertParams(description string, name string, alertType int) *GenerateAlertParams { p := &GenerateAlertParams{} p.p = make(map[string]interface{}) p.p["description"] = description p.p["name"] = name p.p["type"] = alertType return p } // Generates an alert func (s *AlertService) GenerateAlert(p *GenerateAlertParams) (*GenerateAlertResponse, error) { resp, err := s.cs.newRequest("generateAlert", p.toURLValues()) if err != nil { return nil, err } var r GenerateAlertResponse if err := json.Unmarshal(resp, &r); err != nil { return nil, err } // If we have a async client, we need to wait for the async result if s.cs.async { b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout) if err != nil { if err == AsyncTimeoutErr { return &r, err } return nil, err } if err := json.Unmarshal(b, &r); err != nil { return nil, err } } return &r, nil } type GenerateAlertResponse struct { Displaytext string `json:"displaytext"` JobID string `json:"jobid"` Jobstatus int `json:"jobstatus"` Success bool `json:"success"` } type ListAlertsParams struct { p map[string]interface{} } func (p *ListAlertsParams) toURLValues() url.Values { u := url.Values{} if p.p == nil { return u } if v, found := p.p["id"]; found { u.Set("id", v.(string)) } if v, found := p.p["keyword"]; found { u.Set("keyword", v.(string)) } if v, found := p.p["name"]; found { u.Set("name", v.(string)) } if v, found := p.p["page"]; found { vv := strconv.Itoa(v.(int)) u.Set("page", vv) } if v, found := p.p["pagesize"]; found { vv := strconv.Itoa(v.(int)) u.Set("pagesize", vv) } if v, found := p.p["type"]; found { u.Set("type", v.(string)) } return u } func (p *ListAlertsParams) SetId(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["id"] = v } func (p *ListAlertsParams) ResetId() { if p.p != nil && p.p["id"] != nil { delete(p.p, "id") } } func (p *ListAlertsParams) GetId() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["id"].(string) return value, ok } func (p *ListAlertsParams) SetKeyword(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["keyword"] = v } func (p *ListAlertsParams) ResetKeyword() { if p.p != nil && p.p["keyword"] != nil { delete(p.p, "keyword") } } func (p *ListAlertsParams) GetKeyword() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["keyword"].(string) return value, ok } func (p *ListAlertsParams) SetName(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["name"] = v } func (p *ListAlertsParams) ResetName() { if p.p != nil && p.p["name"] != nil { delete(p.p, "name") } } func (p *ListAlertsParams) GetName() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["name"].(string) return value, ok } func (p *ListAlertsParams) SetPage(v int) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["page"] = v } func (p *ListAlertsParams) ResetPage() { if p.p != nil && p.p["page"] != nil { delete(p.p, "page") } } func (p *ListAlertsParams) GetPage() (int, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["page"].(int) return value, ok } func (p *ListAlertsParams) SetPagesize(v int) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["pagesize"] = v } func (p *ListAlertsParams) ResetPagesize() { if p.p != nil && p.p["pagesize"] != nil { delete(p.p, "pagesize") } } func (p *ListAlertsParams) GetPagesize() (int, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["pagesize"].(int) return value, ok } func (p *ListAlertsParams) SetType(v string) { if p.p == nil { p.p = make(map[string]interface{}) } p.p["type"] = v } func (p *ListAlertsParams) ResetType() { if p.p != nil && p.p["type"] != nil { delete(p.p, "type") } } func (p *ListAlertsParams) GetType() (string, bool) { if p.p == nil { p.p = make(map[string]interface{}) } value, ok := p.p["type"].(string) return value, ok } // You should always use this function to get a new ListAlertsParams instance, // as then you are sure you have configured all required params func (s *AlertService) NewListAlertsParams() *ListAlertsParams { p := &ListAlertsParams{} p.p = make(map[string]interface{}) return p } // This is a courtesy helper function, which in some cases may not work as expected! func (s *AlertService) GetAlertID(name string, opts ...OptionFunc) (string, int, error) { p := &ListAlertsParams{} p.p = make(map[string]interface{}) p.p["name"] = name for _, fn := range append(s.cs.options, opts...) { if err := fn(s.cs, p); err != nil { return "", -1, err } } l, err := s.ListAlerts(p) if err != nil { return "", -1, err } if l.Count == 0 { return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) } if l.Count == 1 { return l.Alerts[0].Id, l.Count, nil } if l.Count > 1 { for _, v := range l.Alerts { if v.Name == name { return v.Id, l.Count, nil } } } return "", l.Count, fmt.Errorf("Could not find an exact match for %s: %+v", name, l) } // This is a courtesy helper function, which in some cases may not work as expected! func (s *AlertService) GetAlertByName(name string, opts ...OptionFunc) (*Alert, int, error) { id, count, err := s.GetAlertID(name, opts...) if err != nil { return nil, count, err } r, count, err := s.GetAlertByID(id, opts...) if err != nil { return nil, count, err } return r, count, nil } // This is a courtesy helper function, which in some cases may not work as expected! func (s *AlertService) GetAlertByID(id string, opts ...OptionFunc) (*Alert, int, error) { p := &ListAlertsParams{} p.p = make(map[string]interface{}) p.p["id"] = id for _, fn := range append(s.cs.options, opts...) { if err := fn(s.cs, p); err != nil { return nil, -1, err } } l, err := s.ListAlerts(p) if err != nil { if strings.Contains(err.Error(), fmt.Sprintf( "Invalid parameter id value=%s due to incorrect long value format, "+ "or entity does not exist", id)) { return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l) } return nil, -1, err } if l.Count == 0 { return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) } if l.Count == 1 { return l.Alerts[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Alert UUID: %s!", id) } // Lists all alerts. func (s *AlertService) ListAlerts(p *ListAlertsParams) (*ListAlertsResponse, error) { resp, err := s.cs.newRequest("listAlerts", p.toURLValues()) if err != nil { return nil, err } var r ListAlertsResponse if err := json.Unmarshal(resp, &r); err != nil { return nil, err } return &r, nil } type ListAlertsResponse struct { Count int `json:"count"` Alerts []*Alert `json:"alert"` } type Alert struct { Description string `json:"description"` Id string `json:"id"` JobID string `json:"jobid"` Jobstatus int `json:"jobstatus"` Name string `json:"name"` Sent string `json:"sent"` Type int `json:"type"` } type ListAlertTypesParams struct { p map[string]interface{} } func (p *ListAlertTypesParams) toURLValues() url.Values { u := url.Values{} if p.p == nil { return u } return u } // You should always use this function to get a new ListAlertTypesParams instance, // as then you are sure you have configured all required params func (s *AlertService) NewListAlertTypesParams() *ListAlertTypesParams { p := &ListAlertTypesParams{} p.p = make(map[string]interface{}) return p } // Lists all alerts types func (s *AlertService) ListAlertTypes(p *ListAlertTypesParams) (*ListAlertTypesResponse, error) { resp, err := s.cs.newRequest("listAlertTypes", p.toURLValues()) if err != nil { return nil, err } var r ListAlertTypesResponse if err := json.Unmarshal(resp, &r); err != nil { return nil, err } return &r, nil } type ListAlertTypesResponse struct { Count int `json:"count"` AlertTypes []*AlertType `json:"alerttype"` } type AlertType struct { Description string `json:"description"` Id string `json:"id"` JobID string `json:"jobid"` Jobstatus int `json:"jobstatus"` Name string `json:"name"` Sent string `json:"sent"` Type int `json:"type"` }