|
| 1 | +// © Broadcom. All Rights Reserved. |
| 2 | +// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package disk |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "errors" |
| 10 | + "os" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/vmware/govmomi/cli/flags" |
| 14 | + "github.com/vmware/govmomi/object" |
| 15 | + "github.com/vmware/govmomi/vim25" |
| 16 | + "github.com/vmware/govmomi/vim25/types" |
| 17 | + "github.com/vmware/govmomi/vslm" |
| 18 | + vslmtypes "github.com/vmware/govmomi/vslm/types" |
| 19 | +) |
| 20 | + |
| 21 | +// Manager provides a layer for switching between Virtual Storage Object Manager (VSOM) |
| 22 | +// and Virtual Storage Lifecycle Manager (VSLM). The majority of VSOM methods require a |
| 23 | +// Datastore param, and most VSLM methods do not as it uses the "Global Catalog". |
| 24 | +// VSOM was introduced in vSphere 6.5 (11/2016) and VSLM in vSphere 6.7 U2 (04/2019). |
| 25 | +// The govc disk commands were introduced prior to 6.7 U2 and continue to use VSOM |
| 26 | +// when an optional Datastore (-ds flag) is provided. Otherwise, VSLM global methods |
| 27 | +// are used when a Datastore is not specified. |
| 28 | +// Also note that VSOM methods can be used when connected directly to an ESX hosts, |
| 29 | +// but VSLM global methods require vCenter. |
| 30 | +// A disk managed by these methods are also known as a "First Class Disk" (FCD). |
| 31 | +type Manager struct { |
| 32 | + Client *vim25.Client |
| 33 | + Datastore *object.Datastore |
| 34 | + ObjectManager *vslm.ObjectManager |
| 35 | + GlobalObjectManager *vslm.GlobalObjectManager |
| 36 | +} |
| 37 | + |
| 38 | +var ( |
| 39 | + // vslm does not have a PropertyCollector, the Wait func polls via VslmQueryInfo |
| 40 | + taskTimeout = time.Hour |
| 41 | + |
| 42 | + // Some methods require a Datastore param regardless using vsom or vslm. |
| 43 | + // By default we use Global vslm for those methods, use vsom when this is "false". |
| 44 | + useGlobal = os.Getenv("GOVC_GLOBAL_VSLM") != "false" |
| 45 | +) |
| 46 | + |
| 47 | +func NewManagerFromFlag(ctx context.Context, cmd *flags.DatastoreFlag) (*Manager, error) { |
| 48 | + c, err := cmd.Client() |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + ds, err := cmd.DatastoreIfSpecified() |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + return NewManager(ctx, c, ds) |
| 59 | +} |
| 60 | + |
| 61 | +func NewManager(ctx context.Context, c *vim25.Client, ds *object.Datastore) (*Manager, error) { |
| 62 | + m := &Manager{ |
| 63 | + Client: c, |
| 64 | + Datastore: ds, |
| 65 | + ObjectManager: vslm.NewObjectManager(c), |
| 66 | + } |
| 67 | + |
| 68 | + if ds == nil { |
| 69 | + if err := m.initGlobalManager(ctx); err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return m, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (m *Manager) initGlobalManager(ctx context.Context) error { |
| 78 | + if m.GlobalObjectManager == nil { |
| 79 | + vc, err := vslm.NewClient(ctx, m.Client) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + m.GlobalObjectManager = vslm.NewGlobalObjectManager(vc) |
| 84 | + } |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func (m *Manager) CreateDisk(ctx context.Context, spec types.VslmCreateSpec) (*types.VStorageObject, error) { |
| 89 | + if useGlobal && m.Client.IsVC() { |
| 90 | + if err := m.initGlobalManager(ctx); err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + task, err := m.GlobalObjectManager.CreateDisk(ctx, spec) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + res, err := task.Wait(ctx, taskTimeout) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + |
| 102 | + obj := res.(types.VStorageObject) |
| 103 | + return &obj, nil |
| 104 | + } |
| 105 | + |
| 106 | + task, err := m.ObjectManager.CreateDisk(ctx, spec) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + res, err := task.WaitForResult(ctx) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + obj := res.Result.(types.VStorageObject) |
| 115 | + return &obj, nil |
| 116 | +} |
| 117 | + |
| 118 | +func (m *Manager) Delete(ctx context.Context, id string) error { |
| 119 | + if m.Datastore != nil { |
| 120 | + task, err := m.ObjectManager.Delete(ctx, m.Datastore, id) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + return task.Wait(ctx) |
| 125 | + } |
| 126 | + |
| 127 | + task, err := m.GlobalObjectManager.Delete(ctx, types.ID{Id: id}) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + _, err = task.Wait(ctx, taskTimeout) |
| 132 | + return err |
| 133 | +} |
| 134 | + |
| 135 | +func (m *Manager) Retrieve(ctx context.Context, id string) (*types.VStorageObject, error) { |
| 136 | + if m.Datastore != nil { |
| 137 | + return m.ObjectManager.Retrieve(ctx, m.Datastore, id) |
| 138 | + } |
| 139 | + return m.GlobalObjectManager.Retrieve(ctx, types.ID{Id: id}) |
| 140 | +} |
| 141 | + |
| 142 | +func (m *Manager) List(ctx context.Context) ([]types.ID, error) { |
| 143 | + if m.Datastore != nil { |
| 144 | + return m.ObjectManager.List(ctx, m.Datastore) |
| 145 | + } |
| 146 | + |
| 147 | + // TODO: move this logic to vslm.GlobalObjectManager |
| 148 | + // Need to better understand the QuerySpec + implement in vcsim. |
| 149 | + // For now we just want the complete list of IDs (govc disk.ls) |
| 150 | + maxResults := int32(100) |
| 151 | + |
| 152 | + spec := vslmtypes.VslmVsoVStorageObjectQuerySpec{ |
| 153 | + QueryField: string(vslmtypes.VslmVsoVStorageObjectQuerySpecQueryFieldEnumId), |
| 154 | + QueryOperator: string(vslmtypes.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumGreaterThan), |
| 155 | + } |
| 156 | + var query []vslmtypes.VslmVsoVStorageObjectQuerySpec |
| 157 | + var ids []types.ID |
| 158 | + |
| 159 | + for { |
| 160 | + res, err := m.GlobalObjectManager.ListObjectsForSpec(ctx, query, maxResults) |
| 161 | + if err != nil { |
| 162 | + return nil, err |
| 163 | + } |
| 164 | + |
| 165 | + ids = append(ids, res.Id...) |
| 166 | + |
| 167 | + if res.AllRecordsReturned { |
| 168 | + break |
| 169 | + } |
| 170 | + |
| 171 | + spec.QueryValue = []string{ids[len(ids)-1].Id} |
| 172 | + |
| 173 | + query = []vslmtypes.VslmVsoVStorageObjectQuerySpec{spec} |
| 174 | + } |
| 175 | + |
| 176 | + return ids, nil |
| 177 | +} |
| 178 | + |
| 179 | +func (m *Manager) RegisterDisk(ctx context.Context, path, name string) (*types.VStorageObject, error) { |
| 180 | + if useGlobal && m.Client.IsVC() { |
| 181 | + if err := m.initGlobalManager(ctx); err != nil { |
| 182 | + return nil, err |
| 183 | + } |
| 184 | + return m.GlobalObjectManager.RegisterDisk(ctx, path, name) // VslmRegisterDisk |
| 185 | + } |
| 186 | + return m.ObjectManager.RegisterDisk(ctx, path, name) // RegisterDisk |
| 187 | +} |
| 188 | + |
| 189 | +func (m *Manager) CreateSnapshot(ctx context.Context, id, desc string) (types.ID, error) { |
| 190 | + if m.Datastore != nil { |
| 191 | + task, err := m.ObjectManager.CreateSnapshot(ctx, m.Datastore, id, desc) |
| 192 | + if err != nil { |
| 193 | + return types.ID{}, err |
| 194 | + } |
| 195 | + res, err := task.WaitForResult(ctx, nil) |
| 196 | + if err != nil { |
| 197 | + return types.ID{}, err |
| 198 | + } |
| 199 | + return res.Result.(types.ID), nil |
| 200 | + } |
| 201 | + |
| 202 | + task, err := m.GlobalObjectManager.CreateSnapshot(ctx, types.ID{Id: id}, desc) |
| 203 | + if err != nil { |
| 204 | + return types.ID{}, err |
| 205 | + } |
| 206 | + res, err := task.Wait(ctx, taskTimeout) |
| 207 | + if err != nil { |
| 208 | + return types.ID{}, err |
| 209 | + } |
| 210 | + return res.(types.ID), err |
| 211 | +} |
| 212 | + |
| 213 | +func (m *Manager) DeleteSnapshot(ctx context.Context, id, sid string) error { |
| 214 | + if m.Datastore != nil { |
| 215 | + task, err := m.ObjectManager.DeleteSnapshot(ctx, m.Datastore, id, sid) |
| 216 | + if err != nil { |
| 217 | + return err |
| 218 | + } |
| 219 | + return task.Wait(ctx) |
| 220 | + } |
| 221 | + |
| 222 | + task, err := m.GlobalObjectManager.DeleteSnapshot(ctx, types.ID{Id: id}, types.ID{Id: sid}) |
| 223 | + if err != nil { |
| 224 | + return err |
| 225 | + } |
| 226 | + _, err = task.Wait(ctx, taskTimeout) |
| 227 | + return err |
| 228 | +} |
| 229 | + |
| 230 | +func (m *Manager) RetrieveSnapshotInfo(ctx context.Context, id string) ([]types.VStorageObjectSnapshotInfoVStorageObjectSnapshot, error) { |
| 231 | + if m.Datastore != nil { |
| 232 | + info, err := m.ObjectManager.RetrieveSnapshotInfo(ctx, m.Datastore, id) |
| 233 | + if err != nil { |
| 234 | + return nil, err |
| 235 | + } |
| 236 | + return info.Snapshots, nil |
| 237 | + } |
| 238 | + |
| 239 | + return m.GlobalObjectManager.RetrieveSnapshotInfo(ctx, types.ID{Id: id}) |
| 240 | +} |
| 241 | + |
| 242 | +func (m *Manager) AttachTag(ctx context.Context, id string, tag types.VslmTagEntry) error { |
| 243 | + if useGlobal && m.Client.IsVC() { |
| 244 | + if err := m.initGlobalManager(ctx); err != nil { |
| 245 | + return err |
| 246 | + } |
| 247 | + // TODO: use types.VslmTagEntry |
| 248 | + return m.GlobalObjectManager.AttachTag(ctx, types.ID{Id: id}, tag.ParentCategoryName, tag.TagName) |
| 249 | + } |
| 250 | + return m.ObjectManager.AttachTag(ctx, id, tag) |
| 251 | +} |
| 252 | + |
| 253 | +func (m *Manager) DetachTag(ctx context.Context, id string, tag types.VslmTagEntry) error { |
| 254 | + if useGlobal && m.Client.IsVC() { |
| 255 | + if err := m.initGlobalManager(ctx); err != nil { |
| 256 | + return err |
| 257 | + } |
| 258 | + // TODO: use types.VslmTagEntry |
| 259 | + return m.GlobalObjectManager.DetachTag(ctx, types.ID{Id: id}, tag.ParentCategoryName, tag.TagName) |
| 260 | + } |
| 261 | + return m.ObjectManager.DetachTag(ctx, id, tag) |
| 262 | +} |
| 263 | + |
| 264 | +func (m *Manager) ListAttachedObjects(ctx context.Context, category, tag string) ([]types.ID, error) { |
| 265 | + if m.Datastore != nil { |
| 266 | + // ListVStorageObjectsAttachedToTag |
| 267 | + return m.ObjectManager.ListAttachedObjects(ctx, category, tag) |
| 268 | + } |
| 269 | + |
| 270 | + // VslmListVStorageObjectsAttachedToTag |
| 271 | + return m.GlobalObjectManager.ListAttachedObjects(ctx, category, tag) |
| 272 | +} |
| 273 | + |
| 274 | +func (m *Manager) ListAttachedTags(ctx context.Context, id string) ([]types.VslmTagEntry, error) { |
| 275 | + if m.Datastore != nil { |
| 276 | + // ListTagsAttachedToVStorageObject |
| 277 | + return m.ObjectManager.ListAttachedTags(ctx, id) |
| 278 | + } |
| 279 | + |
| 280 | + // VslmListTagsAttachedToVStorageObject |
| 281 | + return m.GlobalObjectManager.ListAttachedTags(ctx, types.ID{Id: id}) |
| 282 | +} |
| 283 | + |
| 284 | +func (m *Manager) ReconcileDatastoreInventory(ctx context.Context) error { |
| 285 | + if m.Datastore != nil { |
| 286 | + // ReconcileDatastoreInventory_Task |
| 287 | + task, err := m.ObjectManager.ReconcileDatastoreInventory(ctx, m.Datastore) |
| 288 | + if err != nil { |
| 289 | + return err |
| 290 | + } |
| 291 | + return task.Wait(ctx) |
| 292 | + } |
| 293 | + |
| 294 | + // VslmReconcileDatastoreInventory_Task also requires a Datastore |
| 295 | + return errors.New("-R requires -ds") // TODO |
| 296 | +} |
| 297 | + |
| 298 | +func (m *Manager) AttachDisk(ctx context.Context, vm *object.VirtualMachine, id string) error { |
| 299 | + if m.Datastore != nil { |
| 300 | + return vm.AttachDisk(ctx, id, m.Datastore, 0, nil) |
| 301 | + } |
| 302 | + |
| 303 | + task, err := m.GlobalObjectManager.AttachDisk(ctx, types.ID{Id: id}, vm, 0, nil) |
| 304 | + if err != nil { |
| 305 | + return err |
| 306 | + } |
| 307 | + _, err = task.Wait(ctx, taskTimeout) |
| 308 | + return err |
| 309 | +} |
0 commit comments