Example #1
0
    def add(cls, **kwargs):
        unique_key = kwargs.pop("unique_key", None)
        unique_key = AttributeCache.get(unique_key) or abort(
            404, "Unique key is not defined")

        CIType.get_by(name=kwargs['name']) and abort(
            404, "CIType <{0}> is already existed".format(kwargs.get("name")))

        kwargs["alias"] = kwargs["name"] if not kwargs.get(
            "alias") else kwargs["alias"]

        kwargs["unique_id"] = unique_key.id
        ci_type = CIType.create(**kwargs)

        CITypeAttributeManager.add(ci_type.id, [unique_key.id],
                                   is_required=True)

        CITypeCache.clean(ci_type.name)

        if current_app.config.get("USE_ACL"):
            from api.lib.perm.acl.acl import ACLManager
            from api.lib.cmdb.const import ResourceTypeEnum, RoleEnum, PermEnum
            ACLManager().add_resource(ci_type.name, ResourceTypeEnum.CI)
            ACLManager().grant_resource_to_role(ci_type.name,
                                                RoleEnum.CMDB_READ_ALL,
                                                ResourceTypeEnum.CI,
                                                permissions=[PermEnum.READ])

        return ci_type.id
Example #2
0
 def get_ci_types(type_name=None):
     ci_types = CIType.get_by() if type_name is None else CIType.get_by_like(name=type_name)
     res = list()
     for type_dict in ci_types:
         type_dict["unique_key"] = AttributeCache.get(type_dict["unique_id"]).name
         res.append(type_dict)
     return res
Example #3
0
 def get(cls, key):
     if key is None:
         return
     ct = cache.get("CIType::ID::{0}".format(key)) or \
         cache.get("CIType::Name::{0}".format(key)) or \
         cache.get("CIType::Alias::{0}".format(key))
     if ct is None:
         ct = CIType.get_by(name=key, first=True, to_dict=False) or \
              CIType.get_by_id(key) or \
              CIType.get_by(alias=key, first=True, to_dict=False)
         if ct is not None:
             cls.set(ct)
     return ct
Example #4
0
 def get(cls, key):
     if key is None:
         return
     ct = cache.get(cls.PREFIX_NAME.format(key))
     ct = ct or cache.get(cls.PREFIX_ID.format(key))
     ct = ct or cache.get(cls.PREFIX_ALIAS.format(key))
     if ct is None:
         ct = CIType.get_by(name=key, first=True, to_dict=False)
         ct = ct or CIType.get_by_id(key)
         ct = ct or CIType.get_by(alias=key, first=True, to_dict=False)
         if ct is not None:
             cls.set(ct)
     return ct
Example #5
0
    def _validate_unique(type_id=None, name=None, alias=None):
        if name is not None:
            ci_type = CIType.get_by(name=name, first=True, to_dict=False)
        elif alias is not None:
            ci_type = CIType.get_by(alias=alias, first=True, to_dict=False)
        else:
            return

        if not ci_type:
            return

        if type_id is not None and ci_type.id != type_id:
            return abort(
                400, "CIType <{0}> is already existed".format(name or alias))

        if type_id is None and ci_type is not None:
            return abort(
                400, "CIType <{0}> is already existed".format(name or alias))
Example #6
0
def test_delete_ci_type(session, client):
    ci_type_ins = init_ci_types(1)[0]
    url = "/api/v0.1/ci_types/" + str(ci_type_ins.id)

    resp = client.delete(url)

    assert resp.status_code == 200
    # attr should be soft delete
    ci_type_ins = CIType.get_by_id(ci_type_ins.id)
    assert ci_type_ins.deleted is True
    assert ci_type_ins.deleted_at
Example #7
0
    def add(cls, **kwargs):
        unique_key = kwargs.pop("unique_key", None)
        unique_key = AttributeCache.get(unique_key) or abort(
            404, "Unique key is not defined")

        CIType.get_by(name=kwargs['name']) and abort(
            404, "CIType <{0}> is already existed".format(kwargs.get("name")))

        kwargs["alias"] = kwargs["name"] if not kwargs.get(
            "alias") else kwargs["alias"]

        kwargs["unique_id"] = unique_key.id
        ci_type = CIType.create(**kwargs)

        CITypeAttributeManager.add(ci_type.id, [unique_key.id],
                                   is_required=True)

        CITypeCache.clean(ci_type.name)

        return ci_type.id
