mod_baremeetalprofile.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 baremetal
  15. import (
  16. "sort"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. baremetalapi "yunion.io/x/onecloud/pkg/apis/compute/baremetal"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  22. "yunion.io/x/onecloud/pkg/mcclient/modules"
  23. baremetaloptions "yunion.io/x/onecloud/pkg/mcclient/options/compute/baremetal"
  24. )
  25. type BaremetalProfileManager struct {
  26. modulebase.ResourceManager
  27. }
  28. func (m *BaremetalProfileManager) GetMatchProfiles(s *mcclient.ClientSession, oemName string, model string) ([]baremetalapi.BaremetalProfileSpec, error) {
  29. params := baremetaloptions.BaremetalProfileListOptions{}
  30. limit := 0
  31. params.Limit = &limit
  32. params.OemName = []string{"", oemName}
  33. if len(model) > 0 {
  34. params.Model = []string{"", model}
  35. }
  36. results, err := m.List(s, jsonutils.Marshal(params))
  37. if err != nil {
  38. return nil, errors.Wrap(err, "List")
  39. }
  40. ret := make([]baremetalapi.BaremetalProfileSpec, 0)
  41. for i := range results.Data {
  42. details := baremetalapi.BaremetalProfileDetails{}
  43. err := results.Data[i].Unmarshal(&details)
  44. if err != nil {
  45. return nil, errors.Wrap(err, "Unmarshal")
  46. }
  47. ret = append(ret, details.ToSpec())
  48. }
  49. sort.Sort(baremetalapi.BaremetalProfileSpecs(ret))
  50. return ret, nil
  51. }
  52. var (
  53. BaremetalProfiles BaremetalProfileManager
  54. )
  55. func init() {
  56. BaremetalProfiles = BaremetalProfileManager{modules.NewComputeManager("baremetal_profile", "baremetal_profiles",
  57. []string{
  58. "oem_name",
  59. "model",
  60. "lan_channel",
  61. "root_id",
  62. "root_name",
  63. "string_pass",
  64. },
  65. []string{},
  66. )}
  67. modules.RegisterCompute(&BaremetalProfiles)
  68. }