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

     1  package subscriptions
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
     9  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    10  }
    11  
    12  // CreateOpsBuilder is used for creating subscription parameters.
    13  // any struct providing the parameters should implement this interface
    14  type CreateOpsBuilder interface {
    15  	ToSubscriptionCreateMap() (map[string]interface{}, error)
    16  }
    17  
    18  // CreateOps is a struct that contains all the parameters.
    19  type CreateOps struct {
    20  	//Message endpoint
    21  	Endpoint string `json:"endpoint" required:"true"`
    22  	//Protocol of the message endpoint
    23  	Protocol string `json:"protocol" required:"true"`
    24  	//Description of the subscription
    25  	Remark string `json:"remark,omitempty"`
    26  	// Extension config
    27  	Extension *ExtensionSpec `json:"extension,omitempty"`
    28  }
    29  
    30  type ExtensionSpec struct {
    31  	ClientID     string `json:"client_id,omitempty"`
    32  	ClientSecret string `json:"client_secret,omitempty"`
    33  	Keyword      string `json:"keyword,omitempty"`
    34  	SignSecret   string `json:"sign_secret,omitempty"`
    35  }
    36  
    37  func (ops CreateOps) ToSubscriptionCreateMap() (map[string]interface{}, error) {
    38  	return golangsdk.BuildRequestBody(ops, "")
    39  }
    40  
    41  // Create a subscription with given parameters.
    42  func Create(client *golangsdk.ServiceClient, ops CreateOpsBuilder, topicUrn string) (r CreateResult) {
    43  	b, err := ops.ToSubscriptionCreateMap()
    44  	if err != nil {
    45  		r.Err = err
    46  		return
    47  	}
    48  
    49  	_, r.Err = client.Post(createURL(client, topicUrn), b, &r.Body, &golangsdk.RequestOpts{
    50  		OkCodes:     []int{201, 200},
    51  		MoreHeaders: RequestOpts.MoreHeaders,
    52  	})
    53  
    54  	return
    55  }
    56  
    57  // delete a subscription via subscription urn
    58  func Delete(client *golangsdk.ServiceClient, subscriptionUrn string) (r DeleteResult) {
    59  	_, r.Err = client.Delete(deleteURL(client, subscriptionUrn), &golangsdk.RequestOpts{
    60  		OkCodes:     []int{200},
    61  		MoreHeaders: RequestOpts.MoreHeaders,
    62  	})
    63  	return
    64  }
    65  
    66  // list all the subscriptions
    67  func List(client *golangsdk.ServiceClient) (r ListResult) {
    68  	pages, err := pagination.NewPager(client, listURL(client),
    69  		func(r pagination.PageResult) pagination.Page {
    70  			p := SubscriptionPage{pagination.OffsetPageBase{PageResult: r}}
    71  			return p
    72  		}).AllPages()
    73  
    74  	if err != nil {
    75  		r.Err = err
    76  		return
    77  	}
    78  	r.Body = pages.GetBody()
    79  	return
    80  }
    81  
    82  // list all the subscriptions of a topic
    83  func ListFromTopic(client *golangsdk.ServiceClient, topicUrn string) (r ListResult) {
    84  	pages, err := pagination.NewPager(client, listFromTopicURL(client, topicUrn),
    85  		func(r pagination.PageResult) pagination.Page {
    86  			p := SubscriptionPage{pagination.OffsetPageBase{PageResult: r}}
    87  			return p
    88  		}).AllPages()
    89  
    90  	if err != nil {
    91  		r.Err = err
    92  		return
    93  	}
    94  
    95  	r.Body = pages.GetBody()
    96  	return
    97  }