tasks.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 misc
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  22. "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  23. "yunion.io/x/onecloud/pkg/mcclient/modules/devtool"
  24. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  25. "yunion.io/x/onecloud/pkg/mcclient/modules/image"
  26. "yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
  27. "yunion.io/x/onecloud/pkg/mcclient/modules/notify"
  28. )
  29. func RegisterTaskCmds(service string, manager modulebase.Manager, archivedManager modulebase.Manager) {
  30. type TaskListOptions struct {
  31. apis.TaskListInput
  32. }
  33. R(&TaskListOptions{}, fmt.Sprintf("%s-task-list", service), fmt.Sprintf("List tasks on %s server", service), func(s *mcclient.ClientSession, args *TaskListOptions) error {
  34. params := jsonutils.Marshal(args)
  35. result, err := manager.List(s, params)
  36. if err != nil {
  37. return err
  38. }
  39. printList(result, manager.GetColumns(s))
  40. return nil
  41. })
  42. type TaskShowOptions struct {
  43. ID string `help:"ID or name of the task"`
  44. }
  45. R(&TaskShowOptions{}, fmt.Sprintf("%s-task-show", service), fmt.Sprintf("Show details of a %s task", service), func(s *mcclient.ClientSession, args *TaskShowOptions) error {
  46. result, err := manager.GetById(s, args.ID, nil)
  47. if err != nil {
  48. return err
  49. }
  50. printObject(result)
  51. return nil
  52. })
  53. R(&TaskShowOptions{}, fmt.Sprintf("%s-task-cancel", service), fmt.Sprintf("Cancel a %s task", service), func(s *mcclient.ClientSession, args *TaskShowOptions) error {
  54. result, err := manager.PerformAction(s, args.ID, "cancel", nil)
  55. if err != nil {
  56. return err
  57. }
  58. printObject(result)
  59. return nil
  60. })
  61. R(&TaskListOptions{}, fmt.Sprintf("%s-archived-task-list", service), fmt.Sprintf("List archived tasks on %s server", service), func(s *mcclient.ClientSession, args *TaskListOptions) error {
  62. params := jsonutils.Marshal(args)
  63. result, err := archivedManager.List(s, params)
  64. if err != nil {
  65. return err
  66. }
  67. printList(result, archivedManager.GetColumns(s))
  68. return nil
  69. })
  70. R(&TaskShowOptions{}, fmt.Sprintf("%s-archived-task-show", service), fmt.Sprintf("Show details of an archived %s task", service), func(s *mcclient.ClientSession, args *TaskShowOptions) error {
  71. params := jsonutils.NewDict()
  72. params.Set("task_id", jsonutils.NewString(args.ID))
  73. result, err := archivedManager.List(s, params)
  74. if err != nil {
  75. return err
  76. }
  77. if result.Total == 1 {
  78. printObject(result.Data[0])
  79. return nil
  80. } else if result.Total == 0 {
  81. return errors.Wrapf(errors.ErrNotFound, "not found %s", args.ID)
  82. } else {
  83. printList(result, archivedManager.GetColumns(s))
  84. return errors.Wrapf(errors.ErrDuplicateId, "found %d record for %s", result.Total, args.ID)
  85. }
  86. })
  87. }
  88. func init() {
  89. cmds := []struct {
  90. service string
  91. manager modulebase.Manager
  92. archivedManager modulebase.Manager
  93. }{
  94. {
  95. service: "region",
  96. manager: &compute.ComputeTasks,
  97. archivedManager: &compute.ArchivedComputeTasks,
  98. },
  99. {
  100. service: "devtool",
  101. manager: &devtool.DevtoolTasks,
  102. archivedManager: &devtool.ArchivedDevtoolTasks,
  103. },
  104. {
  105. service: "image",
  106. manager: &image.Tasks,
  107. archivedManager: &image.ArchivedTasks,
  108. },
  109. {
  110. service: "identity",
  111. manager: &identity.Tasks,
  112. archivedManager: &identity.ArchivedTasks,
  113. },
  114. {
  115. service: "k8s",
  116. manager: k8s.KubeTasks,
  117. archivedManager: k8s.ArchivedKubeTasks,
  118. },
  119. {
  120. service: "notify",
  121. manager: &notify.Tasks,
  122. archivedManager: &notify.ArchivedTasks,
  123. },
  124. }
  125. for i := range cmds {
  126. c := cmds[i]
  127. RegisterTaskCmds(c.service, c.manager, c.archivedManager)
  128. }
  129. }