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

     1  package subscriptions
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is the structure used to create a new subscription.
     9  type CreateOpts struct {
    10  	// The ID of the event channel associated to the subscription.
    11  	ChannelId string `json:"channel_id" required:"true"`
    12  	// The name of the subscription.
    13  	// The valid length is limited from `1` to `128`, only letters, digits, hyphens (-), underscores (_) and dots (.)
    14  	// are allowed. The name must start with a letter or digit.
    15  	Name string `json:"name" required:"true"`
    16  	// The list of the event sources.
    17  	Sources []interface{} `json:"sources" required:"true"`
    18  	// The list of the event targets.
    19  	Targets []interface{} `json:"targets" required:"true"`
    20  	// The description of the subscription.
    21  	// The valid length is limited from `0` to `128`.
    22  	Description string `json:"description,omitempty"`
    23  }
    24  
    25  var requestOpts = golangsdk.RequestOpts{
    26  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    27  }
    28  
    29  // Create is a method used to create a new subscription using given parameters.
    30  func Create(client *golangsdk.ServiceClient, opts CreateOpts) (*Subscription, error) {
    31  	b, err := golangsdk.BuildRequestBody(opts, "")
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	var r Subscription
    37  	_, err = client.Post(rootURL(client), b, &r, &golangsdk.RequestOpts{
    38  		MoreHeaders: requestOpts.MoreHeaders,
    39  	})
    40  	return &r, err
    41  }
    42  
    43  // Get is a method to query an existing subscription by its ID.
    44  func Get(client *golangsdk.ServiceClient, subscriptionId string) (*Subscription, error) {
    45  	var r Subscription
    46  	_, err := client.Get(resourceURL(client, subscriptionId), &r, &golangsdk.RequestOpts{
    47  		MoreHeaders: requestOpts.MoreHeaders,
    48  	})
    49  	return &r, err
    50  }
    51  
    52  // ListOpts is the structure used to query event subscription list.
    53  type ListOpts struct {
    54  	// The ID of the event channel associated to subscription.
    55  	ChannelId string `q:"channel_id"`
    56  	// Offset from which the query starts.
    57  	// If the offset is less than 0, the value is automatically converted to 0.
    58  	// The valid value ranges from 0 to 100, defaults to 0.
    59  	Offset int `q:"offset"`
    60  	// Number of items displayed on each page.
    61  	// The valid value ranges from 1 to 1000, defaults to 15.
    62  	Limit int `q:"limit"`
    63  	// The query sorting.
    64  	// The default value is 'created_time:DESC'.
    65  	Sort string `q:"sort"`
    66  	// The name of the subscription.
    67  	Name string `q:"name"`
    68  	// The fuzzy name of the subscription.
    69  	FuzzyName string `q:"fuzzy_name"`
    70  	// The fuzzy label of the subscription.
    71  	ConnectionId string `q:"connection_id"`
    72  }
    73  
    74  // List is a method to query the subscription list using given parameters.
    75  func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Subscription, error) {
    76  	url := rootURL(client)
    77  	query, err := golangsdk.BuildQueryString(opts)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	url += query.String()
    82  
    83  	pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    84  		p := SubscriptionPage{pagination.OffsetPageBase{PageResult: r}}
    85  		return p
    86  	}).AllPages()
    87  
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	return ExtractSubscriptions(pages)
    92  }
    93  
    94  // UpdateOpts is the structure used to update an existing subscription.
    95  type UpdateOpts struct {
    96  	SubscriptionId string `json:"-" required:"true"`
    97  	// The description of the subscription.
    98  	Description *string `json:"description,omitempty"`
    99  	// The list of the event sources.
   100  	Sources []interface{} `json:"sources,omitempty"`
   101  	// The list of the event targets.
   102  	Targets []interface{} `json:"targets,omitempty"`
   103  }
   104  
   105  // Update is a method used to modify an existing event subscription using given parameters.
   106  func Update(client *golangsdk.ServiceClient, opts UpdateOpts) (*Subscription, error) {
   107  	b, err := golangsdk.BuildRequestBody(opts, "")
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	var r Subscription
   113  	_, err = client.Put(resourceURL(client, opts.SubscriptionId), b, &r, &golangsdk.RequestOpts{
   114  		MoreHeaders: requestOpts.MoreHeaders,
   115  	})
   116  	return &r, err
   117  }
   118  
   119  // Delete is a method to delete an existing event subscription using its ID.
   120  func Delete(client *golangsdk.ServiceClient, subscriptionId string) error {
   121  	_, err := client.Delete(resourceURL(client, subscriptionId), nil)
   122  	return err
   123  }