A REST API for cloud embedded board reservation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

94 lines
2.7 KiB

  1. #
  2. # Copyright (c) 2020 The FreeBSD Foundation
  3. #
  4. # This software1 was developed by John-Mark Gurney under sponsorship
  5. # from the FreeBSD Foundation.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in the
  14. # documentation and/or other materials provided with the distribution.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. # SUCH DAMAGE.
  27. #
  28. from typing import Optional, Union, Dict, Any
  29. import databases
  30. from pydantic import BaseModel, Field
  31. from datetime import datetime
  32. import orm
  33. import sqlalchemy
  34. __all__ = [ 'make_orm', 'BoardClassInfo', 'Board', 'Error' ]
  35. class BoardClassInfo(BaseModel):
  36. clsname: str
  37. arch: str
  38. class Board(BaseModel):
  39. name: str
  40. brdclass: str
  41. reserved: bool
  42. attrs: Dict[str, Any] = Field(default_factory=dict)
  43. class Config:
  44. orm_mode = True
  45. class Error(BaseModel):
  46. error: str
  47. board: Optional[Board]
  48. def _issubclass(a, b):
  49. try:
  50. return issubclass(a, b)
  51. except TypeError:
  52. return False
  53. class DataWrapper(object):
  54. pass
  55. def make_orm(database):
  56. metadata = sqlalchemy.MetaData()
  57. class BoardStatus(orm.Model):
  58. __tablename__ = 'boardstatus'
  59. __database__ = database
  60. __metadata__ = metadata
  61. board = orm.Text(primary_key=True)
  62. user = orm.Text(index=True)
  63. time_reserved = orm.DateTime(default=lambda: datetime.utcnow())
  64. class APIKey(orm.Model):
  65. __tablename__ = 'apikeys'
  66. __database__ = database
  67. __metadata__ = metadata
  68. user = orm.Text(index=True)
  69. key = orm.Text(primary_key=True)
  70. engine = sqlalchemy.create_engine(str(database.url))
  71. metadata.create_all(engine)
  72. r = DataWrapper()
  73. lcls = locals()
  74. for i in [ 'engine', 'metadata', ] + [ x for x in lcls if _issubclass(lcls[x], orm.Model) ]:
  75. setattr(r, i, lcls[i])
  76. return r