def validate_arguments(arguments): """ Validate argument values: <IP> Arguments not validated: <CONTAINER> <INTERFACE> :param arguments: Docopt processed arguments """ # Validate IP requested_ip = arguments.get("<IP>") if not (requested_ip is None or validate_ip(requested_ip, 4) or validate_ip(requested_ip, 6) or validate_cidr(requested_ip) or requested_ip.lower() in ('ipv4', 'ipv6')): print_paragraph("Invalid IP address specified. Argument must be a " "valid IP or CIDR.") sys.exit(1) # Validate POOL if requested_ip is not None and '/' in requested_ip: requested_pool = IPNetwork(requested_ip) try: client.get_ip_pool_config(requested_pool.version, requested_pool) except KeyError: print_paragraph("Invalid CIDR specified for desired pool. " "No pool found for {0}.".format(requested_pool)) sys.exit(1) # Validate PROFILE endpoint.validate_arguments(arguments)
def get_pool_by_cidr_or_exit(cidr): """ Locate the IP Pool from the specified CIDR, and exit if not found. :param cidr: The pool CIDR. :return: The IPPool. """ try: pool = client.get_ip_pool_config(cidr.version, cidr) except KeyError: print "Pool %s is not found" % cidr sys.exit(1) else: return pool
def ip_pool_remove(cidrs, version): """ Remove the given CIDRs from the IP address allocation pool. :param cidrs: The pools to remove in CIDR format, e.g. 192.168.0.0/16 :param version: 4 or 6 :return: None """ for cidr in cidrs: # Get the existing IP Pool so that we can disable it, try: pool = client.get_ip_pool_config(version, IPNetwork(cidr)) except KeyError: print "%s is not a configured pool." % cidr sys.exit(1) try: # Disable the pool to prevent any further allocation blocks from # being assigned from the pool. Existing allocation blocks will # still exist and may be allocated from until affinity is removed # from the blocks. print "Disabling IP Pool" pool.disabled = True client.set_ip_pool_config(version, pool) # Remove affinity from the allocation blocks for the pool. This # will prevent these blocks from being used for auto-allocations. # We pause before removing the affinities and the pool to allow # any in-progress IP allocations to complete - there is a known # timing window here, which can be closed but at the expense of # additional etcd reads for each affine block allocation - since # deletion of a pool is not common, it is simplest to pause in # between disabling and deleting the pool. print "Removing IPAM configuration for pool" time.sleep(3) client.release_pool_affinities(pool) client.remove_ip_pool(version, pool.cidr) print "Deleted IP Pool" except (KeyError, HostAffinityClaimedError): print_paragraph("Conflicting modifications have been made to the " "IPAM configuration for this pool. Please retry " "the command.") sys.exit(1)
def validate_arguments(arguments): """ Validate argument values: <IP> Arguments not validated: <CONTAINER> <INTERFACE> :param arguments: Docopt processed arguments """ # Validate IP requested_ip = arguments.get("<IP>") if not (requested_ip is None or validate_ip(requested_ip, 4) or validate_ip(requested_ip, 6) or validate_cidr(requested_ip) or requested_ip.lower() in ('ipv4', 'ipv6')): sys.exit('Invalid IP address specified. Argument must be an IP, ' 'a cidr, "ipv4" or "ipv6". ' '"{0}" was given'.format(requested_ip)) # Validate POOL if requested_ip is not None and '/' in requested_ip: try: requested_pool = IPNetwork(requested_ip) except TypeError: sys.exit('Invalid cidr specified for desired pool. ' '"{0}" was given."'.format(pool)) try: pool = client.get_ip_pool_config(requested_pool.version, requested_pool) except KeyError: sys.exit('Invalid cidr specified for desired pool. ' 'No pool found for "{0}"'.format(requested_pool)) # Validate PROFILE endpoint.validate_arguments(arguments)