github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/smn/v2/topics/requests.go (about)

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