profiles.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 baremetal
  15. import (
  16. "context"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/sqlchemy"
  21. baremetalapi "yunion.io/x/onecloud/pkg/apis/compute/baremetal"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. "yunion.io/x/onecloud/pkg/util/stringutils2"
  26. )
  27. type SBaremetalProfileManager struct {
  28. db.SStandaloneAnonResourceBaseManager
  29. }
  30. type SBaremetalProfile struct {
  31. db.SStandaloneAnonResourceBase
  32. // 品牌名称(English)
  33. OemName string `width:"64" charset:"ascii" nullable:"false" default:"" index:"true" list:"user" create:"required"`
  34. Model string `width:"64" charset:"utf8" list:"user" create:"optional"`
  35. LanChannel uint8 `list:"user" update:"user" create:"optional"`
  36. LanChannel2 uint8 `list:"user" update:"user" create:"optional"`
  37. LanChannel3 uint8 `list:"user" update:"user" create:"optional"`
  38. // BMC Root账号名称,默认为 root
  39. RootName string `width:"64" charset:"ascii" nullable:"false" list:"user" update:"user" create:"required" default:"root"`
  40. // BMC Root账号ID,默认为 2
  41. RootId int `list:"user" update:"user" create:"optional" default:"2"`
  42. // 是否要求强密码
  43. StrongPass bool `list:"user" update:"user" create:"optional"`
  44. }
  45. var BaremetalProfileManager *SBaremetalProfileManager
  46. func init() {
  47. BaremetalProfileManager = &SBaremetalProfileManager{
  48. SStandaloneAnonResourceBaseManager: db.NewStandaloneAnonResourceBaseManager(
  49. SBaremetalProfile{},
  50. "baremetal_profiles_tbl",
  51. "baremetal_profile",
  52. "baremetal_profiles",
  53. ),
  54. }
  55. BaremetalProfileManager.SetVirtualObject(BaremetalProfileManager)
  56. }
  57. func (bpm *SBaremetalProfileManager) ListItemFilter(
  58. ctx context.Context,
  59. q *sqlchemy.SQuery,
  60. userCred mcclient.TokenCredential,
  61. query baremetalapi.BaremetalProfileListInput,
  62. ) (*sqlchemy.SQuery, error) {
  63. q, err := bpm.SStandaloneAnonResourceBaseManager.ListItemFilter(ctx, q, userCred, query.StandaloneAnonResourceListInput)
  64. if err != nil {
  65. return nil, errors.Wrap(err, "SStandaloneAnonResourceBaseManager.ListItemFilter")
  66. }
  67. query.Normalize()
  68. if len(query.OemName) > 0 {
  69. q = q.In("oem_name", query.OemName)
  70. }
  71. if len(query.Model) > 0 {
  72. q = q.In("model", query.Model)
  73. }
  74. return q, nil
  75. }
  76. func (bpm *SBaremetalProfileManager) OrderByExtraFields(
  77. ctx context.Context,
  78. q *sqlchemy.SQuery,
  79. userCred mcclient.TokenCredential,
  80. query baremetalapi.BaremetalProfileListInput,
  81. ) (*sqlchemy.SQuery, error) {
  82. var err error
  83. q, err = bpm.SStandaloneAnonResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.StandaloneAnonResourceListInput)
  84. if err != nil {
  85. return nil, errors.Wrap(err, "SStandaloneAnonResourceBaseManager.OrderByExtraFields")
  86. }
  87. return q, nil
  88. }
  89. func (bpm *SBaremetalProfileManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error) {
  90. var err error
  91. q, err = bpm.SStandaloneAnonResourceBaseManager.QueryDistinctExtraField(q, field)
  92. if err == nil {
  93. return q, nil
  94. }
  95. return q, nil
  96. }
  97. func (bpm *SBaremetalProfileManager) ListItemExportKeys(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, keys stringutils2.SSortedStrings) (*sqlchemy.SQuery, error) {
  98. var err error
  99. q, err = bpm.SStandaloneAnonResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
  100. if err != nil {
  101. return nil, errors.Wrap(err, "SVirtualResourceBaseManager.ListItemExportKeys")
  102. }
  103. return q, nil
  104. }
  105. func (bpm *SBaremetalProfileManager) InitializeData() error {
  106. for _, spec := range baremetalapi.PredefinedProfiles {
  107. bp := SBaremetalProfile{}
  108. bp.Id = strings.ToLower(spec.OemName)
  109. if len(bp.Id) == 0 {
  110. bp.Id = baremetalapi.DefaultBaremetalProfileId
  111. }
  112. bp.OemName = strings.ToLower(spec.OemName)
  113. if len(spec.LanChannels) > 0 {
  114. bp.LanChannel = spec.LanChannels[0]
  115. }
  116. if len(spec.LanChannels) > 1 {
  117. bp.LanChannel2 = spec.LanChannels[1]
  118. }
  119. if len(spec.LanChannels) > 2 {
  120. bp.LanChannel3 = spec.LanChannels[2]
  121. }
  122. bp.RootName = spec.RootName
  123. bp.RootId = spec.RootId
  124. bp.StrongPass = spec.StrongPass
  125. bp.SetModelManager(bpm, &bp)
  126. err := bpm.TableSpec().InsertOrUpdate(context.Background(), &bp)
  127. if err != nil {
  128. return errors.Wrapf(err, "insert default baremetal profile %s", jsonutils.Marshal(bp))
  129. }
  130. }
  131. return nil
  132. }
  133. func (bpm *SBaremetalProfileManager) ValidateCreateData(
  134. ctx context.Context,
  135. userCred mcclient.TokenCredential,
  136. ownerId mcclient.IIdentityProvider,
  137. query jsonutils.JSONObject,
  138. input baremetalapi.BaremetalProfileCreateInput,
  139. ) (baremetalapi.BaremetalProfileCreateInput, error) {
  140. var err error
  141. input.StandaloneAnonResourceCreateInput, err = bpm.SStandaloneAnonResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, input.StandaloneAnonResourceCreateInput)
  142. if err != nil {
  143. return input, errors.Wrap(err, "StandaloneAnonResourceBaseManager.ValidateCreateData")
  144. }
  145. input.OemName = strings.ToLower(strings.TrimSpace(input.OemName))
  146. input.Model = strings.ToLower(strings.TrimSpace(input.Model))
  147. // ensure uniquess
  148. uniqQuery := bpm.fetchByOemNameAndModelQuery(input.OemName, input.Model, true)
  149. cnt, err := uniqQuery.CountWithError()
  150. if err != nil {
  151. return input, errors.Wrap(err, "CountWithError")
  152. }
  153. if cnt > 0 {
  154. return input, errors.Wrapf(httperrors.ErrConflict, "%s/%s already exists", input.OemName, input.Model)
  155. }
  156. if input.LanChannel == 0 {
  157. return input, errors.Wrap(httperrors.ErrInputParameter, "lan_channel must be set")
  158. }
  159. if len(input.RootName) == 0 {
  160. return input, errors.Wrap(httperrors.ErrInputParameter, "root_name must be set")
  161. }
  162. return input, nil
  163. }
  164. func (bpm *SBaremetalProfileManager) fetchByOemNameAndModelQuery(oemName, model string, exact bool) *sqlchemy.SQuery {
  165. q := bpm.Query()
  166. if exact {
  167. q = q.Equals("oem_name", oemName)
  168. } else {
  169. q = q.Filter(sqlchemy.OR(
  170. sqlchemy.IsEmpty(q.Field("oem_name")),
  171. sqlchemy.Equals(q.Field("oem_name"), oemName),
  172. ))
  173. }
  174. if exact {
  175. q = q.Equals("model", model)
  176. } else {
  177. q = q.Filter(sqlchemy.OR(
  178. sqlchemy.IsEmpty(q.Field("model")),
  179. sqlchemy.Equals(q.Field("model"), model),
  180. ))
  181. }
  182. return q
  183. }
  184. func (bp *SBaremetalProfile) ValidateUpdateData(
  185. ctx context.Context,
  186. userCred mcclient.TokenCredential,
  187. query jsonutils.JSONObject,
  188. input baremetalapi.BaremetalProfileUpdateInput,
  189. ) (baremetalapi.BaremetalProfileUpdateInput, error) {
  190. var err error
  191. input.StandaloneAnonResourceBaseUpdateInput, err = bp.SStandaloneAnonResourceBase.ValidateUpdateData(ctx, userCred, query, input.StandaloneAnonResourceBaseUpdateInput)
  192. if err != nil {
  193. return input, errors.Wrap(err, "SStandaloneAnonResourceBase.ValidateUpdateData")
  194. }
  195. return input, nil
  196. }