Esempio n. 1
0
def test_cmdb_object_type_init(type_instance_class):
    from cmdb.framework.cmdb_dao import RequiredInitKeyNotFoundError
    with pytest.raises(RequiredInitKeyNotFoundError):
        CmdbType(name='example', active=True, author_id=1, creation_time=datetime.utcnow(),
                 render_meta={}, fields=[])
    type_instance = CmdbType(name='example', active=True, author_id=1, creation_time=datetime.utcnow(),
                             render_meta={}, fields=[], public_id=1)
    assert isinstance(type_instance, type_instance_class)
Esempio n. 2
0
 def test_update_type(self, object_manager):
     _render_data = {
         'summary': [],
         'external': [],
         'sections': []
     }
     type_instance_update_1 = CmdbType(name='exampleX', active=True, author_id=1, creation_time=datetime.utcnow(),
                                       render_meta=_render_data, fields=[], public_id=1)
     object_manager.update_type(type_instance_update_1)
     type_get_instance = object_manager.get_type(public_id=1)
     assert type_get_instance.get_name() == type_instance_update_1.get_name()
Esempio n. 3
0
def test_cmdb_object_type_calls():
    _render_data = {
        'summary': [],
        'external': [],
        'sections': []
    }
    type_instance = CmdbType(name='example', active=True, author_id=1, creation_time=datetime.utcnow(),
                             render_meta=_render_data, fields=[], public_id=1)

    assert type_instance.has_externals() is False
    assert type_instance.has_summaries() is False
    assert type_instance.has_sections() is False
Esempio n. 4
0
def add_type(request_user: User):
    from bson import json_util
    from datetime import datetime
    add_data_dump = json.dumps(request.json)
    try:
        new_type_data = json.loads(add_data_dump,
                                   object_hook=json_util.object_hook)
        new_type_data['public_id'] = object_manager.get_new_id(
            CmdbType.COLLECTION)
        new_type_data['creation_time'] = datetime.utcnow()
    except TypeError as e:
        LOGGER.warning(e)
        return abort(400)
    try:
        type_instance = CmdbType(**new_type_data)
    except CMDBError as e:
        LOGGER.debug(e)
        return abort(400)
    try:
        ack = object_manager.insert_type(type_instance)
    except TypeInsertError:
        return abort(500)

    resp = make_response(ack)
    return resp
Esempio n. 5
0
 def get_types_by(self, sort='public_id', **requirements):
     ack = []
     objects = self._get_many(collection=CmdbType.COLLECTION,
                              sort=sort,
                              **requirements)
     for data in objects:
         ack.append(CmdbType(**data))
     return ack
Esempio n. 6
0
    def update_type(self, data: (CmdbType, dict)):
        if isinstance(data, CmdbType):
            update_type = data
        elif isinstance(data, dict):
            update_type = CmdbType(**data)
        else:
            raise WrongInputFormatError(CmdbType, data,
                                        "Possible data: dict or CmdbType")

        ack = self._update(collection=CmdbType.COLLECTION,
                           public_id=update_type.get_public_id(),
                           data=update_type.to_database())
        if self._event_queue:
            event = Event("cmdb.core.objecttype.updated",
                          {"id": update_type.get_public_id()})
            self._event_queue.put(event)
        return ack
Esempio n. 7
0
 def get_types_by(self, sort='public_id', **requirements):
     try:
         return [
             CmdbType(**data) for data in self._get_many(
                 collection=CmdbType.COLLECTION, sort=sort, **requirements)
         ]
     except Exception as err:
         raise ObjectManagerGetError(err=err)
Esempio n. 8
0
 def get_type(self, public_id: int):
     try:
         return CmdbType(**self.dbm.find_one(collection=CmdbType.COLLECTION,
                                             public_id=public_id))
     except RequiredInitKeyNotFoundError as err:
         raise ObjectManagerInitError(err=err.message)
     except Exception as err:
         raise ObjectManagerGetError(err=err)
Esempio n. 9
0
 def get_all_types(self) -> List[CmdbType]:
     try:
         raw_types: List[dict] = self._get_many(
             collection=CmdbType.COLLECTION)
     except Exception as err:
         raise ObjectManagerGetError(err=err)
     try:
         return [CmdbType(**type) for type in raw_types]
     except Exception as err:
         raise ObjectManagerInitError(err=err)
