Merge "Added http concurrency test suite for agnostic VNF"
[yardstick.git] / ansible / library / shade_api.py
1 #!/usr/bin/env python
2 # Copyright (c) 2017 Intel Corporation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 DOCUMENTATION = '''
17 ---
18 module: shade_api
19 short_description: directly access shade
20 description:
21     - directly access shade API
22 options:
23   method: shade method
24   args: list of ags
25   kwargs: dict of kwargs
26   fact_name: name of ansible fact to store result
27 '''
28
29 try:
30     import shade
31 except ImportError:
32     SHADE_PRESENT = False
33 else:
34     SHADE_PRESENT = True
35
36
37 def main():
38     module = AnsibleModule(
39         argument_spec={
40             'method': {'required': True, 'type': 'str'},
41             'args': {'required': False, 'type': 'list', 'default': []},
42             'kwargs': {'required': False, 'type': 'dict', 'default': {}},
43             'os_auth': {'required': False, 'type': 'dict', 'default': {}},
44             'fact_name': {'required': True, 'type': 'str'},
45         }
46     )
47
48     if not SHADE_PRESENT:
49         module.fail_json(msg="shade not found")
50     shade.simple_logging(debug=True)
51     params = module.params
52     method = params['method']
53     args = params['args']
54     kwargs = params['kwargs']
55     os_auth = params['os_auth']
56     fact_name = params['fact_name']
57     if os_auth:
58         os.environ.update(os_auth)
59
60     c = shade.openstack_cloud()
61     module.debug(args)
62     module.debug(kwargs)
63     ret = getattr(c, method)(*args, **kwargs)
64     if ret:
65         try:
66             # convert to regular dict, might not be necessary
67             ret = ret.toDict()
68         except AttributeError:
69             pass
70     else:
71         ret = {}
72     ansible_facts = {
73         fact_name: ret
74     }
75     module.exit_json(ansible_facts=ansible_facts)
76
77
78 # <<INCLUDE_ANSIBLE_MODULE_COMMON>>
79 from ansible.module_utils.basic import *  # noqa
80
81 if __name__ == '__main__':
82     main()