1#!/usr/bin/python 2# -*- coding: utf-8 -*- 3# 4# Copyright (c) 2016 Red Hat, Inc. 5# 6# This file is part of Ansible 7# 8# Ansible is free software: you can redistribute it and/or modify 9# it under the terms of the GNU General Public License as published by 10# the Free Software Foundation, either version 3 of the License, or 11# (at your option) any later version. 12# 13# Ansible is distributed in the hope that it will be useful, 14# but WITHOUT ANY WARRANTY; without even the implied warranty of 15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16# GNU General Public License for more details. 17# 18# You should have received a copy of the GNU General Public License 19# along with Ansible. If not, see <http://www.gnu.org/licenses/>. 20# 21 22ANSIBLE_METADATA = {'metadata_version': '1.1', 23 'status': ['preview'], 24 'supported_by': 'community'} 25 26 27DOCUMENTATION = ''' 28--- 29module: ovirt_quota_info 30short_description: Retrieve information about one or more oVirt/RHV quotas 31version_added: "2.3" 32author: "Maor Lipchuk (@machacekondra)" 33description: 34 - "Retrieve information about one or more oVirt/RHV quotas." 35 - This module was called C(ovirt_quota_facts) before Ansible 2.9, returning C(ansible_facts). 36 Note that the M(ovirt_quota_info) module no longer returns C(ansible_facts)! 37notes: 38 - "This module returns a variable C(ovirt_quotas), which 39 contains a list of quotas. You need to register the result with 40 the I(register) keyword to use it." 41options: 42 data_center: 43 description: 44 - "Name of the datacenter where quota resides." 45 required: true 46 name: 47 description: 48 - "Name of the quota, can be used as glob expression." 49extends_documentation_fragment: ovirt_info 50''' 51 52EXAMPLES = ''' 53# Examples don't contain auth parameter for simplicity, 54# look at ovirt_auth module to see how to reuse authentication: 55 56# Gather information about quota named C<myquota> in Default datacenter: 57- ovirt_quota_info: 58 data_center: Default 59 name: myquota 60 register: result 61- debug: 62 msg: "{{ result.ovirt_quotas }}" 63''' 64 65RETURN = ''' 66ovirt_quotas: 67 description: "List of dictionaries describing the quotas. Quota attributes are mapped to dictionary keys, 68 all quotas attributes can be found at following url: http://ovirt.github.io/ovirt-engine-api-model/master/#types/quota." 69 returned: On success. 70 type: list 71''' 72 73import fnmatch 74import traceback 75 76from ansible.module_utils.basic import AnsibleModule 77from ansible.module_utils.ovirt import ( 78 check_sdk, 79 create_connection, 80 get_dict_of_struct, 81 ovirt_info_full_argument_spec, 82 search_by_name, 83) 84 85 86def main(): 87 argument_spec = ovirt_info_full_argument_spec( 88 data_center=dict(required=True), 89 name=dict(default=None), 90 ) 91 module = AnsibleModule(argument_spec) 92 is_old_facts = module._name == 'ovirt_quota_facts' 93 if is_old_facts: 94 module.deprecate("The 'ovirt_quota_facts' module has been renamed to 'ovirt_quota_info', " 95 "and the renamed one no longer returns ansible_facts", version='2.13') 96 97 check_sdk(module) 98 99 try: 100 auth = module.params.pop('auth') 101 connection = create_connection(auth) 102 datacenters_service = connection.system_service().data_centers_service() 103 dc_name = module.params['data_center'] 104 dc = search_by_name(datacenters_service, dc_name) 105 if dc is None: 106 raise Exception("Datacenter '%s' was not found." % dc_name) 107 108 quotas_service = datacenters_service.service(dc.id).quotas_service() 109 if module.params['name']: 110 quotas = [ 111 e for e in quotas_service.list() 112 if fnmatch.fnmatch(e.name, module.params['name']) 113 ] 114 else: 115 quotas = quotas_service.list() 116 117 result = dict( 118 ovirt_quotas=[ 119 get_dict_of_struct( 120 struct=c, 121 connection=connection, 122 fetch_nested=module.params.get('fetch_nested'), 123 attributes=module.params.get('nested_attributes'), 124 ) for c in quotas 125 ], 126 ) 127 if is_old_facts: 128 module.exit_json(changed=False, ansible_facts=result) 129 else: 130 module.exit_json(changed=False, **result) 131 except Exception as e: 132 module.fail_json(msg=str(e), exception=traceback.format_exc()) 133 finally: 134 connection.close(logout=auth.get('token') is None) 135 136 137if __name__ == '__main__': 138 main() 139