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
use std::sync::Arc;
use crate::core::client::{VimClient, Result};
/// The *ViewManager* managed object provides methods to create *ContainerView*,
/// *InventoryView*, and *ListView* managed objects.
///
/// The *ViewManager* also maintains a list of managed object references
/// to the views that you have created. Use the *ViewManager.viewList*
/// property to access the views.
///
/// A *View* is a mechanism that supports selection of objects on the server
/// and subsequently, access to those objects. Views can simplify the task of
/// retrieving data from the server. When you use a view, you can use a single
/// invocation of a *PropertyCollector* method
/// to retrieve data or receive notification of changes instead of multiple invocations
/// involving multiple filter specifications. A view exists until you destroy it
/// or until the end of the session.
///
/// The *ViewManager* supports the following views:
/// - A *ContainerView* is based on *Folder*,
/// *Datacenter*, *ComputeResource*,
/// *ResourcePool*, or *HostSystem* managed objects.
/// Use a container view to monitor the container contents and optionally,
/// its descendants.
/// - A *ListView* managed object is based on an arbitrary but
/// specific set of objects. When you create a list view, you provide
/// a list of objects to populate the view
/// (*ViewManager.CreateListView*),
/// or you provide an existing view from which the new view is created
/// (*ViewManager.CreateListViewFromView*).
/// - An *InventoryView* managed object is based on the entire inventory.
/// Use an inventory view as a general mechanism to monitor the inventory
/// or portions of the inventory.
///
/// For example, you might use the following sequence of operations to get the
/// names of all the virtual machines on a server:
/// 1. Create a *ContainerView* for the root folder in the server inventory.
/// For the *ContainerView*, use the *ContainerView.type* property
/// to include only virtual machines.
/// 2. Create a filter specification for the *PropertyCollector*.
/// - Use the *ContainerView* as the starting object in the
/// *ObjectSpec* for the filter.
/// - Use the *TraversalSpec*
/// to select all objects in the view list (all the virtual machines).
/// - Use the *PropertySpec*
/// to retrieve the name property from each virtual machine.
/// 3. Invoke the *PropertyCollector*
/// *PropertyCollector.RetrieveProperties* method.
#[derive(Clone)]
pub struct ViewManager {
client: Arc<dyn VimClient>,
mo_id: String,
}
impl ViewManager {
pub fn new(client: Arc<dyn VimClient>, mo_id: &str) -> Self {
Self {
client,
mo_id: mo_id.to_string(),
}
}
/// Create a *ContainerView* managed object for this session.
///
/// The method returns
/// a reference to a *ContainerView* object that has a list of managed object references.
/// The list references objects in the container and may include references to objects from
/// additional containers. You can configure the resulting list of objects by specifying
/// a type list and recursion. Once you have created the view, the object list always
/// represents the current configuration of the virtual environment and reflects any
/// subsequent changes that occur.
///
/// ***Required privileges:*** System.View
///
/// ## Parameters:
///
/// ### container
/// A reference to an instance of a *Folder*,
/// *Datacenter*, *ComputeResource*,
/// *ResourcePool*, or *HostSystem* object.
///
/// ***Required privileges:*** System.View
///
/// Refers instance of *ManagedEntity*.
///
/// ### r#type
/// An optional list of managed entity types. The server
/// associates only objects of the specified type(s) with the view.
/// If you specify an empty array, the server uses all types.
///
/// ### recursive
/// Whether to include only the immediate children of the
/// container instance, or to include additional objects by
/// following paths beyond the immediate children.
///
/// When recursive is false, the list of objects contains
/// only immediate children.
/// When recursive is true, the server populates the list
/// by following references beyond the immediate children
/// (using a child's references, and then references in the
/// resulting objects, and so on).
///
/// Depending on the container type, the server will use the following
/// properties of the container instance to obtain objects for the
/// view's object list:
/// - *Folder* object - *Folder.childEntity*
/// property.
/// If recursive is false, the container list includes the reference
/// to the child entity in the folder instance.
/// If recursive is true, the server will follow the child
/// folder path(s) to collect additional childEntity references.
/// - *ResourcePool* object - *ResourcePool.vm*
/// and *ResourcePool.resourcePool* properties.
/// If recursive is false, the object list will contain references
/// to the virtual machines associated with this resource pool,
/// and references to virtual machines associated with the
/// immediate child resource pools. If recursive is true,
/// the server will follow all child resource pool paths
/// extending from the immediate children (and their children,
/// and so on) to collect additional references to virtual machines.
/// - *ComputeResource* object - *ComputeResource.host*
/// and *ComputeResource.resourcePool* properties.
/// If recursive is false, the object list will contain references
/// to the host systems associated with this compute resource,
/// references to virtual machines associated with the
/// host systems, and references to virtual machines associated
/// with the immediate child resource pools.
/// If recursive is true, the server will follow the child
/// resource pool paths (and their child resource pool paths,
/// and so on) to collect additional references to virtual machines.
/// - *Datacenter* object - *Datacenter.vmFolder*,
/// *Datacenter.hostFolder*,
/// *Datacenter.datastoreFolder*, and
/// *Datacenter.networkFolder* properties.
/// If recursive is set to false, the server uses the
/// immediate child folders for the virtual machines,
/// hosts, datastores, and networks associated with this
/// datacenter. If recursive is set to true, the server
/// will follow the folder paths to collect references
/// to additional objects.
/// - *HostSystem* object - *HostSystem.vm*
/// property.
/// The view object list contains references to the virtual machines
/// associated with this host system. The value of recursive does not
/// affect this behavior.
///
/// ## Returns:
///
/// Refers instance of *ContainerView*.
pub async fn create_container_view(&self, container: &crate::types::structs::ManagedObjectReference, r#type: Option<&[String]>, recursive: bool) -> Result<crate::types::structs::ManagedObjectReference> {
let input = CreateContainerViewRequestType {container, r#type, recursive, };
let bytes = self.client.invoke("", "ViewManager", &self.mo_id, "CreateContainerView", Some(&input)).await?;
let result: crate::types::structs::ManagedObjectReference = crate::core::client::unmarshal(self.client.transport(), &bytes)?;
Ok(result)
}
/// Create a new *InventoryView* managed object for this session.
///
/// ***Required privileges:*** System.View
///
/// ## Returns:
///
/// Refers instance of *InventoryView*.
pub async fn create_inventory_view(&self) -> Result<crate::types::structs::ManagedObjectReference> {
let bytes = self.client.invoke("", "ViewManager", &self.mo_id, "CreateInventoryView", None).await?;
let result: crate::types::structs::ManagedObjectReference = crate::core::client::unmarshal(self.client.transport(), &bytes)?;
Ok(result)
}
/// Create a *ListView* object for this session.
///
/// The method returns
/// a session object that has a list of managed object references. The list
/// of references corresponds to the input object list.
/// You can modify the resulting list after you have created the object.
///
/// ***Required privileges:*** System.View
///
/// ## Parameters:
///
/// ### obj
/// The initial list of objects in the view.
///
/// ***Required privileges:*** System.View
///
/// ## Returns:
///
/// Refers instance of *ListView*.
pub async fn create_list_view(&self, obj: Option<&[crate::types::structs::ManagedObjectReference]>) -> Result<crate::types::structs::ManagedObjectReference> {
let input = CreateListViewRequestType {obj, };
let bytes = self.client.invoke("", "ViewManager", &self.mo_id, "CreateListView", Some(&input)).await?;
let result: crate::types::structs::ManagedObjectReference = crate::core::client::unmarshal(self.client.transport(), &bytes)?;
Ok(result)
}
/// Create a *ListView* object for this session.
///
/// This method uses an existing
/// view to construct the object list for the new view.
///
/// ***Required privileges:*** System.View
///
/// ## Parameters:
///
/// ### view
/// The view that will provide the object list for the
/// new ListView object.
///
/// Refers instance of *View*.
///
/// ## Returns:
///
/// Refers instance of *ListView*.
pub async fn create_list_view_from_view(&self, view: &crate::types::structs::ManagedObjectReference) -> Result<crate::types::structs::ManagedObjectReference> {
let input = CreateListViewFromViewRequestType {view, };
let bytes = self.client.invoke("", "ViewManager", &self.mo_id, "CreateListViewFromView", Some(&input)).await?;
let result: crate::types::structs::ManagedObjectReference = crate::core::client::unmarshal(self.client.transport(), &bytes)?;
Ok(result)
}
/// An array of view references.
///
/// Each array entry is a managed object reference
/// to a view created by this ViewManager.
///
/// ***Required privileges:*** System.View
///
/// ## Returns:
///
/// Refers instances of *View*.
pub async fn view_list(&self) -> Result<Option<Vec<crate::types::structs::ManagedObjectReference>>> {
let pv_opt = self.client.fetch_property_raw("", "ViewManager", &self.mo_id, "viewList").await?;
match pv_opt {
Some(pv) => Ok(Some(crate::core::client::extract_property(pv)?)),
None => Ok(None),
}
}
}
struct CreateContainerViewRequestType<'a> {
container: &'a crate::types::structs::ManagedObjectReference,
r#type: Option<&'a [String]>,
recursive: bool,
}
impl<'a> miniserde::Serialize for CreateContainerViewRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(CreateContainerViewRequestTypeSer { data: self, seq: 0 }))
}
}
struct CreateContainerViewRequestTypeSer<'b, 'a> {
data: &'b CreateContainerViewRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for CreateContainerViewRequestTypeSer<'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"), &"CreateContainerViewRequestType")),
1 => return Some((std::borrow::Cow::Borrowed("container"), &self.data.container as &dyn miniserde::Serialize)),
2 => {
let Some(ref val) = self.data.r#type else { continue; };
return Some((std::borrow::Cow::Borrowed("type"), val as &dyn miniserde::Serialize));
}
3 => return Some((std::borrow::Cow::Borrowed("recursive"), &self.data.recursive as &dyn miniserde::Serialize)),
_ => return None,
}
}
}
}
struct CreateListViewRequestType<'a> {
obj: Option<&'a [crate::types::structs::ManagedObjectReference]>,
}
impl<'a> miniserde::Serialize for CreateListViewRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(CreateListViewRequestTypeSer { data: self, seq: 0 }))
}
}
struct CreateListViewRequestTypeSer<'b, 'a> {
data: &'b CreateListViewRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for CreateListViewRequestTypeSer<'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"), &"CreateListViewRequestType")),
1 => {
let Some(ref val) = self.data.obj else { continue; };
return Some((std::borrow::Cow::Borrowed("obj"), val as &dyn miniserde::Serialize));
}
_ => return None,
}
}
}
}
struct CreateListViewFromViewRequestType<'a> {
view: &'a crate::types::structs::ManagedObjectReference,
}
impl<'a> miniserde::Serialize for CreateListViewFromViewRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(CreateListViewFromViewRequestTypeSer { data: self, seq: 0 }))
}
}
struct CreateListViewFromViewRequestTypeSer<'b, 'a> {
data: &'b CreateListViewFromViewRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for CreateListViewFromViewRequestTypeSer<'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"), &"CreateListViewFromViewRequestType")),
1 => return Some((std::borrow::Cow::Borrowed("view"), &self.data.view as &dyn miniserde::Serialize)),
_ => return None,
}
}
}