From d94a1ef4cc00fc5b1581e6a3ffabba2c14dbabdc Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Mon, 26 Feb 2018 13:23:22 +0530 Subject: [PATCH] VMware: refactor vmware_datastore_facts (#36423) This fix adds datastore cluster details about datastore in returning facts. Updated documentation and tests. Signed-off-by: Abhijeet Kasurde --- .../cloud/vmware/vmware_datastore_facts.py | 95 ++++++++++++------- .../vmware_datastore_facts/tasks/main.yml | 29 ++---- 2 files changed, 68 insertions(+), 56 deletions(-) diff --git a/lib/ansible/modules/cloud/vmware/vmware_datastore_facts.py b/lib/ansible/modules/cloud/vmware/vmware_datastore_facts.py index fd0ec6b2ba..5e129e0f57 100755 --- a/lib/ansible/modules/cloud/vmware/vmware_datastore_facts.py +++ b/lib/ansible/modules/cloud/vmware/vmware_datastore_facts.py @@ -1,45 +1,52 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# -# Copyright (c) 2017 Tim Rightnour +# Copyright (c) 2017, Tim Rightnour +# Copyright (c) 2018, Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function __metaclass__ = type -ANSIBLE_METADATA = {'metadata_version': '1.1', - 'status': ['preview'], - 'supported_by': 'community'} +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community' +} DOCUMENTATION = ''' --- module: vmware_datastore_facts -short_description: Gather facts about datastores +short_description: Gather facts about datastores available in given vCenter description: - - Gather facts about datastores in VMWare + - This module can be used to gather facts about datastores in VMWare infrastructure. + - All values and VMware object names are case sensitive. version_added: 2.5 author: - Tim Rightnour (@garbled1) notes: - - Tested on vSphere 5.5 + - Tested on vSphere 5.5, 6.0 and 6.5 requirements: - "python >= 2.6" - PyVmomi options: name: - description: - - Name of a datastore to match + description: + - Name of the datastore to match. + - If set, facts of specific datastores are returned. + required: False datacenter: - description: - - Datacenter to search for datastores - - This is required if cluster is not supplied + description: + - Datacenter to search for datastores. + - This parameter is required, if C(cluster) is not supplied. + required: False cluster: - description: - - Cluster to search for datastores - - This is required if datacenter is not supplied - required: False + description: + - Cluster to search for datastores. + - If set, facts of datastores belonging this clusters will be returned. + - This parameter is required, if C(datacenter) is not supplied. + required: False extends_documentation_fragment: vmware.documentation ''' @@ -53,26 +60,48 @@ EXAMPLES = ''' validate_certs: no delegate_to: localhost register: facts + +- name: Gather facts from datacenter about specific datastore + vmware_datastore_facts: + hostname: 192.168.1.209 + username: administrator@vsphere.local + password: vmware + datacenter: DC0 + name: datastore1 + validate_certs: no + delegate_to: localhost + register: facts ''' RETURN = """ -instance: +datastores: description: metadata about the available datastores returned: always - type: dict - sample: None + type: list + sample: [ + { + "accessible": false, + "capacity": 42681237504, + "datastore_cluster": "datacluster0", + "freeSpace": 39638269952, + "maintenanceMode": "normal", + "multipleHostAccess": false, + "name": "datastore2", + "provisioned": 12289211488, + "type": "VMFS", + "uncommitted": 9246243936, + "url": "ds:///vmfs/volumes/5a69b18a-c03cd88c-36ae-5254001249ce/" + }, + ] """ try: - import pyVmomi from pyVmomi import vim except ImportError: pass from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils._text import to_text -from ansible.module_utils.vmware import (connect_to_api, vmware_argument_spec, - get_all_objs, HAS_PYVMOMI, find_obj, +from ansible.module_utils.vmware import (PyVmomi, vmware_argument_spec, get_all_objs, find_cluster_by_name, get_parent_datacenter) @@ -101,14 +130,9 @@ class PyVmomiCache(object): return objects -class PyVmomiHelper(object): +class PyVmomiHelper(PyVmomi): def __init__(self, module): - if not HAS_PYVMOMI: - module.fail_json(msg='pyvmomi module required') - - self.module = module - self.params = module.params - self.content = connect_to_api(self.module) + super(PyVmomiHelper, self).__init__(module) self.cache = PyVmomiCache(self.content, dc_name=self.params['datacenter']) def lookup_datastore(self): @@ -162,6 +186,10 @@ def main(): dds['url'] = summary.url # Calculated values dds['provisioned'] = summary.capacity - summary.freeSpace + summary.uncommitted + dds['datastore_cluster'] = 'N/A' + if isinstance(ds.parent, vim.StoragePod): + dds['datastore_cluster'] = ds.parent.name + if module.params['name']: if dds['name'] == module.params['name']: datastores.extend([dds]) @@ -172,10 +200,7 @@ def main(): # found a datastore if datastores: - try: - module.exit_json(**result) - except Exception as exc: - module.fail_json(msg="Fact gather failed with exception %s" % to_text(exc)) + module.exit_json(**result) else: msg = "Unable to gather datastore facts" if module.params['name']: diff --git a/test/integration/targets/vmware_datastore_facts/tasks/main.yml b/test/integration/targets/vmware_datastore_facts/tasks/main.yml index 6f67563ddb..5c7c8c6713 100644 --- a/test/integration/targets/vmware_datastore_facts/tasks/main.yml +++ b/test/integration/targets/vmware_datastore_facts/tasks/main.yml @@ -1,21 +1,8 @@ # Test code for the vmware_datastore_facts module. -# (c) 2017, Tim Rightnour +# Copyright (c) 2017, Tim Rightnour +# Copyright (c) 2018, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . -# - name: make sure pyvmomi is installed pip: name: pyvmomi @@ -36,11 +23,11 @@ - name: kill vcsim uri: - url: "{{ 'http://' + vcsim + ':5000/killall' }}" + url: http://{{ vcsim }}:5000/killall - name: start vcsim uri: - url: "{{ 'http://' + vcsim + ':5000/spawn?ds=2&datacenter=1&cluster=1&folder=0' }}" + url: http://{{ vcsim }}:5000/spawn?ds=2&datacenter=1&cluster=1&folder=0 register: vcsim_instance - name: Wait for vcsim server to come up online @@ -51,7 +38,7 @@ - name: get a list of Clusters from vcsim uri: - url: "{{ 'http://' + vcsim + ':5000/govc_find?filter=CCR' }}" + url: http://{{ vcsim }}:5000/govc_find?filter=CCR register: clusters - set_fact: @@ -59,7 +46,7 @@ - name: get a list of Datacenters from vcsim uri: - url: "{{ 'http://' + vcsim + ':5000/govc_find?filter=DC' }}" + url: http://{{ vcsim }}:5000/govc_find?filter=DC register: datacenters - set_fact: @@ -67,7 +54,7 @@ - name: get a list of Datastores from vcsim uri: - url: "{{ 'http://' + vcsim + ':5000/govc_find?filter=D' }}" + url: http://{{ vcsim }}:5000/govc_find?filter=D register: datastores - set_fact: