common.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Copyright (c) 2015 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 object
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "path"
  19. "github.com/vmware/govmomi/property"
  20. "github.com/vmware/govmomi/vim25"
  21. "github.com/vmware/govmomi/vim25/methods"
  22. "github.com/vmware/govmomi/vim25/mo"
  23. "github.com/vmware/govmomi/vim25/types"
  24. )
  25. var (
  26. ErrNotSupported = errors.New("product/version specific feature not supported by target")
  27. )
  28. // Common contains the fields and functions common to all objects.
  29. type Common struct {
  30. InventoryPath string
  31. c *vim25.Client
  32. r types.ManagedObjectReference
  33. }
  34. func (c Common) String() string {
  35. ref := fmt.Sprintf("%v", c.Reference())
  36. if c.InventoryPath == "" {
  37. return ref
  38. }
  39. return fmt.Sprintf("%s @ %s", ref, c.InventoryPath)
  40. }
  41. func NewCommon(c *vim25.Client, r types.ManagedObjectReference) Common {
  42. return Common{c: c, r: r}
  43. }
  44. func (c Common) Reference() types.ManagedObjectReference {
  45. return c.r
  46. }
  47. func (c Common) Client() *vim25.Client {
  48. return c.c
  49. }
  50. // Name returns the base name of the InventoryPath field
  51. func (c Common) Name() string {
  52. if c.InventoryPath == "" {
  53. return ""
  54. }
  55. return path.Base(c.InventoryPath)
  56. }
  57. func (c *Common) SetInventoryPath(p string) {
  58. c.InventoryPath = p
  59. }
  60. // ObjectName fetches the mo.ManagedEntity.Name field via the property collector.
  61. func (c Common) ObjectName(ctx context.Context) (string, error) {
  62. var content []types.ObjectContent
  63. err := c.Properties(ctx, c.Reference(), []string{"name"}, &content)
  64. if err != nil {
  65. return "", err
  66. }
  67. for i := range content {
  68. for _, prop := range content[i].PropSet {
  69. return prop.Val.(string), nil
  70. }
  71. }
  72. return "", nil
  73. }
  74. // Properties is a wrapper for property.DefaultCollector().RetrieveOne()
  75. func (c Common) Properties(ctx context.Context, r types.ManagedObjectReference, ps []string, dst interface{}) error {
  76. return property.DefaultCollector(c.c).RetrieveOne(ctx, r, ps, dst)
  77. }
  78. func (c Common) Destroy(ctx context.Context) (*Task, error) {
  79. req := types.Destroy_Task{
  80. This: c.Reference(),
  81. }
  82. res, err := methods.Destroy_Task(ctx, c.c, &req)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return NewTask(c.c, res.Returnval), nil
  87. }
  88. func (c Common) Rename(ctx context.Context, name string) (*Task, error) {
  89. req := types.Rename_Task{
  90. This: c.Reference(),
  91. NewName: name,
  92. }
  93. res, err := methods.Rename_Task(ctx, c.c, &req)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return NewTask(c.c, res.Returnval), nil
  98. }
  99. func (c Common) SetCustomValue(ctx context.Context, key string, value string) error {
  100. req := types.SetCustomValue{
  101. This: c.Reference(),
  102. Key: key,
  103. Value: value,
  104. }
  105. _, err := methods.SetCustomValue(ctx, c.c, &req)
  106. return err
  107. }
  108. func ReferenceFromString(s string) *types.ManagedObjectReference {
  109. var ref types.ManagedObjectReference
  110. if !ref.FromString(s) {
  111. return nil
  112. }
  113. if mo.IsManagedObjectType(ref.Type) {
  114. return &ref
  115. }
  116. return nil
  117. }