Esempio n. 1
0
    def add(cls, **kwargs):
        choice_value = kwargs.pop("choice_value", [])
        kwargs.pop("is_choice", None)
        is_choice = True if choice_value else False
        name = kwargs.pop("name")
        alias = kwargs.pop("alias", "")
        alias = name if not alias else alias
        Attribute.get_by(name=name, first=True) and abort(400, "attribute {0} is already existed".format(name))

        attr = Attribute.create(flush=True,
                                name=name,
                                alias=alias,
                                is_choice=is_choice,
                                **kwargs)

        if choice_value:
            cls._add_choice_values(attr.id, attr.value_type, choice_value)

        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            current_app.logger.error("add attribute error, {0}".format(str(e)))
            return abort(400, "add attribute <{0}> failed".format(name))

        AttributeCache.clean(attr)

        return attr.id
Esempio n. 2
0
    def search_attributes(cls, name=None, alias=None, page=1, page_size=None):
        """
        :param name: 
        :param alias: 
        :param page: 
        :param page_size: 
        :return: attribute, if name is None, then return all attributes
        """
        if name is not None:
            attrs = Attribute.get_by_like(name=name)
        elif alias is not None:
            attrs = Attribute.get_by_like(alias=alias)
        else:
            attrs = Attribute.get_by()

        numfound = len(attrs)
        attrs = attrs[(page - 1) * page_size:][:page_size]
        res = list()
        for attr in attrs:
            attr["is_choice"] and attr.update(
                dict(choice_value=cls.get_choice_values(
                    attr["id"], attr["value_type"])))
            res.append(attr)

        return numfound, res
Esempio n. 3
0
    def update(self, _id, **kwargs):
        attr = Attribute.get_by_id(_id) or abort(404, "Attribute <{0}> does not exist".format(_id))

        if kwargs.get("name"):
            other = Attribute.get_by(name=kwargs['name'], first=True, to_dict=False)
            if other and other.id != attr.id:
                return abort(400, "Attribute name <{0}> cannot be duplicate!".format(kwargs['name']))
        if kwargs.get("alias"):
            other = Attribute.get_by(alias=kwargs['alias'], first=True, to_dict=False)
            if other and other.id != attr.id:
                return abort(400, "Attribute alias <{0}> cannot be duplicate!".format(kwargs['alias']))

        choice_value = kwargs.pop("choice_value", False)
        is_choice = True if choice_value else False
        kwargs['is_choice'] = is_choice

        attr.update(flush=True, **kwargs)

        if is_choice:
            self._add_choice_values(attr.id, attr.value_type, choice_value)
        else:
            self._del_choice_values(attr.id, attr.value_type)

        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            current_app.logger.error("update attribute error, {0}".format(str(e)))
            return abort(400, "update attribute <{0}> failed".format(_id))

        AttributeCache.clean(attr)

        return attr.id
Esempio n. 4
0
 def get_attributes(self, name=None):
     """
     :param name: 
     :return: attribute, if name is None, then return all attributes
     """
     attrs = Attribute.get_by_like(
         name=name) if name is not None else Attribute.get_by()
     res = list()
     for attr in attrs:
         attr["is_choice"] and attr.update(
             dict(choice_value=self.get_choice_values(
                 attr["id"], attr["value_type"])))
         res.append(attr)
     return res
Esempio n. 5
0
    def get(cls, key):
        if key is None:
            return
        attr = cache.get('Field::Name::{0}'.format(key)) \
            or cache.get('Field::ID::{0}'.format(key)) \
            or cache.get('Field::Alias::{0}'.format(key))

        if attr is None:
            attr = Attribute.get_by(name=key, first=True, to_dict=False) \
                   or Attribute.get_by_id(key) \
                   or Attribute.get_by(alias=key, first=True, to_dict=False)
            if attr is not None:
                cls.set(attr)
        return attr
