helper.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. "database/sql"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/utils"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. )
  27. func RunBatchCreateTask(
  28. ctx context.Context,
  29. items []db.IModel,
  30. userCred mcclient.TokenCredential,
  31. data []jsonutils.JSONObject,
  32. pendingUsage SQuota,
  33. pendingRegionUsage SRegionQuota,
  34. taskName string,
  35. parentTaskId string,
  36. ) error {
  37. taskItems := make([]db.IStandaloneModel, len(items))
  38. for i, t := range items {
  39. taskItems[i] = t.(db.IStandaloneModel)
  40. }
  41. params := jsonutils.NewDict()
  42. params.Set("data", jsonutils.NewArray(data...))
  43. task, err := taskman.TaskManager.NewParallelTask(ctx, taskName, taskItems, userCred, params, parentTaskId, "", &pendingUsage, &pendingRegionUsage)
  44. if err != nil {
  45. return errors.Wrapf(err, "NewParallelTask %s", taskName)
  46. }
  47. return task.ScheduleRun(nil)
  48. }
  49. func ValidateScheduleCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *api.ServerCreateInput, hypervisor string) (*api.ServerCreateInput, error) {
  50. var err error
  51. if input.Baremetal {
  52. hypervisor = api.HYPERVISOR_BAREMETAL
  53. }
  54. // base validate_create_data
  55. if (input.PreferHost != "") && hypervisor != api.HYPERVISOR_POD {
  56. bmName := input.PreferHost
  57. bmObj, err := HostManager.FetchByIdOrName(ctx, nil, bmName)
  58. if err != nil {
  59. if err == sql.ErrNoRows {
  60. return nil, httperrors.NewResourceNotFoundError("Host %s not found", bmName)
  61. } else {
  62. return nil, httperrors.NewGeneralError(err)
  63. }
  64. }
  65. baremetal := bmObj.(*SHost)
  66. err = baremetal.IsAssignable(ctx, userCred)
  67. if err != nil {
  68. return nil, errors.Wrap(err, "IsAssignable")
  69. }
  70. if !baremetal.GetEnabled() {
  71. return nil, httperrors.NewInvalidStatusError("Baremetal %s not enabled", bmName)
  72. }
  73. hostDriver, err := baremetal.GetHostDriver()
  74. if err != nil {
  75. return nil, errors.Wrapf(err, "GetHostDriver")
  76. }
  77. if len(hypervisor) > 0 && hypervisor != hostDriver.GetHypervisor() {
  78. return nil, httperrors.NewInputParameterError("cannot run hypervisor %s on specified host with type %s", hypervisor, baremetal.HostType)
  79. }
  80. if len(hypervisor) == 0 {
  81. hypervisor = hostDriver.GetHypervisor()
  82. }
  83. if len(hypervisor) == 0 {
  84. hypervisor = api.HYPERVISOR_DEFAULT
  85. }
  86. driver, err := GetDriver(hypervisor, input.Provider)
  87. if err != nil {
  88. return nil, err
  89. }
  90. _, err = driver.ValidateCreateDataOnHost(ctx, userCred, bmName, baremetal, input)
  91. if err != nil {
  92. return nil, err
  93. }
  94. defaultStorage, err := driver.ChooseHostStorage(baremetal, nil, &api.DiskConfig{}, nil)
  95. if err != nil {
  96. return nil, errors.Wrap(err, "ChooseHostStorage")
  97. }
  98. if defaultStorage == nil {
  99. return nil, httperrors.NewInsufficientResourceError("no valid storage on host")
  100. }
  101. input.PreferHost = baremetal.Id
  102. input.DefaultStorageType = defaultStorage.StorageType
  103. zone, _ := baremetal.GetZone()
  104. input.PreferZone = zone.Id
  105. region, _ := zone.GetRegion()
  106. input.PreferRegion = region.Id
  107. } else {
  108. if len(input.Schedtags) > 0 {
  109. input.Schedtags, err = SchedtagManager.ValidateSchedtags(ctx, userCred, input.Schedtags)
  110. if err != nil {
  111. return nil, httperrors.NewInputParameterError("invalid aggregate_strategy: %s", err)
  112. }
  113. }
  114. if input.PreferWire != "" {
  115. wireStr := input.PreferWire
  116. wireObj, err := WireManager.FetchByIdOrName(ctx, userCred, wireStr)
  117. if err != nil {
  118. if err == sql.ErrNoRows {
  119. return nil, httperrors.NewResourceNotFoundError("Wire %s not found", wireStr)
  120. } else {
  121. return nil, httperrors.NewGeneralError(err)
  122. }
  123. }
  124. wire := wireObj.(*SWire)
  125. input.PreferWire = wire.Id
  126. zone, _ := wire.GetZone()
  127. input.PreferZone = zone.Id
  128. region, _ := zone.GetRegion()
  129. input.PreferRegion = region.Id
  130. } else if input.PreferZone != "" {
  131. zoneStr := input.PreferZone
  132. zoneObj, err := ZoneManager.FetchByIdOrName(ctx, userCred, zoneStr)
  133. if err != nil {
  134. if err == sql.ErrNoRows {
  135. return nil, httperrors.NewResourceNotFoundError("Zone %s not found", zoneStr)
  136. } else {
  137. return nil, httperrors.NewGeneralError(err)
  138. }
  139. }
  140. zone := zoneObj.(*SZone)
  141. input.PreferZone = zone.Id
  142. region, _ := zone.GetRegion()
  143. input.PreferRegion = region.Id
  144. } else if input.PreferRegion != "" {
  145. regionStr := input.PreferRegion
  146. regionObj, err := CloudregionManager.FetchByIdOrName(ctx, userCred, regionStr)
  147. if err != nil {
  148. if err == sql.ErrNoRows {
  149. return nil, httperrors.NewResourceNotFoundError("Region %s not found", regionStr)
  150. } else {
  151. return nil, httperrors.NewGeneralError(err)
  152. }
  153. }
  154. region := regionObj.(*SCloudregion)
  155. input.PreferRegion = region.Id
  156. }
  157. }
  158. // default hypervisor
  159. if len(hypervisor) == 0 {
  160. hypervisor = api.HYPERVISOR_KVM
  161. }
  162. if !utils.IsInStringArray(hypervisor, api.HYPERVISORS) {
  163. return nil, httperrors.NewInputParameterError("Hypervisor %s not supported", hypervisor)
  164. }
  165. input.Hypervisor = hypervisor
  166. return input, nil
  167. }