def appendResourceToCollection(self, coll_id, res_id):
        """Add resource to collection"""

        self.db_conn.connect()
        raw_collection = self.db_conn.get_entity(CommonDatabaseAPI.ET_COLLECTION, coll_id)
        collection = Collection(raw_collection)

        resource = ResourceOperationalAPI()
        res = resource.getResourceInfo(res_id)

        collection.append_resource(res)
        self.db_conn.save_entity(CommonDatabaseAPI.ET_COLLECTION, collection.to_dict())
        self.db_conn.close()
    def removeResourceFromCollection(self, coll_id, res_id):
        """Delete resource from collection"""

        self.db_conn.connect()
        raw_collection = self.db_conn.get_entity(CommonDatabaseAPI.ET_COLLECTION, coll_id)
        collection = Collection(raw_collection)

        resource = ResourceOperationalAPI()
        res = resource.getResourceInfo(res_id)

        collection.remove_resource(res)
        self.db_conn.save_entity(CommonDatabaseAPI.ET_COLLECTION, collection.to_dict())
        self.db_conn.close()
Beispiel #3
0
    def get_resource_params(self, request):
        """ Implement GET method for get resource info """
        if request.GET.get('elem_id', None):
            res_id = request.GET['elem_id']
            resource = ResourceOperationalAPI()

            res_info = resource.getResourceInfo(res_id).to_dict()
            res_spec = self._search_spec(ELEM_TYPES['IS_RES'], res_info['specification_name'])[0]

            # find allowed collections for resource
            spec_filter = {'allowed_types': str(res_info['specification_name'])}
            raw_spec_list = self.specification.findSpecification(ELEM_TYPES['IS_COLL'], spec_filter)
            allowed_spec_coll_list = []
            if raw_spec_list:
                for spec in raw_spec_list:
                    spec_dict = spec.to_dict()
                    allowed_spec_coll_list.append(spec_dict['type_name'])

            # find created collections and fill 'Allowed collection' on web page
            collection = CollectionOperationalAPI()
            raw_allow_coll_list = collection.findCollections({'specification_name__in': allowed_spec_coll_list})
            allow_coll_list = []
            for coll in raw_allow_coll_list:
                coll_dict = coll.to_dict()
                # change key for display in collection on web page
                time_dict = dict()
                time_dict['value'] = coll_dict.pop('_id')
                time_dict['content'] = coll_dict.pop('specification_name')
                allow_coll_list.append(time_dict)

            # find assigned resource in collections and fill 'Assigned collection' on web page
            raw_coll_list = collection.findCollections({'resources': res_id})
            assigned_coll_list = []
            for coll in raw_coll_list:
                coll_dict = coll.to_dict()
                assigned_coll_list.append(coll_dict['_id'])

            # for treeMenu. replace keys :( TODO for refactoring
            spec_params_json = json.dumps(res_spec['params_spec'])
            to_text = spec_params_json.replace('param_name','text')
            items = to_text.replace('children_spec','items')

            json_data = {'res_spec': res_spec['params_spec'],
                         'res_info': res_info,
                         'items': json.loads(items),
                         'allowed_coll': allow_coll_list,
                         'assigned_coll': assigned_coll_list}
            time.sleep(0.1)
            return HttpResponse(json.dumps(json_data), mimetype="application/json")

        return HttpResponse()
    def _search_elem(self, resource_filter, element):
        obj_list = []
        if isinstance(element, ConnectionOperationalAPI):
            obj_list = element.findConnection(resource_filter)

        resource = ResourceOperationalAPI()
        specification = ManagementAPI()

        elems_list = []
        for item in obj_list:
            s = item.to_dict()
            #print s
            spec = specification.findSpecification('connection', {'type_name': s['specification_name']})[0].to_dict()
            s['id'] = s.pop('_id')
            s['connected_res_name'] = resource.getResourceInfo(s['connected_resource']).to_dict()['resource_name']
            s['connecting_res_name'] = resource.getResourceInfo(s['connecting_resource']).to_dict()['resource_name']
            s['connecting_type'] = spec['connecting_type']
            s['connected_type'] = spec['connected_type']

            elems_list.append(s)
        #print elems_list
        return elems_list
    def connectResources(self, conn_type, connecting_res_id, connected_res_id, conn_desc=None, **add_params):
        """ Connecting resource by connect type """
        filter_conn = {'specification_name': conn_type,
                       'connecting_resource': connecting_res_id,
                       'connected_resource': connected_res_id}

        if connecting_res_id == connected_res_id:
            raise BIException('Connection between similar resources is not allowed!')
        elif self.findConnection(filter_conn):
            raise BIException('Connection between resources already exist!')

        connection = Connection(specification_name=conn_type, connecting_resource=connecting_res_id,
                                connected_resource=connected_res_id, description=conn_desc,
                                additional_parameters=add_params)

        res = ResourceOperationalAPI()
        connecting_res = res.getResourceInfo(connecting_res_id)
        connected_res = res.getResourceInfo(connected_res_id)

        if connecting_res.to_dict() and connected_res.to_dict():
            spec = ManagementAPI()

            connecting_res_d = connecting_res.to_dict()
            connected_res_d = connected_res.to_dict()
            connecting_res_spec = spec.findSpecification(self.RESOURCE,
                                                         {'type_name': connecting_res_d['specification_name'],
                                                          'spec_type': self.RESOURCE})[0]
            connected_res_spec = spec.findSpecification(self.RESOURCE, {'type_name': connected_res_d['specification_name'],
                                                                          'spec_type': self.RESOURCE})[0]
            Resource.setup_specification([ResourceSpecification(connecting_res_spec.to_dict())])
            Resource.setup_specification([ResourceSpecification(connected_res_spec.to_dict())])

            connection.connect(connecting_res, connected_res)

            self.db_conn.connect()
            self.db_conn.save_entity(self.CONNECTION, connection.to_dict())
            self.db_conn.close()
