orgcache.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 db
  15. import (
  16. "context"
  17. "yunion.io/x/pkg/errors"
  18. identityapi "yunion.io/x/onecloud/pkg/apis/identity"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  20. "yunion.io/x/onecloud/pkg/mcclient/auth"
  21. identitymodules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  22. identityoptions "yunion.io/x/onecloud/pkg/mcclient/options/identity"
  23. "yunion.io/x/onecloud/pkg/util/tagutils"
  24. )
  25. func FetchOrganizationTags(ctx context.Context, orgIds []string, orgType identityapi.TOrgType) (tagutils.TTagSetList, error) {
  26. s := auth.GetAdminSession(ctx, consts.GetRegion())
  27. opts := identityoptions.OrganizationNodeListOptions{}
  28. opts.Scope = "system"
  29. limit := 2048
  30. opts.Limit = &limit
  31. opts.Id = orgIds
  32. opts.OrgType = string(orgType)
  33. params, err := opts.Params()
  34. if err != nil {
  35. return nil, errors.Wrap(err, "Options")
  36. }
  37. results, err := identitymodules.OrganizationNodes.List(s, params)
  38. if err != nil {
  39. return nil, errors.Wrap(err, "OrganizationNodes.List")
  40. }
  41. tagList := tagutils.TTagSetList{}
  42. for _, orgJson := range results.Data {
  43. v := struct {
  44. Tags tagutils.TTagSet `json:"tags"`
  45. }{}
  46. err := orgJson.Unmarshal(&v)
  47. if err != nil {
  48. return nil, errors.Wrapf(err, "Unmarshal %s", orgJson)
  49. }
  50. tagList = tagList.Append(v.Tags)
  51. }
  52. return tagList, nil
  53. }