Esempio n. 10
0
 def get_type_by(self, **requirements) -> CmdbType:
     try:
         found_type_list = self._get_many(collection=CmdbType.COLLECTION,
                                          limit=1,
                                          **requirements)
         if len(found_type_list) > 0:
             return CmdbType(**found_type_list[0])
         else:
             raise ObjectManagerGetError(
                 err='More than 1 type matches this requirement')
     except (CMDBError, Exception) as e:
         raise ObjectManagerGetError(err=e)
Esempio n. 11
0
def update_type(request_user: User):
    from bson import json_util
    add_data_dump = json.dumps(request.json)
    try:
        new_type_data = json.loads(add_data_dump,
                                   object_hook=json_util.object_hook)
    except TypeError as e:
        LOGGER.warning(e)
        abort(400)
    try:
        update_type_instance = CmdbType(**new_type_data)
    except CMDBError:
        return abort(400)
    try:
        old_fields = object_manager.get_type(
            update_type_instance.get_public_id()).get_fields()
        new_fields = update_type_instance.get_fields()
        if [item for item in old_fields if item not in new_fields]:
            update_type_instance.clean_db = False
        if [item for item in new_fields if item not in old_fields]:
            update_type_instance.clean_db = False
    except CMDBError:
        return abort(500)
    try:
        object_manager.update_type(update_type_instance)
    except CMDBError:
        return abort(500)

    resp = make_response(update_type_instance)
    return resp
Esempio n. 12
0
    def test_add_type(self, object_manager):
        _render_data = {
            'summary': [],
            'external': [],
            'sections': []
        }
        type_instance_new_1 = CmdbType(name='example2', active=True, author_id=1, creation_time=datetime.utcnow(),
                                       render_meta=_render_data, fields=[], public_id=2)
        # CmdbObjectType insert
        ack = object_manager.insert_type(type_instance_new_1)
        assert ack == 2
        type_instance_new_2 = object_manager.get_type(public_id=2)
        assert type_instance_new_2.get_name() == 'example2'

        # dict insert
        type_instance_new_3 = CmdbType(name='example3', active=True, author_id=1, creation_time=datetime.utcnow(),
                                       render_meta=_render_data, fields=[], public_id=3)
        object_manager.insert_type(type_instance_new_3.__dict__)
        type_instance_new_3 = object_manager.get_type(public_id=3)
        assert type_instance_new_3.get_name() == 'example3'

        # insert error test
        type_instance_new_4 = CmdbType(name='example3', active=True, author_id=1, creation_time=datetime.utcnow(),
                                       render_meta=_render_data, fields=[],
                                       public_id=type_instance_new_3.get_public_id())
        with pytest.raises(TypeAlreadyExists):
            object_manager.insert_type(type_instance_new_4)
Esempio n. 13
0
    def get_type_aggregate(self, arguments):
        """This method does not actually
           performs the find() operation
           but instead returns
           a objects sorted by value of the documents that meet the selection criteria.

           Args:
               arguments: query search for
           Returns:
               returns the list of CMDB Types sorted by value of the documents
           """
        type_list = []
        cursor = self.dbm.aggregate(CmdbType.COLLECTION, arguments)
        for document in cursor:
            put_data = json.loads(json_util.dumps(document),
                                  object_hook=object_hook)
            type_list.append(CmdbType(**put_data))
        return type_list
Esempio n. 14
0
 def insert_type(self, data: (CmdbType, dict)):
     if isinstance(data, CmdbType):
         new_type = data
     elif isinstance(data, dict):
         new_type = CmdbType(**data)
     else:
         raise WrongInputFormatError(CmdbType, data,
                                     "Possible data: dict or CmdbType")
     try:
         ack = self._insert(collection=CmdbType.COLLECTION,
                            data=new_type.to_database())
         LOGGER.debug(f"Inserted new type with ack {ack}")
         if self._event_queue:
             event = Event("cmdb.core.objecttype.added",
                           {"id": new_type.get_public_id()})
             self._event_queue.put(event)
     except PublicIDAlreadyExists:
         raise TypeAlreadyExists(type_id=new_type.get_public_id())
     except (CMDBError, InsertError):
         raise TypeInsertError(new_type.get_public_id())
     return ack
Esempio n. 15
0
 def get_type(self, public_id: int):
     try:
         return CmdbType(**self.dbm.find_one(collection=CmdbType.COLLECTION,
                                             public_id=public_id))
     except (CMDBError, Exception) as e:
         raise ObjectManagerGetError(err=e)
Esempio n. 16
0
 def get_all_types(self):
     ack = []
     types = self._get_many(collection=CmdbType.COLLECTION)
     for type_obj in types:
         ack.append(CmdbType(**type_obj))
     return ack
