common.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 returns the base name of the InventoryPath field if set,
  61. // otherwise fetches the mo.ManagedEntity.Name field via the property collector.
  62. func (c Common) ObjectName(ctx context.Context) (string, error) {
  63. var o mo.ManagedEntity
  64. err := c.Properties(ctx, c.Reference(), []string{"name"}, &o)
  65. if err != nil {
  66. return "", err
  67. }
  68. if o.Name != "" {
  69. return o.Name, nil
  70. }
  71. // Network has its own "name" field...
  72. var n mo.Network
  73. err = c.Properties(ctx, c.Reference(), []string{"name"}, &n)
  74. if err != nil {
  75. return "", err
  76. }
  77. return n.Name, nil
  78. }
  79. // Properties is a wrapper for property.DefaultCollector().RetrieveOne()
  80. func (c Common) Properties(ctx context.Context, r types.ManagedObjectReference, ps []string, dst interface{}) error {
  81. return property.DefaultCollector(c.c).RetrieveOne(ctx, r, ps, dst)
  82. }
  83. func (c Common) Destroy(ctx context.Context) (*Task, error) {
  84. req := types.Destroy_Task{
  85. This: c.Reference(),
  86. }
  87. res, err := methods.Destroy_Task(ctx, c.c, &req)
  88. if err != nil {
  89. return nil, err
  90. }
  91. return NewTask(c.c, res.Returnval), nil
  92. }
  93. func (c Common) Rename(ctx context.Context, name string) (*Task, error) {
  94. req := types.Rename_Task{
  95. This: c.Reference(),
  96. NewName: name,
  97. }
  98. res, err := methods.Rename_Task(ctx, c.c, &req)
  99. if err != nil {
  100. return nil, err
  101. }
  102. return NewTask(c.c, res.Returnval), nil
  103. }
  104. func (c Common) SetCustomValue(ctx context.Context, key string, value string) error {
  105. req := types.SetCustomValue{
  106. This: c.Reference(),
  107. Key: key,
  108. Value: value,
  109. }
  110. _, err := methods.SetCustomValue(ctx, c.c, &req)
  111. return err
  112. }