google.cloud.forseti.services.dao module

Database abstraction objects for Forseti Server.

class Model(*args, **kwargs)[source]

Bases: sqlalchemy.ext.declarative.api.Base

Explain model object in database.

__repr__()[source]

String representation.

Returns:
Model represented as
(name=’{}’, handle=’{}’ state=’{}’)
Return type:str
_sa_class_manager = {'created_at_datetime': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f097fc02a10>, 'description': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f097fc02ef0>, 'etag_seed': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f097fc022f0>, 'handle': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f097f9d7050>, 'message': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f097ca97d10>, 'name': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f097f9d7e30>, 'state': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f097fc02a70>, 'warnings': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f097ca97530>, 'watchdog_timer_datetime': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f097fc025f0>}
add_description(description)[source]

Add new description to the model

Parameters:description (str) – the description to be added in json format
add_warning(warning)[source]

Add a warning to the model.

Parameters:warning (str) – Warning message
created_at_datetime
description
etag_seed
get_warnings()[source]

Returns any stored warnings.

Returns:warning message
Return type:str
handle
init_on_load()[source]

Initialization of model when reconstructed from query.

kick_watchdog()[source]

Used during import to notify the import is still progressing.

message
name
set_done(message='')[source]

Indicate a finished import.

Parameters:message (str) – Success message or ‘’
set_error(message)[source]

Indicate a broken import.

Parameters:message (str) – error message
set_inprogress()[source]

Set state to ‘in progress’.

state
warnings
watchdog_timer_datetime
class ModelManager(dbengine)[source]

Bases: object

The Central class to create,list,get and delete models.

ModelManager is mostly used to do the lookup from model name to the session cache which is given in each client’s request.

_create_model_session()[source]

Create a session to read from the models table.

Returns:db session created
Return type:object
_get(handle)[source]

Get model data by name internal.

Parameters:handle (str) – the model handle
Returns:the model in the session maker
Return type:Model
Raises:KeyError – model handle not available
_models(expunge=False)[source]

Return the list of models from the database.

Parameters:expunge (bool) – Whether or not to detach the object from the session for use in another session.
Returns:list of Models in the db
Return type:list
add_description(model_name, new_description, session=None)[source]

Add description to a model.

Parameters:
  • model_name (str) – Model name
  • new_description (str) – The description in json format.
  • session (object) – Database session.
create(**kw)

Decorated functionality, mutexing wrapped function.

Parameters:
  • *args – All args provided to the wrapped function
  • **kw – All kw provided to the wrapped function
Returns:

the execution results of f

Return type:

object

delete(**kw)

Decorated functionality, mutexing wrapped function.

Parameters:
  • *args – All args provided to the wrapped function
  • **kw – All kw provided to the wrapped function
Returns:

the execution results of f

Return type:

object

get(model)[source]

Get model data by handle.

Parameters:model (str) – model handle
Returns:session and ModelAccess object
Return type:tuple
get_description(model_name, session=None)[source]

Get the description to a model.

Parameters:
  • model_name (str) – Model name
  • session (object) – Database session.
Returns:

Dictionary of the model description.

Return type:

json

get_model(model, expunge=True, session=None)[source]

Get model from database by name or handle.

Parameters:
  • model (str) – Model name or handle
  • expunge (bool) – Whether or not to detach the object from the session for use in another session.
  • session (object) – Database session.
Returns:

the dbo of the queried model

Return type:

Model

get_readonly_session()[source]

Get read-only session.

Returns:The read-only session.
Return type:Session
model(model_name, expunge=True, session=None)[source]

Get model from database by name.

Parameters:
  • model_name (str) – Model name or handle
  • expunge (bool) – Whether or not to detach the object from the session for use in another session.
  • session (object) – Database session.
Returns:

the dbo of the queried model

Return type:

Model

models()[source]

Expunging wrapper for _models.

Returns:list of Models in the db
Return type:list
create_engine(*args, **kwargs)[source]

Create engine wrapper to patch database options.

Parameters:
  • *args (list) – Arguments.
  • **kwargs (dict) – Arguments.
Returns:

Engine.

Return type:

object

define_model(model_name, dbengine, model_seed)[source]

Defines table classes which point to the corresponding model.

This means, for each model being accessed this function needs to be called in order to generate a full set of table definitions.

Models are name spaced via a random model seed such that multiple models can exist within the same database. In order to implement the name spacing in an abstract way.

Parameters:
  • model_name (str) – model handle
  • dbengine (object) – db engine
  • model_seed (str) – seed to get etag
Returns:

(sessionmaker, ModelAccess)

Return type:

tuple

generate_model_handle()[source]

Generate random model handle.

Returns:random bytes for handle
Return type:str
generate_model_seed()[source]

Generate random model seed.

Returns:random bytes
Return type:str
session_creator(model_name, filename=None, seed=None, echo=False)[source]

Create a session maker for the model and db file.

Parameters:
  • model_name (str) – the model name
  • filename (str) – the db file to load the sqlite database
  • seed (str) – the unique model handle
  • echo (bool) – whether to echo all the statements
Returns:

session_maker and the ModelAccess object

Return type:

tuple

undefine_model(session_maker, data_access)[source]

Deletes an entire model and the corresponding data in the database.

Parameters:
  • session_maker (func) – session_maker function
  • data_access (ModelAccess) – data access layer