option_manager.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright (c) 2017 VMware, Inc. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package simulator
  14. import (
  15. "strings"
  16. "github.com/vmware/govmomi/object"
  17. "github.com/vmware/govmomi/vim25/methods"
  18. "github.com/vmware/govmomi/vim25/mo"
  19. "github.com/vmware/govmomi/vim25/soap"
  20. "github.com/vmware/govmomi/vim25/types"
  21. )
  22. type OptionManager struct {
  23. mo.OptionManager
  24. }
  25. func NewOptionManager(ref *types.ManagedObjectReference, setting []types.BaseOptionValue) object.Reference {
  26. s := &OptionManager{}
  27. if ref != nil {
  28. s.Self = *ref
  29. }
  30. s.Setting = setting
  31. return s
  32. }
  33. func (m *OptionManager) QueryOptions(req *types.QueryOptions) soap.HasFault {
  34. body := &methods.QueryOptionsBody{}
  35. res := &types.QueryOptionsResponse{}
  36. for _, opt := range m.Setting {
  37. if strings.HasPrefix(opt.GetOptionValue().Key, req.Name) {
  38. res.Returnval = append(res.Returnval, opt)
  39. }
  40. }
  41. if len(res.Returnval) == 0 {
  42. body.Fault_ = Fault("", &types.InvalidName{Name: req.Name})
  43. } else {
  44. body.Res = res
  45. }
  46. return body
  47. }
  48. func (m *OptionManager) find(key string) *types.OptionValue {
  49. for _, opt := range m.Setting {
  50. setting := opt.GetOptionValue()
  51. if setting.Key == key {
  52. return setting
  53. }
  54. }
  55. return nil
  56. }
  57. func (m *OptionManager) UpdateOptions(req *types.UpdateOptions) soap.HasFault {
  58. body := new(methods.UpdateOptionsBody)
  59. for _, change := range req.ChangedValue {
  60. setting := change.GetOptionValue()
  61. // We don't currently include the entire list of default settings for ESX and vCenter,
  62. // this prefix is currently used to test the failure path.
  63. // Real vCenter seems to only allow new options if Key has a "config." prefix.
  64. // TODO: consider behaving the same, which would require including 2 long lists of options in vpx.Setting and esx.Setting
  65. if strings.HasPrefix(setting.Key, "ENOENT.") {
  66. body.Fault_ = Fault("", &types.InvalidName{Name: setting.Key})
  67. return body
  68. }
  69. opt := m.find(setting.Key)
  70. if opt != nil {
  71. // This is an existing option.
  72. opt.Value = setting.Value
  73. continue
  74. }
  75. m.Setting = append(m.Setting, change)
  76. }
  77. body.Res = new(types.UpdateOptionsResponse)
  78. return body
  79. }