github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/fgs/v2/trigger/requests.go (about)

     1  package trigger
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is a struct which will be used to create a new trigger.
     9  type CreateOpts struct {
    10  	// Trigger type, which support:
    11  	//   TIMER
    12  	//   APIG
    13  	//   CTS
    14  	//   DDS
    15  	//   DMS
    16  	//   DIS
    17  	//   LTS
    18  	//   OBS
    19  	//   KAFKA
    20  	//   SMN
    21  	TriggerTypeCode string `json:"trigger_type_code" required:"true"`
    22  	// Trigger status, which support:
    23  	//   ACTIVE
    24  	//   DISABLED
    25  	TriggerStatus string `json:"trigger_status,omitempty"`
    26  	// Message code.
    27  	EventTypeCode string `json:"event_type_code" required:"true"`
    28  	// Event struct.
    29  	EventData map[string]interface{} `json:"event_data" required:"true"`
    30  }
    31  
    32  // CreateOptsBuilder is an interface used to support the construction of the request body for trigger creates.
    33  type CreateOptsBuilder interface {
    34  	ToCreateTriggerMap() (map[string]interface{}, error)
    35  }
    36  
    37  // ToCreateTriggerMap is a method which to build a request body by the CreateOpts.
    38  func (opts CreateOpts) ToCreateTriggerMap() (map[string]interface{}, error) {
    39  	return golangsdk.BuildRequestBody(opts, "")
    40  }
    41  
    42  // Create is a method to create trigger through function urn and CreateOpts.
    43  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder, urn string) (r CreateResult) {
    44  	b, err := opts.ToCreateTriggerMap()
    45  	if err != nil {
    46  		r.Err = err
    47  		return
    48  	}
    49  	_, r.Err = c.Post(createURL(c, urn), b, &r.Body, nil)
    50  	return
    51  }
    52  
    53  // List is a method to obtain an array of one or more trigger for function graph according to the urn.
    54  func List(c *golangsdk.ServiceClient, urn string) pagination.Pager {
    55  	url := listURL(c, urn)
    56  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    57  		return TriggerPage{pagination.SinglePageBase(r)}
    58  	})
    59  }
    60  
    61  // Get is a method to obtain a trigger through function urn, function code and trigger ID.
    62  func Get(c *golangsdk.ServiceClient, urn, triggerType, triggerId string) (r GetResult) {
    63  	_, r.Err = c.Get(resourceURL(c, urn, triggerType, triggerId), &r.Body, &golangsdk.RequestOpts{
    64  		OkCodes: []int{200},
    65  	})
    66  	return
    67  }
    68  
    69  // UpdateOpts is a struct which will be used to update existing trigger.
    70  type UpdateOpts struct {
    71  	TriggerStatus string `json:"trigger_status" required:"true"`
    72  }
    73  
    74  // UpdateOptsBuilder is an interface used to support the construction of the request body for trigger updates.
    75  type UpdateOptsBuilder interface {
    76  	ToUpdateTriggerMap() (map[string]interface{}, error)
    77  }
    78  
    79  // ToUpdateTriggerMap is a method which to build a request body by the UpdateOpts.
    80  func (opts UpdateOpts) ToUpdateTriggerMap() (map[string]interface{}, error) {
    81  	return golangsdk.BuildRequestBody(opts, "")
    82  }
    83  
    84  // Update is a method to update existing trigger through function urn, function code, trigger ID and UpdateOpts.
    85  func Update(c *golangsdk.ServiceClient, opts UpdateOptsBuilder, urn, triggerType, triggerId string) (r UpdateResult) {
    86  	b, err := opts.ToUpdateTriggerMap()
    87  	if err != nil {
    88  		r.Err = err
    89  		return
    90  	}
    91  	_, r.Err = c.Put(resourceURL(c, urn, triggerType, triggerId), b, &r.Body, &golangsdk.RequestOpts{
    92  		OkCodes: []int{200},
    93  	})
    94  	return
    95  }
    96  
    97  // Delete is a method to delete existing trigger.
    98  func Delete(c *golangsdk.ServiceClient, urn, triggerType, triggerId string) (r DeleteResult) {
    99  	_, r.Err = c.Delete(deleteURL(c, urn, triggerType, triggerId), &golangsdk.RequestOpts{
   100  		OkCodes: []int{204, 200},
   101  	})
   102  	return
   103  }
   104  
   105  // DeleteAll is a method to delete all triggers from a function
   106  func DeleteAll(c *golangsdk.ServiceClient, urn string) (r DeleteResult) {
   107  	_, r.Err = c.Delete(deleteAllURL(c, urn), nil)
   108  	return
   109  }