packer-cn/vendor/github.com/xanzy/go-cloudstack/cloudstack/EventService.go

573 lines
12 KiB
Go
Raw Normal View History

2016-10-07 10:39:28 -04:00
//
// Copyright 2018, Sander van Harmelen
2016-10-07 10:39:28 -04:00
//
// Licensed 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 ArchiveEventsParams struct {
2016-10-07 10:39:28 -04:00
p map[string]interface{}
}
func (p *ArchiveEventsParams) toURLValues() url.Values {
2016-10-07 10:39:28 -04:00
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)
2016-10-07 10:39:28 -04:00
}
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 *ArchiveEventsParams) SetEnddate(v string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["enddate"] = v
2016-10-07 10:39:28 -04:00
return
}
func (p *ArchiveEventsParams) SetIds(v []string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["ids"] = v
2016-10-07 10:39:28 -04:00
return
}
func (p *ArchiveEventsParams) SetStartdate(v string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["startdate"] = v
2016-10-07 10:39:28 -04:00
return
}
func (p *ArchiveEventsParams) SetType(v string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["type"] = v
2016-10-07 10:39:28 -04:00
return
}
// You should always use this function to get a new ArchiveEventsParams instance,
// as then you are sure you have configured all required params
func (s *EventService) NewArchiveEventsParams() *ArchiveEventsParams {
p := &ArchiveEventsParams{}
p.p = make(map[string]interface{})
return p
2016-10-07 10:39:28 -04:00
}
// Archive one or more events.
func (s *EventService) ArchiveEvents(p *ArchiveEventsParams) (*ArchiveEventsResponse, error) {
resp, err := s.cs.newRequest("archiveEvents", p.toURLValues())
if err != nil {
return nil, err
2016-10-07 10:39:28 -04:00
}
var r ArchiveEventsResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
2016-10-07 10:39:28 -04:00
}
return &r, nil
2016-10-07 10:39:28 -04:00
}
type ArchiveEventsResponse struct {
Displaytext string `json:"displaytext"`
2019-05-26 05:58:26 -04:00
JobID string `json:"jobid"`
Jobstatus int `json:"jobstatus"`
Success bool `json:"success"`
2016-10-07 10:39:28 -04:00
}
func (r *ArchiveEventsResponse) UnmarshalJSON(b []byte) error {
var m map[string]interface{}
err := json.Unmarshal(b, &m)
if err != nil {
return err
2016-10-07 10:39:28 -04:00
}
if success, ok := m["success"].(string); ok {
m["success"] = success == "true"
b, err = json.Marshal(m)
if err != nil {
return err
}
2016-10-07 10:39:28 -04:00
}
2019-05-26 05:58:26 -04:00
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 ArchiveEventsResponse
return json.Unmarshal(b, (*alias)(r))
2016-10-07 10:39:28 -04:00
}
type DeleteEventsParams struct {
p map[string]interface{}
}
func (p *DeleteEventsParams) toURLValues() url.Values {
u := url.Values{}
2016-10-07 10:39:28 -04:00
if p.p == nil {
return u
2016-10-07 10:39:28 -04:00
}
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
2016-10-07 10:39:28 -04:00
}
func (p *DeleteEventsParams) SetEnddate(v string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["enddate"] = v
2016-10-07 10:39:28 -04:00
return
}
func (p *DeleteEventsParams) SetIds(v []string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["ids"] = v
2016-10-07 10:39:28 -04:00
return
}
func (p *DeleteEventsParams) SetStartdate(v string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["startdate"] = v
return
}
func (p *DeleteEventsParams) SetType(v string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["type"] = v
return
}
// You should always use this function to get a new DeleteEventsParams instance,
2016-10-07 10:39:28 -04:00
// as then you are sure you have configured all required params
func (s *EventService) NewDeleteEventsParams() *DeleteEventsParams {
p := &DeleteEventsParams{}
2016-10-07 10:39:28 -04:00
p.p = make(map[string]interface{})
return p
}
// Delete one or more events.
func (s *EventService) DeleteEvents(p *DeleteEventsParams) (*DeleteEventsResponse, error) {
resp, err := s.cs.newRequest("deleteEvents", p.toURLValues())
2016-10-07 10:39:28 -04:00
if err != nil {
return nil, err
}
var r DeleteEventsResponse
2016-10-07 10:39:28 -04:00
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
}
2016-10-07 10:39:28 -04:00
return &r, nil
}
type DeleteEventsResponse struct {
Displaytext string `json:"displaytext"`
2019-05-26 05:58:26 -04:00
JobID string `json:"jobid"`
Jobstatus int `json:"jobstatus"`
Success bool `json:"success"`
2016-10-07 10:39:28 -04:00
}
func (r *DeleteEventsResponse) 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
}
}
2019-05-26 05:58:26 -04:00
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 DeleteEventsResponse
return json.Unmarshal(b, (*alias)(r))
2016-10-07 10:39:28 -04:00
}
type ListEventTypesParams struct {
p map[string]interface{}
}
func (p *ListEventTypesParams) toURLValues() url.Values {
u := url.Values{}
if p.p == nil {
return u
}
return u
}
// You should always use this function to get a new ListEventTypesParams instance,
// as then you are sure you have configured all required params
func (s *EventService) NewListEventTypesParams() *ListEventTypesParams {
p := &ListEventTypesParams{}
p.p = make(map[string]interface{})
return p
}
// List Event Types
func (s *EventService) ListEventTypes(p *ListEventTypesParams) (*ListEventTypesResponse, error) {
resp, err := s.cs.newRequest("listEventTypes", p.toURLValues())
if err != nil {
return nil, err
}
var r ListEventTypesResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
}
2016-10-07 10:39:28 -04:00
return &r, nil
}
type ListEventTypesResponse struct {
Count int `json:"count"`
EventTypes []*EventType `json:"eventtype"`
}
type EventType struct {
2019-05-26 05:58:26 -04:00
JobID string `json:"jobid"`
Jobstatus int `json:"jobstatus"`
Name string `json:"name"`
2016-10-07 10:39:28 -04:00
}
type ListEventsParams struct {
2016-10-07 10:39:28 -04:00
p map[string]interface{}
}
func (p *ListEventsParams) toURLValues() url.Values {
2016-10-07 10:39:28 -04:00
u := url.Values{}
if p.p == nil {
return u
}
if v, found := p.p["account"]; found {
u.Set("account", v.(string))
}
if v, found := p.p["domainid"]; found {
u.Set("domainid", v.(string))
}
if v, found := p.p["duration"]; found {
vv := strconv.Itoa(v.(int))
u.Set("duration", vv)
}
2016-10-07 10:39:28 -04:00
if v, found := p.p["enddate"]; found {
u.Set("enddate", v.(string))
}
if v, found := p.p["entrytime"]; found {
vv := strconv.Itoa(v.(int))
u.Set("entrytime", vv)
}
if v, found := p.p["id"]; found {
u.Set("id", v.(string))
}
if v, found := p.p["isrecursive"]; found {
vv := strconv.FormatBool(v.(bool))
u.Set("isrecursive", vv)
}
if v, found := p.p["keyword"]; found {
u.Set("keyword", v.(string))
}
if v, found := p.p["level"]; found {
u.Set("level", v.(string))
}
if v, found := p.p["listall"]; found {
vv := strconv.FormatBool(v.(bool))
u.Set("listall", vv)
}
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["projectid"]; found {
u.Set("projectid", v.(string))
2016-10-07 10:39:28 -04:00
}
if v, found := p.p["startdate"]; found {
u.Set("startdate", v.(string))
}
if v, found := p.p["startid"]; found {
u.Set("startid", v.(string))
}
2016-10-07 10:39:28 -04:00
if v, found := p.p["type"]; found {
u.Set("type", v.(string))
}
return u
}
func (p *ListEventsParams) SetAccount(v string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["account"] = v
2016-10-07 10:39:28 -04:00
return
}
func (p *ListEventsParams) SetDomainid(v string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["domainid"] = v
2016-10-07 10:39:28 -04:00
return
}
func (p *ListEventsParams) SetDuration(v int) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["duration"] = v
2016-10-07 10:39:28 -04:00
return
}
func (p *ListEventsParams) SetEnddate(v string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["enddate"] = v
2016-10-07 10:39:28 -04:00
return
}
func (p *ListEventsParams) SetEntrytime(v int) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["entrytime"] = v
return
2016-10-07 10:39:28 -04:00
}
func (p *ListEventsParams) SetId(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
2016-10-07 10:39:28 -04:00
}
p.p["id"] = v
return
}
2016-10-07 10:39:28 -04:00
func (p *ListEventsParams) SetIsrecursive(v bool) {
if p.p == nil {
p.p = make(map[string]interface{})
2016-10-07 10:39:28 -04:00
}
p.p["isrecursive"] = v
return
2016-10-07 10:39:28 -04:00
}
func (p *ListEventsParams) SetKeyword(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["keyword"] = v
return
2016-10-07 10:39:28 -04:00
}
func (p *ListEventsParams) SetLevel(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["level"] = v
return
2016-10-07 10:39:28 -04:00
}
func (p *ListEventsParams) SetListall(v bool) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
2016-10-07 10:39:28 -04:00
}
p.p["listall"] = v
return
}
func (p *ListEventsParams) SetPage(v int) {
if p.p == nil {
p.p = make(map[string]interface{})
2016-10-07 10:39:28 -04:00
}
p.p["page"] = v
return
2016-10-07 10:39:28 -04:00
}
func (p *ListEventsParams) SetPagesize(v int) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["pagesize"] = v
2016-10-07 10:39:28 -04:00
return
}
func (p *ListEventsParams) SetProjectid(v string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["projectid"] = v
2016-10-07 10:39:28 -04:00
return
}
func (p *ListEventsParams) SetStartdate(v string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["startdate"] = v
return
}
func (p *ListEventsParams) SetStartid(v string) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["startid"] = v
return
}
func (p *ListEventsParams) SetType(v string) {
2016-10-07 10:39:28 -04:00
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p["type"] = v
return
}
// You should always use this function to get a new ListEventsParams instance,
2016-10-07 10:39:28 -04:00
// as then you are sure you have configured all required params
func (s *EventService) NewListEventsParams() *ListEventsParams {
p := &ListEventsParams{}
2016-10-07 10:39:28 -04:00
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 *EventService) GetEventByID(id string, opts ...OptionFunc) (*Event, int, error) {
p := &ListEventsParams{}
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.ListEvents(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.Events[0], l.Count, nil
}
return nil, l.Count, fmt.Errorf("There is more then one result for Event UUID: %s!", id)
}
// A command to list events.
func (s *EventService) ListEvents(p *ListEventsParams) (*ListEventsResponse, error) {
resp, err := s.cs.newRequest("listEvents", p.toURLValues())
2016-10-07 10:39:28 -04:00
if err != nil {
return nil, err
}
var r ListEventsResponse
2016-10-07 10:39:28 -04:00
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
}
2016-10-07 10:39:28 -04:00
return &r, nil
}
type ListEventsResponse struct {
Count int `json:"count"`
Events []*Event `json:"event"`
}
type Event struct {
Account string `json:"account"`
Created string `json:"created"`
Description string `json:"description"`
Domain string `json:"domain"`
Domainid string `json:"domainid"`
Id string `json:"id"`
2019-05-26 05:58:26 -04:00
JobID string `json:"jobid"`
Jobstatus int `json:"jobstatus"`
Level string `json:"level"`
Parentid string `json:"parentid"`
Project string `json:"project"`
Projectid string `json:"projectid"`
State string `json:"state"`
Type string `json:"type"`
Username string `json:"username"`
2016-10-07 10:39:28 -04:00
}