mirror of https://github.com/veops/cmdb.git
66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
# -*- coding:utf-8 -*-
|
|
|
|
|
|
from flask import request
|
|
from flask import abort
|
|
from flask import current_app
|
|
|
|
from api.resource import APIView
|
|
from api.lib.perm.acl import role_required
|
|
from api.lib.cmdb.const import RoleEnum
|
|
from api.lib.cmdb.attribute import AttributeManager
|
|
from api.lib.decorator import args_required
|
|
from api.lib.utils import handle_arg_list
|
|
|
|
|
|
class AttributeSearchView(APIView):
|
|
url_prefix = ("/attributes/s", "/attributes/search")
|
|
|
|
def get(self):
|
|
q = request.values.get("q")
|
|
attrs = AttributeManager().get_attributes(name=q)
|
|
count = len(attrs)
|
|
return self.jsonify(numfound=count, attributes=attrs)
|
|
|
|
|
|
class AttributeView(APIView):
|
|
url_prefix = ("/attributes", "/attributes/<string:attr_name>", "/attributes/<int:attr_id>")
|
|
|
|
def get(self, attr_name=None, attr_id=None):
|
|
attr_manager = AttributeManager()
|
|
attr_dict = None
|
|
if attr_name is not None:
|
|
attr_dict = attr_manager.get_attribute_by_name(attr_name)
|
|
if attr_dict is None:
|
|
attr_dict = attr_manager.get_attribute_by_alias(attr_name)
|
|
elif attr_id is not None:
|
|
attr_dict = attr_manager.get_attribute_by_id(attr_id)
|
|
if attr_dict is not None:
|
|
return self.jsonify(attribute=attr_dict)
|
|
abort(404, "Attribute is not found")
|
|
|
|
@role_required(RoleEnum.CONFIG)
|
|
@args_required("name")
|
|
def post(self):
|
|
choice_value = handle_arg_list(request.values.get("choice_value"))
|
|
params = request.values
|
|
params["choice_value"] = choice_value
|
|
current_app.logger.debug(params)
|
|
|
|
attr_id = AttributeManager.add(**params)
|
|
return self.jsonify(attr_id=attr_id)
|
|
|
|
@role_required(RoleEnum.CONFIG)
|
|
def put(self, attr_id):
|
|
choice_value = handle_arg_list(request.values.get("choice_value"))
|
|
params = request.values
|
|
params["choice_value"] = choice_value
|
|
current_app.logger.debug(params)
|
|
AttributeManager().update(attr_id, **params)
|
|
return self.jsonify(attr_id=attr_id)
|
|
|
|
@role_required(RoleEnum.CONFIG)
|
|
def delete(self, attr_id):
|
|
attr_name = AttributeManager.delete(attr_id)
|
|
return self. jsonify(message="attribute {0} deleted".format(attr_name))
|