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

     1  package topics
     2  
     3  import (
     4  	"github.com/chnsz/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-50, Default value:3
    17  	Partition int `json:"partition,omitempty"`
    18  	// topic replications, value range: 1-3, Default value:3
    19  	Replication int `json:"replication,omitempty"`
    20  	// aging time in hours, value range: 1-168, , Default value: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  // List all topics belong to the instance id
    48  func List(client *golangsdk.ServiceClient, instanceID string) (r ListResult) {
    49  	_, r.Err = client.Get(rootURL(client, instanceID), &r.Body, nil)
    50  	return
    51  }
    52  
    53  // Delete given topics belong to the instance id
    54  func Delete(client *golangsdk.ServiceClient, instanceID string, topics []string) (r DeleteResult) {
    55  	var delOpts = struct {
    56  		Topics []string `json:"topics" required:"true"`
    57  	}{Topics: topics}
    58  
    59  	b, err := golangsdk.BuildRequestBody(delOpts, "")
    60  	if err != nil {
    61  		r.Err = err
    62  		return
    63  	}
    64  
    65  	_, r.Err = client.Post(deleteURL(client, instanceID), b, &r.Body, &golangsdk.RequestOpts{
    66  		OkCodes: []int{200},
    67  	})
    68  
    69  	return
    70  }