Esempio n. 6
0
    def get(cls, key):
        if key is None:
            return
        attr = cache.get(cls.PREFIX_NAME.format(key))
        attr = attr or cache.get(cls.PREFIX_ID.format(key))
        attr = attr or cache.get(cls.PREFIX_ALIAS.format(key))

        if attr is None:
            attr = Attribute.get_by(name=key, first=True, to_dict=False)
            attr = attr or Attribute.get_by_id(key)
            attr = attr or Attribute.get_by(
                alias=key, first=True, to_dict=False)
            if attr is not None:
                cls.set(attr)
        return attr
Esempio n. 7
0
    def add(cls, **kwargs):
        choice_value = kwargs.pop("choice_value", [])
        kwargs.pop("is_choice", None)
        is_choice = True if choice_value else False
        name = kwargs.pop("name")
        alias = kwargs.pop("alias", "")
        alias = name if not alias else alias
        Attribute.get_by(name=name, first=True) and abort(
            400, "attribute name <{0}> is duplicated".format(name))
        Attribute.get_by(alias=alias, first=True) and abort(
            400, "attribute alias <{0}> is duplicated".format(name))

        attr = Attribute.create(flush=True,
                                name=name,
                                alias=alias,
                                is_choice=is_choice,
                                **kwargs)

        if choice_value:
            cls._add_choice_values(attr.id, attr.value_type, choice_value)

        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            current_app.logger.error("add attribute error, {0}".format(str(e)))
            return abort(400, "add attribute <{0}> failed".format(name))

        AttributeCache.clean(attr)

        if current_app.config.get("USE_ES"):
            from api.extensions import es
            other = dict()
            other['index'] = True if attr.is_index else False
            if attr.value_type == ValueTypeEnum.TEXT:
                other['analyzer'] = 'ik_max_word'
                other['search_analyzer'] = 'ik_smart'
                if attr.is_index:
                    other["fields"] = {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
            es.update_mapping(name, ValueTypeMap.es_type[attr.value_type],
                              other)

        return attr.id
Esempio n. 8
0
 def get_attribute_by_id(self, _id):
     attr = Attribute.get_by_id(_id).to_dict()
     if attr and attr["is_choice"]:
         attr.update(
             dict(choice_value=self.get_choice_values(
                 attr["id"], attr["value_type"])))
     return attr
Esempio n. 9
0
 def get_attribute_by_alias(self, alias):
     attr = Attribute.get_by(alias=alias, first=True)
     if attr and attr["is_choice"]:
         attr.update(
             dict(choice_value=self.get_choice_values(
                 attr["id"], attr["value_type"])))
     return attr
Esempio n. 10
0
 def get_attribute_by_name(self, name):
     attr = Attribute.get_by(name=name, first=True)
     if attr and attr["is_choice"]:
         attr.update(
             dict(choice_value=self.get_choice_values(
                 attr["id"], attr["value_type"])))
     return attr
Esempio n. 11
0
def init_cache():
    db.session.remove()

    if current_app.config.get("USE_ES"):
        from api.extensions import es
        from api.models.cmdb import Attribute
        from api.lib.cmdb.utils import ValueTypeMap
        attributes = Attribute.get_by(to_dict=False)
        for attr in attributes:
            other = dict()
            other['index'] = True if attr.is_index else False
            if attr.value_type == ValueTypeEnum.TEXT:
                other['analyzer'] = 'ik_max_word'
                other['search_analyzer'] = 'ik_smart'
                if attr.is_index:
                    other["fields"] = {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
            try:
                es.update_mapping(attr.name,
                                  ValueTypeMap.es_type[attr.value_type], other)
            except Exception as e:
                print(e)

    cis = CI.get_by(to_dict=False)
    for ci in cis:
        if current_app.config.get("USE_ES"):
            res = es.get_index_id(ci.id)
            if res:
                continue
        else:
            res = rd.get([ci.id], REDIS_PREFIX_CI)
            if res and list(filter(lambda x: x, res)):
                continue

        m = api.lib.cmdb.ci.CIManager()
        ci_dict = m.get_ci_by_id_from_db(ci.id,
                                         need_children=False,
                                         use_master=False)

        if current_app.config.get("USE_ES"):
            es.create(ci_dict)
        else:
            rd.create_or_update({ci.id: json.dumps(ci_dict)}, REDIS_PREFIX_CI)

    ci_relations = CIRelation.get_by(to_dict=False)
    relations = dict()
    for cr in ci_relations:
        relations.setdefault(cr.first_ci_id, {}).update(
            {cr.second_ci_id: cr.second_ci.type_id})
    for i in relations:
        relations[i] = json.dumps(relations[i])
    if relations:
        rd.create_or_update(relations, REDIS_PREFIX_CI_RELATION)

    db.session.remove()
Esempio n. 12
0
def init_attributes(num=1):
    attrs = []
    for i in range(num):
        attrs.append(
            Attribute.create(name=uuid.uuid4().hex[:8],
                             alias=uuid.uuid4().hex[:8],
                             value_type=str(random.randint(0, 100) % 3)))
    return attrs
Esempio n. 13
0
def test_delete_attribute(session, client):
    attr_ins = init_attributes(1)[0]
    url = "/api/v0.1/attributes/" + str(attr_ins.id)

    resp = client.delete(url)

    assert resp.status_code == 200
    # attr should be soft delete
    attr_ins = Attribute.get_by_id(attr_ins.id)
    assert attr_ins.deleted is True
    assert attr_ins.deleted_at
Esempio n. 14
0
def test_create_attribute(session, client):
    url = "/api/v0.1/attributes"
    payload = {"name": "region", "alias": "区域", "value_type": "2"}

    resp = client.post(url, json=payload)

    # check resp status code and content
    assert resp.status_code == 200
    assert resp.json["attr_id"]

    # check there is a attribute in database
    attr_id = resp.json["attr_id"]
    attr_ins = Attribute.get_by_id(attr_id)
    assert attr_ins.id == attr_id
    assert attr_ins.name == "region"
    assert attr_ins.alias == "区域"
Esempio n. 15
0
def test_update_attribute(session, client):
    attr_ins = init_attributes(1)[0]

    url = "/api/v0.1/attributes/" + str(attr_ins.id)
    payload = {
        "name": "update",
    }

    resp = client.put(url, json=payload)

    # check resp status code and content
    assert resp.status_code == 200
    assert resp.json["attr_id"] == attr_ins.id

    # check attribute updated in database
    attr_ins = Attribute.get_by_id(attr_ins.id)
    assert attr_ins.name == "update"
Esempio n. 16
0
    def delete(_id):
        attr = Attribute.get_by_id(_id) or abort(404, "Attribute <{0}> does not exist".format(_id))
        name = attr.name

        if attr.is_choice:
            choice_table = type_map["choice"].get(attr.value_type)
            db.session.query(choice_table).filter(choice_table.attr_id == _id).delete()  # FIXME: session conflict
            db.session.flush()

        AttributeCache.clean(attr)

        attr.soft_delete()

        for i in CITypeAttribute.get_by(attr_id=_id, to_dict=False):
            i.soft_delete()

        for i in PreferenceShowAttributes.get_by(attr_id=_id, to_dict=False):
            i.soft_delete()

        return name
Esempio n. 17
0
    def update(self, _id, **kwargs):
        attr = Attribute.get_by_id(_id) or abort(404, "Attribute <{0}> does not exist".format(_id))

        choice_value = kwargs.pop("choice_value", False)
        is_choice = True if choice_value else False

        attr.update(flush=True, **kwargs)

        if is_choice:
            self._add_choice_values(attr.id, attr.value_type, choice_value)

        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            current_app.logger.error("update attribute error, {0}".format(str(e)))
            return abort(400, "update attribute <{0}> failed".format(_id))

        AttributeCache.clean(attr)

        return attr.id