profiles.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 profiles
  15. import (
  16. "context"
  17. "yunion.io/x/pkg/errors"
  18. baremetalapi "yunion.io/x/onecloud/pkg/apis/compute/baremetal"
  19. "yunion.io/x/onecloud/pkg/baremetal/options"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  21. "yunion.io/x/onecloud/pkg/mcclient/auth"
  22. baremetalmodules "yunion.io/x/onecloud/pkg/mcclient/modules/compute/baremetal"
  23. )
  24. func GetProfile(ctx context.Context, sysinfo *types.SSystemInfo) (*baremetalapi.BaremetalProfileSpec, error) {
  25. adminSession := auth.GetAdminSession(ctx, options.Options.Region)
  26. specs, err := baremetalmodules.BaremetalProfiles.GetMatchProfiles(adminSession, sysinfo.OemName, sysinfo.Model)
  27. if err != nil {
  28. return nil, errors.Wrapf(err, "GetMatchProfile %s %s", sysinfo.OemName, sysinfo.Model)
  29. }
  30. if len(specs) > 0 {
  31. return &specs[len(specs)-1], nil
  32. }
  33. return nil, errors.ErrNotFound
  34. }
  35. func GetLanChannels(ctx context.Context, sysinfo *types.SSystemInfo) ([]uint8, error) {
  36. profile, err := GetProfile(ctx, sysinfo)
  37. if err != nil {
  38. return nil, errors.Wrap(err, "GetProfile")
  39. }
  40. return profile.LanChannels, nil
  41. }
  42. func GetDefaultLanChannel(ctx context.Context, sysinfo *types.SSystemInfo) (uint8, error) {
  43. channels, err := GetLanChannels(ctx, sysinfo)
  44. if err != nil {
  45. return 0, errors.Wrap(err, "GetLanChannels")
  46. }
  47. if len(channels) > 0 {
  48. return channels[0], nil
  49. }
  50. return 0, errors.ErrNotFound
  51. }
  52. func GetRootId(ctx context.Context, sysinfo *types.SSystemInfo) (int, error) {
  53. profile, err := GetProfile(ctx, sysinfo)
  54. if err != nil {
  55. return 0, errors.Wrap(err, "GetProfile")
  56. }
  57. return profile.RootId, nil
  58. }
  59. func GetRootName(ctx context.Context, sysinfo *types.SSystemInfo) (string, error) {
  60. profile, err := GetProfile(ctx, sysinfo)
  61. if err != nil {
  62. return "", errors.Wrap(err, "GetProfile")
  63. }
  64. return profile.RootName, nil
  65. }
  66. func IsStrongPass(ctx context.Context, sysinfo *types.SSystemInfo) (bool, error) {
  67. profile, err := GetProfile(ctx, sysinfo)
  68. if err != nil {
  69. return false, errors.Wrap(err, "GetProfile")
  70. }
  71. return profile.StrongPass, nil
  72. }