Esempio n. 1
0
    def post(self):
        params = request.values

        type_name = params.get("name")
        type_alias = params.get("alias")
        type_alias = type_name if not type_alias else type_alias
        params['alias'] = type_alias

        manager = CITypeManager()
        type_id = manager.add(**params)

        return self.jsonify(type_id=type_id)
Esempio n. 2
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
Esempio n. 3
0
def test_get_ci_by_types(session, client):
    ci = init_ci(1)[0]
    ci_type = CITypeManager.get_ci_types(ci.get("ci_type"))[0]
    url = "/api/v0.1/ci/type/{}".format(ci_type["id"])

    resp = client.get(url)
    assert resp.status_code == 200

    assert resp.json['cis'][0]['ci_id'] == ci['ci_id']
Esempio n. 4
0
    def get(self, type_id=None, type_name=None):
        q = request.args.get("type_name")

        if type_id is not None:
            ci_types = [CITypeCache.get(type_id).to_dict()]
        elif type_name is not None:
            ci_types = [CITypeCache.get(type_name).to_dict()]
        else:
            ci_types = CITypeManager().get_ci_types(q)
        count = len(ci_types)

        return self.jsonify(numfound=count, ci_types=ci_types)
Esempio n. 5
0
    def add_heartbeat(ci_type, unique_value):
        ci_type = CITypeManager().check_is_existed(ci_type)

        unique_key = AttributeCache.get(ci_type.unique_id)
        value_table = TableMap(attr_name=unique_key.name).table

        v = value_table.get_by(attr_id=unique_key.id,
                               value=unique_value,
                               to_dict=False,
                               first=True) \
            or abort(404, "not found")

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

        ci.update(heartbeat=datetime.datetime.now())
Esempio n. 6
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']]
Esempio n. 7
0
 def get(self):
     q = request.args.get("q")
     res = CITypeManager.query(q)
     return self.jsonify(ci_type=res)
Esempio n. 8
0
 def delete(self, type_id):
     CITypeManager.delete(type_id)
     return self.jsonify(type_id=type_id)
Esempio n. 9
0
    def put(self, type_id):
        params = request.values

        manager = CITypeManager()
        manager.update(type_id, **params)
        return self.jsonify(type_id=type_id)
Esempio n. 10
0
 def post(self, type_id):
     enable = request.values.get("enable", True)
     CITypeManager.set_enabled(type_id, enabled=enable)
     return self.jsonify(type_id=type_id, enable=enable)