hostfiles.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/sqlchemy"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  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. "yunion.io/x/onecloud/pkg/util/stringutils2"
  27. )
  28. type SHostFileManager struct {
  29. db.SInfrasResourceBaseManager
  30. }
  31. var HostFileManager *SHostFileManager
  32. func init() {
  33. HostFileManager = &SHostFileManager{
  34. SInfrasResourceBaseManager: db.NewInfrasResourceBaseManager(
  35. SHostFile{},
  36. "hostfiles_tbl",
  37. "hostfile",
  38. "hostfiles",
  39. ),
  40. }
  41. HostFileManager.SetVirtualObject(HostFileManager)
  42. }
  43. // +onecloud:model-api-gen
  44. type SHostFile struct {
  45. db.SInfrasResourceBase
  46. Type computeapi.HostFileType `width:"64" charset:"ascii" nullable:"false" list:"domain" create:"domain_required"`
  47. Path string `width:"256" charset:"utf8" nullable:"true" list:"domain" update:"domain" create:"domain_optional"`
  48. Content string `charset:"utf8" nullable:"true" get:"domain" update:"domain" create:"domain_optional"`
  49. }
  50. func (manager *SHostFileManager) ValidateCreateData(
  51. ctx context.Context,
  52. userCred mcclient.TokenCredential,
  53. ownerId mcclient.IIdentityProvider,
  54. query jsonutils.JSONObject,
  55. input computeapi.HostFileCreateInput,
  56. ) (computeapi.HostFileCreateInput, error) {
  57. var err error
  58. input.InfrasResourceBaseCreateInput, err = manager.SInfrasResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, input.InfrasResourceBaseCreateInput)
  59. if err != nil {
  60. return input, errors.Wrap(err, "SInfrasResourceBaseManager.ValidateCreateData")
  61. }
  62. if len(input.Type) == 0 {
  63. return input, httperrors.NewInputParameterError("Type is required")
  64. }
  65. return input, nil
  66. }
  67. func (manager *SHostFileManager) ValidateUpdateData(
  68. ctx context.Context,
  69. userCred mcclient.TokenCredential,
  70. ownerId mcclient.IIdentityProvider,
  71. query jsonutils.JSONObject,
  72. input computeapi.HostFileUpdateInput,
  73. ) (computeapi.HostFileUpdateInput, error) {
  74. return input, nil
  75. }
  76. func (manager *SHostFileManager) ListItemFilter(
  77. ctx context.Context,
  78. q *sqlchemy.SQuery,
  79. userCred mcclient.TokenCredential,
  80. query computeapi.HostFileListInput,
  81. ) (*sqlchemy.SQuery, error) {
  82. var err error
  83. q, err = manager.SInfrasResourceBaseManager.ListItemFilter(ctx, q, userCred, query.InfrasResourceBaseListInput)
  84. if err != nil {
  85. return nil, errors.Wrap(err, "SInfrasResourceBaseManager.ListItemFilter")
  86. }
  87. if len(query.Type) > 0 {
  88. q = q.In("type", query.Type)
  89. }
  90. if len(query.Path) > 0 {
  91. q = q.Equals("path", query.Path)
  92. }
  93. return q, nil
  94. }
  95. func (manager *SHostFileManager) FetchCustomizeColumns(
  96. ctx context.Context,
  97. userCred mcclient.TokenCredential,
  98. query jsonutils.JSONObject,
  99. objs []interface{},
  100. fields stringutils2.SSortedStrings,
  101. isList bool,
  102. ) []api.HostFileDetails {
  103. rows := make([]api.HostFileDetails, len(objs))
  104. infrasRows := manager.SInfrasResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
  105. hostFileIds := make([]string, 0, len(objs))
  106. for i := range rows {
  107. rows[i] = api.HostFileDetails{
  108. InfrasResourceBaseDetails: infrasRows[i],
  109. }
  110. hostFileIds = append(hostFileIds, objs[i].(*SHostFile).Id)
  111. }
  112. hostFiles, err := fetchHostFilesHosts(hostFileIds)
  113. if err != nil {
  114. log.Errorf("fetchHostFilesHosts error: %v", err)
  115. }
  116. for i := range rows {
  117. rows[i].Hosts = hostFiles[hostFileIds[i]]
  118. }
  119. return rows
  120. }
  121. func (hostfile *SHostFile) ValidateDeleteCondition(ctx context.Context, info api.HostFileDetails) error {
  122. err := hostfile.SInfrasResourceBase.ValidateDeleteCondition(ctx, jsonutils.Marshal(info))
  123. if err != nil {
  124. return errors.Wrap(err, "SInfrasResourceBase.ValidateDeleteCondition")
  125. }
  126. if len(info.Hosts) > 0 {
  127. return httperrors.NewNotEmptyError("hostfile is used by hosts")
  128. }
  129. return nil
  130. }