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

     1  package topics
     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 topic parameters.
    12  //any struct providing the parameters should implement this interface
    13  type CreateOpsBuilder interface {
    14  	ToTopicCreateMap() (map[string]interface{}, error)
    15  }
    16  
    17  //CreateOps is a struct that contains all the parameters.
    18  type CreateOps struct {
    19  	//Name of the topic to be created
    20  	Name string `json:"name" required:"true"`
    21  
    22  	//Topic display name
    23  	DisplayName string `json:"display_name,omitempty"`
    24  
    25  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
    26  }
    27  
    28  func (ops CreateOps) ToTopicCreateMap() (map[string]interface{}, error) {
    29  	return golangsdk.BuildRequestBody(ops, "")
    30  }
    31  
    32  //CreateOpsBuilder is used for updating topic parameters.
    33  //any struct providing the parameters should implement this interface
    34  type UpdateOpsBuilder interface {
    35  	ToTopicUpdateMap() (map[string]interface{}, error)
    36  }
    37  
    38  //UpdateOps is a struct that contains all the parameters.
    39  type UpdateOps struct {
    40  	//Topic display name
    41  	DisplayName string `json:"display_name,omitempty"`
    42  }
    43  
    44  func (ops UpdateOps) ToTopicUpdateMap() (map[string]interface{}, error) {
    45  	return golangsdk.BuildRequestBody(ops, "")
    46  }
    47  
    48  //Create a topic with given parameters.
    49  func Create(client *golangsdk.ServiceClient, ops CreateOpsBuilder) (r CreateResult) {
    50  	b, err := ops.ToTopicCreateMap()
    51  	if err != nil {
    52  		r.Err = err
    53  		return
    54  	}
    55  
    56  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
    57  		OkCodes:     []int{201, 200},
    58  		MoreHeaders: RequestOpts.MoreHeaders,
    59  	})
    60  
    61  	return
    62  }
    63  
    64  //Update a topic with given parameters.
    65  func Update(client *golangsdk.ServiceClient, ops UpdateOpsBuilder, id string) (r UpdateResult) {
    66  	b, err := ops.ToTopicUpdateMap()
    67  	if err != nil {
    68  		r.Err = err
    69  		return
    70  	}
    71  
    72  	_, r.Err = client.Put(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
    73  		OkCodes:     []int{200},
    74  		MoreHeaders: RequestOpts.MoreHeaders,
    75  	})
    76  
    77  	return
    78  }
    79  
    80  //delete a topic via id
    81  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
    82  	_, r.Err = client.Delete(deleteURL(client, id), &golangsdk.RequestOpts{
    83  		OkCodes:     []int{200},
    84  		MoreHeaders: RequestOpts.MoreHeaders,
    85  	})
    86  	return
    87  }
    88  
    89  //get a topic with detailed information by id
    90  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    91  	_, r.Err = client.Get(getURL(client, id), &r.Body, &golangsdk.RequestOpts{
    92  		MoreHeaders: RequestOpts.MoreHeaders,
    93  	})
    94  	return
    95  }
    96  
    97  //list all the topics
    98  func List(client *golangsdk.ServiceClient) (r ListResult) {
    99  	_, r.Err = client.Get(listURL(client), &r.Body, &golangsdk.RequestOpts{
   100  		MoreHeaders: RequestOpts.MoreHeaders,
   101  	})
   102  	return
   103  }