|
| 1 | +from flask import abort |
| 2 | +from bkr.api.lab_controllers import LABCONTROLLERS |
| 3 | +from bkr.api.arches import ARCHES |
| 4 | +#from bkr.model import System |
| 5 | + |
| 6 | +SYSTEMS = {} |
| 7 | + |
| 8 | +def post(system): |
| 9 | + fqdn = system.get("fqdn") |
| 10 | + owner = system.get("owner") |
| 11 | + status = system.get("status", "unavailable") |
| 12 | + status_reason = system.get("status_reason", "") |
| 13 | + arches = system.get("arches", []) |
| 14 | + power = system.get("power", {}) |
| 15 | + location = system.get("location", "") |
| 16 | + lender = system.get("lender", "") |
| 17 | + vender = system.get("vender", "") |
| 18 | + model = system.get("model", "") |
| 19 | + serial = system.get("serial", "") |
| 20 | + lab_controller = system.get("lab_controller", "") |
| 21 | + |
| 22 | + if lab_controller and lab_controller not in LABCONTROLLERS: |
| 23 | + abort( |
| 24 | + 406, |
| 25 | + f"Lab Controller {lab_controller} doesn't exist", |
| 26 | + ) |
| 27 | + |
| 28 | + for arch in arches: |
| 29 | + if arch not in ARCHES: |
| 30 | + abort( |
| 31 | + 406, |
| 32 | + f"{arch} doesn't exist, create it first.", |
| 33 | + ) |
| 34 | + |
| 35 | + if fqdn and fqdn not in SYSTEMS: |
| 36 | + SYSTEMS[fqdn] = { |
| 37 | + "fqdn": fqdn, |
| 38 | + "owner": owner, |
| 39 | + "status": status, |
| 40 | + "status_reason": status_reason, |
| 41 | + "arches": arches, |
| 42 | + "power": power, |
| 43 | + "location": location, |
| 44 | + "lender": lender, |
| 45 | + "vender": vender, |
| 46 | + "model": model, |
| 47 | + "serial": serial, |
| 48 | + "lab_controller": lab_controller, |
| 49 | + } |
| 50 | + return SYSTEMS[fqdn], 201 |
| 51 | + else: |
| 52 | + abort( |
| 53 | + 406, |
| 54 | + f"System with fqdn {fqdn} already exists", |
| 55 | + ) |
| 56 | + |
| 57 | +def search(offset=0, limit=None): |
| 58 | + start = 0 |
| 59 | + end = len(SYSTEMS.values()) |
| 60 | + if offset and limit: |
| 61 | + start = offset * limit |
| 62 | + end = start + limit |
| 63 | + return list(SYSTEMS.values())[start:end] |
| 64 | + |
| 65 | +def get(fqdn): |
| 66 | + if fqdn in SYSTEMS: |
| 67 | + return SYSTEMS[fqdn] |
| 68 | + else: |
| 69 | + abort( |
| 70 | + 404, |
| 71 | + f"System with fqdn {fqdn} not found", |
| 72 | + ) |
| 73 | + |
| 74 | +def put(fqdn): |
| 75 | + pass |
| 76 | + |
| 77 | +def delete(fqdn): |
| 78 | + pass |
0 commit comments