def populate(self, subnet, description, vlan=None): """Populate subnet instance's attributes. @type subnet: string @param subnet: A string that represents the subnet. Example: 10.0.0.0/16. The last '.0' is not required, e.g. 10.0.0/16. @type description: string @param description: Something that describes the given subnet. It is free text, but local policies might use this in a parsable format to describe the usage of the given subnet. @type vlan: int @param vlan: A number that represents what VLAN the subnet corresponds to. """ Entity.populate(self, self.const.entity_dns_subnet) # If __in_db is present, it must be True; calling populate on # an object where __in_db is present and False is very likely # a programming error. # # If __in_db in not present, we'll set it to False. try: if not self.__in_db: raise RuntimeError("populate() called multiple times.") except AttributeError: self.__in_db = False self.subnet_ip, subnet_mask = subnet.split('/') self.subnet_mask = int(subnet_mask) if len(self.subnet_ip.split('.')) == 3: self.subnet_ip = self.subnet_ip + ".0" (self.ip_min, self.ip_max) = IPCalc().ip_range_by_netmask(self.subnet_ip, self.subnet_mask) self.description = description self.vlan = vlan self.name_prefix = '' max_res = max(cereconf.DEFAULT_RESERVED_BY_NET_SIZE.values()) self.no_of_reserved_adr = cereconf.DEFAULT_RESERVED_BY_NET_SIZE.get( self.subnet_mask, max_res) self.calculate_reserved_addresses()
"""Usage: generate_subnet_ldif.py [--logger-name=console ...] Write IP subnet information from Cerebrum to an LDIF file, which can then be loaded into LDAP. See Cerebrum/default_config.py:LDAP_SUBNETS for configuration. """ import cerebrum_path import cereconf from Cerebrum.Utils import Factory from Cerebrum.modules.LDIFutils import ldapconf, iso2utf, \ ldif_outfile, end_ldif_outfile, entry_string, container_entry_string from Cerebrum.modules.dns.Subnet import Subnet from Cerebrum.modules.dns.IPUtils import IPCalc netmask_to_ip = IPCalc().netmask_to_ip def write_subnet_ldif(): DN = ldapconf('SUBNETS', 'dn') startAttr, endAttr, objectClasses = ldapconf('SUBNETS', 'rangeSchema') objectClasses = ('top', 'ipNetwork') + tuple(objectClasses) db = Factory.get('Database')() f = ldif_outfile('SUBNETS') f.write(container_entry_string('SUBNETS')) for row in Subnet(db).search(): cn = "%s/%s" % (row['subnet_ip'], row['subnet_mask']) desc = row['description'] f.write( entry_string( "cn=%s,%s" % (cn, DN), {