Esempio n. 17
0
    def generate_types(self) -> list:
        type_list = list()
        generation_date = self._faker.date_time_between(start_date="-100d",
                                                        end_date="-30d")

        type_list.append(
            CmdbType(
                **{
                    "public_id":
                    1,
                    "label":
                    "Leased Lines",
                    "name":
                    "leased_lines",
                    "description":
                    "A leased line is a private bidirectional or symmetric telecommunications circuit "
                    "between two or more locations provided according to a commercial contract.",
                    "version":
                    "1.0.0",
                    "active":
                    True,
                    "clean_db":
                    True,
                    "access": {
                        "groups": "",
                        "users": ""
                    },
                    "category_id":
                    2,
                    "author_id":
                    1,
                    "creation_time":
                    generation_date,
                    "render_meta": {
                        "icon":
                        "fas fa-cube",
                        "external": [],
                        "summary": {
                            "fields":
                            ["state", "product_name", "transfer_rate"]
                        },
                        "sections": [
                            {
                                "type": "section",
                                "name": "connection state",
                                "label": "Connection State",
                                "fields": [
                                    "state",
                                ]
                            },
                            {
                                "type": "section",
                                "name": "linedetails",
                                "label": "Linedetails",
                                "fields": ["product_name", "transfer_rate"]
                            },
                            {
                                "type":
                                "section",
                                "name":
                                "location_a",
                                "label":
                                "Location A",
                                "fields": [
                                    "company_name_a", "street_a", "zip_a",
                                    "city_a", "location_details_a"
                                ]
                            },
                            {
                                "type":
                                "section",
                                "name":
                                "location_b",
                                "label":
                                "Location B",
                                "fields": [
                                    "company_name_b", "street_b", "zip_b",
                                    "city_b", "location_details_b"
                                ]
                            },
                        ]
                    },
                    "fields": [
                        {
                            "label": "State",
                            "name": "state",
                            "type": "checkbox"
                        },
                        {
                            "label": "Product Name",
                            "name": "product_name",
                            "type": "text"
                        },
                        {
                            "label": "Transfer Rate",
                            "name": "transfer_rate",
                            "type": "text"
                        },
                        {
                            "label": "Company Name A",
                            "name": "company_name_a",
                            "type": "text"
                        },
                        {
                            "label": "Street A",
                            "name": "street_a",
                            "type": "text"
                        },
                        {
                            "label": "ZIP A",
                            "required": True,
                            "name": "zip_a",
                            "type": "text"
                        },
                        {
                            "label": "City A",
                            "name": "city_a",
                            "type": "text"
                        },
                        {
                            "label": "Location Details A",
                            "name": "location_details_a",
                            "type": "text"
                        },
                        {
                            "label": "Company Name B",
                            "name": "company_name_b",
                            "type": "text"
                        },
                        {
                            "label": "Street B",
                            "name": "street_b",
                            "type": "text"
                        },
                        {
                            "label": "ZIP B",
                            "required": True,
                            "name": "zip_b",
                            "type": "text"
                        },
                        {
                            "label": "City B",
                            "name": "city_b",
                            "type": "text"
                        },
                        {
                            "label": "Location Details B",
                            "name": "location_details_b",
                            "type": "text"
                        },
                    ],
                }))
        type_list.append(
            CmdbType(
                **{
                    "public_id":
                    2,
                    "title":
                    "Switch",
                    "name":
                    "switch",
                    "description":
                    "A switch (also called switching hub, bridging hub, officially MAC bridge) "
                    "is a computer networking device that connects devices on a computer network "
                    "by using packet switching to receive, process, "
                    "and forward data to the destination device. ",
                    "version":
                    "1.0.0",
                    "active":
                    True,
                    "clean_db":
                    True,
                    "access": {
                        "groups": "",
                        "users": ""
                    },
                    "author_id":
                    1,
                    "category_id":
                    3,
                    "creation_time":
                    generation_date,
                    "render_meta": {
                        "icon":
                        "fas fa-cube",
                        "external": [],
                        "summary": {
                            "fields": ["management_ip", "hostname"]
                        },
                        "sections":
                        [{
                            "type":
                            "section",
                            "name":
                            "management",
                            "label":
                            "Management",
                            "fields": [
                                "management_ip", "hostname", "monitoring",
                                "os", "username", "password"
                            ]
                        }, {
                            "type": "section",
                            "name": "location",
                            "label": "Location",
                            "fields": [
                                "address",
                                "building",
                                "room",
                                "rack",
                            ]
                        }, {
                            "type":
                            "section",
                            "name":
                            "hardware",
                            "label":
                            "Hardware",
                            "fields": [
                                "manufacturer", "supplier", "model",
                                "serial_number", "software_version"
                            ]
                        }]
                    },
                    "fields": [
                        {
                            "label": "Management IP",
                            "name": "management_ip",
                            "type": "text"
                        },
                        {
                            "label": "Hostname",
                            "name": "hostname",
                            "type": "text"
                        },
                        {
                            "label": "Monitoring",
                            "name": "monitoring",
                            "type": "checkbox"
                        },
                        {
                            "label": "Operating System",
                            "name": "os",
                            "type": "text"
                        },
                        {
                            "label": "Username",
                            "name": "username",
                            "type": "text"
                        },
                        {
                            "label": "Password",
                            "name": "password",
                            "type": "password",
                            "required": True,
                        },
                        {
                            "label": "Address",
                            "name": "address",
                            "type": "text"
                        },
                        {
                            "label": "Building",
                            "name": "building",
                            "type": "text"
                        },
                        {
                            "label": "Room",
                            "name": "room",
                            "type": "text"
                        },
                        {
                            "label": "Rack",
                            "name": "rack",
                            "type": "text"
                        },
                        {
                            "label": "Manufacturer",
                            "name": "manufacturer",
                            "type": "text"
                        },
                        {
                            "label": "Supplier",
                            "name": "supplier",
                            "type": "text"
                        },
                        {
                            "label": "Model",
                            "name": "model",
                            "type": "text"
                        },
                        {
                            "label": "Serial Number",
                            "name": "serial_number",
                            "type": "text"
                        },
                        {
                            "label": "Software Version",
                            "name": "software_version",
                            "type": "text"
                        },
                    ]
                }))

        type_list.append(
            CmdbType(
                **{
                    "public_id":
                    3,
                    "title":
                    "Router",
                    "name":
                    "router",
                    "description":
                    "A router is a networking device that forwards data packets "
                    "between computer networks.",
                    "version":
                    "1.0.0",
                    "active":
                    True,
                    "clean_db":
                    True,
                    "author_id":
                    1,
                    "access": {
                        "groups": "",
                        "users": ""
                    },
                    "category_id":
                    3,
                    "creation_time":
                    generation_date,
                    "render_meta": {
                        "icon":
                        "fas fa-cube",
                        "external": [],
                        "summary": {
                            "fields": ["management_ip", "hostname"]
                        },
                        "sections":
                        [{
                            "type":
                            "section",
                            "name":
                            "management",
                            "label":
                            "Management",
                            "fields": [
                                "management_ip", "hostname", "monitoring",
                                "os", "username", "password"
                            ]
                        }, {
                            "type": "section",
                            "name": "location",
                            "label": "Location",
                            "fields": [
                                "address",
                                "building",
                                "room",
                                "rack",
                            ]
                        }, {
                            "type":
                            "section",
                            "name":
                            "hardware",
                            "label":
                            "Hardware",
                            "fields": [
                                "manufacturer", "supplier", "model",
                                "serial_number", "software_version"
                            ]
                        }]
                    },
                    "fields": [
                        {
                            "label": "Management IP",
                            "name": "management_ip",
                            "type": "text"
                        },
                        {
                            "label": "Hostname",
                            "name": "hostname",
                            "type": "text"
                        },
                        {
                            "label": "Monitoring",
                            "name": "monitoring",
                            "type": "checkbox"
                        },
                        {
                            "label": "Operating System",
                            "name": "os",
                            "type": "text"
                        },
                        {
                            "label": "Username",
                            "name": "username",
                            "type": "text"
                        },
                        {
                            "label": "Password",
                            "name": "password",
                            "type": "password",
                            "required": True,
                        },
                        {
                            "label": "Address",
                            "name": "address",
                            "type": "text"
                        },
                        {
                            "label": "Building",
                            "name": "building",
                            "type": "text"
                        },
                        {
                            "label": "Room",
                            "name": "room",
                            "type": "text"
                        },
                        {
                            "label": "Rack",
                            "name": "rack",
                            "type": "text"
                        },
                        {
                            "label": "Manufacturer",
                            "name": "manufacturer",
                            "type": "text"
                        },
                        {
                            "label": "Supplier",
                            "name": "supplier",
                            "type": "text"
                        },
                        {
                            "label": "Model",
                            "name": "model",
                            "type": "text"
                        },
                        {
                            "label": "Serial Number",
                            "name": "serial_number",
                            "type": "text"
                        },
                        {
                            "label": "Software Version",
                            "name": "software_version",
                            "type": "text"
                        },
                    ]
                }))
        type_list.append(
            CmdbType(
                **{
                    "public_id":
                    4,
                    "label":
                    "Locations",
                    "name":
                    "locations",
                    "description":
                    "Company building and locations",
                    "version":
                    "1.0.0",
                    "active":
                    True,
                    "clean_db":
                    True,
                    "author_id":
                    1,
                    "access": {
                        "groups": "",
                        "users": ""
                    },
                    "category_id":
                    0,
                    "creation_time":
                    generation_date,
                    "render_meta": {
                        "icon":
                        "fas fa-cube",
                        "external": [],
                        "summary": {
                            "fields": ["street", "city"]
                        },
                        "sections": [{
                            "type":
                            "section",
                            "name":
                            "location_details",
                            "label":
                            "Location-Details",
                            "fields": [
                                "naming", "description", "entrance",
                                "person_in_charge"
                            ]
                        }, {
                            "type":
                            "section",
                            "name":
                            "address",
                            "label":
                            "Address",
                            "fields":
                            ["street", "zip", "city", "map-lang", "map-long"]
                        }]
                    },
                    "fields": [{
                        "required": True,
                        "label": "Naming",
                        "description": "Short name of the location",
                        "placeholder": "Calling name",
                        "name": "naming",
                        "type": "text"
                    }, {
                        "label": "Description",
                        "name": "description",
                        "type": "textarea",
                    }, {
                        "label": "Entrance",
                        "name": "entrance",
                        "type": "text"
                    }, {
                        "label": "Person in Charge",
                        "name": "person_in_charge",
                        "type": "ref"
                    }, {
                        "label": "Street",
                        "name": "street",
                        "type": "text"
                    }, {
                        "label": "ZIP",
                        "required": True,
                        "name": "zip",
                        "type": "text"
                    }, {
                        "label": "Entrance",
                        "name": "city",
                        "type": "text"
                    }, {
                        "label": "Latitude",
                        "required": True,
                        "name": "map-lang",
                        "type": "text"
                    }, {
                        "label": "Longitude",
                        "required": True,
                        "name": "map-long",
                        "type": "text"
                    }]
                }))
        type_list.append(
            CmdbType(
                **{
                    "public_id":
                    5,
                    "label":
                    "Servers",
                    "name":
                    "servers",
                    "description":
                    "Server connections and hostname",
                    "version":
                    "1.0.0",
                    "active":
                    True,
                    "clean_db":
                    True,
                    "author_id":
                    1,
                    "access": {
                        "groups": "",
                        "users": ""
                    },
                    "category_id":
                    0,
                    "creation_time":
                    generation_date,
                    "render_meta": {
                        "icon":
                        "fas fa-cube",
                        "external": [{
                            "href": "ssh:/{}/",
                            "fields": ["ipv4"],
                            "label": "SSH",
                            "name": "ssh",
                            "icon": "fas fa-ethernet"
                        }],
                        "summary": {
                            "fields": ["hostname"]
                        },
                        "sections": [{
                            "type":
                            "section",
                            "name":
                            "management",
                            "label":
                            "Management",
                            "fields": [
                                "hostname",
                                "ipv4",
                                "ipv4_network_class",
                                "ipv4_intranet",
                                "ipv6",
                            ]
                        }]
                    },
                    "fields": [{
                        "required": True,
                        "label": "Hostname",
                        "name": "hostname",
                        "type": "text"
                    }, {
                        "required": True,
                        "label": "IPv4 Public",
                        "placeholder": "127.0.0.1",
                        "name": "ipv4",
                        "type": "text"
                    }, {
                        "label":
                        "IPv4 Network Class",
                        "placeholder":
                        "A",
                        "name":
                        "ipv4_network_class",
                        "description":
                        "An IPv4 address class is a categorical division of internet protocol "
                        "addresses in IPv4-based routing",
                        "type":
                        "text"
                    }, {
                        "label": "IPv4 Private",
                        "placeholder": "127.0.0.1",
                        "name": "ipv4_intranet",
                        "type": "text"
                    }, {
                        "label": "IPv6",
                        "placeholder": "[2001:0db8:85a3:08d3::0370:7344]",
                        "name": "ipv6",
                        "type": "text"
                    }]
                }))

        return type_list