| 1 |
# Copyright (c) 2021-2022 Hewlett Packard Enterprise, Inc. All rights reserved. |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 6 |
# Do not use this from other collections or standalone plugins/modules! |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import time |
| 11 |
import typing as t |
| 12 |
|
| 13 |
from ansible_collections.community.general.plugins.module_utils._redfish_utils import RedfishUtils |
| 14 |
|
| 15 |
|
| 16 |
class iLORedfishUtils(RedfishUtils): |
| 17 |
def get_ilo_sessions(self) -> dict[str, t.Any]: |
| 18 |
result: dict[str, t.Any] = {} |
| 19 |
# listing all users has always been slower than other operations, why? |
| 20 |
session_list = [] |
| 21 |
sessions_results = [] |
| 22 |
# Get these entries, but does not fail if not found |
| 23 |
properties = ["Description", "Id", "Name", "UserName"] |
| 24 |
|
| 25 |
# Changed self.sessions_uri to Hardcoded string. |
| 26 |
response = self.get_request(f"{self.root_uri}{self.service_root}SessionService/Sessions/") |
| 27 |
if not response["ret"]: |
| 28 |
return response |
| 29 |
result["ret"] = True |
| 30 |
data = response["data"] |
| 31 |
|
| 32 |
current_session = None |
| 33 |
if "Oem" in data: |
| 34 |
if data["Oem"]["Hpe"]["Links"]["MySession"]["@odata.id"]: |
| 35 |
current_session = data["Oem"]["Hpe"]["Links"]["MySession"]["@odata.id"] |
| 36 |
|
| 37 |
for sessions in data["Members"]: |
| 38 |
# session_list[] are URIs |
| 39 |
session_list.append(sessions["@odata.id"]) |
| 40 |
# for each session, get details |
| 41 |
for uri in session_list: |
| 42 |
session = {} |
| 43 |
if uri != current_session: |
| 44 |
response = self.get_request(self.root_uri + uri) |
| 45 |
if not response["ret"]: |
| 46 |
return response |
| 47 |
data = response["data"] |
| 48 |
for property in properties: |
| 49 |
if property in data: |
| 50 |
session[property] = data[property] |
| 51 |
sessions_results.append(session) |
| 52 |
result["msg"] = sessions_results |
| 53 |
result["ret"] = True |
| 54 |
return result |
| 55 |
|
| 56 |
def set_ntp_server(self, mgr_attributes) -> dict[str, t.Any]: |
| 57 |
result: dict[str, t.Any] = {} |
| 58 |
setkey = mgr_attributes["mgr_attr_name"] |
| 59 |
|
| 60 |
nic_info = self.get_manager_ethernet_uri() |
| 61 |
ethuri = nic_info["nic_addr"] |
| 62 |
|
| 63 |
response = self.get_request(self.root_uri + ethuri) |
| 64 |
if not response["ret"]: |
| 65 |
return response |
| 66 |
result["ret"] = True |
| 67 |
data = response["data"] |
| 68 |
payload: dict[str, t.Any] = {"DHCPv4": {"UseNTPServers": ""}} |
| 69 |
|
| 70 |
if data["DHCPv4"]["UseNTPServers"]: |
| 71 |
payload["DHCPv4"]["UseNTPServers"] = False |
| 72 |
res_dhv4 = self.patch_request(self.root_uri + ethuri, payload) |
| 73 |
if not res_dhv4["ret"]: |
| 74 |
return res_dhv4 |
| 75 |
|
| 76 |
payload = {"DHCPv6": {"UseNTPServers": ""}} |
| 77 |
|
| 78 |
if data["DHCPv6"]["UseNTPServers"]: |
| 79 |
payload["DHCPv6"]["UseNTPServers"] = False |
| 80 |
res_dhv6 = self.patch_request(self.root_uri + ethuri, payload) |
| 81 |
if not res_dhv6["ret"]: |
| 82 |
return res_dhv6 |
| 83 |
|
| 84 |
datetime_uri = f"{self.manager_uri}DateTime" |
| 85 |
|
| 86 |
listofips = mgr_attributes["mgr_attr_value"].split(" ") |
| 87 |
if len(listofips) > 2: |
| 88 |
return {"ret": False, "changed": False, "msg": "More than 2 NTP Servers mentioned"} |
| 89 |
|
| 90 |
ntp_list = [] |
| 91 |
for ips in listofips: |
| 92 |
ntp_list.append(ips) |
| 93 |
|
| 94 |
while len(ntp_list) < 2: |
| 95 |
ntp_list.append("0.0.0.0") |
| 96 |
|
| 97 |
payload = {setkey: ntp_list} |
| 98 |
|
| 99 |
response1 = self.patch_request(self.root_uri + datetime_uri, payload) |
| 100 |
if not response1["ret"]: |
| 101 |
return response1 |
| 102 |
|
| 103 |
return {"ret": True, "changed": True, "msg": f"Modified {mgr_attributes['mgr_attr_name']}"} |
| 104 |
|
| 105 |
def set_time_zone(self, attr) -> dict[str, t.Any]: |
| 106 |
key = attr["mgr_attr_name"] |
| 107 |
|
| 108 |
uri = f"{self.manager_uri}DateTime/" |
| 109 |
response = self.get_request(self.root_uri + uri) |
| 110 |
if not response["ret"]: |
| 111 |
return response |
| 112 |
|
| 113 |
data = response["data"] |
| 114 |
|
| 115 |
if key not in data: |
| 116 |
return {"ret": False, "changed": False, "msg": f"Key {key} not found"} |
| 117 |
|
| 118 |
timezones = data["TimeZoneList"] |
| 119 |
index = "" |
| 120 |
for tz in timezones: |
| 121 |
if attr["mgr_attr_value"] in tz["Name"]: |
| 122 |
index = tz["Index"] |
| 123 |
break |
| 124 |
|
| 125 |
payload = {key: {"Index": index}} |
| 126 |
response = self.patch_request(self.root_uri + uri, payload) |
| 127 |
if not response["ret"]: |
| 128 |
return response |
| 129 |
|
| 130 |
return {"ret": True, "changed": True, "msg": f"Modified {attr['mgr_attr_name']}"} |
| 131 |
|
| 132 |
def set_dns_server(self, attr) -> dict[str, t.Any]: |
| 133 |
key = attr["mgr_attr_name"] |
| 134 |
nic_info = self.get_manager_ethernet_uri() |
| 135 |
uri = nic_info["nic_addr"] |
| 136 |
|
| 137 |
listofips = attr["mgr_attr_value"].split(" ") |
| 138 |
if len(listofips) > 3: |
| 139 |
return {"ret": False, "changed": False, "msg": "More than 3 DNS Servers mentioned"} |
| 140 |
|
| 141 |
dns_list = [] |
| 142 |
for ips in listofips: |
| 143 |
dns_list.append(ips) |
| 144 |
|
| 145 |
while len(dns_list) < 3: |
| 146 |
dns_list.append("0.0.0.0") |
| 147 |
|
| 148 |
payload = {"Oem": {"Hpe": {"IPv4": {key: dns_list}}}} |
| 149 |
|
| 150 |
response = self.patch_request(self.root_uri + uri, payload) |
| 151 |
if not response["ret"]: |
| 152 |
return response |
| 153 |
|
| 154 |
return {"ret": True, "changed": True, "msg": f"Modified {attr['mgr_attr_name']}"} |
| 155 |
|
| 156 |
def set_domain_name(self, attr) -> dict[str, t.Any]: |
| 157 |
key = attr["mgr_attr_name"] |
| 158 |
|
| 159 |
nic_info = self.get_manager_ethernet_uri() |
| 160 |
ethuri = nic_info["nic_addr"] |
| 161 |
|
| 162 |
response = self.get_request(self.root_uri + ethuri) |
| 163 |
if not response["ret"]: |
| 164 |
return response |
| 165 |
|
| 166 |
data = response["data"] |
| 167 |
|
| 168 |
payload: dict[str, t.Any] = {"DHCPv4": {"UseDomainName": ""}} |
| 169 |
|
| 170 |
if data["DHCPv4"]["UseDomainName"]: |
| 171 |
payload["DHCPv4"]["UseDomainName"] = False |
| 172 |
res_dhv4 = self.patch_request(self.root_uri + ethuri, payload) |
| 173 |
if not res_dhv4["ret"]: |
| 174 |
return res_dhv4 |
| 175 |
|
| 176 |
payload = {"DHCPv6": {"UseDomainName": ""}} |
| 177 |
|
| 178 |
if data["DHCPv6"]["UseDomainName"]: |
| 179 |
payload["DHCPv6"]["UseDomainName"] = False |
| 180 |
res_dhv6 = self.patch_request(self.root_uri + ethuri, payload) |
| 181 |
if not res_dhv6["ret"]: |
| 182 |
return res_dhv6 |
| 183 |
|
| 184 |
domain_name = attr["mgr_attr_value"] |
| 185 |
|
| 186 |
payload = {"Oem": {"Hpe": {key: domain_name}}} |
| 187 |
|
| 188 |
response = self.patch_request(self.root_uri + ethuri, payload) |
| 189 |
if not response["ret"]: |
| 190 |
return response |
| 191 |
return {"ret": True, "changed": True, "msg": f"Modified {attr['mgr_attr_name']}"} |
| 192 |
|
| 193 |
def set_wins_registration(self, mgrattr) -> dict[str, t.Any]: |
| 194 |
Key = mgrattr["mgr_attr_name"] |
| 195 |
|
| 196 |
nic_info = self.get_manager_ethernet_uri() |
| 197 |
ethuri = nic_info["nic_addr"] |
| 198 |
|
| 199 |
payload = {"Oem": {"Hpe": {"IPv4": {Key: False}}}} |
| 200 |
|
| 201 |
response = self.patch_request(self.root_uri + ethuri, payload) |
| 202 |
if not response["ret"]: |
| 203 |
return response |
| 204 |
return {"ret": True, "changed": True, "msg": f"Modified {mgrattr['mgr_attr_name']}"} |
| 205 |
|
| 206 |
def get_server_poststate(self) -> dict[str, t.Any]: |
| 207 |
# Get server details |
| 208 |
response = self.get_request(self.root_uri + self.systems_uri) |
| 209 |
if not response["ret"]: |
| 210 |
return response |
| 211 |
server_data = response["data"] |
| 212 |
|
| 213 |
if "Hpe" in server_data["Oem"]: |
| 214 |
return {"ret": True, "server_poststate": server_data["Oem"]["Hpe"]["PostState"]} |
| 215 |
else: |
| 216 |
return {"ret": True, "server_poststate": server_data["Oem"]["Hp"]["PostState"]} |
| 217 |
|
| 218 |
def wait_for_ilo_reboot_completion(self, polling_interval=60, max_polling_time=1800) -> dict[str, t.Any]: |
| 219 |
# This method checks if OOB controller reboot is completed |
| 220 |
time.sleep(10) |
| 221 |
|
| 222 |
# Check server poststate |
| 223 |
state = self.get_server_poststate() |
| 224 |
if not state["ret"]: |
| 225 |
return state |
| 226 |
|
| 227 |
count = int(max_polling_time / polling_interval) |
| 228 |
times = 0 |
| 229 |
|
| 230 |
# When server is powered OFF |
| 231 |
pcount = 0 |
| 232 |
while state["server_poststate"] in ["PowerOff", "Off"] and pcount < 5: |
| 233 |
time.sleep(10) |
| 234 |
state = self.get_server_poststate() |
| 235 |
if not state["ret"]: |
| 236 |
return state |
| 237 |
|
| 238 |
if state["server_poststate"] not in ["PowerOff", "Off"]: |
| 239 |
break |
| 240 |
pcount = pcount + 1 |
| 241 |
if state["server_poststate"] in ["PowerOff", "Off"]: |
| 242 |
return {"ret": False, "changed": False, "msg": "Server is powered OFF"} |
| 243 |
|
| 244 |
# When server is not rebooting |
| 245 |
if state["server_poststate"] in ["InPostDiscoveryComplete", "FinishedPost"]: |
| 246 |
return {"ret": True, "changed": False, "msg": "Server is not rebooting"} |
| 247 |
|
| 248 |
while state["server_poststate"] not in ["InPostDiscoveryComplete", "FinishedPost"] and count > times: |
| 249 |
state = self.get_server_poststate() |
| 250 |
if not state["ret"]: |
| 251 |
return state |
| 252 |
|
| 253 |
if state["server_poststate"] in ["InPostDiscoveryComplete", "FinishedPost"]: |
| 254 |
return {"ret": True, "changed": True, "msg": "Server reboot is completed"} |
| 255 |
time.sleep(polling_interval) |
| 256 |
times = times + 1 |
| 257 |
|
| 258 |
return {"ret": False, "changed": False, "msg": f"Server Reboot has failed, server state: {state} "} |