-
Notifications
You must be signed in to change notification settings - Fork 765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add VmomiSupport.VmomiJSONEncoder for encoding pyVmomi objects as JSON #732
Conversation
@jeking3, VMware has approved your signed contributor license agreement. |
I didn't change anything relative to SSL, so it's odd that two SSL tests are failing. @tianhao64 this also happens locally for me. I'll take another look as to why. |
Updated for v6.7.1. |
@pgbidkar when will the next release be? |
@jeking3 Looks like the vcrpy latest release is not working well with existing SSL tests. If you pull the latest code or update vcrpy==1.12.0 in test-requirements.txt, the tests should pass. |
@tianhao64 I did that (actually set it to I have pull requests in other projects that depends on this change getting merged and released. I think it provides significant value. Let me know if anything is blocking this from getting in. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
I tried testing your changes in my Local test env
Command I ran is
python .\sample\dump_all_json.py -s -u
-p -t 'VirtualMachine' -i 'vm-id'
Getting this error
Traceback (most recent call last):
File ".\sample\dump_all_json.py", line 80, in
main()
File ".\sample\dump_all_json.py", line 77, in main
sort_keys=True, indent=4))
File "C:\Python27-13\Lib\json_init_.py", line 251, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "C:\Python27-13\Lib\json\encoder.py", line 209, in encode
chunks = list(chunks)
File "C:\Python27-13\Lib\json\encoder.py", line 442, in _iterencode
o = _default(o)
File "C:\Work\venvs\venv-2713\lib\site-packages\pyVmomi\VmomiSupport.py", line 345, in default
super(VmomiJSONEncoder, self).default(obj)
File "C:\Python27-13\Lib\json\encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: 'vim.VirtualMachine:vm-id' is not JSON serializable
Please can you retest and confirm?
@@ -273,6 +274,106 @@ def __call__(self, **kwargs): | |||
else: | |||
raise AttributeError("'%s' does not exist" % self.name) | |||
|
|||
|
|||
class VmomiJSONEncoder(json.JSONEncoder): | |||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation in pyVmomi code is 3 space. Looks like you did 4 space indentation.
Please can you check and change that?
@pgbidkar I do not see what you are seeing. This works fine for me:
There must be something different in your environment. Are you running against a real vCenter or a simulator? |
@pgbidkar 3 space indents are non-standard, you should consider standardizing. I made the changes you requested. |
Hi @jeking3 , I am running sample with Real vCenter |
Not sure where you got "dump_all_json.py" from; the sample included is called "dumpjson.py". Line 345 is not the proper line for calling to default, line 352 is. Do you have your virtual environment set up with the latest copy of this PR? https://github.com/vmware/pyvmomi/pull/732/files#diff-3d209b7e252f047d63b9b948cb74282cR352 |
@jeking3 Created fresh Virtual env. Cloned json-encode branch. Sample is running fine now. |
Any thoughts? |
Merging the change. Thanks |
Thanks. Now the only thing blocking upstream changes is a pip release. |
|
This fixes #21
This closes #669
This allows for deep JSON encoding of pyVmomi Managed Objects by providing a JSONEncoder. Tested with Datacenter, Datastore, HostSystem, Network, VirtualMachine objects against a VCSA version 6.7. A sample python script is provided allowing any object to be dumped by type and MOID.
Example: Serialize a VirtualMachine
Example: Serialize a list of VirtualMachines and their snapshots
Example: Serialize a vim.Network using the dumpjson example
Command Line
Assuming you have already run tox:
Code
Or perhaps you just want to see the code:
Result
Note this is perhaps a poor example, since the encoder handles deep structures completely, but the other examples are rather huge.