Skip to content

Commit

Permalink
Merge pull request vmware#738 from jeking3/json-datatypes
Browse files Browse the repository at this point in the history
JSON: remove dynamicProperty and dynamicType if empty; add _vimtype to DataObjects
  • Loading branch information
tianhao64 committed Dec 18, 2018
2 parents 37c3582 + 827747f commit 35e9bc6
Show file tree
Hide file tree
Showing 19 changed files with 4,730 additions and 3,537 deletions.
29 changes: 27 additions & 2 deletions pyVmomi/VmomiSupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ class VmomiJSONEncoder(json.JSONEncoder):
_vimref is the moRef (ex: 'vim.VirtualMachine:vm-42')
_vimtype is the class name (ex: 'vim.VirtualMachine')
When a DataObject is encoded, it gains one property:
_vimtype is the class name (ex: 'vim.VirtualMachineQuestionInfo')
If the dynamicProperty and dynamicType are empty, they are optionally
omitted from the results of DataObjects and ManagedObjects
@example "Explode only the object passed in"
data = json.dumps(vm, cls=VmomiJSONEncoder)
Expand All @@ -310,12 +316,29 @@ def __init__(self, *args, **kwargs):
expand in the conversion process. Directly add any
objects to the list, or add the type of any object
using type(templateOf('VirtualMachine')) for example.
@param strip_dynamic (bool) Every object normally contains
a dynamicProperty list and a dynamicType field even if
the contents are [] and None respectively. These fields
clutter the output making it more difficult for a person
to read and bloat the size of the result unnecessarily.
This option removes them if they are the empty values above.
The default is False.
"""
self._done = set()
self._first = True
self._explode = kwargs.pop('explode', None)
self._strip_dynamic = kwargs.pop('strip_dynamic', False)
super(VmomiJSONEncoder, self).__init__(*args, **kwargs)

def _remove_empty_dynamic_if(self, result):
if self._strip_dynamic:
if 'dynamicProperty' in result and len(result['dynamicProperty']) == 0:
result.pop('dynamicProperty')
if 'dynamicType' in result and not result['dynamicType']:
result.pop('dynamicType')
return result

def default(self, obj): # pylint: disable=method-hidden
if self._first:
self._first = False
Expand All @@ -332,10 +355,12 @@ def default(self, obj): # pylint: disable=method-hidden
result['_vimtype'] = obj.__class__.__name__
for prop in obj._GetPropertyList():
result[prop.name] = getattr(obj, prop.name)
return result
return self._remove_empty_dynamic_if(result)
return str(obj).strip("'") # see FormatObject below
if isinstance(obj, DataObject):
return obj.__dict__
result = {k:v for k,v in obj.__dict__.items()}
result['_vimtype'] = obj.__class__.__name__
return self._remove_empty_dynamic_if(result)
if isinstance(obj, binary):
result = base64.b64encode(obj)
if PY3: # see FormatObject below
Expand Down
5 changes: 2 additions & 3 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ class VCRTestBase(unittest.TestCase):
def setUp(self):
monkey_patch_vcrpy()
logging.basicConfig()
# pretty loud with mismatch warnings...
# vcr_log = logging.getLogger('vcr')
# vcr_log.setLevel(logging.DEBUG)
vcr_log = logging.getLogger('vcr')
vcr_log.setLevel(logging.WARNING)
2 changes: 1 addition & 1 deletion tests/files/test_json_datacenter_explode.expect

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/files/test_json_datastore_explode.expect
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"_vimid": "datastore-15", "_vimref": "vim.Datastore:datastore-15", "_vimtype": "vim.Datastore", "alarmActionsEnabled": true, "availableField": [], "browser": "vim.host.DatastoreBrowser:datastoreBrowser-datastore-15", "capability": {"directoryHierarchySupported": true, "dynamicProperty": [], "dynamicType": null, "nativeSnapshotSupported": false, "perFileThinProvisioningSupported": true, "rawDiskMappingsSupported": true, "seSparseSupported": true, "storageIORMSupported": true, "topLevelDirectoryCreateSupported": true, "upitSupported": null, "vmdkExpandSupported": true, "vmfsSparseSupported": false, "vsanSparseSupported": false}, "configIssue": [], "configStatus": "gray", "customValue": [], "declaredAlarmState": [{"acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-110", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-110.datastore-15", "overallStatus": "gray", "time": "2018-09-19T18:00:22.353264Z"}, {"acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-24", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-24.datastore-15", "overallStatus": "gray", "time": "2018-09-19T18:00:22.353397Z"}, {"acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-28", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-28.datastore-15", "overallStatus": "gray", "time": "2018-09-19T18:00:22.353475Z"}, {"acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-29", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-29.datastore-15", "overallStatus": "gray", "time": "2018-09-19T18:00:22.353541Z"}, {"acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-53", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-53.datastore-15", "overallStatus": "gray", "time": "2018-09-19T18:00:22.353598Z"}, {"acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-7", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-7.datastore-15", "overallStatus": "green", "time": "2018-09-19T18:00:22.353783Z"}, {"acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-79", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-79.datastore-15", "overallStatus": "gray", "time": "2018-09-19T18:00:22.353844Z"}, {"acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-80", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-80.datastore-15", "overallStatus": "gray", "time": "2018-09-19T18:00:22.356378Z"}, {"acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-84", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-84.datastore-15", "overallStatus": "gray", "time": "2018-09-19T18:00:22.356497Z"}], "disabledMethod": ["DatastoreExitMaintenanceMode_Task"], "effectiveRole": [-1], "host": [{"dynamicProperty": [], "dynamicType": null, "key": "vim.HostSystem:host-14", "mountInfo": {"accessMode": "readWrite", "accessible": true, "dynamicProperty": [], "dynamicType": null, "inaccessibleReason": null, "mounted": true, "path": "/vmfs/volumes/5b7ee5c9-4e8f1921-93b3-90b11c51ac73"}}], "info": {"aliasOf": null, "containerId": null, "dynamicProperty": [], "dynamicType": null, "freeSpace": 177978998784, "lun": [], "maxFileSize": 70368744177664, "maxMemoryFileSize": 70368744177664, "maxPhysicalRDMFileSize": 70368744177664, "maxVirtualDiskCapacity": 68169720922112, "maxVirtualRDMFileSize": 68169720922112, "name": "datastore1", "timestamp": "2018-10-25T00:07:06.456385Z", "url": "ds:///vmfs/volumes/5b7ee5c9-4e8f1921-93b3-90b11c51ac73/", "vmfs": {"blockSize": 1024, "blockSizeMb": 1, "capacity": 590826438656, "dynamicProperty": [], "dynamicType": null, "extent": [{"diskName": "naa.6b8ca3a0e65228002312a2e3098048bf", "dynamicProperty": [], "dynamicType": null, "partition": 3}], "forceMountedInfo": null, "local": true, "majorVersion": 6, "maxBlocks": 63963136, "name": "datastore1", "scsiDiskType": null, "ssd": false, "type": "VMFS", "unmapBandwidthSpec": null, "unmapGranularity": 1024, "unmapPriority": "low", "uuid": "5b7ee5c9-4e8f1921-93b3-90b11c51ac73", "version": "6.82", "vmfsUpgradable": false}}, "iormConfiguration": {"congestionThreshold": 30, "congestionThresholdMode": "automatic", "dynamicProperty": [], "dynamicType": null, "enabled": false, "percentOfPeakThroughput": 90, "reservableIopsThreshold": null, "reservationEnabled": true, "statsAggregationDisabled": false, "statsCollectionEnabled": false}, "name": "datastore1", "overallStatus": "green", "parent": "vim.Folder:group-s5", "permission": [], "recentTask": [], "summary": {"accessible": true, "capacity": 590826438656, "datastore": "vim.Datastore:datastore-15", "dynamicProperty": [], "dynamicType": null, "freeSpace": 177978998784, "maintenanceMode": "normal", "multipleHostAccess": false, "name": "datastore1", "type": "VMFS", "uncommitted": 335226992919, "url": "ds:///vmfs/volumes/5b7ee5c9-4e8f1921-93b3-90b11c51ac73/"}, "tag": [], "triggeredAlarmState": [], "value": [], "vm": ["vim.VirtualMachine:vm-127", "vim.VirtualMachine:vm-69", "vim.VirtualMachine:vm-17", "vim.VirtualMachine:vm-42", "vim.VirtualMachine:vm-18", "vim.VirtualMachine:vm-36", "vim.VirtualMachine:vm-19", "vim.VirtualMachine:vm-21", "vim.VirtualMachine:vm-20", "vim.VirtualMachine:vm-22", "vim.VirtualMachine:vm-32"]}
{"_vimid": "datastore-15", "_vimref": "vim.Datastore:datastore-15", "_vimtype": "vim.Datastore", "alarmActionsEnabled": true, "availableField": [], "browser": "vim.host.DatastoreBrowser:datastoreBrowser-datastore-15", "capability": {"_vimtype": "vim.Datastore.Capability", "directoryHierarchySupported": true, "dynamicProperty": [], "dynamicType": null, "nativeSnapshotSupported": false, "perFileThinProvisioningSupported": true, "rawDiskMappingsSupported": true, "seSparseSupported": true, "storageIORMSupported": true, "topLevelDirectoryCreateSupported": true, "upitSupported": null, "vmdkExpandSupported": true, "vmfsSparseSupported": false, "vsanSparseSupported": false}, "configIssue": [], "configStatus": "gray", "customValue": [], "declaredAlarmState": [{"_vimtype": "vim.alarm.AlarmState", "acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-110", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-110.datastore-15", "overallStatus": "gray", "time": "2018-12-13T22:54:02.499309Z"}, {"_vimtype": "vim.alarm.AlarmState", "acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-24", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-24.datastore-15", "overallStatus": "gray", "time": "2018-12-13T22:54:02.499309Z"}, {"_vimtype": "vim.alarm.AlarmState", "acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-28", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-28.datastore-15", "overallStatus": "gray", "time": "2018-12-13T22:54:02.499309Z"}, {"_vimtype": "vim.alarm.AlarmState", "acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-29", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-29.datastore-15", "overallStatus": "gray", "time": "2018-12-13T22:54:02.499309Z"}, {"_vimtype": "vim.alarm.AlarmState", "acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-53", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-53.datastore-15", "overallStatus": "gray", "time": "2018-12-13T22:54:02.499309Z"}, {"_vimtype": "vim.alarm.AlarmState", "acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-7", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-7.datastore-15", "overallStatus": "yellow", "time": "2018-12-14T14:16:05.020652Z"}, {"_vimtype": "vim.alarm.AlarmState", "acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-79", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-79.datastore-15", "overallStatus": "gray", "time": "2018-12-13T22:54:02.499309Z"}, {"_vimtype": "vim.alarm.AlarmState", "acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-80", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-80.datastore-15", "overallStatus": "gray", "time": "2018-12-13T22:54:02.499309Z"}, {"_vimtype": "vim.alarm.AlarmState", "acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-84", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-84.datastore-15", "overallStatus": "gray", "time": "2018-12-13T22:54:02.499309Z"}], "disabledMethod": ["DatastoreExitMaintenanceMode_Task"], "effectiveRole": [-1], "host": [{"_vimtype": "vim.Datastore.HostMount", "dynamicProperty": [], "dynamicType": null, "key": "vim.HostSystem:host-14", "mountInfo": {"_vimtype": "vim.host.MountInfo", "accessMode": "readWrite", "accessible": true, "dynamicProperty": [], "dynamicType": null, "inaccessibleReason": null, "mounted": true, "path": "/vmfs/volumes/5b7ee5c9-4e8f1921-93b3-90b11c51ac73"}}], "info": {"_vimtype": "vim.host.VmfsDatastoreInfo", "aliasOf": null, "containerId": null, "dynamicProperty": [], "dynamicType": null, "freeSpace": 124541468672, "lun": [], "maxFileSize": 70368744177664, "maxMemoryFileSize": 70368744177664, "maxPhysicalRDMFileSize": 70368744177664, "maxVirtualDiskCapacity": 68169720922112, "maxVirtualRDMFileSize": 68169720922112, "name": "datastore1", "timestamp": "2018-12-18T12:16:29.731789Z", "url": "ds:///vmfs/volumes/5b7ee5c9-4e8f1921-93b3-90b11c51ac73/", "vmfs": {"_vimtype": "vim.host.VmfsVolume", "blockSize": 1024, "blockSizeMb": 1, "capacity": 590826438656, "dynamicProperty": [], "dynamicType": null, "extent": [{"_vimtype": "vim.host.ScsiDisk.Partition", "diskName": "naa.6b8ca3a0e65228002312a2e3098048bf", "dynamicProperty": [], "dynamicType": null, "partition": 3}], "forceMountedInfo": null, "local": true, "majorVersion": 6, "maxBlocks": 63963136, "name": "datastore1", "scsiDiskType": null, "ssd": false, "type": "VMFS", "unmapBandwidthSpec": null, "unmapGranularity": 1024, "unmapPriority": "low", "uuid": "5b7ee5c9-4e8f1921-93b3-90b11c51ac73", "version": "6.82", "vmfsUpgradable": false}}, "iormConfiguration": {"_vimtype": "vim.StorageResourceManager.IORMConfigInfo", "congestionThreshold": 30, "congestionThresholdMode": "automatic", "dynamicProperty": [], "dynamicType": null, "enabled": false, "percentOfPeakThroughput": 90, "reservableIopsThreshold": null, "reservationEnabled": true, "statsAggregationDisabled": false, "statsCollectionEnabled": false}, "name": "datastore1", "overallStatus": "yellow", "parent": "vim.Folder:group-s5", "permission": [], "recentTask": [], "summary": {"_vimtype": "vim.Datastore.Summary", "accessible": true, "capacity": 590826438656, "datastore": "vim.Datastore:datastore-15", "dynamicProperty": [], "dynamicType": null, "freeSpace": 124541468672, "maintenanceMode": "normal", "multipleHostAccess": false, "name": "datastore1", "type": "VMFS", "uncommitted": 372001753348, "url": "ds:///vmfs/volumes/5b7ee5c9-4e8f1921-93b3-90b11c51ac73/"}, "tag": [], "triggeredAlarmState": [{"_vimtype": "vim.alarm.AlarmState", "acknowledged": false, "acknowledgedByUser": null, "acknowledgedTime": null, "alarm": "vim.alarm.Alarm:alarm-7", "dynamicProperty": [], "dynamicType": null, "entity": "vim.Datastore:datastore-15", "eventKey": null, "key": "alarm-7.datastore-15", "overallStatus": "yellow", "time": "2018-12-14T14:16:05.020652Z"}], "value": [], "vm": ["vim.VirtualMachine:vm-20", "vim.VirtualMachine:vm-849", "vim.VirtualMachine:vm-1638", "vim.VirtualMachine:vm-36", "vim.VirtualMachine:vm-69", "vim.VirtualMachine:vm-32", "vim.VirtualMachine:vm-22"]}
2 changes: 1 addition & 1 deletion tests/files/test_json_host_explode.expect

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/files/test_json_network_explode.expect
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"_vimid": "network-16", "_vimref": "vim.Network:network-16", "_vimtype": "vim.Network", "alarmActionsEnabled": true, "availableField": [], "configIssue": [], "configStatus": "green", "customValue": [], "declaredAlarmState": [], "disabledMethod": [], "effectiveRole": [-1], "host": ["vim.HostSystem:host-14", "vim.HostSystem:host-25"], "name": "VM Network", "overallStatus": "green", "parent": "vim.Folder:group-n6", "permission": [], "recentTask": [], "summary": {"accessible": true, "dynamicProperty": [], "dynamicType": null, "ipPoolId": null, "ipPoolName": "", "name": "VM Network", "network": "vim.Network:network-16"}, "tag": [], "triggeredAlarmState": [], "value": [], "vm": ["vim.VirtualMachine:vm-17", "vim.VirtualMachine:vm-18", "vim.VirtualMachine:vm-19", "vim.VirtualMachine:vm-20", "vim.VirtualMachine:vm-21", "vim.VirtualMachine:vm-22", "vim.VirtualMachine:vm-27", "vim.VirtualMachine:vm-28", "vim.VirtualMachine:vm-29", "vim.VirtualMachine:vm-32", "vim.VirtualMachine:vm-36", "vim.VirtualMachine:vm-42", "vim.VirtualMachine:vm-69", "vim.VirtualMachine:vm-86", "vim.VirtualMachine:vm-111", "vim.VirtualMachine:vm-127"]}
{"_vimid": "network-16", "_vimref": "vim.Network:network-16", "_vimtype": "vim.Network", "alarmActionsEnabled": true, "availableField": [], "configIssue": [], "configStatus": "green", "customValue": [], "declaredAlarmState": [], "disabledMethod": [], "effectiveRole": [-1], "host": ["vim.HostSystem:host-14", "vim.HostSystem:host-25"], "name": "VM Network", "overallStatus": "green", "parent": "vim.Folder:group-n6", "permission": [], "recentTask": [], "summary": {"_vimtype": "vim.Network.Summary", "accessible": true, "dynamicProperty": [], "dynamicType": null, "ipPoolId": null, "ipPoolName": "", "name": "VM Network", "network": "vim.Network:network-16"}, "tag": [], "triggeredAlarmState": [], "value": [], "vm": ["vim.VirtualMachine:vm-20", "vim.VirtualMachine:vm-22", "vim.VirtualMachine:vm-27", "vim.VirtualMachine:vm-849", "vim.VirtualMachine:vm-32", "vim.VirtualMachine:vm-1717", "vim.VirtualMachine:vm-36", "vim.VirtualMachine:vm-175", "vim.VirtualMachine:vm-176", "vim.VirtualMachine:vm-1638", "vim.VirtualMachine:vm-69", "vim.VirtualMachine:vm-86", "vim.VirtualMachine:vm-227", "vim.VirtualMachine:vm-1731"]}
2 changes: 1 addition & 1 deletion tests/files/test_json_vm_explode_default.expect

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/files/test_json_vm_explode_objs_match.expect

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/files/test_json_vm_explode_strip_dynamic.expect

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/files/test_json_vm_explode_type_match.expect

Large diffs are not rendered by default.

Loading

0 comments on commit 35e9bc6

Please sign in to comment.