event.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 notify
  15. import (
  16. "fmt"
  17. "strings"
  18. )
  19. var (
  20. Event SNotifyEvent
  21. // 创建
  22. ActionCreate SAction = "create"
  23. // 删除
  24. ActionDelete SAction = "delete"
  25. // 放入回收站
  26. ActionPendingDelete SAction = "pending_delete"
  27. // 更新
  28. ActionUpdate SAction = "update"
  29. // 重装系统
  30. ActionRebuildRoot SAction = "rebuild_root"
  31. // 重置密码
  32. ActionResetPassword SAction = "reset_password"
  33. // 配置变更
  34. ActionChangeConfig SAction = "change_config"
  35. // 扩容
  36. ActionResize SAction = "resize"
  37. // 到期释放
  38. ActionExpiredRelease SAction = "expired_release"
  39. // 执行,例如自动快照策略执行创建快照、弹性伸缩策略执行扩容、定时任务执行等
  40. ActionExecute SAction = "execute"
  41. // IP变更
  42. ActionChangeIpaddr SAction = "change_ipaddr"
  43. // 同步状态
  44. ActionSyncStatus SAction = "sync_status"
  45. // 清理数据
  46. ActionCleanData SAction = "clean_data"
  47. // 迁移
  48. ActionMigrate SAction = "migrate"
  49. // 添加备份服务器
  50. ActionCreateBackupServer SAction = "add_backup_server"
  51. // 删除备份服务器
  52. ActionDelBackupServer SAction = "delete_backup_server"
  53. // 同步新建
  54. ActionSyncCreate SAction = "sync_create"
  55. // 同步更新
  56. ActionSyncUpdate SAction = "sync_update"
  57. // 同步删除
  58. ActionSyncDelete SAction = "sync_delete"
  59. // 同步账号状态
  60. ActionSyncAccountStatus SAction = "sync_account_status"
  61. // 下线
  62. ActionOffline SAction = "offline"
  63. // 系统崩溃
  64. ActionSystemPanic SAction = "panic"
  65. // 系统异常
  66. ActionSystemException SAction = "exception"
  67. // 锁定,例如用户锁定
  68. ActionLock SAction = "lock"
  69. // 超出数量
  70. ActionExceedCount SAction = "exceed_count"
  71. // 密码即将过期
  72. ActionPasswordExpireSoon SAction = "password_expire_soon"
  73. // 一致性检查
  74. ActionChecksumTest SAction = "checksum_test"
  75. // 任务队列阻塞
  76. ActionWorkerBlock SAction = "woker_block"
  77. // 网络同步失败
  78. ActionNetOutOfSync SAction = "net_out_of_sync"
  79. // MySQL同步失败
  80. ActionMysqlOutOfSync SAction = "mysql_out_of_sync"
  81. // 服务异常
  82. ActionServiceAbnormal SAction = "service_abnormal"
  83. // 服务器崩溃
  84. ActionServerPanicked SAction = "server_panicked"
  85. // 挂载
  86. ActionAttach SAction = "attach"
  87. // 卸载
  88. ActionDetach SAction = "detach"
  89. // 宿主机宕机
  90. ActionHostDown SAction = "host_down"
  91. // 宕机自动迁移
  92. ActionHostDownAutoMigrate SAction = "host_down_auto_migrate"
  93. // 透传设备创建
  94. ActionIsolatedDeviceCreate SAction = "isolated_device_create"
  95. // 透传设备更新
  96. ActionIsolatedDeviceUpdate SAction = "isolated_device_update"
  97. // 透传设备删除
  98. ActionIsolatedDeviceDelete SAction = "isolated_device_delete"
  99. // 状态变更
  100. ActionStatusChanged SAction = "status_changed"
  101. // 启动
  102. ActionStart SAction = "start"
  103. // 停止
  104. ActionStop SAction = "stop"
  105. // 重置
  106. ActionReset SAction = "reset"
  107. // 重启
  108. ActionRestart SAction = "restart"
  109. // 运行任务
  110. ActionRunTask SAction = "run_task"
  111. ResultFailed SResult = "failed"
  112. ResultSucceed SResult = "succeed"
  113. )
  114. const (
  115. DelimiterInEvent = "/"
  116. )
  117. type SAction string
  118. type SResult string
  119. type SNotifyEvent struct {
  120. resourceType string
  121. action SAction
  122. result SResult
  123. }
  124. func (se SNotifyEvent) WithResourceType(rt string) SNotifyEvent {
  125. se.resourceType = rt
  126. return se
  127. }
  128. func (se SNotifyEvent) WithAction(a SAction) SNotifyEvent {
  129. se.action = a
  130. return se
  131. }
  132. func (se SNotifyEvent) WithResult(r SResult) SNotifyEvent {
  133. se.result = r
  134. return se
  135. }
  136. func (se SNotifyEvent) ResourceType() string {
  137. return se.resourceType
  138. }
  139. func (se SNotifyEvent) Action() SAction {
  140. return se.action
  141. }
  142. func (se SNotifyEvent) ActionWithResult(delimiter string) string {
  143. ar := string(se.action)
  144. if len(se.result) > 0 {
  145. ar += delimiter + string(se.result)
  146. }
  147. return strings.ToUpper(ar)
  148. }
  149. func (se SNotifyEvent) Result() SResult {
  150. if se.result == "" {
  151. return ResultSucceed
  152. }
  153. return se.result
  154. }
  155. func (se SNotifyEvent) String() string {
  156. return se.StringWithDeli(DelimiterInEvent)
  157. }
  158. func (se SNotifyEvent) StringWithDeli(delimiter string) string {
  159. str := strings.ToUpper(fmt.Sprintf("%s%s%s", se.ResourceType(), delimiter, se.Action()))
  160. if se.result != "" {
  161. str += delimiter + strings.ToUpper(string(se.result))
  162. }
  163. return str
  164. }