Adds attached volumes to instance facts (#23132)

* Adds attached volumes to instance facts

* Fixed identation and modified coding style

* includes revisions after maintainers review

* Coding style modified

* simplifies getting items from dict
This commit is contained in:
Khachatur Nazaretyan 2017-04-09 11:59:41 +02:00 committed by René Moser
parent 69c8dcc811
commit e8f1747a88

View file

@ -180,6 +180,11 @@ cloudstack_instance.instance_name:
returned: success returned: success
type: string type: string
sample: i-44-3992-VM sample: i-44-3992-VM
cloudstack_instance.volumes:
description: List of dictionaries of the volumes attached to the instance.
returned: success
type: list
sample: '[ { name: "ROOT-1369", type: "ROOT", size: 10737418240 }, { name: "data01, type: "DATADISK", size: 10737418240 } ]'
''' '''
import base64 import base64
@ -227,6 +232,20 @@ class AnsibleCloudStackInstanceFacts(AnsibleCloudStack):
break break
return self.instance return self.instance
def get_volumes(self, instance):
volume_details = []
if instance:
args = {}
args['account'] = instance.get('account')
args['domainid'] = instance.get('domainid')
args['projectid'] = instance.get('projectid')
args['virtualmachineid'] = instance['id']
volumes = self.cs.listVolumes(**args)
if volumes:
for vol in volumes['volume']:
volume_details.append({'size': vol['size'], 'type': vol['type'], 'name': vol['name']})
return volume_details
def run(self): def run(self):
instance = self.get_instance() instance = self.get_instance()
@ -253,6 +272,9 @@ class AnsibleCloudStackInstanceFacts(AnsibleCloudStack):
for nic in instance['nic']: for nic in instance['nic']:
if nic['isdefault'] and 'ipaddress' in nic: if nic['isdefault'] and 'ipaddress' in nic:
self.result['default_ip'] = nic['ipaddress'] self.result['default_ip'] = nic['ipaddress']
volumes = self.get_volumes(instance)
if volumes:
self.result['volumes'] = volumes
return self.result return self.result