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

     1  package subscriptions
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  )
     6  
     7  var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
     8  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
     9  }
    10  
    11  //CreateOpsBuilder is used for creating subscription parameters.
    12  //any struct providing the parameters should implement this interface
    13  type CreateOpsBuilder interface {
    14  	ToSubscriptionCreateMap() (map[string]interface{}, error)
    15  }
    16  
    17  //CreateOps is a struct that contains all the parameters.
    18  type CreateOps struct {
    19  	//Message endpoint
    20  	Endpoint string `json:"endpoint" required:"true"`
    21  	//Protocol of the message endpoint
    22  	Protocol string `json:"protocol" required:"true"`
    23  	//Description of the subscription
    24  	Remark string `json:"remark,omitempty"`
    25  }
    26  
    27  func (ops CreateOps) ToSubscriptionCreateMap() (map[string]interface{}, error) {
    28  	return golangsdk.BuildRequestBody(ops, "")
    29  }
    30  
    31  //Create a subscription with given parameters.
    32  func Create(client *golangsdk.ServiceClient, ops CreateOpsBuilder, topicUrn string) (r CreateResult) {
    33  	b, err := ops.ToSubscriptionCreateMap()
    34  	if err != nil {
    35  		r.Err = err
    36  		return
    37  	}
    38  
    39  	_, r.Err = client.Post(createURL(client, topicUrn), b, &r.Body, &golangsdk.RequestOpts{
    40  		OkCodes:     []int{201, 200},
    41  		MoreHeaders: RequestOpts.MoreHeaders,
    42  	})
    43  
    44  	return
    45  }
    46  
    47  //delete a subscription via subscription urn
    48  func Delete(client *golangsdk.ServiceClient, subscriptionUrn string) (r DeleteResult) {
    49  	_, r.Err = client.Delete(deleteURL(client, subscriptionUrn), &golangsdk.RequestOpts{
    50  		OkCodes:     []int{200},
    51  		MoreHeaders: RequestOpts.MoreHeaders,
    52  	})
    53  	return
    54  }
    55  
    56  //get a subscription with detailed information by subscription urn
    57  //func Get(client *golangsdk.ServiceClient, subscriptionUrn string) (r GetResult) {
    58  //	_, r.Err = client.Get(getURL(client, subscriptionUrn), &r.Body, &RequestOpts)
    59  //	return
    60  //}
    61  
    62  //list all the subscriptions
    63  func List(client *golangsdk.ServiceClient) (r ListResult) {
    64  	_, r.Err = client.Get(listURL(client), &r.Body, &golangsdk.RequestOpts{
    65  		MoreHeaders: RequestOpts.MoreHeaders,
    66  	})
    67  	return
    68  }
    69  
    70  //list all the subscriptions
    71  func ListFromTopic(client *golangsdk.ServiceClient, subscriptionUrn string) (r ListResult) {
    72  	_, r.Err = client.Get(listFromTopicURL(client, subscriptionUrn), &r.Body, &golangsdk.RequestOpts{
    73  		MoreHeaders: RequestOpts.MoreHeaders,
    74  	})
    75  	return
    76  }