mod_specs.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "net/url"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  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 SpecsManager struct {
  27. modulebase.ResourceManager
  28. }
  29. func generateSpecURL(model, ident, action string, params jsonutils.JSONObject) string {
  30. url := utils.ComposeURL("specs", model, ident, action)
  31. if params != nil {
  32. qs := params.QueryString()
  33. if len(qs) > 0 {
  34. url = fmt.Sprintf("%s?%s", url, qs)
  35. }
  36. }
  37. return url
  38. }
  39. func newSpecActionURL(model, ident, action string, params jsonutils.JSONObject) string {
  40. return generateSpecURL(model, ident, action, params)
  41. }
  42. func newSpecURL(model string, params jsonutils.JSONObject) string {
  43. return generateSpecURL(model, "", "", params)
  44. }
  45. func (this *SpecsManager) GetHostSpecs(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  46. return this.GetModelSpecs(s, "hosts", params)
  47. }
  48. func (this *SpecsManager) GetIsolatedDevicesSpecs(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  49. return this.GetModelSpecs(s, "isolated_devices", params)
  50. }
  51. func (this *SpecsManager) GetAllSpecs(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  52. return this.GetModelSpecs(s, "", params)
  53. }
  54. func (this *SpecsManager) GetModelSpecs(s *mcclient.ClientSession, model string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  55. url := newSpecURL(model, params)
  56. return modulebase.Get(this.ResourceManager, s, url, this.Keyword)
  57. }
  58. func (this *SpecsManager) GetObjects(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  59. dict := params.(*jsonutils.JSONDict)
  60. model, err := dict.GetString("kind")
  61. if err != nil {
  62. return nil, httperrors.NewInputParameterError("Not found kind in query: %v", err)
  63. }
  64. dict.Remove("kind")
  65. specKey, err := params.GetString("key")
  66. if err != nil {
  67. return nil, httperrors.NewInputParameterError("Not found key in query: %v", err)
  68. }
  69. dict.Remove("key")
  70. specKey = url.QueryEscape(specKey)
  71. url := newSpecActionURL(model, specKey, "resource", dict)
  72. return modulebase.Get(this.ResourceManager, s, url, this.Keyword)
  73. }
  74. func (this *SpecsManager) SpecsQueryModelObjects(s *mcclient.ClientSession, model string, specKeys []string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  75. if len(specKeys) == 0 {
  76. return nil, fmt.Errorf("Spec keys must provided")
  77. }
  78. specKey := url.QueryEscape(strings.Join(specKeys, "/"))
  79. url := newSpecActionURL(model, specKey, "resource", params)
  80. return modulebase.Get(this.ResourceManager, s, url, this.Keyword)
  81. }
  82. var (
  83. Specs SpecsManager
  84. )
  85. func init() {
  86. Specs = SpecsManager{modules.NewComputeManager("spec", "specs",
  87. []string{},
  88. []string{})}
  89. modules.RegisterCompute(&Specs)
  90. }