github.com/vmware/govmomi@v0.37.1/object/common.go (about) 1 /* 2 Copyright (c) 2015 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package object 18 19 import ( 20 "context" 21 "errors" 22 "fmt" 23 "path" 24 25 "github.com/vmware/govmomi/property" 26 "github.com/vmware/govmomi/vim25" 27 "github.com/vmware/govmomi/vim25/methods" 28 "github.com/vmware/govmomi/vim25/mo" 29 "github.com/vmware/govmomi/vim25/types" 30 ) 31 32 var ( 33 ErrNotSupported = errors.New("product/version specific feature not supported by target") 34 ) 35 36 // Common contains the fields and functions common to all objects. 37 type Common struct { 38 InventoryPath string 39 40 c *vim25.Client 41 r types.ManagedObjectReference 42 } 43 44 func (c Common) String() string { 45 ref := fmt.Sprintf("%v", c.Reference()) 46 47 if c.InventoryPath == "" { 48 return ref 49 } 50 51 return fmt.Sprintf("%s @ %s", ref, c.InventoryPath) 52 } 53 54 func NewCommon(c *vim25.Client, r types.ManagedObjectReference) Common { 55 return Common{c: c, r: r} 56 } 57 58 func (c Common) Reference() types.ManagedObjectReference { 59 return c.r 60 } 61 62 func (c Common) Client() *vim25.Client { 63 return c.c 64 } 65 66 // Name returns the base name of the InventoryPath field 67 func (c Common) Name() string { 68 if c.InventoryPath == "" { 69 return "" 70 } 71 return path.Base(c.InventoryPath) 72 } 73 74 func (c *Common) SetInventoryPath(p string) { 75 c.InventoryPath = p 76 } 77 78 // ObjectName fetches the mo.ManagedEntity.Name field via the property collector. 79 func (c Common) ObjectName(ctx context.Context) (string, error) { 80 var content []types.ObjectContent 81 82 err := c.Properties(ctx, c.Reference(), []string{"name"}, &content) 83 if err != nil { 84 return "", err 85 } 86 87 for i := range content { 88 for _, prop := range content[i].PropSet { 89 return prop.Val.(string), nil 90 } 91 } 92 93 return "", nil 94 } 95 96 // Properties is a wrapper for property.DefaultCollector().RetrieveOne() 97 func (c Common) Properties(ctx context.Context, r types.ManagedObjectReference, ps []string, dst interface{}) error { 98 return property.DefaultCollector(c.c).RetrieveOne(ctx, r, ps, dst) 99 } 100 101 func (c Common) Destroy(ctx context.Context) (*Task, error) { 102 req := types.Destroy_Task{ 103 This: c.Reference(), 104 } 105 106 res, err := methods.Destroy_Task(ctx, c.c, &req) 107 if err != nil { 108 return nil, err 109 } 110 111 return NewTask(c.c, res.Returnval), nil 112 } 113 114 func (c Common) Rename(ctx context.Context, name string) (*Task, error) { 115 req := types.Rename_Task{ 116 This: c.Reference(), 117 NewName: name, 118 } 119 120 res, err := methods.Rename_Task(ctx, c.c, &req) 121 if err != nil { 122 return nil, err 123 } 124 125 return NewTask(c.c, res.Returnval), nil 126 } 127 128 func (c Common) SetCustomValue(ctx context.Context, key string, value string) error { 129 req := types.SetCustomValue{ 130 This: c.Reference(), 131 Key: key, 132 Value: value, 133 } 134 135 _, err := methods.SetCustomValue(ctx, c.c, &req) 136 return err 137 } 138 139 func ReferenceFromString(s string) *types.ManagedObjectReference { 140 var ref types.ManagedObjectReference 141 if !ref.FromString(s) { 142 return nil 143 } 144 if mo.IsManagedObjectType(ref.Type) { 145 return &ref 146 } 147 return nil 148 }