hostfilejoints.go 6.2 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 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. "yunion.io/x/sqlchemy"
  22. computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. )
  27. type SHostFileJontsManager struct {
  28. db.SModelBaseManager
  29. }
  30. var HostFileJointsManager *SHostFileJontsManager
  31. func init() {
  32. HostFileJointsManager = &SHostFileJontsManager{
  33. SModelBaseManager: db.NewModelBaseManager(
  34. SHostFileJoint{},
  35. "hostfilejoints_tbl",
  36. "hostfilejoint",
  37. "hostfilejoints",
  38. ),
  39. }
  40. HostFileJointsManager.SetVirtualObject(HostFileJointsManager)
  41. }
  42. // +onecloud:model-api-gen
  43. type SHostFileJoint struct {
  44. db.SModelBase
  45. HostId string `width:"128" charset:"ascii" nullable:"false" primary:"true"`
  46. HostFileId string `width:"128" charset:"ascii" nullable:"false" primary:"true"`
  47. Deleted bool `nullable:"false"`
  48. }
  49. func (host *SHost) PerformSetHostFiles(
  50. ctx context.Context,
  51. userCred mcclient.TokenCredential,
  52. query jsonutils.JSONObject,
  53. input computeapi.HostSetHostFilesInput,
  54. ) (jsonutils.JSONObject, error) {
  55. hostFileIds := make([]string, 0)
  56. for _, hostFileName := range input.HostFiles {
  57. hostFileObj, err := HostFileManager.FetchByIdOrName(ctx, userCred, hostFileName)
  58. if err != nil {
  59. if errors.Cause(err) == sql.ErrNoRows {
  60. return nil, httperrors.NewResourceNotFoundError2(HostFileManager.Keyword(), hostFileName)
  61. }
  62. return nil, errors.Wrap(err, "HostFileManager.FetchByIdOrName")
  63. }
  64. hostFile := hostFileObj.(*SHostFile)
  65. if hostFile.DomainId != host.DomainId {
  66. domains := hostFile.GetSharedDomains()
  67. if !utils.IsInArray(host.DomainId, domains) {
  68. return nil, errors.Wrapf(httperrors.ErrNoPermission, "host file %s not accessible to host %s", hostFile.Name, host.Name)
  69. }
  70. }
  71. hostFileIds = append(hostFileIds, hostFileObj.GetId())
  72. }
  73. err := HostFileJointsManager.setHostFiles(ctx, userCred, host.Id, hostFileIds)
  74. if err != nil {
  75. return nil, errors.Wrap(err, "HostFileJontsManager.setHostFiles")
  76. }
  77. return nil, nil
  78. }
  79. func (manager *SHostFileJontsManager) getHostFiles(hostId string) ([]SHostFileJoint, error) {
  80. q := manager.Query().Equals("host_id", hostId)
  81. hostFiles := make([]SHostFileJoint, 0)
  82. err := db.FetchModelObjects(manager, q, &hostFiles)
  83. if err != nil {
  84. return nil, errors.Wrap(err, "db.FetchModelObjects")
  85. }
  86. return hostFiles, nil
  87. }
  88. func (manager *SHostFileJontsManager) setHostFiles(
  89. ctx context.Context,
  90. userCred mcclient.TokenCredential,
  91. hostId string,
  92. hostFileIds []string,
  93. ) error {
  94. hostFiles, err := manager.getHostFiles(hostId)
  95. if err != nil {
  96. return errors.Wrap(err, "manager.getHostFiles")
  97. }
  98. existingIds := make(map[string]bool)
  99. for i := range hostFiles {
  100. existingIds[hostFiles[i].HostFileId] = true
  101. if utils.IsInStringArray(hostFiles[i].HostFileId, hostFileIds) {
  102. hostFiles[i].Deleted = false
  103. } else {
  104. hostFiles[i].Deleted = true
  105. }
  106. }
  107. for i := range hostFileIds {
  108. if _, ok := existingIds[hostFileIds[i]]; !ok {
  109. hostFileJoint := SHostFileJoint{
  110. HostId: hostId,
  111. HostFileId: hostFileIds[i],
  112. Deleted: false,
  113. }
  114. hostFileJoint.SetModelManager(manager, &hostFileJoint)
  115. hostFiles = append(hostFiles, hostFileJoint)
  116. }
  117. }
  118. errs := make([]error, 0)
  119. for i := range hostFiles {
  120. err := manager.TableSpec().InsertOrUpdate(ctx, &hostFiles[i])
  121. if err != nil {
  122. errs = append(errs, err)
  123. }
  124. }
  125. if len(errs) > 0 {
  126. return errors.NewAggregate(errs)
  127. }
  128. return nil
  129. }
  130. func (h *SHost) getHostFiles() ([]computeapi.SHostFile, error) {
  131. q := HostFileManager.Query()
  132. jointQ := HostFileJointsManager.Query().Equals("host_id", h.Id).SubQuery()
  133. q = q.Join(jointQ, sqlchemy.Equals(q.Field("id"), jointQ.Field("host_file_id")))
  134. hostFiles := make([]computeapi.SHostFile, 0)
  135. err := q.All(&hostFiles)
  136. if err != nil {
  137. return nil, errors.Wrap(err, "q.All")
  138. }
  139. return hostFiles, nil
  140. }
  141. func fetchHostHostFiles(hostIds []string) (map[string][]string, error) {
  142. q := HostFileJointsManager.Query().In("host_id", hostIds)
  143. hostFilesQ := HostFileManager.Query().SubQuery()
  144. q = q.Join(hostFilesQ, sqlchemy.Equals(q.Field("host_file_id"), hostFilesQ.Field("id")))
  145. q = q.AppendField(q.Field("host_id"))
  146. q = q.AppendField(hostFilesQ.Field("name"))
  147. results := make([]struct {
  148. HostId string
  149. Name string
  150. }, 0)
  151. err := q.All(&results)
  152. if err != nil {
  153. return nil, errors.Wrap(err, "q.All")
  154. }
  155. hostFiles := make(map[string][]string)
  156. for _, result := range results {
  157. hostFiles[result.HostId] = append(hostFiles[result.HostId], result.Name)
  158. }
  159. return hostFiles, nil
  160. }
  161. func fetchHostFilesHosts(hostFileIds []string) (map[string][]string, error) {
  162. q := HostFileJointsManager.Query().In("host_file_id", hostFileIds)
  163. hostQ := HostManager.Query().SubQuery()
  164. q = q.Join(hostQ, sqlchemy.Equals(q.Field("host_id"), hostQ.Field("id")))
  165. q = q.AppendField(q.Field("host_file_id"))
  166. q = q.AppendField(hostQ.Field("name"))
  167. results := make([]struct {
  168. HostFileId string
  169. Name string
  170. }, 0)
  171. err := q.All(&results)
  172. if err != nil {
  173. return nil, errors.Wrap(err, "q.All")
  174. }
  175. hostFiles := make(map[string][]string)
  176. for _, result := range results {
  177. hostFiles[result.HostFileId] = append(hostFiles[result.HostFileId], result.Name)
  178. }
  179. return hostFiles, nil
  180. }
  181. func (host *SHost) GetDetailsHostFiles(
  182. ctx context.Context,
  183. userCred mcclient.TokenCredential,
  184. query jsonutils.JSONObject,
  185. ) (jsonutils.JSONObject, error) {
  186. hostFiles, err := host.getHostFiles()
  187. if err != nil {
  188. return nil, errors.Wrap(err, "GetHostFiles")
  189. }
  190. hostFilesObj := jsonutils.NewDict()
  191. hostFilesObj.Add(jsonutils.Marshal(hostFiles), "host_files")
  192. return hostFilesObj, nil
  193. }