cloudaccountresource.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/pkg/util/reflectutils"
  21. "yunion.io/x/sqlchemy"
  22. api "yunion.io/x/onecloud/pkg/apis/cloudid"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/validators"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. "yunion.io/x/onecloud/pkg/util/stringutils2"
  27. )
  28. // +onecloud:swagger-gen-ignore
  29. type SCloudaccountResourceBaseManager struct {
  30. }
  31. type SCloudaccountResourceBase struct {
  32. // 云账号Id
  33. CloudaccountId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" json:"cloudaccount_id"`
  34. }
  35. func (manager *SCloudaccountResourceBaseManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query api.CloudaccountResourceListInput) (*sqlchemy.SQuery, error) {
  36. if len(query.CloudaccountId) > 0 {
  37. _, err := validators.ValidateModel(ctx, userCred, CloudaccountManager, &query.CloudaccountId)
  38. if err != nil {
  39. return nil, err
  40. }
  41. q = q.Equals("cloudaccount_id", query.CloudaccountId)
  42. }
  43. if len(query.Provider) > 0 {
  44. sq := CloudaccountManager.Query().SubQuery()
  45. q = q.Join(sq, sqlchemy.Equals(q.Field("cloudaccount_id"), sq.Field("id"))).Filter(sqlchemy.In(sq.Field("provider"), query.Provider))
  46. }
  47. return q, nil
  48. }
  49. func (manager *SCloudaccountResourceBaseManager) FetchCustomizeColumns(
  50. ctx context.Context,
  51. userCred mcclient.TokenCredential,
  52. query jsonutils.JSONObject,
  53. objs []interface{},
  54. fields stringutils2.SSortedStrings,
  55. isList bool,
  56. ) []api.CloudaccountResourceDetails {
  57. rows := make([]api.CloudaccountResourceDetails, len(objs))
  58. accountIds := make([]string, len(objs))
  59. for i := range objs {
  60. var base *SCloudaccountResourceBase
  61. err := reflectutils.FindAnonymouStructPointer(objs[i], &base)
  62. if err != nil {
  63. log.Errorf("Cannot find SCloudaccountResourceBase in %#v: %s", objs[i], err)
  64. } else if base != nil && len(base.CloudaccountId) > 0 {
  65. accountIds[i] = base.CloudaccountId
  66. }
  67. }
  68. accounts := make(map[string]SCloudaccount)
  69. err := db.FetchStandaloneObjectsByIds(CloudaccountManager, accountIds, &accounts)
  70. if err != nil {
  71. log.Errorf("FetchStandaloneObjectsByIds fail %v", err)
  72. return rows
  73. }
  74. for i := range rows {
  75. if account, ok := accounts[accountIds[i]]; ok {
  76. rows[i].Cloudaccount = account.Name
  77. rows[i].Provider = account.Provider
  78. rows[i].Brand = account.Brand
  79. if len(rows[i].Brand) == 0 {
  80. rows[i].Brand = account.Provider
  81. }
  82. rows[i].IamLoginUrl = account.IamLoginUrl
  83. }
  84. }
  85. return rows
  86. }
  87. func (self *SCloudaccountResourceBase) GetCloudaccount() (*SCloudaccount, error) {
  88. account, err := CloudaccountManager.FetchById(self.CloudaccountId)
  89. if err != nil {
  90. return nil, errors.Wrap(err, "CloudaccountManager.FetchById")
  91. }
  92. return account.(*SCloudaccount), nil
  93. }