def post(self): """ Creating a resource instance """ # =1= getting the resource to create from the json resource_json_serializer = json_serializer.resource_serializer() _resource = resource_json_serializer.from_json(self.req.body) # =2= add the created resource to the registry _user_id = 'user1' _location = '/' + self.term + '/' + _user_id + '/' + str(_resource.occi_core_id) location_registry().register_location(_location, _resource) # =3= execute the backend command by sending the resource object for _backend in backend_registry().get_backends().values(): _backend.create(_resource) # =4= return OK with the Location of the created resource location_result = json.dumps({"location": _location}) self.res.body = location_result return self.res
def put(self): """ Creating a resource instance by providing the path in the name-space hierarchy (To do) Or Updating a resource instance (Done) """ # =1= get old resources _old_resource = location_registry().get_object(self.req.path) # =2= get new resources resource_json_serializer = json_serializer.resource_serializer() _resource = resource_json_serializer.from_json(self.req.body) # =3= unregister old and register new location_registry().unregister_location(self.req.path) location_registry().register_location(self.req.path, _resource) # =4= execute the backend command by sending the resource object for _backend in backend_registry().get_backends().values(): _backend.update(_old_resource, _resource) self.res.body = 'Resource ' + self.req.path + ' has been updated' return self.res
def post(self): """ Creating a resource instance """ # =1= getting the resource to create from the json resource_json_serializer = json_serializer.resource_serializer() _resource = resource_json_serializer.from_json(self.req.body) # =2= add the created resource to the registry _user_id = 'user1' _location = '/' + self.term + '/' + _user_id + '/' + str( _resource.occi_core_id) location_registry().register_location(_location, _resource) # =3= execute the backend command by sending the resource object for _backend in backend_registry().get_backends().values(): _backend.create(_resource) # =4= return OK with the Location of the created resource location_result = json.dumps({"location": _location}) self.res.body = location_result return self.res
def run_server(self): """ to run the server """ print ("\n______________________________________________________________________________________\n" "The OCNI server is running at: ") wsgi.server(eventlet.listen((config.OCNI_IP, int(config.OCNI_PORT))), self.app) print ("\n______________________________________________________________________________________\n" "Closing correctly 'locations' and 'objects' Databases: ") location_registry(). close_locations_db() location_registry().close_objects_db()
def get(self): """ Retrieving a resource instance """ resource_json_serializer = json_serializer.resource_serializer() _object = location_registry().get_object(self.req.path) if _object is not None: result_json = json.loads(resource_json_serializer.to_json(_object)) result_dump = cStringIO.StringIO() json.dump(result_json, result_dump, indent=4 * ' ') self.res.body = result_dump.getvalue() #for _backend in backend_registry().get_backends().values(): # _backend.read() else: #self.res.status = '' self.res.body = 'No resource with this PATH' return self.res
def renderer(self, obj): header = {} link_value = '' link_param = '' if isinstance(obj, Link): _location_registry = location_registry() _location_of_obj = _location_registry.get_location(obj) _source = obj.occi_core_source _target_location = obj.occi_core_target _target_object = _location_registry.get_object(_target_location) logger.debug('the Target location of this link is: ' + _target_location) logger.debug('the Target object of this link is: ' + _target_object.__repr__()) link_value += '<' + _target_location + '>;\nrel="' + _target_object.kind.__repr__( ) + '";\nself="' + _location_of_obj + '";\n' link_param += 'category="' + obj.kind.__repr__() + '";' for attribute in obj.kind.attributes: link_param += '\n' + attribute.name + '="' + obj.__getattribute__( re.sub('\.', '_', attribute.name)) + '";' else: logger.warning("Object bad type: Only a Link can be rendered") raise ("Object bad type: Only a Link can be rendered") header[header_link] = link_value + link_param return header
def renderer(self, obj): header = {} link_value = '' link_param = '' if isinstance(obj, Link): _location_registry = location_registry() _location_of_obj = _location_registry.get_location(obj) _source = obj.occi_core_source _target_location = obj.occi_core_target _target_object = _location_registry.get_object(_target_location) logger.debug('the Target location of this link is: ' + _target_location) logger.debug('the Target object of this link is: ' + _target_object.__repr__()) link_value += '<' + _target_location + '>;\nrel="' + _target_object.kind.__repr__() + '";\nself="' + _location_of_obj + '";\n' link_param += 'category="' + obj.kind.__repr__() + '";' for attribute in obj.kind.attributes: link_param += '\n' + attribute.name + '="' + obj.__getattribute__( re.sub('\.', '_', attribute.name)) + '";' else: logger.warning("Object bad type: Only a Link can be rendered") raise ("Object bad type: Only a Link can be rendered") header[header_link] = link_value + link_param return header
def run_server(self): """ to run the server """ print( "\n______________________________________________________________________________________\n" "The OCNI server is running at: ") wsgi.server(eventlet.listen((config.OCNI_IP, int(config.OCNI_PORT))), self.app) print( "\n______________________________________________________________________________________\n" "Closing correctly 'locations' and 'objects' Databases: ") location_registry().close_locations_db() location_registry().close_objects_db()
def delete(self): """ Deleting a resource instance """ # =1= get resources _resource = location_registry().get_object(self.req.path) # =2= remove resources location_registry().unregister_location(self.req.path) # =3= execute the backend command by sending the resource object for _backend in backend_registry().get_backends().values(): _backend.delete(_resource) # =4= return OK with the Location of the created resource self.res.body = 'Resource with this PATH has been removed' return self.res
def get(self): """ Retrieving All resource instances Below a Path """ _path = '/' + self.term + '/' + self.user + '/' res = location_registry().get_locations_under_path(_path) self.res.body = jsonpickle.encode(res, unpicklable=False) return self.res
def renderer(self, obj): header = {} category_value = '' category_param = '' if isinstance(obj, Kind): _category = obj _classe = 'kind' elif isinstance(obj, Mixin): _category = obj _classe = 'mixin' elif isinstance(obj, Action): _category = obj.category _classe = 'action' else: logger.warning( "Object bad type: Only a kind, mixin or an action can be rendered as a category" ) raise ( "Object bad type: Only a kind, mixin or an action can be rendered as a category" ) category_value += _category.term + ';\nscheme="' + _category.scheme + '";\nclass="' + _classe + '";\n' if _category.title != '': category_param += 'title="' + _category.title + '";\n' if _category.related.__len__() > 0: __related_objects = '' for rel in _category.related: __related_objects += rel.__repr__() + " " category_param += 'rel="' + __related_objects + '";\n' _location_registry = location_registry() _location = _location_registry.get_location(_category) category_param += 'location=' + _location + ';\n' if _classe == 'kind' or _classe == 'mixin': __attributes = '' for __attribute in _category.attributes: __attributes += __attribute.name + ' ' category_param += 'attributes="' + __attributes + '";\n' if _classe == 'kind' or _classe == 'mixin': __actions = '' for __action in _category.actions: __actions += __action.__repr__() + ' ' category_param += 'actions="' + __actions + '";' header[header_category] = category_value + category_param return header
def to_json(self, obj): """ method to convert an object (OCCI object) to a JSON format """ if isinstance(obj, Kind): _category = obj _classe = 'kind' elif isinstance(obj, Mixin): _category = obj _classe = 'mixin' elif isinstance(obj, Action): _category = obj.category _classe = 'action' else: logger.warning("Object bad type: Only a kind, mixin or an action can be rendered as a category") raise ("Object bad type: Only a kind, mixin or an action can be rendered as a category") category_dict = {'term': _category.term, 'scheme': _category.scheme, 'class': _classe, 'title': _category.title} self.category_rel_dict = OrderedDict({'rel': []}) if _category.related.__len__() > 0: category_rel_values = list() for rel in _category.related: category_rel_values.append(rel.__repr__()) self.category_rel_dict = {'rel': category_rel_values} self._location_registry = location_registry() _location = self._location_registry.get_location(_category) self.category_location_dict = OrderedDict({'location': _location}) self.category_attributes_dict = OrderedDict({'attributes': []}) if _classe == 'kind' or _classe == 'mixin': category_attributes_values = list() for __attribute in _category.attributes: category_attributes_values.append(__attribute.name) self.category_attributes_dict = {'attributes': category_attributes_values} self.category_actions_dict = OrderedDict({'actions': []}) if _classe == 'kind' or _classe == 'mixin': category_actions_values = list() for __action in _category.actions: category_actions_values.append(__action.__repr__()) self.category_actions_dict = {'actions': category_actions_values} result_json = OrderedDict( category_dict.items() + self.category_rel_dict.items() + self.category_location_dict.items() + self.category_attributes_dict.items() + self.category_actions_dict.items()) result_dump = cStringIO.StringIO() json.dump(result_json, result_dump, indent=4 * ' ') logger.debug('category_serializer_to_json-serialization=' + result_dump.getvalue()) return result_dump.getvalue()
def renderer(self, obj): header = {} category_value = '' category_param = '' if isinstance(obj, Kind): _category = obj _classe = 'kind' elif isinstance(obj, Mixin): _category = obj _classe = 'mixin' elif isinstance(obj, Action): _category = obj.category _classe = 'action' else: logger.warning("Object bad type: Only a kind, mixin or an action can be rendered as a category") raise ("Object bad type: Only a kind, mixin or an action can be rendered as a category") category_value += _category.term + ';\nscheme="' + _category.scheme + '";\nclass="' + _classe + '";\n' if _category.title != '': category_param += 'title="' + _category.title + '";\n' if _category.related.__len__() > 0: __related_objects = '' for rel in _category.related: __related_objects += rel.__repr__() + " " category_param += 'rel="' + __related_objects + '";\n' _location_registry = location_registry() _location = _location_registry.get_location(_category) category_param += 'location=' + _location + ';\n' if _classe == 'kind' or _classe == 'mixin': __attributes = '' for __attribute in _category.attributes: __attributes += __attribute.name + ' ' category_param += 'attributes="' + __attributes + '";\n' if _classe == 'kind' or _classe == 'mixin': __actions = '' for __action in _category.actions: __actions += __action.__repr__() + ' ' category_param += 'actions="' + __actions + '";' header[header_category] = category_value + category_param return header
def renderer(self, obj, action): header = {} link_value = '' if isinstance(obj, Resource): _location_registry = location_registry() _location_of_obj = _location_registry.get_location(obj) _location_of_action = _location_of_obj + '?action=' + action.category.term link_value += '<' + _location_of_action + '>;\nrel="' + action.__repr__() + '"' else: logger.warning("Object bad type: Only a Resource can be rendered") raise ("Object bad type: Only a Resource can be rendered") header[header_link] = link_value return header
def renderer(self, obj, action): header = {} link_value = '' if isinstance(obj, Resource): _location_registry = location_registry() _location_of_obj = _location_registry.get_location(obj) _location_of_action = _location_of_obj + '?action=' + action.category.term link_value += '<' + _location_of_action + '>;\nrel="' + action.__repr__( ) + '"' else: logger.warning("Object bad type: Only a Resource can be rendered") raise ("Object bad type: Only a Resource can be rendered") header[header_link] = link_value return header
def get(self): """ Retrieving a resource instance """ resource_json_serializer = json_serializer.resource_serializer() _object = location_registry().get_object(self.req.path) if _object is not None: result_json = json.loads(resource_json_serializer.to_json(_object)) result_dump = cStringIO.StringIO() json.dump(result_json, result_dump, indent=4 * ' ') self.res.body = result_dump.getvalue() else: #self.res.status = '' self.res.body = 'No resource with this PATH' return self.res
def to_json(self, obj): """ method to convert an object (OCCI object) to a JSON format """ self.entity_dict = OrderedDict({'occi.core.id': obj.occi_core_id, 'occi.core.title': obj.occi_core_title}) self.kind_dict = OrderedDict( {'kind': {'term': obj.kind.term, 'scheme': obj.kind.scheme, 'class': 'kind', 'title': obj.kind.title}}) self.mixins_dict = OrderedDict({'mixins': []}) mixins_values = list() for __mixin in obj.mixins: mixins_values.append( {'term': __mixin.term, 'scheme': __mixin.scheme, 'class': 'mixin', 'title': __mixin.title}) self.mixins_dict = {'mixins': mixins_values} self.attributes_dict = OrderedDict({'attributes': ''}) attributes_values = OrderedDict() # to extract attributes defined directly by the Kind for __attribute in obj.kind.attributes: _a = obj.__getattribute__(re.sub('\.', '_', __attribute.name)) _b = jsonpickle.encode(_a, unpicklable=False) attributes_values[str(__attribute.name)] = jsonpickle.decode(_b) # to extract attributes defined by the related Kinds for __kind_related in obj.kind.related: # to do (not urgent): attributes of the related kind of the related kind (recursively) # we use this if because tha main attributes of Resource and Link should be outside of Attributes bloc if __kind_related.__repr__() != Resource._kind.__repr__() and __kind_related.__repr__() != Link._kind.__repr__(): for __attribute in category_registry().get_kind(str(__kind_related)).attributes: _a = obj.__getattribute__(re.sub('\.', '_', __attribute.name)) _b = jsonpickle.encode(_a, unpicklable=False) attributes_values[str(__attribute.name)] = jsonpickle.decode(_b) # to extract attributes defined by the mixins and related mixins of each mixin for __mixin in obj.mixins: for __attribute in __mixin.attributes: _cc = obj.__getattribute__(re.sub('\.', '_', __attribute.name)) _d = jsonpickle.encode(_cc, unpicklable=False) attributes_values[str(__attribute.name)] = jsonpickle.decode(_d) for __mixin_related in __mixin.related: # to do (not urgent): attributes of the related mixin of the related mixin (recursively) for __attribute in category_registry().get_kind(str(__mixin_related)).attributes: _e = obj.__getattribute__(re.sub('\.', '_', __attribute.name)) _f = jsonpickle.encode(_e, unpicklable=False) attributes_values[str(__attribute.name)] = jsonpickle.decode(_f) self.attributes_dict = {'attributes': attributes_values} self.link_dict = OrderedDict( {'occi.core.source': obj.occi_core_source, 'occi.core.target': obj.occi_core_target}) self.location_dict = OrderedDict({'location': location_registry().get_location(obj)}) result_json = OrderedDict( self.entity_dict.items() + self.kind_dict.items() + self.mixins_dict.items() + self.attributes_dict.items() + self.link_dict.items() + self.location_dict.items()) result_dump = cStringIO.StringIO() json.dump(result_json, result_dump, indent=4 * ' ') logger.debug('link_serialization_to_json=' + result_dump.getvalue()) return result_dump.getvalue()
category_registry().register_kind(Compute._kind) category_registry().register_kind(Network._kind) category_registry().register_kind(Storage._kind) category_registry().register_kind(NetworkInterface._kind) category_registry().register_kind(StorageLink._kind) # register OCCI Infrastructure mixins category_registry().register_mixin(IPNetworking()) category_registry().register_mixin(IPNetworkInterface()) # ====================================================================================== # the location registry # ====================================================================================== # register OCCI Core kind locations location_registry().register_location("/resource/", Resource._kind) location_registry().register_location("/link/", Link._kind) # register OCCI Infrastructure kind locations location_registry().register_location("/compute/", Compute._kind) location_registry().register_location("/network/", Network._kind) location_registry().register_location("/storage/", Storage._kind) location_registry().register_location("/networkinterface/", NetworkInterface._kind) location_registry().register_location("/storagelink/", StorageLink._kind) # register OCCI Infrastructure mixin locations location_registry().register_location("/ipnetworking/", IPNetworking()) location_registry().register_location("/ipnetworkinterface/", IPNetworkInterface()) #################### To test category_serializer to_json function ################################################# #ser = category_serializer()
'Gone': 410, 'Unsupported Media Type': 415, 'Internal Server Error': 500, 'Not Implemented': 501, 'Service Unavailable': 503 } # ====================================================================================== # Reinialization of the locations registry and objects registry (clear ZODB) # ====================================================================================== result = shell_ask.query_yes_no_quit( " \n_______________________________________________________________\n" " Do you want to purge 'locations' and 'objects' Databases (DB reinialization)?", "no") if result == 'yes': location_registry().purge_locations_db() location_registry().purge_objects_db() # ====================================================================================== # the category registry # ====================================================================================== #category_registry = category_registry() # register OCCI Core kinds #category_registry().register_kind(Entity._kind) #category_registry().register_kind(Resource._kind) #category_registry().register_kind(Link._kind) # register OCCI Infrastructure kinds category_registry().register_kind(Compute._kind)
'Unauthorized': 401, 'Forbidden': 403, 'Method Not Allowed': 405, 'Conflict': 409, 'Gone': 410, 'Unsupported Media Type': 415, 'Internal Server Error': 500, 'Not Implemented': 501, 'Service Unavailable': 503} # ====================================================================================== # Reinialization of the locations registry and objects registry (clear ZODB) # ====================================================================================== if config.OCNI_PURGE_DB == "1": location_registry().purge_locations_db() location_registry().purge_objects_db() # ====================================================================================== # the category registry # ====================================================================================== #category_registry = category_registry() # register OCCI Core kinds #category_registry().register_kind(Entity._kind) #category_registry().register_kind(Resource._kind) #category_registry().register_kind(Link._kind) # register OCCI Infrastructure kinds