From a71076c0bea5cbe33f6c283f3b89147597aec2a5 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Wed, 10 Apr 2019 17:12:00 -0400 Subject: [PATCH] Implement redfish set_net_configuration This allows using redfish to set network parameters. Change-Id: Icab70a377194ccc2a5e2058692e6b4a37a9057b3 --- pyghmi/redfish/command.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/pyghmi/redfish/command.py b/pyghmi/redfish/command.py index a60607d3..84eee569 100644 --- a/pyghmi/redfish/command.py +++ b/pyghmi/redfish/command.py @@ -116,6 +116,11 @@ def _mask_to_cidr(mask): maskn >>= 1 return cidr +def _cidr_to_mask(cidr): + return socket.inet_ntop( + socket.AF_INET, struct.pack( + '!I', (2**32-1) ^ (2**(32-cidr)-1))) + class SensorReading(object): def __init__(self, healthinfo): @@ -468,8 +473,32 @@ class Command(object): redfishsettings = {'Attributes': changeset} self._do_web_request(self._setbiosurl, redfishsettings, 'PATCH') + def set_net_configuration(self, ipv4_address=None, ipv4_configuration=None, + ipv4_gateway=None): + patch = {} + ipinfo = {} + netmask = None + if ipv4_address: + if '/' in ipv4_address: + ipv4_address, cidr = ipv4_address.split('/') + netmask = _cidr_to_mask(int(cidr)) + patch['IPv4StaticAddresses'] = [ipinfo] + ipinfo['Address'] = ipv4_address + if netmask: + ipinfo['SubnetMask'] = netmask + if ipv4_gateway: + patch['IPv4StaticAddresses'] = [ipinfo] + ipinfo['Gateway'] = ipv4_gateway + if ipv4_configuration.lower() == 'dhcp': + patch['DHCPv4'] = {'DHCPEnabled': True} + elif (ipv4_configuration == 'static' or + 'IPv4StaticAddresses' in patch): + patch['DHCPv4'] = {'DHCPEnabled': False} + if patch: + self._do_web_request(self._bmcnicurl, patch, 'PATCH') + def get_net_configuration(self): - netcfg = self._do_web_request(self._bmcnicurl) + netcfg = self._do_web_request(self._bmcnicurl, cache=False) ipv4 = netcfg.get('IPv4Addresses', {}) if not ipv4: raise exc.PyghmiException('Unable to locate network information')