host_isolated_device_models.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. "yunion.io/x/onecloud/pkg/util/stringutils2"
  23. )
  24. // +onecloud:swagger-gen-ignore
  25. type SHostIsolatedDeviceModelManager struct {
  26. SHostJointsManager
  27. }
  28. var HostIsolatedDeviceModelManager *SHostIsolatedDeviceModelManager
  29. func init() {
  30. db.InitManager(func() {
  31. HostIsolatedDeviceModelManager = &SHostIsolatedDeviceModelManager{
  32. SHostJointsManager: NewHostJointsManager(
  33. "host_id",
  34. SHostIsolatedDeviceModel{},
  35. "host_isolated_device_models_tbl",
  36. "host_isolated_device_model",
  37. "host_isolated_device_models",
  38. IsolatedDeviceModelManager,
  39. ),
  40. }
  41. HostIsolatedDeviceModelManager.SetVirtualObject(HostIsolatedDeviceModelManager)
  42. HostIsolatedDeviceModelManager.TableSpec().AddIndex(false, "host_id", "isolated_device_model_id")
  43. })
  44. }
  45. // +onecloud:swagger-gen-ignore
  46. type SHostIsolatedDeviceModel struct {
  47. SHostJointsBase
  48. // 宿主机Id
  49. HostId string `width:"36" charset:"ascii" nullable:"false" list:"domain" create:"required" json:"host_id"`
  50. // 设备类型Id
  51. IsolatedDeviceModelId string `width:"36" charset:"ascii" nullable:"false" list:"domain" create:"required" json:"isolated_device_model_id" index:"true"`
  52. }
  53. func (manager *SHostIsolatedDeviceModelManager) GetMasterFieldName() string {
  54. return "host_id"
  55. }
  56. func (manager *SHostIsolatedDeviceModelManager) GetSlaveFieldName() string {
  57. return "isolated_device_model_id"
  58. }
  59. func (self *SHostIsolatedDeviceModel) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
  60. return db.DeleteModel(ctx, userCred, self)
  61. }
  62. func (self *SHostIsolatedDeviceModel) Detach(ctx context.Context, userCred mcclient.TokenCredential) error {
  63. return db.DetachJoint(ctx, userCred, self)
  64. }
  65. func (manager *SHostIsolatedDeviceModelManager) FetchCustomizeColumns(
  66. ctx context.Context,
  67. userCred mcclient.TokenCredential,
  68. query jsonutils.JSONObject,
  69. objs []interface{},
  70. fields stringutils2.SSortedStrings,
  71. isList bool,
  72. ) []api.HostIsolatedDeviceModelDetails {
  73. rows := make([]api.HostIsolatedDeviceModelDetails, len(objs))
  74. hostRows := manager.SHostJointsManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
  75. devModelIds := make([]string, len(rows))
  76. for i := range rows {
  77. rows[i] = api.HostIsolatedDeviceModelDetails{
  78. HostJointResourceDetails: hostRows[i],
  79. }
  80. devModelIds[i] = objs[i].(*SHostIsolatedDeviceModel).IsolatedDeviceModelId
  81. }
  82. devModels := make(map[string]SIsolatedDeviceModel)
  83. err := db.FetchStandaloneObjectsByIds(IsolatedDeviceModelManager, devModelIds, &devModels)
  84. if err != nil {
  85. log.Errorf("db.FetchStandaloneObjectsByIds fail %s", err)
  86. return rows
  87. }
  88. for i := range rows {
  89. if devModel, ok := devModels[devModelIds[i]]; ok {
  90. rows[i] = objs[i].(*SHostIsolatedDeviceModel).getExtraDetails(devModel, rows[i])
  91. }
  92. }
  93. return rows
  94. }
  95. func (self *SHostIsolatedDeviceModel) getExtraDetails(devModel SIsolatedDeviceModel, out api.HostIsolatedDeviceModelDetails) api.HostIsolatedDeviceModelDetails {
  96. out.Model = devModel.Model
  97. out.VendorId = devModel.VendorId
  98. out.DeviceId = devModel.DeviceId
  99. out.DevType = devModel.DevType
  100. out.HotPluggable = devModel.HotPluggable.Bool()
  101. out.DisableAutoDetect = devModel.DisableAutoDetect.Bool()
  102. return out
  103. }
  104. func (self *SHostIsolatedDeviceModel) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
  105. self.SHostJointsBase.PostCreate(ctx, userCred, ownerId, query, data)
  106. iHost, err := HostManager.FetchByIdOrName(ctx, userCred, self.HostId)
  107. if err != nil {
  108. log.Errorf("failed fetch host %s: %s", self.HostId, err)
  109. return
  110. }
  111. host := iHost.(*SHost)
  112. log.Infof("start request host %s scan isolated devices", host.GetName())
  113. if err := host.RequestScanIsolatedDevices(ctx, userCred); err != nil {
  114. log.Errorf("failed scan isolated device %s", err)
  115. }
  116. }