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
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
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
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
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))
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
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])
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
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