service_url.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. "reflect"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/gotypes"
  21. api "yunion.io/x/onecloud/pkg/apis/devtool"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. )
  26. type SServiceUrl struct {
  27. db.SStatusStandaloneResourceBase
  28. Service string `width:"32" charset:"ascii" list:"user" create:"required"`
  29. ServerId string `width:"128" charset:"ascii" list:"user" create:"required"`
  30. Url string `wdith:"32" charset:"ascii" list:"user"`
  31. ServerAnsibleInfo *SServerAnisbleInfo `width:"128" list:"user" create:"required"`
  32. FailedReason string
  33. }
  34. type SServiceUrlManager struct {
  35. db.SStatusStandaloneResourceBaseManager
  36. }
  37. type SServerAnisbleInfo struct {
  38. User string `json:"user"`
  39. IP string `json:"ip"`
  40. Port int `json:"port"`
  41. Name string `json:"name"`
  42. }
  43. var ServiceUrlManager *SServiceUrlManager
  44. func init() {
  45. gotypes.RegisterSerializable(reflect.TypeOf(&SServerAnisbleInfo{}), func() gotypes.ISerializable {
  46. return &SServerAnisbleInfo{}
  47. })
  48. ServiceUrlManager = &SServiceUrlManager{
  49. SStatusStandaloneResourceBaseManager: db.NewStatusStandaloneResourceBaseManager(
  50. SServiceUrl{},
  51. "serviceurl_tbl",
  52. "serviceurl",
  53. "serviceurls",
  54. ),
  55. }
  56. ServiceUrlManager.SetVirtualObject(ServiceUrlManager)
  57. }
  58. func (ai *SServerAnisbleInfo) String() string {
  59. return jsonutils.Marshal(ai).String()
  60. }
  61. func (ai *SServerAnisbleInfo) IsZero() bool {
  62. return ai == nil
  63. }
  64. func (su *SServiceUrl) MarkCreateFailed(reason string) {
  65. _, err := db.Update(su, func() error {
  66. su.Status = api.SERVICEURL_STATUS_CREATE_FAILED
  67. su.FailedReason = reason
  68. return nil
  69. })
  70. if err != nil {
  71. log.Errorf("unable to mark createfailed for sshinfo: %v", err)
  72. }
  73. }
  74. func (su *SServiceUrl) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
  75. su.SetStatus(ctx, userCred, api.SERVICEURL_STATUS_CREATING, "")
  76. task, err := taskman.TaskManager.NewTask(ctx, "ServiceUrlCreateTask", su, userCred, nil, "", "")
  77. if err != nil {
  78. log.Errorf("start ServiceUrlCreateTask failed: %v", err)
  79. }
  80. task.ScheduleRun(nil)
  81. }