Example #1
0
    def add(self, ip_address, canonical_hostname, *args):
        """Add new hostname to the container.

        Each host is composed by:

            * IP address
            * Main hostname
            * Aliases to the main hostname

        At this moment multiple aliases are accepted and inserted but
        only one is used and saved back.

        :Results: None
        """
        if not utils.is_valid_ip(ip_address):
            raise InvalidIPException("%s is not a valid IP address"
                                     % ip_address)
        if not utils.is_valid_hostname(canonical_hostname):
            raise InvalidHostnameException("%s is not a valid hostname" % canonical_hostname)
        self.__hosts.append({
            "ip_address": ip_address,
            "canonical_hostname": canonical_hostname,
            "aliases": args
        })
        self.__save(self.hosts_file)
Example #2
0
    def delete(self, canonical_hostname):
        """Delete existing hostname.

        A valid hostname must be passed, no IP or alias are valid arguments.

        :Results: None
        """
        if not utils.is_valid_hostname(canonical_hostname):
            raise InvalidHostnameException("%s is not a valid hostname" % canonical_hostname)
        self.__hosts = [
            {
                "ip_address": host["ip_address"],
                "canonical_hostname": host["canonical_hostname"],
                "aliases": host["aliases"]
            }
            for host in self.__hosts if host["canonical_hostname"] != canonical_hostname
        ]
        self.__save(self.hosts_file)