actions.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package logger
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. modules "yunion.io/x/onecloud/pkg/mcclient/modules/logger"
  20. )
  21. type BaseActionListOptions struct {
  22. Scope string `help:"scope" choices:"project|domain|system"`
  23. Since string `help:"Show logs since specific date" metavar:"DATETIME"`
  24. Until string `help:"Show logs until specific date" metavar:"DATETIME"`
  25. Limit int64 `help:"Limit number of logs" default:"20"`
  26. Offset int64 `help:"Offset"`
  27. Ascending bool `help:"Ascending order"`
  28. Descending bool `help:"Descending order"`
  29. Field []string `help:"field options"`
  30. Action []string `help:"Log action"`
  31. Search string `help:"Filter action logs by obj_name, using 'like' syntax."`
  32. Admin bool `help:"admin mode"`
  33. Succ bool `help:"Show success action log only"`
  34. Fail bool `help:"Show failed action log only"`
  35. SystemAccount bool `help:"Show system account log"`
  36. NormalAccount bool `help:"Show normal account log"`
  37. User []string `help:"filter by operator user"`
  38. Project []string `help:"filter by owner project"`
  39. PagingMarker string `help:"marker for pagination"`
  40. }
  41. type ActionListOptions struct {
  42. BaseActionListOptions
  43. Service []string `help:"service name"`
  44. Id string `help:"" metavar:"OBJ_ID"`
  45. Type []string `help:"Type of relevant object" metavar:"OBJ_TYPE"`
  46. }
  47. type TypeActionListOptions struct {
  48. BaseActionListOptions
  49. ID string `help:"" metavar:"OBJ_ID"`
  50. }
  51. func doActionList(s *mcclient.ClientSession, args *ActionListOptions) error {
  52. params := jsonutils.NewDict()
  53. if len(args.Service) > 0 {
  54. params.Add(jsonutils.NewStringArray(args.Service), "service")
  55. }
  56. if len(args.Type) > 0 {
  57. params.Add(jsonutils.NewStringArray(args.Type), "obj_type")
  58. }
  59. if len(args.Id) > 0 {
  60. params.Add(jsonutils.NewString(args.Id), "obj_id")
  61. }
  62. if len(args.Search) > 0 {
  63. params.Add(jsonutils.NewString(args.Search), "search")
  64. }
  65. if len(args.User) > 0 {
  66. params.Add(jsonutils.NewStringArray(args.User), "user")
  67. }
  68. if len(args.Project) > 0 {
  69. params.Add(jsonutils.NewStringArray(args.Project), "project")
  70. }
  71. if len(args.Scope) > 0 {
  72. params.Add(jsonutils.NewString(args.Scope), "scope")
  73. }
  74. if len(args.PagingMarker) > 0 {
  75. params.Add(jsonutils.NewString(args.PagingMarker), "paging_marker")
  76. }
  77. if len(args.Since) > 0 {
  78. params.Add(jsonutils.NewString(args.Since), "since")
  79. }
  80. if len(args.Until) > 0 {
  81. params.Add(jsonutils.NewString(args.Until), "until")
  82. }
  83. if args.Limit > 0 {
  84. params.Add(jsonutils.NewInt(args.Limit), "limit")
  85. }
  86. if args.Offset > 0 {
  87. params.Add(jsonutils.NewInt(args.Offset), "offset")
  88. }
  89. if args.Field != nil {
  90. params.Add(jsonutils.NewStringArray(args.Field), "field")
  91. }
  92. if args.Ascending && !args.Descending {
  93. params.Add(jsonutils.NewString("asc"), "order")
  94. } else if !args.Ascending && args.Descending {
  95. params.Add(jsonutils.NewString("desc"), "order")
  96. }
  97. if len(args.Action) > 0 {
  98. params.Add(jsonutils.NewStringArray(args.Action), "action")
  99. }
  100. if args.Admin {
  101. params.Add(jsonutils.JSONTrue, "admin")
  102. }
  103. if args.Succ && args.Fail {
  104. return fmt.Errorf("succ and fail can't go together")
  105. }
  106. if args.Succ {
  107. params.Add(jsonutils.JSONTrue, "success")
  108. }
  109. if args.Fail {
  110. params.Add(jsonutils.JSONFalse, "success")
  111. }
  112. if args.SystemAccount && args.NormalAccount {
  113. return fmt.Errorf("system account and normal account can't go together")
  114. }
  115. if args.SystemAccount {
  116. params.Add(jsonutils.JSONTrue, "is_system_account")
  117. }
  118. if args.NormalAccount {
  119. params.Add(jsonutils.JSONFalse, "is_system_account")
  120. }
  121. logs, err := modules.Actions.List(s, params)
  122. if err != nil {
  123. return err
  124. }
  125. printList(logs, modules.Actions.GetColumns(s))
  126. return nil
  127. }
  128. func init() {
  129. R(&ActionListOptions{}, "action-show", "Show operation action logs", doActionList)
  130. type ActionSplitTableOptions struct {
  131. }
  132. R(&ActionSplitTableOptions{}, "action-splitable", "Show splitables", func(s *mcclient.ClientSession, args *ActionSplitTableOptions) error {
  133. resp, err := modules.Actions.Get(s, "splitable", nil)
  134. if err != nil {
  135. return err
  136. }
  137. printObject(resp)
  138. return nil
  139. })
  140. type ActionPurgeOptions struct {
  141. Tables []string
  142. }
  143. R(&ActionPurgeOptions{}, "action-purge-splitable", "Purge action splitables", func(s *mcclient.ClientSession, args *ActionPurgeOptions) error {
  144. resp, err := modules.Actions.PerformClassAction(s, "purge-splitable", jsonutils.Marshal(args))
  145. if err != nil {
  146. return err
  147. }
  148. printObject(resp)
  149. return nil
  150. })
  151. R(&TypeActionListOptions{}, "server-action", "Show operation action logs of server", func(s *mcclient.ClientSession, args *TypeActionListOptions) error {
  152. nargs := ActionListOptions{BaseActionListOptions: args.BaseActionListOptions, Id: args.ID, Type: []string{"server"}}
  153. return doActionList(s, &nargs)
  154. })
  155. R(&TypeActionListOptions{}, "host-action", "Show operation action logs of host", func(s *mcclient.ClientSession, args *TypeActionListOptions) error {
  156. nargs := ActionListOptions{BaseActionListOptions: args.BaseActionListOptions, Id: args.ID, Type: []string{"host"}}
  157. return doActionList(s, &nargs)
  158. })
  159. R(&TypeActionListOptions{}, "vcenter-action", "Show operation action logs of vcenter", func(s *mcclient.ClientSession, args *TypeActionListOptions) error {
  160. nargs := ActionListOptions{BaseActionListOptions: args.BaseActionListOptions, Id: args.ID, Type: []string{"vcenter"}}
  161. return doActionList(s, &nargs)
  162. })
  163. }