github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/autoscaling/v1/activitylogs/results.go (about) 1 package activitylogs 2 3 import ( 4 "strconv" 5 6 "github.com/chnsz/golangsdk/pagination" 7 ) 8 9 // ActivityLog is the structure that represents the activity log detail. 10 type ActivityLog struct { 11 ID string `json:"id"` 12 Status string `json:"status"` 13 StartTime string `json:"start_time"` 14 EndTime string `json:"end_time"` 15 InstanceRemovedList string `json:"instance_removed_list"` 16 InstanceDeletedList string `json:"instance_deleted_list"` 17 InstanceAddedList string `json:"instance_added_list"` 18 InstanceValue int `json:"instance_value"` 19 DesireValue int `json:"desire_value"` 20 ScalingValue int `json:"scaling_value"` 21 Description string `json:"description"` 22 } 23 24 // ActivityLogPage is a single page maximum result representing a query by start_number page. 25 type ActivityLogPage struct { 26 pagination.OffsetPageBase 27 } 28 29 // NextStartNumber returns startNumber of the next element of the page. 30 func (current ActivityLogPage) NextStartNumber() int { 31 q := current.URL.Query() 32 // get `start_number` and `limit` from query path. 33 // If the limit is not set, it will be set to `20` by default for querying. 34 startNumber, _ := strconv.Atoi(q.Get("start_number")) 35 limit, _ := strconv.Atoi(q.Get("limit")) 36 if limit == 0 { 37 limit = 20 38 } 39 40 return startNumber + limit 41 } 42 43 // NextPageURL generates the URL for the page of results after this one. 44 func (current ActivityLogPage) NextPageURL() (string, error) { 45 next := current.NextStartNumber() 46 if next == 0 { 47 return "", nil 48 } 49 50 currentURL := current.URL 51 q := currentURL.Query() 52 q.Set("start_number", strconv.Itoa(next)) 53 currentURL.RawQuery = q.Encode() 54 55 return currentURL.String(), nil 56 } 57 58 // IsEmpty checks whether a ActivityLogPage struct is empty. 59 func (b ActivityLogPage) IsEmpty() (bool, error) { 60 groups, err := ExtractActivityLogs(b) 61 return len(groups) == 0, err 62 } 63 64 // ExtractActivityLogs is a method to extract the list of activity logs for specified scaling group. 65 func ExtractActivityLogs(r pagination.Page) ([]ActivityLog, error) { 66 var s []ActivityLog 67 err := r.(ActivityLogPage).Result.ExtractIntoSlicePtr(&s, "scaling_activity_log") 68 return s, err 69 }