github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/cts/v1/tracker/requests.go (about) 1 package tracker 2 3 import ( 4 "reflect" 5 6 "github.com/huaweicloud/golangsdk" 7 ) 8 9 // ListOpts allows the filtering and sorting of paginated collections through 10 // the API. Filtering is achieved by passing in struct field values that map to 11 // the attributes you want to see returned. 12 type ListOpts struct { 13 TrackerName string `q:"tracker_name"` 14 BucketName string 15 FilePrefixName string 16 Status string 17 } 18 19 // List returns collection of Tracker. It accepts a ListOpts struct, which allows you to filter and sort 20 // the returned collection for greater efficiency. 21 func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Tracker, error) { 22 var r ListResult 23 _, r.Err = client.Get(rootURL(client), &r.Body, &golangsdk.RequestOpts{ 24 OkCodes: []int{200}, 25 }) 26 27 allTracker, err := r.ExtractTracker() 28 if err != nil { 29 return nil, err 30 } 31 32 return FilterTracker(allTracker, opts) 33 } 34 35 func FilterTracker(tracker []Tracker, opts ListOpts) ([]Tracker, error) { 36 37 var refinedTracker []Tracker 38 var matched bool 39 m := map[string]interface{}{} 40 41 if opts.BucketName != "" { 42 m["BucketName"] = opts.BucketName 43 } 44 if opts.FilePrefixName != "" { 45 m["FilePrefixName"] = opts.FilePrefixName 46 } 47 if opts.Status != "" { 48 m["Status"] = opts.Status 49 } 50 51 if len(m) > 0 && len(tracker) > 0 { 52 for _, trackers := range tracker { 53 matched = true 54 55 for key, value := range m { 56 if sVal := getStructField(&trackers, key); !(sVal == value) { 57 matched = false 58 } 59 } 60 61 if matched { 62 refinedTracker = append(refinedTracker, trackers) 63 } 64 } 65 } else { 66 refinedTracker = tracker 67 } 68 return refinedTracker, nil 69 } 70 71 func getStructField(v *Tracker, field string) string { 72 r := reflect.ValueOf(v) 73 f := reflect.Indirect(r).FieldByName(field) 74 return string(f.String()) 75 } 76 77 // CreateOptsBuilder allows extensions to add additional parameters to the 78 // Create request. 79 type CreateOptsBuilder interface { 80 ToTrackerCreateMap() (map[string]interface{}, error) 81 } 82 83 // CreateOptsWithSMN contains the options for create a Tracker. This object is 84 // passed to tracker.Create(). 85 type CreateOptsWithSMN struct { 86 BucketName string `json:"bucket_name" required:"true"` 87 FilePrefixName string `json:"file_prefix_name,omitempty"` 88 SimpleMessageNotification SimpleMessageNotification `json:"smn,omitempty"` 89 } 90 91 // CreateOpts contains the options for create a Tracker. This object is 92 // passed to tracker.Create(). 93 type CreateOpts struct { 94 BucketName string `json:"bucket_name" required:"true"` 95 FilePrefixName string `json:"file_prefix_name,omitempty"` 96 } 97 98 type SimpleMessageNotification struct { 99 IsSupportSMN bool `json:"is_support_smn"` 100 TopicID string `json:"topic_id"` 101 Operations []string `json:"operations" required:"true"` 102 IsSendAllKeyOperation bool `json:"is_send_all_key_operation"` 103 NeedNotifyUserList []string `json:"need_notify_user_list,omitempty"` 104 } 105 106 // ToTrackerCreateMap assembles a request body based on the contents of a 107 // CreateOpts. 108 func (opts CreateOpts) ToTrackerCreateMap() (map[string]interface{}, error) { 109 return golangsdk.BuildRequestBody(opts, "") 110 } 111 112 // ToTrackerCreateMap assembles a request body based on the contents of a 113 // CreateOpts. 114 func (opts CreateOptsWithSMN) ToTrackerCreateMap() (map[string]interface{}, error) { 115 return golangsdk.BuildRequestBody(opts, "") 116 } 117 118 // Create will create a new tracker based on the values in CreateOpts. To extract 119 // the tracker name from the response, call the Extract method on the 120 // CreateResult. 121 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 122 b, err := opts.ToTrackerCreateMap() 123 124 if err != nil { 125 r.Err = err 126 return 127 } 128 _, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{ 129 OkCodes: []int{201}, 130 }) 131 return 132 } 133 134 // UpdateOptsWithSMN contains all the values needed to update a tracker 135 type UpdateOptsWithSMN struct { 136 Status string `json:"status,omitempty"` 137 BucketName string `json:"bucket_name" required:"true"` 138 FilePrefixName string `json:"file_prefix_name,omitempty"` 139 SimpleMessageNotification SimpleMessageNotification `json:"smn,omitempty"` 140 } 141 142 // UpdateOpts contains all the values needed to update a tracker 143 type UpdateOpts struct { 144 Status string `json:"status,omitempty"` 145 BucketName string `json:"bucket_name" required:"true"` 146 FilePrefixName string `json:"file_prefix_name,omitempty"` 147 } 148 149 // UpdateOptsBuilder allows extensions to add additional parameters to the 150 // Update request. 151 type UpdateOptsBuilder interface { 152 ToTrackerUpdateMap() (map[string]interface{}, error) 153 } 154 155 // ToTrackerUpdateMap builds an update body based on UpdateOpts. 156 func (opts UpdateOptsWithSMN) ToTrackerUpdateMap() (map[string]interface{}, error) { 157 return golangsdk.BuildRequestBody(opts, "") 158 } 159 160 func (opts UpdateOpts) ToTrackerUpdateMap() (map[string]interface{}, error) { 161 return golangsdk.BuildRequestBody(opts, "") 162 } 163 164 func Update(client *golangsdk.ServiceClient, opts UpdateOptsBuilder) (r UpdateResult) { 165 b, err := opts.ToTrackerUpdateMap() 166 if err != nil { 167 r.Err = err 168 return 169 } 170 _, r.Err = client.Put(resourceURL(client), b, &r.Body, &golangsdk.RequestOpts{ 171 OkCodes: []int{200}, 172 }) 173 return 174 } 175 176 // Delete will permanently delete a particular tracker. 177 func Delete(client *golangsdk.ServiceClient) (r DeleteResult) { 178 _, r.Err = client.Delete(rootURL(client), &golangsdk.RequestOpts{ 179 OkCodes: []int{204}, 180 JSONBody: nil, 181 }) 182 return 183 }