Example #8
0
def init_acl():
    _app = AppCache.get('cmdb') or App.create(name='cmdb')
    app_id = _app.id

    # 1. add resource type
    for resource_type in ResourceTypeEnum.all():
        try:
            ResourceTypeCRUD.add(app_id, resource_type, '', PermEnum.all())
        except AbortException:
            pass

    # 2. add role
    try:
        RoleCRUD.add_role(RoleEnum.CONFIG, app_id, True)
    except AbortException:
        pass
    try:
        RoleCRUD.add_role(RoleEnum.CMDB_READ_ALL, app_id, False)
    except AbortException:
        pass

    # 3. add resource and grant
    ci_types = CIType.get_by(to_dict=False)
    type_id = ResourceType.get_by(name=ResourceTypeEnum.CI,
                                  first=True,
                                  to_dict=False).id
    for ci_type in ci_types:
        try:
            ResourceCRUD.add(ci_type.name, type_id, app_id)
        except AbortException:
            pass

        ACLManager().grant_resource_to_role(ci_type.name,
                                            RoleEnum.CMDB_READ_ALL,
                                            ResourceTypeEnum.CI,
                                            [PermEnum.READ])

    relation_views = PreferenceRelationView.get_by(to_dict=False)
    type_id = ResourceType.get_by(name=ResourceTypeEnum.RELATION_VIEW,
                                  first=True,
                                  to_dict=False).id
    for view in relation_views:
        try:
            ResourceCRUD.add(view.name, type_id, app_id)
        except AbortException:
            pass

        ACLManager().grant_resource_to_role(view.name, RoleEnum.CMDB_READ_ALL,
                                            ResourceTypeEnum.RELATION_VIEW,
                                            [PermEnum.READ])
Example #9
0
def init_ci_types(num=1):
    attrs = init_attributes(num)

    ci_types = []
    for i in range(num):
        ci_type = CIType.create(name=uuid.uuid4().hex[:8],
                                alias=uuid.uuid4().hex[:8],
                                unique_id=attrs[i].id)
        CITypeAttribute.create(
            type_id=ci_type.id,
            attr_id=attrs[i].id,
        )
        ci_types.append(ci_type)

    return ci_types
Example #10
0
    def get(cls, key):
        if key is None:
            return

        attrs = cache.get("CITypeAttribute::Name::{0}".format(key)) \
            or cache.get("CITypeAttribute::ID::{0}".format(key))
        if not attrs:
            attrs = CITypeAttribute.get_by(type_id=key, to_dict=False)
            if not attrs:
                ci_type = CIType.get_by(name=key, first=True, to_dict=False)
                if ci_type is not None:
                    attrs = CITypeAttribute.get_by(type_id=ci_type.id,
                                                   to_dict=False)
            if attrs is not None:
                cls.set(key, attrs)
        return attrs
Example #11
0
    def get(cls, key):
        if key is None:
            return

        attrs = cache.get(cls.PREFIX_NAME.format(key))
        attrs = attrs or cache.get(cls.PREFIX_ID.format(key))
        if not attrs:
            attrs = CITypeAttribute.get_by(type_id=key, to_dict=False)
            if not attrs:
                ci_type = CIType.get_by(name=key, first=True, to_dict=False)
                if ci_type is not None:
                    attrs = CITypeAttribute.get_by(type_id=ci_type.id,
                                                   to_dict=False)
            if attrs is not None:
                cls.set(key, attrs)
        return attrs
Example #12
0
def test_update_ci_type(session, client):
    ci_type_ins = init_ci_types(1)[0]

    url = "/api/v0.1/ci_types/" + str(ci_type_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["type_id"] == ci_type_ins.id

    # check ci_type updated in database
    ci_type_ins = CIType.get_by_id(ci_type_ins.id)
    assert ci_type_ins.name == "update"
Example #13
0
def test_create_ci_type(session, client):
    attr = init_attributes(1)[0]

    url = "/api/v0.1/ci_types"
    payload = {
        "name": "test",
        "alias": "测试",
        "unique_key": attr.id
    }

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

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

    # check there is a attribute in database
    type_id = resp.json["type_id"]
    ci_type_ins = CIType.get_by_id(type_id)
    assert ci_type_ins.id == type_id
    assert ci_type_ins.name == "test"
    assert ci_type_ins.alias == "测试"
    assert ci_type_ins.unique_id == attr.id
Example #14
0
    def check_is_existed(key):
        ci_type = CITypeCache.get(key) or abort(
            404, "CIType <{0}> is not existed".format(key))

        return CIType.get_by_id(ci_type.id)