Juan Hernandez has uploaded a new change for review.
Change subject: sdk: Add empty export method to decorator base
......................................................................
sdk: Add empty export method to decorator base
Decorators for resources and collections extend a common base class, for
example:
class VMSnapshotDisks(Base)
The resource decorators also extend the corresponding parameter class:
class VMSnapshotDisk(params.Disk, Base)
This means that resource decorators implement the "export" method,
responsible for generating the XML representation of the entity, but
collection decorators don't implement it.
There are situations where decorators are used as parameters, for
example, when creating a VM from a snapshot one could use the following
code:
snapshot = vm.snapshots.get(id="...")
The resulting object is a decorator, and it contains references to
decorators of collections, for example to the collection of disks. Later
this object can be used as a parameter, as follows:
snapshots = ovirtsdk.xml.params.Snapshots()
snapshots.add_snapshot(snapshot)
newvm = ovirtsdk.xml.params.VM(
name="newvm",
snapshots=snapshots,
...)
api.vms.add(newvm)
When doing this the infrastructure will try to generate the XML
document, calling the "export" method on the new VM object, and this
will recursively call the "export" methods of all the referenced
objects, including the collection decorators, which will fail because
they don't have such method.
This usage is not good practice, and not efficient, it is better to
avoid using decorators as parameters:
snapshot = ovirtsdk.params.Snapshot(id="...")
snapshots = ovirtsdk.params.Snapshots()
snapshots.add_snapshot(snapshot)
newvm = ovirtsdk.xml.params.VM(
name="newvm",
snapshots=snapshots,
...)
api.vms.add(newvm)
As this is difficult to enforce this patch adds to the Base class an
empty "export" method, so that this operations won't fail.
Change-Id: I7843dea85c9f09de5fb3439f57dba71ce6a28898
Signed-off-by: Juan Hernandez <[email protected]>
---
M src/ovirtsdk/infrastructure/common.py
1 file changed, 6 insertions(+), 0 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-sdk refs/changes/31/24131/1
diff --git a/src/ovirtsdk/infrastructure/common.py
b/src/ovirtsdk/infrastructure/common.py
index 5fbc581..5e5f714 100644
--- a/src/ovirtsdk/infrastructure/common.py
+++ b/src/ovirtsdk/infrastructure/common.py
@@ -43,3 +43,9 @@
raise ImmutableError(name)
else:
super(Base, self).__setattr__(name, value)
+
+ def export(self, outfile, level, namespace_='', name_='',
namespacedef_='', pretty_print=True):
+ # This empty method is necessary in order to avoid exceptions when the
+ # infrastructure tries to invoke it on a collection decorator that is
+ # used as a parameter.
+ pass
--
To view, visit http://gerrit.ovirt.org/24131
To unsubscribe, visit http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I7843dea85c9f09de5fb3439f57dba71ce6a28898
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine-sdk
Gerrit-Branch: master
Gerrit-Owner: Juan Hernandez <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches