Skip to content

Commit

Permalink
Add a 'datastore.host.get' vPoller method for retrieving all HostSystems
Browse files Browse the repository at this point in the history
attached to a specific datastore
  • Loading branch information
dnaeon committed May 23, 2014
1 parent e4a41cf commit 18d44a7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ The table below summarizes the list of currently supported methods by `vPoller`
| vm.process.get | Get the running processes in a vim.VirtualMachine |
| datastore.discover | Discover all vim.Datastore objects |
| datastore.get | Get properties for a vim.Datastore object |
| datastore.host.get | Get all HostSystem objects using a specific datastore |

## Requirements

Expand Down
3 changes: 2 additions & 1 deletion src/misc-tools/vcli.source
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ _vcli_vm_process_get() { vpoller-client -m vm.process.get "$@" | jq '.' ;}

_vcli_datastore_discover() { vpoller-client -m datastore.discover "$@" | jq '.' ;}
_vcli_datastore_get() { vpoller-client -m datastore.get "$@" | jq '.' ;}
_vcli_datastore_host_get() { vpoller-client -m datastore.host.get "$@" | jq '.' ;}

alias 'vcli-about=_vcli_about'
alias 'vcli-event=_vcli_event'
Expand Down Expand Up @@ -74,4 +75,4 @@ alias 'vcli-vm-host-get=_vcli_vm_host_get'
alias 'vcli-vm-process-get=_vcli_vm_host_get'
alias 'vcli-datastore-discover=_vcli_datastore_discover'
alias 'vcli-datastore-get=_vcli_datastore_get'

alias 'vcli-datastore-host-get=_vcli_datastore_host_get'
56 changes: 56 additions & 0 deletions src/vpoller/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1460,3 +1460,59 @@ def datastore_get(self, msg):
obj_property_name='info.url',
obj_property_value=msg['name']
)

def datastore_host_get(self, msg):
"""
Get all HostSystem objects attached to a specific Datastore
Example client message would be:
{
"method": "datastore.host.get",
"hostname": "vc01.example.org",
"name": "ds:///vmfs/volumes/643f118a-a970df28/",
}
"""
logging.info('[%s] Getting HostSystem list using Datastore %s', self.host, msg['name'])

# Find the Datastore by it's 'info.url' property and get the HostSystem objects using it
data = self._get_object_properties(
properties=['info.name', 'info.url', 'host'],
obj_type=pyVmomi.vim.Datastore,
obj_property_name='info.url',
obj_property_value=msg['name']
)

if data['success'] != 0:
return data

# Get properties from the result
props = data['result'][0]
obj_name, obj_url, obj_host = props['info.name'], props['info.url'], props['host']

# obj_host is a list of DatastoreHostMount[] objects, but we need a list of HostSystem ones instead
obj_host = [h.key for h in obj_host]

# Get a list view of the hosts from this datastore object and collect properties
view_ref = self.get_list_view(obj=obj_host)
result = {}
result['name'] = obj_name
result['url'] = obj_url
result['host'] = self.collect_properties(
view_ref=view_ref,
obj_type=pyVmomi.vim.HostSystem,
path_set=['name']
)

view_ref.DestroyView()

r = {
'success': 0,
'msg': 'Successfully discovered objects',
'result': result,
}

logging.debug('[%s] Returning result from operation: %s', self.host, r)

return r
4 changes: 4 additions & 0 deletions src/vpoller/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ def process_client_msg(self, msg):
'method': self.agents[vsphere_host].datastore_get,
'msg_attr': ('method', 'hostname', 'name', 'properties'),
},
'datastore.host.get': {
'method': self.agents[vsphere_host].datastore_host_get,
'msg_attr': ('method', 'hostname', 'name'),
},
}

if msg['method'] not in methods:
Expand Down

0 comments on commit 18d44a7

Please sign in to comment.