Skip to content

Commit c308c38

Browse files
authored
AP-398 Added always_send field into DHCPOption (#52)
* Added always_send field into DHCPOption * lint with black fix * Changed the way the object is updated. * Removed bool cast from DHCPOptionValue constructor
1 parent d8defba commit c308c38

1 file changed

Lines changed: 38 additions & 4 deletions

File tree

ns1/ipam.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,8 +1032,11 @@ def __init__(self, address_family, options, server_options=None):
10321032
Create the DHCP options class that can be used by the IPAM API
10331033
10341034
:param str address_family: This is either dhcpv4 or dhcpv6
1035-
:param dict options: This is a dict representing the options set. Valid options are listed in :attr:`ns1.ipam.DHCPOptions.OPTIONS`
1035+
:param list options: This is a list of :class:`ns1.ipam.DHCPOptionsValue` objects representing the DHCP options
10361036
"""
1037+
self.option_list = {}
1038+
self.address_family = ""
1039+
self.options = []
10371040

10381041
if server_options is None:
10391042
self.server_options = {}
@@ -1050,12 +1053,43 @@ def update(self, address_family, options, server_options=None):
10501053
self.server_options = server_options
10511054

10521055
self.address_family = address_family
1053-
self.__dict__.update(options)
1056+
self.options = options
10541057
self.__dict__.update(server_options)
1058+
10551059
self.option_list = {
10561060
"options": [
1057-
{"name": "%s/%s" % (self.address_family, key), "value": value}
1058-
for key, value in options.items()
1061+
option.generate_option(address_family)
1062+
for option in self.options
10591063
]
10601064
}
1065+
10611066
self.option_list.update(self.server_options)
1067+
1068+
1069+
class DHCPOptionValue:
1070+
def __init__(self, key, value, always_send=None):
1071+
"""
1072+
Create the DHCPOptionValue class that can be used as value with :class:`ns1.ipam.DHCPOptions`
1073+
1074+
:param key str option name
1075+
:param value any option value
1076+
:param always_send bool indicates whether this option be sent back in lease or not
1077+
"""
1078+
self.key = key
1079+
self.value = value
1080+
self.always_send = always_send
1081+
1082+
def generate_option(self, address_family):
1083+
"""
1084+
Generates dhcp option value with a proper format
1085+
1086+
:param address_family str one of dhcpv4 or dhcpv6 family name
1087+
"""
1088+
option = {
1089+
"name": "%s/%s" % (address_family, self.key),
1090+
"value": self.value,
1091+
}
1092+
if self.always_send is not None:
1093+
option["always_send"] = self.always_send
1094+
1095+
return option

0 commit comments

Comments
 (0)