Add attribute_list_api

Change-Id: I1bd75170a4d308339bf34fa5778d64c5d097d4bd
This commit is contained in:
hejunli 2022-11-24 14:49:44 +08:00
parent 4b60abbb68
commit 962ceed371
4 changed files with 136 additions and 0 deletions

View File

@ -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):

View File

@ -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

View File

@ -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',

View File

@ -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,
)