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

     1  package custom
     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 custom event source.
     9  type CreateOpts struct {
    10  	// The ID of the event channel to which the custom event source belongs.
    11  	ChannelId string `json:"channel_id" required:"true"`
    12  	// The name of the custom event source.
    13  	Name string `json:"name" required:"true"`
    14  	// The type of custom event source.
    15  	// The valid values are as follows:
    16  	// + APPLICATION (default)
    17  	// + RABBITMQ
    18  	// + ROCKETMQ
    19  	Type string `json:"type,omitempty"`
    20  	// The description of the custom event source.
    21  	Description string `json:"description,omitempty"`
    22  	// The configuration detail of the event source, in JSON format.
    23  	Detail interface{} `json:"detail,omitempty"`
    24  }
    25  
    26  var requestOpts = golangsdk.RequestOpts{
    27  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    28  }
    29  
    30  // Create is a method used to create a new custom event source using given parameters.
    31  func Create(client *golangsdk.ServiceClient, opts CreateOpts) (*Source, error) {
    32  	b, err := golangsdk.BuildRequestBody(opts, "")
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	var r Source
    38  	_, err = client.Post(rootURL(client), b, &r, &golangsdk.RequestOpts{
    39  		MoreHeaders: requestOpts.MoreHeaders,
    40  	})
    41  	return &r, err
    42  }
    43  
    44  // Get is a method to query an existing event source by its ID.
    45  func Get(client *golangsdk.ServiceClient, sourceId string) (*Source, error) {
    46  	var r Source
    47  	_, err := client.Get(resourceURL(client, sourceId), &r, &golangsdk.RequestOpts{
    48  		MoreHeaders: requestOpts.MoreHeaders,
    49  	})
    50  	return &r, err
    51  }
    52  
    53  // ListOpts is the structure used to query event source list.
    54  type ListOpts struct {
    55  	// The ID of the event channel to which the event source belongs.
    56  	ChannelId string `q:"channel_id"`
    57  	// Offset from which the query starts.
    58  	// If the offset is less than 0, the value is automatically converted to 0.
    59  	// The valid value ranges from 0 to 100, defaults to 0.
    60  	Offset int `q:"offset"`
    61  	// Number of items displayed on each page.
    62  	// The valid value ranges from 1 to 1000, defaults to 15.
    63  	Limit int `q:"limit"`
    64  	// The query sorting.
    65  	// The default value is 'created_time:DESC'.
    66  	Sort string `q:"sort"`
    67  	// The type of the event source provider.
    68  	// + OFFICIAL: official cloud service event source.
    69  	// + CUSTOM: the user-defined event source.
    70  	// + PARTNER: partner event source.
    71  	ProviderType string `q:"provider_type"`
    72  	// The name of the event source.
    73  	Name string `q:"name"`
    74  	// The fuzzy name of the event source.
    75  	FuzzyName string `q:"fuzzy_name"`
    76  	// The fuzzy label of the event source.
    77  	FuzzyLabel string `q:"fuzzy_label"`
    78  }
    79  
    80  // List is a method to query the event source list using given parameters.
    81  func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Source, error) {
    82  	url := rootURL(client)
    83  	query, err := golangsdk.BuildQueryString(opts)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	url += query.String()
    88  
    89  	pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    90  		p := SourcePage{pagination.OffsetPageBase{PageResult: r}}
    91  		return p
    92  	}).AllPages()
    93  
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	return ExtractSources(pages)
    98  }
    99  
   100  // UpdateOpts is the structure used to update an existing custom event source.
   101  type UpdateOpts struct {
   102  	// The ID of the event source.
   103  	SourceId string `json:"-" required:"true"`
   104  	// The description of the event source.
   105  	Description *string `json:"description,omitempty"`
   106  	// The configuration detail of the event source, in JSON format.
   107  	Detail interface{} `json:"detail,omitempty"`
   108  }
   109  
   110  // Update is a method used to modify an existing custom event source using given parameters.
   111  func Update(client *golangsdk.ServiceClient, opts UpdateOpts) (*Source, error) {
   112  	b, err := golangsdk.BuildRequestBody(opts, "")
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	var r Source
   118  	_, err = client.Put(resourceURL(client, opts.SourceId), b, &r, &golangsdk.RequestOpts{
   119  		MoreHeaders: requestOpts.MoreHeaders,
   120  	})
   121  	return &r, err
   122  }
   123  
   124  // Delete is a method to delete an existing custom event source using its ID.
   125  func Delete(client *golangsdk.ServiceClient, sourceId string) error {
   126  	_, err := client.Delete(resourceURL(client, sourceId), nil)
   127  	return err
   128  }