def update(context, request): params = dict() domain = request.matchdict['domain'] Alias.get(request.db_session, domain) try: if "destination" in request.params: params['instance_id'] = Instance.get_by_domain( request.db_session, request.params['destination']).id if "new_domain" in request.params: check_domain_not_used(request, request.params['new_domain']) params['new_domain'] = validate_hostname( request.params['new_domain']) except NoResultFound: raise ParamsError("No instance for domain {}"\ .format(request.params['destination'])) if not params: raise ParamsError("Missing update fields") params['domain'] = domain return request.submit_task('alias.update', **params)
def test_validate_hostname(self): long_string = 'a' * 512 for hname in ('www.example.com.', 'invalid', 'www.a&b.com', 'www.a/b.com', long_string): self.assertRaises(ValidationError, validators.validate_hostname, hname) for hname in ('www.example.com', 'example.com', 'sub.sub.example.com', 'www.42.com', '4r.aybu.it'): self.assertEqual(hname, validators.validate_hostname(hname))
def create(context, request): try: source = validate_hostname(request.params['source']) instance = Instance.get_by_domain(request.db_session, request.params['destination']) http_code = request.params.get('http_code', 301) target_path = request.params.get('target_path', '') check_domain_not_used(request, source) except KeyError as e: raise ParamsError(e) except NoResultFound: raise ParamsError('No instance for domain {}'\ .format(request.params['destination'])) else: params = dict(source=source, instance_id=instance.id, http_code=http_code, target_path=target_path) return request.submit_task('redirect.create', **params)
def update(context, request): params = dict() source = request.matchdict['source'] Redirect.get(request.db_session, source) specs = ( ('new_source', check_domain_not_used, [request]), ('destination', Instance.get_by_domain, [request.db_session]), ('http_code', None, None), ('target_path', None, None) ) try: for attr, validate_fun, fun_args in specs: if attr in request.params: if validate_fun: fun_args.append(request.params[attr]) params[attr] = validate_fun(*fun_args) else: params[attr] = request.params[attr] except NoResultFound: raise ParamsError("No instance for domain {}"\ .format(request.params['destination'])) if not params: raise ParamsError("Missing update fields") params['source'] = source if "destination" in params: params['instance_id'] = params['destination'].id del params['destination'] if "new_source" in params: params['new_source'] = validate_hostname(params['new_source']) return request.submit_task('redirect.update', **params)
def deploy(context, request): try: params = dict( domain=request.params['domain'], owner_email=request.params['owner_email'], environment_name=request.params['environment_name'], technical_contact_email=request.params.get( 'technical_contact_email', request.params['owner_email']), theme_name=request.params.get('theme_name'), default_language=request.params.get('default_language', u'it'), database_password=request.params.get('database_password'), enabled=True if request.params.get('enabled') else False, verbose=True if request.params.get('verbose') else False, ) check_domain_not_used(request, params['domain']) params['domain'] = validate_hostname(params['domain']) # try to get the instance, as it MUST not exists Instance.get_by_domain(request.db_session, params['domain']) except KeyError as e: log.exception("Error validating params") raise ParamsError(e) except NoResultFound: # no instance found, validate relations. try: field = 'owner_email' cls = 'user' User.log.debug("Validating owner %s", params[field]) owner = User.get(request.db_session, params[field]) if not owner: raise NoResultFound() if params['technical_contact_email'] != params['owner_email']: field = 'technical_contact_email' User.log.debug("Validating contact %s", params[field]) ctc = User.get(request.db_session, params[field]) if not ctc: raise NoResultFound() field = 'environment_name' cls = 'environment' Environment.log.debug("validating environment %s", params[field]) env = Environment.get(request.db_session, params[field]) if not env: raise NoResultFound() field = 'theme_name' cls = 'theme' if params[field]: Theme.log.debug("Validating theme %s", params[field]) theme = Theme.get(request.db_session, params[field]) if not theme: raise NoResultFound() except NoResultFound: raise ParamsError('{} "{}" for {} not found'\ .format(cls.title(), params[field], field)) else: log.info("Submitting task") # relations exists, submit tasks return request.submit_task('instance.deploy', **params) else: # found instance, conflict error = 'instance for domain {} already exists'\ .format(params['domain']) raise HTTPConflict(headers={'X-Request-Error': error})