driver.go 3.7 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 redfish
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  21. )
  22. type IRedfishDriverFactory interface {
  23. Name() string
  24. NewApi(endpoint, username, password string, debug bool) IRedfishDriver
  25. }
  26. type IRedfishDriver interface {
  27. Login(ctx context.Context) error
  28. Logout(ctx context.Context) error
  29. Probe(ctx context.Context) error
  30. BasePath() string
  31. VersionKey() string
  32. LinkKey() string
  33. MemberKey() string
  34. LogItemsKey() string
  35. ParseRoot(root jsonutils.JSONObject) error
  36. GetParent(parent jsonutils.JSONObject) jsonutils.JSONObject
  37. GetResource(ctx context.Context, resname ...string) (string, jsonutils.JSONObject, error)
  38. GetResourceCount(ctx context.Context, resname ...string) (int, error)
  39. GetVirtualCdromInfo(ctx context.Context) (string, SCdromInfo, error)
  40. MountVirtualCdrom(ctx context.Context, path string, cdromUrl string, boot bool) error
  41. UmountVirtualCdrom(ctx context.Context, path string) error
  42. GetLanConfigs(ctx context.Context) ([]types.SIPMILanConfig, error)
  43. GetSystemInfo(ctx context.Context) (string, SSystemInfo, error)
  44. SetNextBootDev(ctx context.Context, dev string) error
  45. // SetNextBootVirtualCdrom(ctx context.Context) error
  46. Reset(ctx context.Context, action string) error
  47. GetSystemLogsPath() string
  48. GetManagerLogsPath() string
  49. GetClearSystemLogsPath() string
  50. GetClearManagerLogsPath() string
  51. ReadSystemLogs(ctx context.Context, since time.Time) ([]SEvent, error)
  52. ReadManagerLogs(ctx context.Context, since time.Time) ([]SEvent, error)
  53. ClearSystemLogs(ctx context.Context) error
  54. ClearManagerLogs(ctx context.Context) error
  55. BmcReset(ctx context.Context) error
  56. GetBiosInfo(ctx context.Context) (SBiosInfo, error)
  57. GetIndicatorLED(ctx context.Context) (bool, error)
  58. SetIndicatorLED(ctx context.Context, on bool) error
  59. GetPowerPath() string
  60. GetThermalPath() string
  61. GetPower(ctx context.Context) ([]SPower, error)
  62. GetThermal(ctx context.Context) ([]STemperature, error)
  63. GetNTPConf(ctx context.Context) (SNTPConf, error)
  64. SetNTPConf(ctx context.Context, conf SNTPConf) error
  65. GetConsoleJNLP(ctx context.Context) (string, error)
  66. }
  67. var defaultFactory IRedfishDriverFactory
  68. var factories map[string]IRedfishDriverFactory
  69. func init() {
  70. factories = make(map[string]IRedfishDriverFactory)
  71. }
  72. func RegisterApiFactory(f IRedfishDriverFactory) {
  73. factories[f.Name()] = f
  74. }
  75. func RegisterDefaultApiFactory(f IRedfishDriverFactory) {
  76. defaultFactory = f
  77. }
  78. func NewRedfishDriver(ctx context.Context, endpoint string, username, password string, debug bool) IRedfishDriver {
  79. for k, factory := range factories {
  80. drv := factory.NewApi(endpoint, username, password, debug)
  81. err := drv.Probe(ctx)
  82. if err == nil {
  83. log.Infof("Found %s Redfish REST Api Driver for endpoint %q", k, endpoint)
  84. return drv
  85. }
  86. }
  87. if defaultFactory == nil {
  88. return nil
  89. }
  90. drv := defaultFactory.NewApi(endpoint, username, password, debug)
  91. err := drv.Probe(ctx)
  92. if err == nil {
  93. log.Infof("Use generic Redfish REST Api Driver for endpoint %q", endpoint)
  94. return drv
  95. }
  96. log.Errorf("No Redfish driver found of endpoint %q", endpoint)
  97. return nil
  98. }