github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/cloudeyeservice/alarmrule/requests.go (about)

     1  package alarmrule
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  )
     8  
     9  type CreateOptsBuilder interface {
    10  	ToAlarmRuleCreateMap() (map[string]interface{}, error)
    11  }
    12  
    13  type DimensionOpts struct {
    14  	Name  string `json:"name" required:"true"`
    15  	Value string `json:"value" required:"true"`
    16  }
    17  
    18  type MetricOpts struct {
    19  	Namespace  string          `json:"namespace" required:"true"`
    20  	MetricName string          `json:"metric_name" required:"true"`
    21  	Dimensions []DimensionOpts `json:"dimensions" required:"true"`
    22  }
    23  
    24  type ConditionOpts struct {
    25  	Period             int    `json:"period" required:"true"`
    26  	Filter             string `json:"filter" required:"true"`
    27  	ComparisonOperator string `json:"comparison_operator" required:"true"`
    28  	// The Value ranges from 0 to MAX_VALUE
    29  	Value int    `json:"value"`
    30  	Unit  string `json:"unit,omitempty"`
    31  	Count int    `json:"count" required:"true"`
    32  }
    33  
    34  type ActionOpts struct {
    35  	Type             string   `json:"type" required:"true"`
    36  	NotificationList []string `json:"notificationList" required:"true"`
    37  }
    38  
    39  type CreateOpts struct {
    40  	AlarmName               string        `json:"alarm_name" required:"true"`
    41  	AlarmDescription        string        `json:"alarm_description,omitempty"`
    42  	AlarmType               string        `json:"alarm_type,omitempty"`
    43  	AlarmLevel              int           `json:"alarm_level,omitempty"`
    44  	Metric                  MetricOpts    `json:"metric" required:"true"`
    45  	Condition               ConditionOpts `json:"condition" required:"true"`
    46  	AlarmActions            []ActionOpts  `json:"alarm_actions,omitempty"`
    47  	InsufficientdataActions []ActionOpts  `json:"insufficientdata_actions,omitempty"`
    48  	OkActions               []ActionOpts  `json:"ok_actions,omitempty"`
    49  	AlarmEnabled            bool          `json:"alarm_enabled"`
    50  	AlarmActionEnabled      bool          `json:"alarm_action_enabled"`
    51  	EnterpriseProjectID     string        `json:"enterprise_project_id,omitempty"`
    52  }
    53  
    54  func (opts CreateOpts) ToAlarmRuleCreateMap() (map[string]interface{}, error) {
    55  	return golangsdk.BuildRequestBody(opts, "")
    56  }
    57  
    58  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    59  	b, err := opts.ToAlarmRuleCreateMap()
    60  	if err != nil {
    61  		r.Err = err
    62  		return
    63  	}
    64  	log.Printf("[DEBUG] create AlarmRule url:%q, body=%#v, opt=%#v", rootURL(c), b, opts)
    65  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}}
    66  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
    67  	return
    68  }
    69  
    70  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    71  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
    72  	return
    73  }
    74  
    75  type UpdateOptsBuilder interface {
    76  	ToAlarmRuleUpdateMap() (map[string]interface{}, error)
    77  }
    78  
    79  type UpdateOpts struct {
    80  	Name          string         `json:"alarm_name,omitempty"`
    81  	AlarmType     string         `json:"alarm_type,omitempty"`
    82  	AlarmLevel    int            `json:"alarm_level,omitempty"`
    83  	Description   *string        `json:"alarm_description,omitempty"`
    84  	ActionEnabled *bool          `json:"alarm_action_enabled,omitempty"`
    85  	Condition     *ConditionOpts `json:"condition,omitempty"`
    86  	// in actual, alarm_actions and ok_actions don't support to update
    87  	AlarmActions []ActionOpts `json:"alarm_actions,omitempty"`
    88  	OkActions    []ActionOpts `json:"ok_actions,omitempty"`
    89  }
    90  
    91  func (opts UpdateOpts) ToAlarmRuleUpdateMap() (map[string]interface{}, error) {
    92  	return golangsdk.BuildRequestBody(opts, "")
    93  }
    94  
    95  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
    96  	b, err := opts.ToAlarmRuleUpdateMap()
    97  	if err != nil {
    98  		r.Err = err
    99  		return
   100  	}
   101  	_, r.Err = c.Put(resourceURL(c, id), b, nil, &golangsdk.RequestOpts{
   102  		OkCodes: []int{204},
   103  	})
   104  	return
   105  }
   106  
   107  type EnableOptsBuilder interface {
   108  	ToAlarmRuleEnableMap() (map[string]interface{}, error)
   109  }
   110  
   111  type EnableOpts struct {
   112  	AlarmEnabled bool `json:"alarm_enabled"`
   113  }
   114  
   115  func (opts EnableOpts) ToAlarmRuleEnableMap() (map[string]interface{}, error) {
   116  	return golangsdk.BuildRequestBody(opts, "")
   117  }
   118  
   119  func Enable(c *golangsdk.ServiceClient, id string, opts EnableOpts) (r EnableResult) {
   120  	b, err := opts.ToAlarmRuleEnableMap()
   121  	if err != nil {
   122  		r.Err = err
   123  		return
   124  	}
   125  	_, r.Err = c.Put(actionURL(c, id), b, nil, &golangsdk.RequestOpts{
   126  		OkCodes: []int{204},
   127  	})
   128  	return
   129  }
   130  
   131  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   132  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}}
   133  	_, r.Err = c.Delete(resourceURL(c, id), reqOpt)
   134  	return
   135  }