1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use std::sync::Arc;
use crate::core::client::{VimClient, Result};
/// The Snapshot managed object type specifies the interface to individual snapshots
/// of a virtual machine.
///
/// Although these are managed objects, they are subordinate to
/// their virtual machine.
#[derive(Clone)]
pub struct VirtualMachineSnapshot {
client: Arc<dyn VimClient>,
mo_id: String,
}
impl VirtualMachineSnapshot {
pub fn new(client: Arc<dyn VimClient>, mo_id: &str) -> Self {
Self {
client,
mo_id: mo_id.to_string(),
}
}
/// Obtains an export lease on this snapshot.
///
/// The export lease contains
/// a list of URLs for the virtual disks for this snapshot, as well as
/// a ticket giving access to the URLs.
///
/// See *HttpNfcLease* for information on how to use the lease.
///
/// ***Required privileges:*** VApp.Export
///
/// ## Returns:
///
/// The export lease on this *VirtualMachineSnapshot*. The
/// export task continues running until the lease is completed by the
/// caller.
///
/// Refers instance of *HttpNfcLease*.
///
/// ## Errors:
///
/// ***TaskInProgress***: if the virtual machine is busy.
///
/// ***InvalidState***: if the operation cannot be performed because of the
/// virtual machine's current state. For example, if the virtual machine
/// configuration information is not available.
///
/// ***FileFault***: if there is an error accessing the virtual machine files.
pub async fn export_snapshot(&self) -> Result<crate::types::structs::ManagedObjectReference> {
let bytes = self.client.invoke("", "VirtualMachineSnapshot", &self.mo_id, "ExportSnapshot", None).await?;
let result: crate::types::structs::ManagedObjectReference = crate::core::client::unmarshal(self.client.transport(), &bytes)?;
Ok(result)
}
/// Removes this snapshot and deletes any associated storage.
///
/// ***Required privileges:*** VirtualMachine.State.RemoveSnapshot
///
/// ## Parameters:
///
/// ### remove_children
/// Flag to specify removal of the entire snapshot subtree.
///
/// ### consolidate
/// (optional) If set to true, the virtual disk associated
/// with this snapshot will be merged with other disk if possible. Defaults to true.
///
/// ## Returns:
///
/// This method returns a *Task* object with which to monitor the
/// operation.
///
/// Refers instance of *Task*.
///
/// ## Errors:
///
/// ***TaskInProgress***: if the virtual machine is busy.
pub async fn remove_snapshot_task(&self, remove_children: bool, consolidate: Option<bool>) -> Result<crate::types::structs::ManagedObjectReference> {
let input = RemoveSnapshotRequestType {remove_children, consolidate, };
let bytes = self.client.invoke("", "VirtualMachineSnapshot", &self.mo_id, "RemoveSnapshot_Task", Some(&input)).await?;
let result: crate::types::structs::ManagedObjectReference = crate::core::client::unmarshal(self.client.transport(), &bytes)?;
Ok(result)
}
/// Rename this snapshot with either a new name or a new description or both.
///
/// At least one of these must be specified when calling the rename method.
///
/// ***Required privileges:*** VirtualMachine.State.RenameSnapshot
///
/// ## Parameters:
///
/// ### name
/// New name for the snapshot.
///
/// ### description
/// New description for the snapshot.
///
/// ## Errors:
///
/// ***NotSupported***: if the host product does not support snapshot rename.
///
/// ***InvalidName***: if the specified snapshot name is not valid.
///
/// ***TaskInProgress***: if the virtual machine is busy.
///
/// ***InvalidPowerState***: if the operation cannot be performed in the current
/// power state of the virtual machine.
///
/// ***InvalidState***: if the operation cannot be performed in the current state
/// of the virtual machine. For example, the virtual machine's configuration
/// is not available.
pub async fn rename_snapshot(&self, name: Option<&str>, description: Option<&str>) -> Result<()> {
let input = RenameSnapshotRequestType {name, description, };
self.client.invoke_void("", "VirtualMachineSnapshot", &self.mo_id, "RenameSnapshot", Some(&input)).await
}
/// Change the execution state of the virtual machine to the state of this snapshot.
///
/// ***Required privileges:*** VirtualMachine.State.RevertToSnapshot
///
/// ## Parameters:
///
/// ### host
/// (optional) Choice of host for the virtual machine, in case this
/// operation causes the virtual machine to power on.
///
/// If a snapshot was taken while a virtual machine was powered on, and this operation
/// is invoked after the virtual machine was powered off, the operation causes the
/// virtual machine to power on to reach the snapshot state. This parameter can be
/// used to specify a choice of host where the virtual machine should power on.
///
/// If this parameter is not set and the vBalance feature is configured for automatic
/// load balancing, a host is automatically selected. Otherwise, the virtual machine
/// keeps its existing host affiliation.
///
/// Refers instance of *HostSystem*.
///
/// ### suppress_power_on
/// (optional) If set to true, the virtual
/// machine will not be powered on regardless of the power state when
/// the snapshot was created. Default to false.
///
/// ## Returns:
///
/// This method returns a *Task* object with which to monitor the
/// operation.
///
/// Refers instance of *Task*.
///
/// ## Errors:
///
/// ***TaskInProgress***: if the virtual machine is busy.
///
/// ***NotSupported***: if the host product does not support snapshots.
///
/// ***InsufficientResourcesFault***: if this operation would violate a resource
/// usage policy.
///
/// ***InvalidPowerState***: if the operation cannot be performed in the current
/// power state of the virtual machine.
///
/// ***InvalidState***: if the operation cannot be performed in the current state
/// of the virtual machine. For example, the virtual machine's configuration
/// is not available.
///
/// ***VmConfigFault***: if a configuration issue prevents the power-on. Typically, a
/// more specific fault, such as UnsupportedVmxLocation, is thrown.
///
/// ***FileFault***: if there is a problem accessing the virtual machine on the
/// filesystem.
pub async fn revert_to_snapshot_task(&self, host: Option<&crate::types::structs::ManagedObjectReference>, suppress_power_on: Option<bool>) -> Result<crate::types::structs::ManagedObjectReference> {
let input = RevertToSnapshotRequestType {host, suppress_power_on, };
let bytes = self.client.invoke("", "VirtualMachineSnapshot", &self.mo_id, "RevertToSnapshot_Task", Some(&input)).await?;
let result: crate::types::structs::ManagedObjectReference = crate::core::client::unmarshal(self.client.transport(), &bytes)?;
Ok(result)
}
/// Assigns a value to a custom field.
///
/// The setCustomValue method requires
/// whichever updatePrivilege is defined as one of the
/// *CustomFieldDef.fieldInstancePrivileges*
/// for the CustomFieldDef whose value is being changed.
///
/// ## Parameters:
///
/// ### key
/// The name of the field whose value is to be updated.
///
/// ### value
/// Value to be assigned to the custom field.
pub async fn set_custom_value(&self, key: &str, value: &str) -> Result<()> {
let input = SetCustomValueRequestType {key, value, };
self.client.invoke_void("", "VirtualMachineSnapshot", &self.mo_id, "setCustomValue", Some(&input)).await
}
/// List of custom field definitions that are valid for the object's type.
///
/// The fields are sorted by *CustomFieldDef.name*.
///
/// ***Required privileges:*** System.View
pub async fn available_field(&self) -> Result<Option<Vec<crate::types::structs::CustomFieldDef>>> {
let pv_opt = self.client.fetch_property_raw("", "VirtualMachineSnapshot", &self.mo_id, "availableField").await?;
match pv_opt {
Some(pv) => Ok(Some(crate::core::client::extract_property(pv)?)),
None => Ok(None),
}
}
/// All snapshots for which this snapshot is the parent.
///
/// ## Returns:
///
/// Refers instances of *VirtualMachineSnapshot*.
pub async fn child_snapshot(&self) -> Result<Option<Vec<crate::types::structs::ManagedObjectReference>>> {
let pv_opt = self.client.fetch_property_raw("", "VirtualMachineSnapshot", &self.mo_id, "childSnapshot").await?;
match pv_opt {
Some(pv) => Ok(Some(crate::core::client::extract_property(pv)?)),
None => Ok(None),
}
}
/// Information about the configuration of this virtual machine when this snapshot was
/// taken.
///
/// The datastore paths for the virtual machine disks point to the head of the disk
/// chain that represents the disk at this given snapshot. The fileInfo.fileLayout
/// field is not set.
pub async fn config(&self) -> Result<crate::types::structs::VirtualMachineConfigInfo> {
let pv_opt = self.client.fetch_property_raw("", "VirtualMachineSnapshot", &self.mo_id, "config").await?;
let pv = pv_opt.ok_or_else(|| crate::core::client::VimError::ParseError("property config was empty".to_string()))?;
let result: crate::types::structs::VirtualMachineConfigInfo = crate::core::client::extract_property(pv)?;
Ok(result)
}
/// List of custom field values.
///
/// Each value uses a key to associate
/// an instance of a *CustomFieldStringValue* with
/// a custom field definition.
///
/// ***Required privileges:*** System.View
pub async fn value(&self) -> Result<Option<Vec<Box<dyn crate::types::traits::CustomFieldValueTrait>>>> {
let pv_opt = self.client.fetch_property_raw("", "VirtualMachineSnapshot", &self.mo_id, "value").await?;
match pv_opt {
Some(pv) => Ok(Some(crate::core::client::extract_property(pv)?)),
None => Ok(None),
}
}
/// The virtual machine for which the snapshot was taken.
///
/// ## Returns:
///
/// Refers instance of *VirtualMachine*.
pub async fn vm(&self) -> Result<crate::types::structs::ManagedObjectReference> {
let pv_opt = self.client.fetch_property_raw("", "VirtualMachineSnapshot", &self.mo_id, "vm").await?;
let pv = pv_opt.ok_or_else(|| crate::core::client::VimError::ParseError("property vm was empty".to_string()))?;
let result: crate::types::structs::ManagedObjectReference = crate::core::client::extract_property(pv)?;
Ok(result)
}
}
struct RemoveSnapshotRequestType {
remove_children: bool,
consolidate: Option<bool>,
}
impl miniserde::Serialize for RemoveSnapshotRequestType {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(RemoveSnapshotRequestTypeSer { data: self, seq: 0 }))
}
}
struct RemoveSnapshotRequestTypeSer<'b> {
data: &'b RemoveSnapshotRequestType,
seq: usize,
}
impl<'b> miniserde::ser::Map for RemoveSnapshotRequestTypeSer<'b> {
fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
loop {
let seq = self.seq;
self.seq += 1;
match seq {
0 => return Some((std::borrow::Cow::Borrowed("_typeName"), &"RemoveSnapshotRequestType")),
1 => return Some((std::borrow::Cow::Borrowed("removeChildren"), &self.data.remove_children as &dyn miniserde::Serialize)),
2 => {
let Some(ref val) = self.data.consolidate else { continue; };
return Some((std::borrow::Cow::Borrowed("consolidate"), val as &dyn miniserde::Serialize));
}
_ => return None,
}
}
}
}
struct RenameSnapshotRequestType<'a> {
name: Option<&'a str>,
description: Option<&'a str>,
}
impl<'a> miniserde::Serialize for RenameSnapshotRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(RenameSnapshotRequestTypeSer { data: self, seq: 0 }))
}
}
struct RenameSnapshotRequestTypeSer<'b, 'a> {
data: &'b RenameSnapshotRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for RenameSnapshotRequestTypeSer<'b, 'a> {
fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
loop {
let seq = self.seq;
self.seq += 1;
match seq {
0 => return Some((std::borrow::Cow::Borrowed("_typeName"), &"RenameSnapshotRequestType")),
1 => {
let Some(ref val) = self.data.name else { continue; };
return Some((std::borrow::Cow::Borrowed("name"), val as &dyn miniserde::Serialize));
}
2 => {
let Some(ref val) = self.data.description else { continue; };
return Some((std::borrow::Cow::Borrowed("description"), val as &dyn miniserde::Serialize));
}
_ => return None,
}
}
}
}
struct RevertToSnapshotRequestType<'a> {
host: Option<&'a crate::types::structs::ManagedObjectReference>,
suppress_power_on: Option<bool>,
}
impl<'a> miniserde::Serialize for RevertToSnapshotRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(RevertToSnapshotRequestTypeSer { data: self, seq: 0 }))
}
}
struct RevertToSnapshotRequestTypeSer<'b, 'a> {
data: &'b RevertToSnapshotRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for RevertToSnapshotRequestTypeSer<'b, 'a> {
fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
loop {
let seq = self.seq;
self.seq += 1;
match seq {
0 => return Some((std::borrow::Cow::Borrowed("_typeName"), &"RevertToSnapshotRequestType")),
1 => {
let Some(ref val) = self.data.host else { continue; };
return Some((std::borrow::Cow::Borrowed("host"), val as &dyn miniserde::Serialize));
}
2 => {
let Some(ref val) = self.data.suppress_power_on else { continue; };
return Some((std::borrow::Cow::Borrowed("suppressPowerOn"), val as &dyn miniserde::Serialize));
}
_ => return None,
}
}
}
}
struct SetCustomValueRequestType<'a> {
key: &'a str,
value: &'a str,
}
impl<'a> miniserde::Serialize for SetCustomValueRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(SetCustomValueRequestTypeSer { data: self, seq: 0 }))
}
}
struct SetCustomValueRequestTypeSer<'b, 'a> {
data: &'b SetCustomValueRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for SetCustomValueRequestTypeSer<'b, 'a> {
fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
let seq = self.seq;
self.seq += 1;
match seq {
0 => return Some((std::borrow::Cow::Borrowed("_typeName"), &"setCustomValueRequestType")),
1 => return Some((std::borrow::Cow::Borrowed("key"), &self.data.key as &dyn miniserde::Serialize)),
2 => return Some((std::borrow::Cow::Borrowed("value"), &self.data.value as &dyn miniserde::Serialize)),
_ => return None,
}
}
}