From 962ceed37100a2a44d5f64b6ad779b6777bf7b55 Mon Sep 17 00:00:00 2001 From: hejunli Date: Thu, 24 Nov 2022 14:49:44 +0800 Subject: [PATCH] Add attribute_list_api Change-Id: I1bd75170a4d308339bf34fa5778d64c5d097d4bd --- cyborg/api/controllers/v2/__init__.py | 2 + cyborg/api/controllers/v2/attributes.py | 121 ++++++++++++++++++++++++ cyborg/common/policy.py | 12 +++ cyborg/policies/__init__.py | 1 + 4 files changed, 136 insertions(+) create mode 100644 cyborg/api/controllers/v2/attributes.py diff --git a/cyborg/api/controllers/v2/__init__.py b/cyborg/api/controllers/v2/__init__.py index 6ff279b2..be2e3626 100644 --- a/cyborg/api/controllers/v2/__init__.py +++ b/cyborg/api/controllers/v2/__init__.py @@ -25,6 +25,7 @@ from cyborg.api import expose from cyborg.api.controllers import base from cyborg.api.controllers import link from cyborg.api.controllers.v2 import arqs +from cyborg.api.controllers.v2 import attributes from cyborg.api.controllers.v2 import deployables from cyborg.api.controllers.v2 import device_profiles from cyborg.api.controllers.v2 import devices @@ -85,6 +86,7 @@ class Controller(rest.RestController): accelerator_requests = arqs.ARQsController() devices = devices.DevicesController() deployables = deployables.DeployablesController() + attributes = attributes.AttributesController() @expose.expose(V2) def get(self): diff --git a/cyborg/api/controllers/v2/attributes.py b/cyborg/api/controllers/v2/attributes.py new file mode 100644 index 00000000..193b6c5d --- /dev/null +++ b/cyborg/api/controllers/v2/attributes.py @@ -0,0 +1,121 @@ +# Copyright 2022 Inspur. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import pecan +import wsme +from wsme import types as wtypes + +from oslo_log import log + +from cyborg.api.controllers import base +from cyborg.api.controllers import link +from cyborg.api.controllers import types +from cyborg.api import expose +from cyborg.common import authorize_wsgi +from cyborg import objects +LOG = log.getLogger(__name__) + + +class Attribute(base.APIBase): + """API representation of a attribute. + + This class enforces type checking and value constraints, and converts + between the internal object model and the API representation of + a attribute. See module notes above. + """ + + """The UUID of the attribute""" + uuid = types.uuid + + """The deployable_id of the attribute""" + deployable_id = wtypes.IntegerType() + + """The key of the device attribute""" + key = wtypes.text + + """The value of the attribute""" + value = wtypes.text + + created_at = wtypes.datetime.datetime + updated_at = wtypes.datetime.datetime + + """A list containing a self link""" + links = wsme.wsattr([link.Link], readonly=True) + + def __init__(self, **kwargs): + super(Attribute, self).__init__(**kwargs) + self.fields = [] + for field in objects.Attribute.fields: + self.fields.append(field) + setattr(self, field, kwargs.get(field, wtypes.Unset)) + + @classmethod + def convert_with_links(cls, obj_attribute): + api_attribute = cls(**obj_attribute.as_dict()) + api_attribute.links = [ + link.Link.make_link('self', pecan.request.public_url, + 'attribute', api_attribute.uuid) + ] + return api_attribute + + def get_attribute(self, obj_attribute): + api_obj = {} + for field in ['uuid', 'deployable_id', 'key', 'value']: + api_obj[field] = obj_attribute[field] + for field in ['created_at', 'updated_at']: + api_obj[field] = str(obj_attribute[field]) + api_obj['links'] = [ + link.Link.make_link_dict('attributes', api_obj['uuid']) + ] + return api_obj + + +class AttributeCollection(Attribute): + """API representation of a collection of attributes.""" + + """A list containing attribute objects""" + attributes = [Attribute] + + @classmethod + def convert_with_links(cls, obj_attributes): + collection = cls() + collection.attributes = [ + Attribute.convert_with_links(obj_attribute) + for obj_attribute in obj_attributes] + return collection + + def get_attributes(self, obj_attributes): + api_obj_attributes = [ + self.get_attribute(obj_attribute) + for obj_attribute in obj_attributes] + return api_obj_attributes + + +class AttributesController(base.CyborgController, + AttributeCollection): + """REST controller for Attributes.""" + + @authorize_wsgi.authorize_wsgi("cyborg:attribute", "get_all", False) + @expose.expose(AttributeCollection, wtypes.text) + def get_all(self): + """Retrieve a list of attributes.""" + + LOG.info('[attributes] get_all.') + context = pecan.request.context + api_obj_attributes = objects.Attribute.get_by_filter(context, {}) + + ret = AttributeCollection.convert_with_links(api_obj_attributes) + LOG.info('[Attributes] get_all returned: %s', ret) + return ret diff --git a/cyborg/common/policy.py b/cyborg/common/policy.py index 68157902..6e02fe19 100644 --- a/cyborg/common/policy.py +++ b/cyborg/common/policy.py @@ -62,6 +62,18 @@ deployable_policies = [ description='FPGA programming.'), ] +attribute_policies = [ + policy.RuleDefault('cyborg:attribute:get_one', + 'rule:allow', + description='Show attribute detail'), + policy.RuleDefault('cyborg:attribute:get_all', + 'rule:allow', + description='Retrieve all attribute records'), + policy.RuleDefault('cyborg:attribute:delete', + 'rule:allow', + description='Delete attribute records.'), +] + fpga_policies = [ policy.RuleDefault('cyborg:fpga:get_one', 'rule:allow', diff --git a/cyborg/policies/__init__.py b/cyborg/policies/__init__.py index 2a25284e..54bd835d 100644 --- a/cyborg/policies/__init__.py +++ b/cyborg/policies/__init__.py @@ -29,6 +29,7 @@ def list_policies(): # by new policies old_policy.device_policies, old_policy.deployable_policies, + old_policy.attribute_policies, old_policy.accelerator_request_policies, old_policy.fpga_policies, )