def new_loan_request(body): "User Loan Requesting, returns the id of the loan" if not body.ldap_email: raise BadRequest("Missing LDAP E-Mail") if not p.slaveloan.admin.can(): if not body.ldap_email == current_user.authenticated_email: raise BadRequest("You can't request loans on behalf of others.") if not body.requested_slavetype: raise BadRequest("Missing slavetype") slavetype = slave_to_slavetype(body.requested_slavetype) if not slavetype: raise BadRequest("Unsupported slavetype") if not body.bugzilla_email: # Set bugzilla e-mail to ldap e-mail by default body.bugzilla_email = body.ldap_email session = g.db.session('relengapi') try: h = Humans.as_unique(session, ldap=body.ldap_email, bugzilla=body.bugzilla_email) if h.bugzilla != body.bugzilla_email: h.bugzilla = body.bugzilla_email except sa.exc.IntegrityError: raise InternalServerError("Integrity Error from Database, please retry.") if body.loan_bug_id: l = Loans(status="PENDING", human=h, bug_id=body.loan_bug_id) else: l = Loans(status="PENDING", human=h) hist_line = "%s issued a loan request for slavetype %s (original: '%s')" % \ (current_user.authenticated_email, slavetype, body.requested_slavetype) if body.ldap_email != current_user.authenticated_email: hist_line += "on behalf of %s" % body.ldap_email history = History(for_loan=l, timestamp=tz.utcnow(), msg=hist_line) session.add(l) session.add(history) session.commit() logger.info(hist_line) chain_of_stuff = task_groups.generate_loan(loanid=l.id, slavetype=slavetype) chain_of_stuff.delay() return l.to_wsme()
def new_loan_request(body): "User Loan Requesting, returns the id of the loan" if not body.ldap_email: raise BadRequest("Missing LDAP E-Mail") if not p.slaveloan.admin.can(): if not body.ldap_email == current_user.authenticated_email: raise BadRequest("You can't request loans on behalf of others.") if body.status: if not p.slaveloan.admin.can(): raise Forbidden("Permission denied to set loan status manually") if body.status not in ["PENDING", "COMPLETE", "ACTIVE"]: raise BadRequest("Loan status (%s) is unsupported at this time" % body.status) if body.status and body.status != 'PENDING': if not body.fqdn: raise BadRequest("Must set Machine FQDN") if not body.ipaddress: raise BadRequest("Must set Machine IP Address") else: if body.fqdn or body.ipaddress: if p.slaveloan.admin.can(): msg = ("Unable to explicitly set fqdn or ipaddress when not" "also explicitly setting status") if body.status == "PENDING": msg += " (to something other than PENDING)" raise BadRequest(msg) else: raise Forbidden( "Permission denied to set fqdn and ipaddress manually") if not body.requested_slavetype: if not body.fqdn and not body.ipaddress: raise BadRequest("Missing slavetype") else: if body.fqdn or body.ipaddress: raise BadRequest( "Unable to request a host if you're passing in the specifics") slavetype = slave_to_slavetype(body.requested_slavetype) if not slavetype: raise BadRequest("Unsupported slavetype") if not body.bugzilla_email: # Set bugzilla e-mail to ldap e-mail by default body.bugzilla_email = body.ldap_email session = g.db.session('relengapi') try: h = Humans.as_unique(session, ldap=body.ldap_email, bugzilla=body.bugzilla_email) if h.bugzilla != body.bugzilla_email: h.bugzilla = body.bugzilla_email except sa.exc.IntegrityError: raise InternalServerError( "Integrity Error from Database, please retry.") m = None if body.fqdn and body.ipaddress: try: m = Machines.as_unique(session, fqdn=body.fqdn, ipaddress=body.ipaddress) except sa.exc.IntegrityError: raise InternalServerError( "Integrity Error from Database, please retry.") loan_data = dict(human=h) if body.loan_bug_id: loan_data.update(dict(bug_id=body.loan_bug_id)) if m: loan_data.update(dict(machine=m)) if body.status: loan_data.update(dict(status=body.status)) else: loan_data.update(dict(status="PENDING")) l = Loans(**loan_data) if m: hist_line = "%s logged a %s loan on host: %s (ip: %s)" % \ (current_user.authenticated_email, body.status, body.fqdn, body.ipaddress) else: hist_line = "%s issued a loan request for slavetype %s (original: '%s')" % \ (current_user.authenticated_email, slavetype, body.requested_slavetype) if body.ldap_email != current_user.authenticated_email: hist_line += " on behalf of %s" % body.ldap_email history = History(for_loan=l, timestamp=tz.utcnow(), msg=hist_line) session.add(l) session.add(history) session.commit() logger.info(hist_line) if not m: chain_of_stuff = task_groups.generate_loan(loanid=l.id, slavetype=slavetype) chain_of_stuff.delay() return l.to_wsme()
def new_loan_request(body): "User Loan Requesting, returns the id of the loan" if not body.ldap_email: raise BadRequest("Missing LDAP E-Mail") if not p.slaveloan.admin.can(): if not body.ldap_email == current_user.authenticated_email: raise BadRequest("You can't request loans on behalf of others.") if body.status: if not p.slaveloan.admin.can(): raise Forbidden("Permission denied to set loan status manually") if body.status not in ["PENDING", "COMPLETE", "ACTIVE"]: raise BadRequest("Loan status (%s) is unsupported at this time" % body.status) if body.status and body.status != 'PENDING': if not body.fqdn: raise BadRequest("Must set Machine FQDN") if not body.ipaddress: raise BadRequest("Must set Machine IP Address") else: if body.fqdn or body.ipaddress: if p.slaveloan.admin.can(): msg = ("Unable to explicitly set fqdn or ipaddress when not" "also explicitly setting status") if body.status == "PENDING": msg += " (to something other than PENDING)" raise BadRequest(msg) else: raise Forbidden("Permission denied to set fqdn and ipaddress manually") if not body.requested_slavetype: if not body.fqdn and not body.ipaddress: raise BadRequest("Missing slavetype") else: if body.fqdn or body.ipaddress: raise BadRequest("Unable to request a host if you're passing in the specifics") slavetype = slave_to_slavetype(body.requested_slavetype) if not slavetype: raise BadRequest("Unsupported slavetype") if not body.bugzilla_email: # Set bugzilla e-mail to ldap e-mail by default body.bugzilla_email = body.ldap_email session = g.db.session('relengapi') try: h = Humans.as_unique(session, ldap=body.ldap_email, bugzilla=body.bugzilla_email) if h.bugzilla != body.bugzilla_email: h.bugzilla = body.bugzilla_email except sa.exc.IntegrityError: raise InternalServerError("Integrity Error from Database, please retry.") m = None if body.fqdn and body.ipaddress: try: m = Machines.as_unique(session, fqdn=body.fqdn, ipaddress=body.ipaddress) except sa.exc.IntegrityError: raise InternalServerError("Integrity Error from Database, please retry.") loan_data = dict(human=h) if body.loan_bug_id: loan_data.update(dict(bug_id=body.loan_bug_id)) if m: loan_data.update(dict(machine=m)) if body.status: loan_data.update(dict(status=body.status)) else: loan_data.update(dict(status="PENDING")) l = Loans(**loan_data) if m: hist_line = "%s logged a %s loan on host: %s (ip: %s)" % \ (current_user.authenticated_email, body.status, body.fqdn, body.ipaddress) else: hist_line = "%s issued a loan request for slavetype %s (original: '%s')" % \ (current_user.authenticated_email, slavetype, body.requested_slavetype) if body.ldap_email != current_user.authenticated_email: hist_line += " on behalf of %s" % body.ldap_email history = History(for_loan=l, timestamp=tz.utcnow(), msg=hist_line) session.add(l) session.add(history) session.commit() logger.info(hist_line) if not m: chain_of_stuff = task_groups.generate_loan(loanid=l.id, slavetype=slavetype) chain_of_stuff.delay() return l.to_wsme()