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

     1  package topics
     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 topic parameters.
    13  // any struct providing the parameters should implement this interface
    14  type CreateOpsBuilder interface {
    15  	ToTopicCreateMap() (map[string]interface{}, error)
    16  }
    17  
    18  // CreateOps is a struct that contains all the parameters.
    19  type CreateOps struct {
    20  	//Name of the topic to be created
    21  	Name string `json:"name" required:"true"`
    22  
    23  	//Topic display name
    24  	DisplayName string `json:"display_name,omitempty"`
    25  
    26  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
    27  }
    28  
    29  func (ops CreateOps) ToTopicCreateMap() (map[string]interface{}, error) {
    30  	return golangsdk.BuildRequestBody(ops, "")
    31  }
    32  
    33  // CreateOpsBuilder is used for updating topic parameters.
    34  // any struct providing the parameters should implement this interface
    35  type UpdateOpsBuilder interface {
    36  	ToTopicUpdateMap() (map[string]interface{}, error)
    37  }
    38  
    39  // UpdateOps is a struct that contains all the parameters.
    40  type UpdateOps struct {
    41  	//Topic display name
    42  	DisplayName string `json:"display_name,omitempty"`
    43  }
    44  
    45  func (ops UpdateOps) ToTopicUpdateMap() (map[string]interface{}, error) {
    46  	return golangsdk.BuildRequestBody(ops, "")
    47  }
    48  
    49  // Create a topic with given parameters.
    50  func Create(client *golangsdk.ServiceClient, ops CreateOpsBuilder) (r CreateResult) {
    51  	b, err := ops.ToTopicCreateMap()
    52  	if err != nil {
    53  		r.Err = err
    54  		return
    55  	}
    56  
    57  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
    58  		OkCodes:     []int{201, 200},
    59  		MoreHeaders: RequestOpts.MoreHeaders,
    60  	})
    61  
    62  	return
    63  }
    64  
    65  // Update a topic with given parameters.
    66  func Update(client *golangsdk.ServiceClient, ops UpdateOpsBuilder, id string) (r UpdateResult) {
    67  	b, err := ops.ToTopicUpdateMap()
    68  	if err != nil {
    69  		r.Err = err
    70  		return
    71  	}
    72  
    73  	_, r.Err = client.Put(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
    74  		OkCodes:     []int{200},
    75  		MoreHeaders: RequestOpts.MoreHeaders,
    76  	})
    77  
    78  	return
    79  }
    80  
    81  // delete a topic via id
    82  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
    83  	_, r.Err = client.Delete(deleteURL(client, id), &golangsdk.RequestOpts{
    84  		OkCodes:     []int{200},
    85  		MoreHeaders: RequestOpts.MoreHeaders,
    86  	})
    87  	return
    88  }
    89  
    90  // get a topic with detailed information by id
    91  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    92  	_, r.Err = client.Get(getURL(client, id), &r.Body, &golangsdk.RequestOpts{
    93  		MoreHeaders: RequestOpts.MoreHeaders,
    94  	})
    95  	return
    96  }
    97  
    98  // list all the topics
    99  func List(client *golangsdk.ServiceClient) (r ListResult) {
   100  	pages, err := pagination.NewPager(client, listURL(client),
   101  		func(r pagination.PageResult) pagination.Page {
   102  			p := TopicPage{pagination.OffsetPageBase{PageResult: r}}
   103  			return p
   104  		}).AllPages()
   105  
   106  	if err != nil {
   107  		r.Err = err
   108  		return
   109  	}
   110  
   111  	r.Body = pages.GetBody()
   112  	return
   113  }
   114  
   115  type UpdatePoliciesOpts struct {
   116  	// the value can be empty
   117  	Value string `json:"value"`
   118  }
   119  
   120  // Update policies of the topic.
   121  func UpdatePolicies(client *golangsdk.ServiceClient, ops UpdatePoliciesOpts, id, policyName string) (r UpdateResult) {
   122  	b, err := golangsdk.BuildRequestBody(ops, "")
   123  	if err != nil {
   124  		r.Err = err
   125  		return
   126  	}
   127  
   128  	_, r.Err = client.Put(updatePoliciesURL(client, id, policyName), b, &r.Body, &golangsdk.RequestOpts{
   129  		OkCodes:     []int{200},
   130  		MoreHeaders: RequestOpts.MoreHeaders,
   131  	})
   132  
   133  	return
   134  }
   135  
   136  // get policies of the topic
   137  func GetPolicies(client *golangsdk.ServiceClient, id, policyName string) (r GetPoliciesResult) {
   138  	_, r.Err = client.Get(getPoliciesURL(client, id, policyName), &r.Body, &golangsdk.RequestOpts{
   139  		MoreHeaders: RequestOpts.MoreHeaders,
   140  	})
   141  	return
   142  }