From d3f203535a2881f213c51f275a3b3759a284d9d6 Mon Sep 17 00:00:00 2001 From: Xicheng Chang Date: Thu, 9 Jun 2016 20:13:22 -0400 Subject: [PATCH] Initial tinycore support on compass-core. 1. Added an endpoint for adding machines. 2. Added user concept to machine model. Change-Id: I603296d30dbd7182678d98fd5f8f56503cbdc634 --- compass/api/api.py | 22 ++++++++++++++++++-- compass/db/api/machine.py | 44 ++++++++++++++++++++++++++++++++++++--- compass/db/models.py | 2 ++ 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/compass/api/api.py b/compass/api/api.py index e2091835..3cfb8d8f 100644 --- a/compass/api/api.py +++ b/compass/api/api.py @@ -1582,6 +1582,22 @@ def list_machines(): ) +@app.route("/machines", methods=['POST']) +def add_machine(): + """add machine by tinycore. + + supported fileds: [ + 'tag', 'location', 'ipmi_credentials', + 'machine_attributes' + ] + """ + data = _get_request_data() + return utils.make_json_response( + 200, + machine_api.add_machine(**data) + ) + + @app.route("/machines/", methods=['GET']) @log_user_action @login_required @@ -1605,7 +1621,8 @@ def update_machine(machine_id): """update machine. Supported fields: [ - 'tag', 'location', 'ipmi_credentials' + 'tag', 'location', 'ipmi_credentials', + 'machine_attributes' ] """ data = _get_request_data() @@ -1625,7 +1642,8 @@ def patch_machine(machine_id): """patch machine. Supported fields: [ - 'tag', 'location', 'ipmi_credentials' + 'tag', 'location', 'ipmi_credentials', + 'machine_attributes' ] """ data = _get_request_data() diff --git a/compass/db/api/machine.py b/compass/db/api/machine.py index ef8f5c21..123d6070 100644 --- a/compass/db/api/machine.py +++ b/compass/db/api/machine.py @@ -27,16 +27,21 @@ from compass.utils import setting_wrapper as setting from compass.utils import util -SUPPORTED_FIELDS = ['mac', 'tag', 'location'] +MACHINE_PRIMARY_FILEDS = ['mac', 'owner_id'] +SUPPORTED_FIELDS = [ + 'mac', 'tag', 'location', + 'machine_attributes', 'owner_id'] IGNORE_FIELDS = ['id', 'created_at', 'updated_at'] -UPDATED_FIELDS = ['ipmi_credentials', 'tag', 'location'] +UPDATED_FIELDS = [ + 'ipmi_credentials', 'machine_attributes', + 'tag', 'location'] PATCHED_FIELDS = [ 'patched_ipmi_credentials', 'patched_tag', 'patched_location' ] RESP_FIELDS = [ 'id', 'mac', 'ipmi_credentials', 'switches', 'switch_ip', - 'port', 'vlans', + 'port', 'vlans', 'machine_attributes', 'owner_id', 'tag', 'location', 'created_at', 'updated_at' ] RESP_DEPLOY_FIELDS = [ @@ -56,6 +61,39 @@ def _get_machine(machine_id, session=None, **kwargs): ) +@utils.supported_filters( + MACHINE_PRIMARY_FILEDS, + optional_support_keys=SUPPORTED_FIELDS +) +@utils.input_validates(mac=utils.check_mac) +def _add_machine(mac, owner_id=None, session=None, **kwargs): + """Add a machine.""" + if isinstance(owner_id, (int, long)): + return utils.add_db_object( + session, models.Machine, + True, + mac, + owner_id=owner_id, + **kwargs + ) + raise exception.InvalidParameter( + 'owner id %s type is not int compatible' % owner_id + ) + + +@database.run_in_session() +@utils.wrap_to_dict(RESP_FIELDS) +def add_machine( + mac, owner_id=None, session=None, user=None, **kwargs +): + """Add a machine.""" + return _add_machine( + mac, + owner_id=owner_id, + session=session, **kwargs + ) + + def get_machine_internal(machine_id, session=None, **kwargs): """Helper function to other files under db/api.""" return _get_machine(machine_id, session=session, **kwargs) diff --git a/compass/db/models.py b/compass/db/models.py index b27cd3c8..2063d03d 100644 --- a/compass/db/models.py +++ b/compass/db/models.py @@ -1523,6 +1523,8 @@ class Machine(BASE, HelperMixin, TimestampMixin): ipmi_credentials = Column(JSONEncoded, default={}) tag = Column(JSONEncoded, default={}) location = Column(JSONEncoded, default={}) + owner_id = Column(Integer, ForeignKey('user.id')) + machine_attributes = Column(JSONEncoded, default={}) switch_machines = relationship( SwitchMachine,