This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/devicehub.py

141 lines
5.7 KiB
Python
Raw Normal View History

import os
2019-01-23 15:55:04 +00:00
import uuid
2018-04-27 17:16:43 +00:00
from typing import Type
2019-01-23 15:55:04 +00:00
import boltons.urlutils
import click
import click_spinner
import ereuse_utils.cli
2019-01-19 18:19:35 +00:00
from ereuse_utils.session import DevicehubClient
2019-01-23 15:55:04 +00:00
from flask.globals import _app_ctx_stack, g
2018-04-27 17:16:43 +00:00
from flask_sqlalchemy import SQLAlchemy
2018-09-07 10:38:02 +00:00
from teal.teal import Teal
2018-04-27 17:16:43 +00:00
from ereuse_devicehub.auth import Auth
2018-04-10 15:06:39 +00:00
from ereuse_devicehub.client import Client
2019-01-23 15:55:04 +00:00
from ereuse_devicehub.config import DevicehubConfig
2018-04-27 17:16:43 +00:00
from ereuse_devicehub.db import db
2018-06-20 21:18:15 +00:00
from ereuse_devicehub.dummy.dummy import Dummy
2018-09-29 10:24:22 +00:00
from ereuse_devicehub.resources.device.search import DeviceSearch
2019-01-23 15:55:04 +00:00
from ereuse_devicehub.resources.inventory import Inventory, InventoryDef
2018-04-10 15:06:39 +00:00
class Devicehub(Teal):
test_client_class = Client
2018-06-20 21:18:15 +00:00
Dummy = Dummy
2018-04-27 17:16:43 +00:00
2018-05-17 07:59:17 +00:00
def __init__(self,
2019-01-23 15:55:04 +00:00
inventory: str,
config: DevicehubConfig = DevicehubConfig(),
2018-04-27 17:16:43 +00:00
db: SQLAlchemy = db,
import_name=__name__.split('.')[0],
2018-04-27 17:16:43 +00:00
static_url_path=None,
static_folder='static',
2018-05-17 07:59:17 +00:00
static_host=None,
host_matching=False,
subdomain_matching=False,
2018-04-27 17:16:43 +00:00
template_folder='templates',
instance_path=None,
instance_relative_config=False,
root_path=None,
Auth: Type[Auth] = Auth):
2019-01-23 15:55:04 +00:00
assert inventory
super().__init__(config, db, inventory, import_name, static_url_path, static_folder,
static_host,
2018-05-17 07:59:17 +00:00
host_matching, subdomain_matching, template_folder, instance_path,
instance_relative_config, root_path, False, Auth)
2019-01-23 15:55:04 +00:00
self.id = inventory
"""The Inventory ID of this instance. In Teal is the app.schema."""
2018-06-20 21:18:15 +00:00
self.dummy = Dummy(self)
@self.cli.group(short_help='Inventory management.',
help='Manages the inventory {}.'.format(os.environ.get('dhi')))
def inv():
pass
inv.command('add')(self.init_db)
inv.command('del')(self.delete_inventory)
inv.command('search')(self.regenerate_search)
2019-01-23 15:55:04 +00:00
self.before_request(self._prepare_request)
2018-09-29 10:24:22 +00:00
2019-01-23 15:55:04 +00:00
# noinspection PyMethodOverriding
@click.option('--name', '-n',
default='Test 1',
help='The human name of the inventory.')
@click.option('--org-name', '-on',
default='My Organization',
help='The name of the default organization that owns this inventory.')
@click.option('--org-id', '-oi',
default='foo-bar',
help='The Tax ID of the organization.')
@click.option('--tag-url', '-tu',
type=ereuse_utils.cli.URL(scheme=True, host=True, path=False),
default='http://example.com',
help='The base url (scheme and host) of the tag provider.')
@click.option('--tag-token', '-tt',
type=click.UUID,
default='899c794e-1737-4cea-9232-fdc507ab7106',
help='The token provided by the tag provider. It is an UUID.')
@click.option('--erase/--no-erase',
default=False,
2019-02-04 12:38:46 +00:00
help='Delete the schema before? '
'If --common is set this includes the common database.')
2019-01-23 16:59:29 +00:00
@click.option('--common/--no-common',
2019-01-23 15:55:04 +00:00
default=False,
help='Creates common databases. Only execute if the database is empty.')
def init_db(self, name: str,
org_name: str,
org_id: str,
tag_url: boltons.urlutils.URL,
tag_token: uuid.UUID,
erase: bool,
common: bool):
"""Creates an inventory.
This creates the database and adds the inventory to the
inventory tables with the passed-in settings, and does nothing if the
inventory already exists.
After you create the inventory you might want to create an user
executing *dh user add*.
"""
2019-01-23 15:55:04 +00:00
assert _app_ctx_stack.top, 'Use an app context.'
print('Initializing database...'.ljust(30), end='')
with click_spinner.spinner():
if erase:
2019-02-04 12:38:46 +00:00
self.db.drop_all(common_schema=common)
assert not db.has_schema(self.id), 'Schema {} already exists.'.format(self.id)
2019-01-23 15:55:04 +00:00
exclude_schema = 'common' if not common else None
self._init_db(exclude_schema=exclude_schema)
InventoryDef.set_inventory_config(name, org_name, org_id, tag_url, tag_token)
2019-01-21 15:08:55 +00:00
DeviceSearch.set_all_devices_tokens_if_empty(self.db.session)
2019-01-23 15:55:04 +00:00
self._init_resources(exclude_schema=exclude_schema)
self.db.session.commit()
print('done.')
2018-10-31 11:27:16 +00:00
@click.confirmation_option(prompt='Are you sure you want to delete the inventory {}?'
.format(os.environ.get('dhi')))
def delete_inventory(self):
"""Erases an inventory.
This removes its private database and its entry in the common
inventory.
This deletes users that have only access to this inventory.
"""
InventoryDef.delete_inventory()
self.db.session.commit()
self.db.drop_all(common_schema=False)
2018-10-31 11:27:16 +00:00
def regenerate_search(self):
"""Re-creates from 0 all the search tables."""
DeviceSearch.regenerate_search_table(self.db.session)
db.session.commit()
print('Done.')
2019-01-23 15:55:04 +00:00
def _prepare_request(self):
"""Prepares request stuff."""
inv = g.inventory = Inventory.current # type: Inventory
g.tag_provider = DevicehubClient(base_url=inv.tag_provider,
token=DevicehubClient.encode_token(inv.tag_token))