1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
"""
SoftLayer.CLI.custom_types
~~~~~~~~~~~~~~~~~~~~~~~~
Custom type declarations extending click.ParamType
:license: MIT, see LICENSE for more details.
"""
import click
# pylint: disable=inconsistent-return-statements
class NetworkParamType(click.ParamType):
"""Validates a network parameter type and converts to a tuple.
todo: Implement to ipaddress.ip_network once the ipaddress backport
module can be added as a dependency or is available on all
supported python versions.
"""
name = 'network'
def convert(self, value, param, ctx):
try:
# Inlined from python standard ipaddress module
# https://docs.python.org/3/library/ipaddress.html
address = str(value).split('/')
if len(address) != 2:
raise ValueError("Only one '/' permitted in %r" % value)
ip_address, cidr = address
return (ip_address, int(cidr))
except ValueError:
self.fail('{} is not a valid network'.format(value), param, ctx)
|