Beispiel #6
0
    def get_connection_by_type(self, request):
        """ Get connection type for resource """
        if request.is_ajax():
            res_id = request.GET.get('elem_id', None)
            conn_type = request.GET.get('conn_id', None)

            connection = ConnectionOperationalAPI()
            resource = ResourceOperationalAPI()
            connected_res = connection.getLinkedResources(res_id, conn_type=conn_type, conn_direction=CONNECTED)
            connecting_res = connection.getLinkedResources(res_id, conn_type=conn_type, conn_direction=CONNECTING)

            connected_list = []
            for tt in connecting_res:
                conn_res_id = tt.to_dict()['connected_resource']
                res_info = resource.getResourceInfo(conn_res_id).to_dict()
                connected_list.append(res_info['resource_name'])

            connecting_list = []
            for tt in connected_res:
                conn_res_id = tt.to_dict()['connecting_resource']
                res_info = resource.getResourceInfo(conn_res_id).to_dict()
                connecting_list.append(res_info['resource_name'])
            return HttpResponse(json.dumps({'connecting_list': connecting_list,
                                            'connected_list': connected_list}), mimetype="application/json")
Beispiel #7
0
    def save_element(self, request):
        """ Saving and updating elements """
        elem_type = request.POST.get('res_name',)
        elem_desc = request.POST.get('elem_desc',) # must be elem_desc
        elem_id = request.POST.get('elem_id',)

        res_status = request.POST.get('res_status',)
        res_name = request.POST.get('res_name1',)
        res_sys = request.POST.get('res_sys',)
        res_loc = request.POST.get('res_loc',)
        res_dep = request.POST.get('res_dep',)
        res_own = request.POST.get('res_own',)
        res_type = request.POST.get('res_type',)

        raw_param_res = request.POST.get('res_param',)

        old_assigned_to_coll = request.POST.get('old_assigned_to_coll',)
        new_assigned_to_coll = request.POST.get('new_assigned_to_coll',)
        if old_assigned_to_coll:
            old_assigned_to_coll = json.loads(old_assigned_to_coll)
        if new_assigned_to_coll:
            new_assigned_to_coll = json.loads(new_assigned_to_coll)

        # resource
        if res_type == ELEM_TYPES['IS_RES']:
            resource = ResourceOperationalAPI()

            spec = self._search_spec(ELEM_TYPES['IS_RES'], elem_type)[0]
            Resource.setup_specification([ResourceSpecification(spec)])

            if elem_id: # UPDATE
                self._update_collections(old_assigned_to_coll, new_assigned_to_coll, elem_id)
                resource.updateResource(elem_id, res_name, res_status, elem_desc, res_sys, res_loc, res_dep, res_own,
                                        **json.loads(raw_param_res))
            else: # CREATE
                resource.createResource(elem_type, res_name, res_status, elem_desc, res_sys, res_loc, res_dep, res_own,
                                        **json.loads(raw_param_res))
            return HttpResponse()

        # collections
        if res_type == ELEM_TYPES['IS_COLL']:
            collection = CollectionOperationalAPI()
            coll_spec = self._search_spec(ELEM_TYPES['IS_COLL'], elem_type)[0]
            Collection.setup_specification([CollectionSpecification(coll_spec)])

            if elem_id:
                collection.updateCollectionInfo(elem_id, elem_desc, additional_parameters=json.loads(raw_param_res))
            else:
                collection.createCollection(elem_type, elem_desc, additional_parameters=json.loads(raw_param_res))

            return HttpResponse()

        # connections
        if res_type == ELEM_TYPES['IS_CONN']:
            connection = ConnectionOperationalAPI()

            connecting_res_id = request.POST.get('connecting_res_id',)
            connected_res_id = request.POST.get('connected_res_id',)

            conn_spec = self._search_spec(ELEM_TYPES['IS_CONN'], elem_type)[0]
            Connection.setup_specification([ConnectionSpecification(conn_spec)])

            if elem_id:
                connection.updateConnection(elem_id, elem_desc, connecting_res_id, connected_res_id,
                                            additional_parameters=json.loads(raw_param_res))
            else:
                connection.connectResources(elem_type, connecting_res_id, connected_res_id, elem_desc,
                                            **json.loads(raw_param_res))

            return HttpResponse()