sshinfo.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 models
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/tristate"
  20. api "yunion.io/x/onecloud/pkg/apis/devtool"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. )
  25. type SSshInfo struct {
  26. db.SStatusStandaloneResourceBase
  27. ServerId string `width:"128" charset:"ascii" list:"user" create:"required"`
  28. ServerName string `width:"128" charset:"utf8" list:"user" create:"optional"`
  29. ServerHypervisor string `width:"16" charset:"ascii" create:"optional"`
  30. ForwardId string `width:"128" charset:"ascii" create:"optional"`
  31. User string `width:"36" list:"user" create:"optional"`
  32. Host string `width:"36" charset:"ascii" list:"user" create:"optional"`
  33. Port int `width:"8" charset:"ascii" list:"user" create:"optional"`
  34. NeedClean tristate.TriState
  35. FailedReason string
  36. }
  37. type SSshInfoManager struct {
  38. db.SStatusStandaloneResourceBaseManager
  39. }
  40. var SshInfoManager *SSshInfoManager
  41. func init() {
  42. SshInfoManager = &SSshInfoManager{
  43. SStatusStandaloneResourceBaseManager: db.NewStatusStandaloneResourceBaseManager(
  44. SSshInfo{},
  45. "sshinfo_tbl",
  46. "sshinfo",
  47. "sshinfos",
  48. ),
  49. }
  50. SshInfoManager.SetVirtualObject(SshInfoManager)
  51. }
  52. func (si *SSshInfo) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
  53. si.SetStatus(ctx, userCred, api.SSHINFO_STATUS_CREATING, "")
  54. task, err := taskman.TaskManager.NewTask(ctx, "SshInfoCreateTask", si, userCred, nil, "", "")
  55. if err != nil {
  56. log.Errorf("start SshInfoCreateTask failed: %v", err)
  57. }
  58. task.ScheduleRun(nil)
  59. }
  60. func (si *SSshInfo) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
  61. return nil
  62. }
  63. func (si *SSshInfo) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error {
  64. return si.SStatusStandaloneResourceBase.Delete(ctx, userCred)
  65. }
  66. func (si *SSshInfo) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
  67. si.SetStatus(ctx, userCred, api.SSHINFO_STATUS_DELETING, "")
  68. task, err := taskman.TaskManager.NewTask(ctx, "SshInfoDeleteTask", si, userCred, nil, "", "")
  69. if err != nil {
  70. log.Errorf("start SshInfoDeleteTask failed: %v", err)
  71. }
  72. task.ScheduleRun(nil)
  73. return nil
  74. }
  75. func (si *SSshInfo) MarkCreateFailed(reason string) {
  76. _, err := db.Update(si, func() error {
  77. si.Status = api.SSHINFO_STATUS_CREATE_FAILED
  78. si.FailedReason = reason
  79. return nil
  80. })
  81. if err != nil {
  82. log.Errorf("unable to mark createfailed for sshinfo: %v", err)
  83. }
  84. }
  85. func (si *SSshInfo) MarkDeleteFailed(reason string) {
  86. _, err := db.Update(si, func() error {
  87. si.Status = api.SSHINFO_STATUS_DELETE_FAILED
  88. si.FailedReason = reason
  89. return nil
  90. })
  91. if err != nil {
  92. log.Errorf("unable to mark deletefailed for sshinfo: %v", err)
  93. }
  94. }