mod_metadatas.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 compute
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/util/httputils"
  19. "yunion.io/x/pkg/util/printutils"
  20. "yunion.io/x/pkg/utils"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  24. "yunion.io/x/onecloud/pkg/mcclient/modules"
  25. )
  26. type MetadataManager struct {
  27. modulebase.ResourceManager
  28. }
  29. var (
  30. ComputeMetadatas MetadataManager
  31. IdentityMetadatas MetadataManager
  32. ImageMetadatas MetadataManager
  33. )
  34. func init() {
  35. ComputeMetadatas = MetadataManager{modules.NewComputeManager("metadata", "metadatas",
  36. []string{"id", "key", "value"},
  37. []string{})}
  38. // !!! Register computer metadata ONLY !!! QIUJIAN
  39. // allpw register multiple metadatas! 20240815
  40. modules.Register(&ComputeMetadatas)
  41. IdentityMetadatas = MetadataManager{modules.NewIdentityV3Manager("metadata", "metadatas",
  42. []string{"id", "key", "value"},
  43. []string{})}
  44. modules.Register(&IdentityMetadatas)
  45. ImageMetadatas = MetadataManager{modules.NewImageManager("metadata", "metadatas",
  46. []string{"id", "key", "value"},
  47. []string{})}
  48. modules.Register(&ImageMetadatas)
  49. }
  50. func (this *MetadataManager) getModule(session *mcclient.ClientSession, params jsonutils.JSONObject) (modulebase.Manager, error) {
  51. service := "compute"
  52. if params.Contains("service") {
  53. service, _ = params.GetString("service")
  54. } else {
  55. // 若参数有resources,可根据资源类型自动判断服务类型
  56. resources := []string{}
  57. if params.Contains("resources") { // yunionapi
  58. err := params.Unmarshal(&resources, "resources")
  59. if err != nil {
  60. return nil, httperrors.NewInputParameterError("invalid resources format")
  61. }
  62. } else if params.Contains("resources.0") { // climc
  63. resource, _ := params.GetString("resources.0")
  64. if len(resource) > 0 {
  65. resources = append(resources, resource)
  66. }
  67. }
  68. if len(resources) >= 1 {
  69. resource := resources[0]
  70. find := false
  71. keyStrings := []string{resource, resource + "s", resource + "ies"}
  72. mods, _ := modulebase.GetRegisterdModules()
  73. for _, keyString := range keyStrings {
  74. if utils.IsInStringArray(keyString, mods) {
  75. mod, err := modulebase.GetModule(session, keyString)
  76. if err == nil {
  77. service = mod.ServiceType()
  78. find = true
  79. break
  80. }
  81. }
  82. }
  83. if !find {
  84. return nil, fmt.Errorf("No such module %s", resource)
  85. }
  86. }
  87. }
  88. _, err := session.GetServiceURL(service, "", httputils.POST)
  89. if err != nil {
  90. return nil, httperrors.NewNotFoundError("service %s not found error: %v", service, err)
  91. }
  92. return &modulebase.ResourceManager{
  93. BaseManager: *modulebase.NewBaseManager(service, "", "", []string{}, []string{}),
  94. Keyword: "metadata", KeywordPlural: "metadatas",
  95. }, nil
  96. }
  97. func (this *MetadataManager) List(session *mcclient.ClientSession, params jsonutils.JSONObject) (*printutils.ListResult, error) {
  98. mod, err := this.getModule(session, params)
  99. if err != nil {
  100. return nil, err
  101. }
  102. return mod.List(session, params)
  103. }
  104. func (this *MetadataManager) Get(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  105. mod, err := this.getModule(session, params)
  106. if err != nil {
  107. return nil, err
  108. }
  109. return mod.Get(session, id, params)
  110. }