Initial commit
[ta/infra-ansible.git] / roles / baremetal_provision / library / os_node_power_check.py
1 #!/usr/bin/python
2
3 # Copyright 2019 Nokia
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import fnmatch
18
19 try:
20     import shade
21     from shade import meta
22     HAS_SHADE = True
23 except ImportError:
24     HAS_SHADE = False
25
26 DOCUMENTATION = '''
27 ---
28 module: os_node_power_check
29 short_description: Retrieve facts about ironic nodes list provided,
30                    Returns a list of nodes which are not in specified power state
31 version_added: "2.0"
32 description:
33     - Retrieve facts about ironic nodes list provided,
34       Returns a list of nodes which are not in specified power state
35 requirements:
36     - "python >= 2.6"
37     - "shade"
38 options:
39    nodes_details:
40      description:
41        - List of dicts [{uuid: <ironic_node_id>, name: <ironic_node_name>},....], containing ironic node ids and names.
42      required: True
43      default: None
44    ironic_url:
45      description:
46        - The ironic URL to connect to
47      required: True
48      default: None
49    power_state:
50      description:
51        - The specified power state in which nodes are expected to be
52      required: True
53      default: None
54 returns:
55      power_pending_list
56 extends_documentation_fragment: openstack
57 '''
58
59 EXAMPLES = '''
60 - os_node_power_check:
61     nodes_details:
62       - uuid: <id-1>
63         name: <name-1>
64       - uuid: <id-2>
65         name: <name-2>
66     power_state: 'power on'
67 '''
68
69 def main():
70     argument_spec = openstack_full_argument_spec(
71         nodes_details=dict(type='list', required=True), 
72         ironic_url=dict(required=False),
73         power_state=dict(required=True),
74     )
75     module_kwargs = openstack_module_kwargs()
76     module = AnsibleModule(argument_spec, **module_kwargs)
77
78     if not HAS_SHADE:
79         module.fail_json(msg='shade is required for this module')
80     if (module.params['auth_type'] in [None, 'None'] and
81             module.params['ironic_url'] is None):
82         module.fail_json(msg="Authentication appears to be disabled, "
83                              "Please define an ironic_url parameter")
84
85     if (module.params['ironic_url'] and
86             module.params['auth_type'] in [None, 'None']):
87         module.params['auth'] = dict(
88             endpoint=module.params['ironic_url']
89         )
90
91     try:
92         cloud = shade.operator_cloud(**module.params)
93
94         nodes_details = module.params['nodes_details']
95         power_state = module.params['power_state']
96         power_pending_list = []
97
98         for node in nodes_details:
99             node_name = node['uuid']
100             node_details = cloud.get_machine(node_name)
101
102             if not node_details['power_state'] == power_state:
103                 power_pending_list.append(node_name)
104
105         module.exit_json(changed=False, ansible_facts=dict(power_pending_list=power_pending_list))
106
107     except shade.OpenStackCloudException as e:
108         module.fail_json(msg=str(e))
109
110 # this is magic, see lib/ansible/module_common.py
111 from ansible.module_utils.basic import *
112 from ansible.module_utils.openstack import *
113 if __name__ == '__main__':
114     main()