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