예제 #1
0
 def put(self, type_id=None):
     """
     attributes is list, only support raw data request
     :param type_id: 
     :return: 
     """
     attributes = request.values.get("attributes")
     current_app.logger.debug(attributes)
     if not isinstance(attributes, list):
         return abort(400, "attributes must be list")
     CITypeAttributeManager.update(type_id, attributes)
     return self.jsonify(attributes=attributes)
예제 #2
0
파일: ci.py 프로젝트: xuweiwei2011/cmdb-1
    def get_ci_by_id_from_db(ci_id, ret_key=RetKey.NAME, fields=None, need_children=True, use_master=False):
        """
        
        :param ci_id: 
        :param ret_key: name, id or alias
        :param fields: list
        :param need_children: 
        :param use_master: whether to use master db
        :return: 
        """

        ci = CI.get_by_id(ci_id) or abort(404, "CI <{0}> is not existed".format(ci_id))

        res = dict()

        if need_children:
            children = CIRelationManager.get_children(ci_id, ret_key=ret_key)  # one floor
            res.update(children)

        ci_type = CITypeCache.get(ci.type_id)
        res["ci_type"] = ci_type.name

        fields = CITypeAttributeManager.get_attr_names_by_type_id(ci.type_id) if not fields else fields
        unique_key = AttributeCache.get(ci_type.unique_id)
        _res = AttributeValueManager().get_attr_values(fields,
                                                       ci_id,
                                                       ret_key=ret_key,
                                                       unique_key=unique_key,
                                                       use_master=use_master)
        res.update(_res)

        res['type_id'] = ci_type.id
        res['ci_id'] = ci_id

        return res
예제 #3
0
    def add(cls,
            ci_type_name,
            exist_policy=ExistPolicy.REPLACE,
            _no_attribute_policy=ExistPolicy.IGNORE,
            **ci_dict):
        """
        
        :param ci_type_name: 
        :param exist_policy: replace or reject or need
        :param _no_attribute_policy: ignore or reject
        :param ci_dict: 
        :return: 
        """

        ci_type = CITypeManager.check_is_existed(ci_type_name)

        unique_key = AttributeCache.get(ci_type.unique_id) or abort(
            400, 'illegality unique attribute')

        unique_value = ci_dict.get(unique_key.name)
        unique_value = unique_value or ci_dict.get(unique_key.alias)
        unique_value = unique_value or ci_dict.get(unique_key.id)
        unique_value = unique_value or abort(
            400, '{0} missing'.format(unique_key.name))

        existed = cls.ci_is_exist(unique_key, unique_value)
        if existed is not None:
            if exist_policy == ExistPolicy.REJECT:
                return abort(400, 'CI is already existed')
            if existed.type_id != ci_type.id:
                existed.update(type_id=ci_type.id)
            ci = existed
        else:
            if exist_policy == ExistPolicy.NEED:
                return abort(404,
                             'CI <{0}> does not exist'.format(unique_value))
            ci = CI.create(type_id=ci_type.id)

        ci_type_attrs_name = [
            attr["name"] for attr in
            CITypeAttributeManager().get_attributes_by_type_id(ci_type.id)
        ]
        value_manager = AttributeValueManager()
        for p, v in ci_dict.items():
            if p not in ci_type_attrs_name:
                current_app.logger.warning(
                    'ci_type: {0} not has attribute {1}, please check!'.format(
                        ci_type_name, p))
                continue
            try:
                value_manager.create_or_update_attr_value(
                    p, v, ci, _no_attribute_policy)
            except BadRequest as e:
                if existed is None:
                    cls.delete(ci.id)
                raise e

        ci_cache.apply_async([ci.id], queue=CMDB_QUEUE)

        return ci.id
예제 #4
0
 def get(self, type_id=None, type_name=None):
     t = CITypeCache.get(type_id) or CITypeCache.get(type_name) or abort(404, "CIType does not exist")
     type_id = t.id
     unique_id = t.unique_id
     unique = AttributeCache.get(unique_id).name
     return self.jsonify(attributes=CITypeAttributeManager.get_attributes_by_type_id(type_id),
                         type_id=type_id,
                         unique_id=unique_id,
                         unique=unique)
예제 #5
0
def init_ci_with_type(ci_types):
    force_add_user()
    cis = []
    manager = CIManager()
    for ci_type in ci_types:
        attrs = CITypeAttributeManager.get_attributes_by_type_id(ci_type.id)
        ci_id = manager.add(ci_type.name, **fake_attr_value(attrs[0]))
        cis.append(manager.get_ci_by_id_from_db(ci_id))
    return cis
예제 #6
0
def init_ci(num=1):
    # store ci need has user
    force_add_user()
    ci_type = init_ci_types(1)[0]
    attrs = CITypeAttributeManager.get_attributes_by_type_id(ci_type.id)
    manager = CIManager()
    result = []

    for i in range(num):
        ci_id = manager.add(ci_type.name, **fake_attr_value(attrs[0]))
        result.append(manager.get_ci_by_id_from_db(ci_id))

    return result
예제 #7
0
def test_create_ci(session, client):
    ci_type = init_ci_types(1)[0]
    attrs = CITypeAttributeManager.get_attributes_by_type_id(ci_type.id)
    url = "/api/v0.1/ci"

    fake_value = fake_attr_value(attrs[0])

    payload = {"ci_type": ci_type.id, **fake_value}

    resp = client.post(url, json=payload)
    assert resp.status_code == 200
    assert resp.json["ci_id"]

    ci_id = resp.json["ci_id"]
    ci = CIManager().get_ci_by_id_from_db(ci_id)
    assert ci[attrs[0]["name"]] == fake_value[attrs[0]['name']]
예제 #8
0
def test_update_ci(session, client):
    ci = init_ci(1)[0]
    ci_id = ci.get("ci_id")
    ci_type = CITypeManager.get_ci_types(ci.get("ci_type"))[0]
    attrs = CITypeAttributeManager.get_attributes_by_type_id(ci_type["id"])
    url = "/api/v0.1/ci/{}".format(ci_id)

    fake_value = fake_attr_value(attrs[0])

    payload = {**fake_value}

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

    assert resp.status_code == 200
    assert resp.json["ci_id"] == ci_id
    ci = CIManager().get_ci_by_id_from_db(ci_id)
    assert ci[attrs[0]['name']] == fake_value[attrs[0]['name']]
예제 #9
0
    def update(self, ci_id, **ci_dict):
        ci = self.confirm_ci_existed(ci_id)

        ci_type_attrs_name = [
            attr["name"] for attr in
            CITypeAttributeManager().get_attributes_by_type_id(ci.type_id)
        ]
        value_manager = AttributeValueManager()
        for p, v in ci_dict.items():
            if p not in ci_type_attrs_name:
                current_app.logger.warning(
                    'ci_type: {0} not has attribute {1}, please check!'.format(
                        ci.type_id, p))
                continue
            try:
                value_manager.create_or_update_attr_value(p, v, ci)
            except BadRequest as e:
                raise e

        ci_cache.apply_async([ci_id], queue=CMDB_QUEUE)