| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2016-2017, Hewlett Packard Enterprise Development LP |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: oneview_enclosure_info |
| 11 |
short_description: Retrieve information about one or more Enclosures |
| 12 |
description: |
| 13 |
- Retrieve information about one or more of the Enclosures from OneView. |
| 14 |
requirements: |
| 15 |
- hpOneView >= 2.0.1 |
| 16 |
author: |
| 17 |
- Felipe Bulsoni (@fgbulsoni) |
| 18 |
- Thiago Miotto (@tmiotto) |
| 19 |
- Adriane Cardozo (@adriane-cardozo) |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
version_added: 3.3.0 |
| 23 |
# This was backported to 2.5.4 and 1.3.11 as well, since this was a bugfix |
| 24 |
options: |
| 25 |
name: |
| 26 |
description: |
| 27 |
- Enclosure name. |
| 28 |
type: str |
| 29 |
options: |
| 30 |
description: |
| 31 |
- 'List with options to gather additional information about an Enclosure and related resources. Options allowed: V(script), |
| 32 |
V(environmentalConfiguration), and V(utilization). For the option V(utilization), you can provide specific parameters.' |
| 33 |
type: list |
| 34 |
elements: raw |
| 35 |
|
| 36 |
extends_documentation_fragment: |
| 37 |
- community.general._oneview |
| 38 |
- community.general._oneview.factsparams |
| 39 |
- community.general._attributes |
| 40 |
- community.general._attributes.info_module |
| 41 |
""" |
| 42 |
|
| 43 |
EXAMPLES = r""" |
| 44 |
- name: Gather information about all Enclosures |
| 45 |
community.general.oneview_enclosure_info: |
| 46 |
hostname: 172.16.101.48 |
| 47 |
username: administrator |
| 48 |
password: my_password |
| 49 |
api_version: 500 |
| 50 |
no_log: true |
| 51 |
delegate_to: localhost |
| 52 |
register: result |
| 53 |
|
| 54 |
- name: Print fetched information about Enclosures |
| 55 |
ansible.builtin.debug: |
| 56 |
msg: "{{ result.enclosures }}" |
| 57 |
|
| 58 |
- name: Gather paginated, filtered and sorted information about Enclosures |
| 59 |
community.general.oneview_enclosure_info: |
| 60 |
params: |
| 61 |
start: 0 |
| 62 |
count: 3 |
| 63 |
sort: name:descending |
| 64 |
filter: status=OK |
| 65 |
hostname: 172.16.101.48 |
| 66 |
username: administrator |
| 67 |
password: my_password |
| 68 |
api_version: 500 |
| 69 |
no_log: true |
| 70 |
delegate_to: localhost |
| 71 |
register: result |
| 72 |
|
| 73 |
- name: Print fetched information about paginated, filtered and sorted list of Enclosures |
| 74 |
ansible.builtin.debug: |
| 75 |
msg: "{{ result.enclosures }}" |
| 76 |
|
| 77 |
- name: Gather information about an Enclosure by name |
| 78 |
community.general.oneview_enclosure_info: |
| 79 |
name: Enclosure-Name |
| 80 |
hostname: 172.16.101.48 |
| 81 |
username: administrator |
| 82 |
password: my_password |
| 83 |
api_version: 500 |
| 84 |
no_log: true |
| 85 |
delegate_to: localhost |
| 86 |
register: result |
| 87 |
|
| 88 |
- name: Print fetched information about Enclosure found by name |
| 89 |
ansible.builtin.debug: |
| 90 |
msg: "{{ result.enclosures }}" |
| 91 |
|
| 92 |
- name: Gather information about an Enclosure by name with options |
| 93 |
community.general.oneview_enclosure_info: |
| 94 |
name: Test-Enclosure |
| 95 |
options: |
| 96 |
- script # optional |
| 97 |
- environmentalConfiguration # optional |
| 98 |
- utilization # optional |
| 99 |
hostname: 172.16.101.48 |
| 100 |
username: administrator |
| 101 |
password: my_password |
| 102 |
api_version: 500 |
| 103 |
no_log: true |
| 104 |
delegate_to: localhost |
| 105 |
register: result |
| 106 |
|
| 107 |
- name: Print fetched information about Enclosure found by name |
| 108 |
ansible.builtin.debug: |
| 109 |
msg: "{{ result.enclosures }}" |
| 110 |
|
| 111 |
- name: Print fetched information about Enclosure Script |
| 112 |
ansible.builtin.debug: |
| 113 |
msg: "{{ result.enclosure_script }}" |
| 114 |
|
| 115 |
- name: Print fetched information about Enclosure Environmental Configuration |
| 116 |
ansible.builtin.debug: |
| 117 |
msg: "{{ result.enclosure_environmental_configuration }}" |
| 118 |
|
| 119 |
- name: Print fetched information about Enclosure Utilization |
| 120 |
ansible.builtin.debug: |
| 121 |
msg: "{{ result.enclosure_utilization }}" |
| 122 |
|
| 123 |
- name: "Gather information about an Enclosure with temperature data at a resolution of one sample per day, between two |
| 124 |
specified dates" |
| 125 |
community.general.oneview_enclosure_info: |
| 126 |
name: Test-Enclosure |
| 127 |
options: |
| 128 |
- utilization: # optional |
| 129 |
fields: AmbientTemperature |
| 130 |
filter: |
| 131 |
- startDate=2016-07-01T14:29:42.000Z |
| 132 |
- endDate=2017-07-01T03:29:42.000Z |
| 133 |
view: day |
| 134 |
refresh: false |
| 135 |
hostname: 172.16.101.48 |
| 136 |
username: administrator |
| 137 |
password: my_password |
| 138 |
api_version: 500 |
| 139 |
no_log: true |
| 140 |
delegate_to: localhost |
| 141 |
register: result |
| 142 |
|
| 143 |
- name: Print fetched information about Enclosure found by name |
| 144 |
ansible.builtin.debug: |
| 145 |
msg: "{{ result.enclosures }}" |
| 146 |
|
| 147 |
- name: Print fetched information about Enclosure Utilization |
| 148 |
ansible.builtin.debug: |
| 149 |
msg: "{{ result.enclosure_utilization }}" |
| 150 |
""" |
| 151 |
|
| 152 |
RETURN = r""" |
| 153 |
enclosures: |
| 154 |
description: Has all the OneView information about the Enclosures. |
| 155 |
returned: Always, but can be null. |
| 156 |
type: dict |
| 157 |
|
| 158 |
enclosure_script: |
| 159 |
description: Has all the OneView information about the script of an Enclosure. |
| 160 |
returned: When requested, but can be null. |
| 161 |
type: str |
| 162 |
|
| 163 |
enclosure_environmental_configuration: |
| 164 |
description: Has all the OneView information about the environmental configuration of an Enclosure. |
| 165 |
returned: When requested, but can be null. |
| 166 |
type: dict |
| 167 |
|
| 168 |
enclosure_utilization: |
| 169 |
description: Has all the OneView information about the utilization of an Enclosure. |
| 170 |
returned: When requested, but can be null. |
| 171 |
type: dict |
| 172 |
""" |
| 173 |
|
| 174 |
from ansible_collections.community.general.plugins.module_utils._oneview import OneViewModuleBase |
| 175 |
|
| 176 |
|
| 177 |
class EnclosureInfoModule(OneViewModuleBase): |
| 178 |
argument_spec = dict(name=dict(type="str"), options=dict(type="list", elements="raw"), params=dict(type="dict")) |
| 179 |
|
| 180 |
def __init__(self): |
| 181 |
super().__init__( |
| 182 |
additional_arg_spec=self.argument_spec, |
| 183 |
supports_check_mode=True, |
| 184 |
) |
| 185 |
|
| 186 |
def execute_module(self): |
| 187 |
info = {} |
| 188 |
|
| 189 |
if self.module.params["name"]: |
| 190 |
enclosures = self._get_by_name(self.module.params["name"]) |
| 191 |
|
| 192 |
if self.options and enclosures: |
| 193 |
info = self._gather_optional_info(self.options, enclosures[0]) |
| 194 |
else: |
| 195 |
enclosures = self.oneview_client.enclosures.get_all(**self.facts_params) |
| 196 |
|
| 197 |
info["enclosures"] = enclosures |
| 198 |
|
| 199 |
return dict(changed=False, **info) |
| 200 |
|
| 201 |
def _gather_optional_info(self, options, enclosure): |
| 202 |
enclosure_client = self.oneview_client.enclosures |
| 203 |
info = {} |
| 204 |
|
| 205 |
if options.get("script"): |
| 206 |
info["enclosure_script"] = enclosure_client.get_script(enclosure["uri"]) |
| 207 |
if options.get("environmentalConfiguration"): |
| 208 |
env_config = enclosure_client.get_environmental_configuration(enclosure["uri"]) |
| 209 |
info["enclosure_environmental_configuration"] = env_config |
| 210 |
if options.get("utilization"): |
| 211 |
info["enclosure_utilization"] = self._get_utilization(enclosure, options["utilization"]) |
| 212 |
|
| 213 |
return info |
| 214 |
|
| 215 |
def _get_utilization(self, enclosure, params): |
| 216 |
fields = view = refresh = filter = "" |
| 217 |
|
| 218 |
if isinstance(params, dict): |
| 219 |
fields = params.get("fields") |
| 220 |
view = params.get("view") |
| 221 |
refresh = params.get("refresh") |
| 222 |
filter = params.get("filter") |
| 223 |
|
| 224 |
return self.oneview_client.enclosures.get_utilization( |
| 225 |
enclosure["uri"], fields=fields, filter=filter, refresh=refresh, view=view |
| 226 |
) |
| 227 |
|
| 228 |
def _get_by_name(self, name): |
| 229 |
return self.oneview_client.enclosures.get_by("name", name) |
| 230 |
|
| 231 |
|
| 232 |
def main(): |
| 233 |
EnclosureInfoModule().run() |
| 234 |
|
| 235 |
|
| 236 |
if __name__ == "__main__": |
| 237 |
main() |