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

     1  package topics
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  )
     6  
     7  // CreateOpsBuilder is an interface which is used for creating a kafka topic
     8  type CreateOpsBuilder interface {
     9  	ToTopicCreateMap() (map[string]interface{}, error)
    10  }
    11  
    12  // CreateOps is a struct that contains all the parameters of create function
    13  type CreateOps struct {
    14  	// the name/ID of a topic
    15  	Name string `json:"id" required:"true"`
    16  	// topic partitions, value range: 1-100
    17  	Partition int `json:"partition,omitempty"`
    18  	// topic replications, value range: 1-3
    19  	Replication int `json:"replication,omitempty"`
    20  	// aging time in hours, value range: 1-168, defaults to 72
    21  	RetentionTime int `json:"retention_time,omitempty"`
    22  
    23  	SyncMessageFlush bool `json:"sync_message_flush,omitempty"`
    24  	SyncReplication  bool `json:"sync_replication,omitempty"`
    25  }
    26  
    27  // ToTopicCreateMap is used for type convert
    28  func (ops CreateOps) ToTopicCreateMap() (map[string]interface{}, error) {
    29  	return golangsdk.BuildRequestBody(ops, "")
    30  }
    31  
    32  // Create a kafka topic with given parameters
    33  func Create(client *golangsdk.ServiceClient, instanceID string, ops CreateOpsBuilder) (r CreateResult) {
    34  	b, err := ops.ToTopicCreateMap()
    35  	if err != nil {
    36  		r.Err = err
    37  		return
    38  	}
    39  
    40  	_, r.Err = client.Post(rootURL(client, instanceID), b, &r.Body, &golangsdk.RequestOpts{
    41  		OkCodes: []int{200},
    42  	})
    43  
    44  	return
    45  }
    46  
    47  // UpdateOptsBuilder is an interface which can build the map paramter of update function
    48  type UpdateOptsBuilder interface {
    49  	ToTopicUpdateMap() (map[string]interface{}, error)
    50  }
    51  
    52  // UpdateOpts is a struct which represents the parameters of update function
    53  type UpdateOpts struct {
    54  	Topics []UpdateItem `json:"topics" required:"true"`
    55  }
    56  
    57  // UpdateItem represents the object of one topic in update function
    58  type UpdateItem struct {
    59  	// Name can not be updated
    60  	Name             string `json:"id" required:"true"`
    61  	Partition        *int   `json:"new_partition_numbers,omitempty"`
    62  	RetentionTime    *int   `json:"retention_time,omitempty"`
    63  	SyncMessageFlush *bool  `json:"sync_message_flush,omitempty"`
    64  	SyncReplication  *bool  `json:"sync_replication,omitempty"`
    65  }
    66  
    67  // ToTopicUpdateMap is used for type convert
    68  func (opts UpdateOpts) ToTopicUpdateMap() (map[string]interface{}, error) {
    69  	return golangsdk.BuildRequestBody(opts, "")
    70  }
    71  
    72  // Update is a method which can be able to update topics
    73  func Update(client *golangsdk.ServiceClient, instanceID string, opts UpdateOptsBuilder) (r UpdateResult) {
    74  	body, err := opts.ToTopicUpdateMap()
    75  	if err != nil {
    76  		r.Err = err
    77  		return
    78  	}
    79  
    80  	_, r.Err = client.Put(rootURL(client, instanceID), body, nil, &golangsdk.RequestOpts{
    81  		OkCodes: []int{204},
    82  	})
    83  	return
    84  }
    85  
    86  // Get an topic with detailed information by instance id and topic name
    87  func Get(client *golangsdk.ServiceClient, instanceID, topicName string) (r GetResult) {
    88  	_, r.Err = client.Get(getURL(client, instanceID, topicName), &r.Body, nil)
    89  	return
    90  }
    91  
    92  // List all topics belong to the instance id
    93  func List(client *golangsdk.ServiceClient, instanceID string) (r ListResult) {
    94  	_, r.Err = client.Get(rootURL(client, instanceID), &r.Body, nil)
    95  	return
    96  }
    97  
    98  // Delete given topics belong to the instance id
    99  func Delete(client *golangsdk.ServiceClient, instanceID string, topics []string) (r DeleteResult) {
   100  	var delOpts = struct {
   101  		Topics []string `json:"topics" required:"true"`
   102  	}{Topics: topics}
   103  
   104  	b, err := golangsdk.BuildRequestBody(delOpts, "")
   105  	if err != nil {
   106  		r.Err = err
   107  		return
   108  	}
   109  
   110  	_, r.Err = client.Post(deleteURL(client, instanceID), b, &r.Body, &golangsdk.RequestOpts{
   111  		OkCodes: []int{200},
   112  	})
   113  
   114  	return
   115  }