github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/cloudeyeservice/v1/alarmrule/requests.go (about)

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