def add_cname_record(db, cname_record_name, target_name, fail_on_exists=True): """ Creates a CNAME-record. If fail_on_exists=False, it will simply return without doing anything, if the CNAME-record already exists. This is due to the method being used by OU._setup_project_hosts, which can be run several times for the same project when it is reconfigured. :param db: A Cerebrum-database instance :type db: Cerebrum.Database :param cname_record_name: FQDN of the CNAME-record :type cname_record_name: str :param target_name: FQDN of the target domain :type target_name: str :param fail_on_exists: True or False :type fail_on_exists: bool """ cname = CNameRecord(db) dns_owner = DnsOwner(db) constants = Factory.get('Constants') try: dns_owner.find_by_name(target_name) proxy_dns_owner_ref = dns_owner.entity_id except Errors.NotFoundError: raise Errors.NotFoundError('%s does not exist.' % target_name) dns_owner.clear() try: dns_owner.find_by_name(cname_record_name) except Errors.NotFoundError: dns_owner.populate(constants.DnsZone(cereconf.DNS_DEFAULT_ZONE), cname_record_name) dns_owner.write_db() cname_dns_owner_ref = dns_owner.entity_id try: cname.find_by_cname_owner_id(cname_dns_owner_ref) if fail_on_exists: raise Errors.RealityError('CNAME %s already exists.' % cname_record_name) except Errors.NotFoundError: cname.populate(cname_dns_owner_ref, proxy_dns_owner_ref) cname.write_db()
def _create_guest_account(self, responsible_id, end_date, fname, lname, mobile, guest_group): """ Helper method for creating a guest account. Note that this method does not validate any input, that must already have been done before calling this method... @rtype: Account @return: The created guest account """ owner_group = self._get_owner_group() # Get all settings for the given guest type: settings = guestconfig.GUEST_TYPES[guest_group.group_name] ac = self.Account_class(self.db) name = ac.suggest_unames(self.const.account_namespace, fname, lname, maxlen=guestconfig.GUEST_MAX_LENGTH_USERNAME, prefix=settings['prefix'], suffix='')[0] if settings['prefix'] and not name.startswith(settings['prefix']): # TODO/FIXME: Seems suggest_unames ditches the prefix setting if # there's not a lot of good usernames left with the given # constraints. # We could either fix suggest_uname (but that could lead to # complications with the imports), or we could try to mangle the # name and come up with new suggestions. raise Errors.RealityError("No potential usernames available") # TODO: make use of ac.create() instead, when it has been defined # properly. ac.populate(name=name, owner_type=self.const.entity_group, owner_id=owner_group.entity_id, np_type=self.const.account_guest, creator_id=responsible_id, expire_date=None) ac.write_db() # Tag the account as a guest account: ac.populate_trait(code=self.const.trait_guest_owner, target_id=responsible_id) # Save the guest's name: ac.populate_trait(code=self.const.trait_guest_name, strval='%s %s' % (fname, lname)) # Set the quarantine: ac.add_entity_quarantine( qtype=self.const.quarantine_guest_old, creator=responsible_id, # TBD: or should creator be bootstrap_account? description='Guest account auto-expire', start=end_date) # Add spreads for spr in settings.get('spreads', ()): try: spr = int(self.const.Spread(spr)) except Errors.NotFoundError: self.logger.warn('Unknown guest spread: %s' % spr) continue ac.add_spread(spr) # Add guest account to correct group guest_group.add_member(ac.entity_id) # Save the phone number if mobile: ac.add_contact_info(source=self.const.system_manual, type=self.const.contact_mobile_phone, value=mobile) ac.write_db() return ac