mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-06 07:11:13 +02:00
Create /hypervisor and /controller namespace
This commit is contained in:
231 files changed
+1576
-1480
No files matched your search
@@ -36,11 +36,18 @@ class Documentation(object):
|
||||
self._directory = directory
|
||||
|
||||
def write(self):
|
||||
self.write_documentation(False)
|
||||
self.write_documentation("hypervisor")
|
||||
# Controller documentation
|
||||
self.write_documentation(True)
|
||||
self.write_documentation("controller")
|
||||
# Write doc common to both
|
||||
self.write_documentation("common")
|
||||
|
||||
def write_documentation(self, controller):
|
||||
def write_documentation(self, doc_type):
|
||||
"""
|
||||
Build all the doc page for handlers
|
||||
|
||||
:param doc_type: Type of doc to generate (controller, hypervisor or common)
|
||||
"""
|
||||
for handler_name in sorted(self._documentation):
|
||||
print("Build {}".format(handler_name))
|
||||
|
||||
@@ -50,25 +57,27 @@ class Documentation(object):
|
||||
if api_version is None:
|
||||
continue
|
||||
|
||||
if self._documentation[handler_name][path].get("controller", False):
|
||||
if "controller." in handler_name:
|
||||
server_type = "controller"
|
||||
if not controller:
|
||||
continue
|
||||
elif "hypervisor." in handler_name:
|
||||
server_type = "hypervisor"
|
||||
else:
|
||||
server_type = "server"
|
||||
if controller:
|
||||
continue
|
||||
server_type = "common"
|
||||
|
||||
self._create_handler_directory(handler_name, api_version, server_type)
|
||||
if doc_type != server_type:
|
||||
continue
|
||||
|
||||
filename = self._file_path(path)
|
||||
handler_doc = self._documentation[handler_name][path]
|
||||
with open("{}/api/v{}/{}/{}/{}.rst".format(self._directory, api_version, server_type, handler_name, filename), 'w+') as f:
|
||||
f.write('{}\n----------------------------------------------------------------------------------------------------------------------\n\n'.format(path))
|
||||
handler = handler_name.replace(server_type + ".", "")
|
||||
|
||||
self._create_handler_directory(handler, api_version, server_type)
|
||||
with open("{}/api/v{}/{}/{}/{}.rst".format(self._directory, api_version, server_type, handler, filename), 'w+') as f:
|
||||
f.write('{}\n------------------------------------------------------------------------------------------------------------------------------------------\n\n'.format(path))
|
||||
f.write('.. contents::\n')
|
||||
for method in handler_doc["methods"]:
|
||||
f.write('\n{} {}\n'.format(method["method"], path.replace("{", '**{').replace("}", "}**")))
|
||||
f.write('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n')
|
||||
f.write('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n')
|
||||
f.write('{}\n\n'.format(method["description"]))
|
||||
|
||||
if len(method["parameters"]) > 0:
|
||||
@@ -93,7 +102,7 @@ class Documentation(object):
|
||||
f.write("Output\n*******\n")
|
||||
self._write_json_schema(f, method["output_schema"])
|
||||
|
||||
self._include_query_example(f, method, path, api_version)
|
||||
self._include_query_example(f, method, path, api_version, server_type)
|
||||
|
||||
def _create_handler_directory(self, handler_name, api_version, server_type):
|
||||
"""Create a directory for the handler and add an index inside"""
|
||||
@@ -103,18 +112,20 @@ class Documentation(object):
|
||||
|
||||
with open("{}/api/v{}/{}/{}.rst".format(self._directory, api_version, server_type, handler_name), "w+") as f:
|
||||
f.write(handler_name.replace("api.", "").replace("_", " ", ).capitalize())
|
||||
f.write("\n---------------------\n\n")
|
||||
f.write("\n-----------------------------\n\n")
|
||||
f.write(".. toctree::\n :glob:\n :maxdepth: 2\n\n {}/*\n".format(handler_name))
|
||||
|
||||
def _include_query_example(self, f, method, path, api_version):
|
||||
def _include_query_example(self, f, method, path, api_version, server_type):
|
||||
"""If a sample session is available we include it in documentation"""
|
||||
m = method["method"].lower()
|
||||
query_path = "{}_{}.txt".format(m, self._file_path(path))
|
||||
query_path = "{}_{}_{}.txt".format(server_type, m, self._file_path(path))
|
||||
if os.path.isfile(os.path.join(self._directory, "api", "examples", query_path)):
|
||||
f.write("Sample session\n***************\n")
|
||||
f.write("\n\n.. literalinclude:: ../../../examples/{}\n\n".format(query_path))
|
||||
|
||||
def _file_path(self, path):
|
||||
path = path.replace("hypervisor", "")
|
||||
path = path.replace("controller", "")
|
||||
return re.sub("^v1", "", re.sub("[^a-z0-9]", "", path))
|
||||
|
||||
def _write_definitions(self, f, schema):
|
||||
|
||||
+26
-21
@@ -123,29 +123,34 @@ class Route(object):
|
||||
api_version = kw.get("api_version", 1)
|
||||
raw = kw.get("raw", False)
|
||||
|
||||
# If it's a JSON api endpoint just register the endpoint an do nothing
|
||||
if api_version is None:
|
||||
cls._path = path
|
||||
else:
|
||||
cls._path = "/v{version}{path}".format(path=path, version=api_version)
|
||||
|
||||
def register(func):
|
||||
route = cls._path
|
||||
# Add the type of server to the route
|
||||
if "controller" in func.__module__:
|
||||
route = "/v{version}/controller{path}".format(path=path, version=api_version)
|
||||
elif "hypervisor" in func.__module__:
|
||||
route = "/v{version}/hypervisor{path}".format(path=path, version=api_version)
|
||||
elif "handlers.api" in func.__module__:
|
||||
route = "/v{version}{path}".format(path=path, version=api_version)
|
||||
else:
|
||||
route = path
|
||||
|
||||
handler = func.__module__.replace("_handler", "").replace("gns3server.handlers.api.", "")
|
||||
cls._documentation.setdefault(handler, {})
|
||||
cls._documentation[handler].setdefault(route, {"api_version": api_version,
|
||||
"controller": kw.get("controller", False),
|
||||
"methods": []})
|
||||
# Compute metadata for the documentation
|
||||
if api_version:
|
||||
handler = func.__module__.replace("_handler", "").replace("gns3server.handlers.api.", "")
|
||||
cls._documentation.setdefault(handler, {})
|
||||
cls._documentation[handler].setdefault(route, {"api_version": api_version,
|
||||
"controller": kw.get("controller", False),
|
||||
"methods": []})
|
||||
|
||||
cls._documentation[handler][route]["methods"].append({
|
||||
"method": method,
|
||||
"status_codes": kw.get("status_codes", {200: "OK"}),
|
||||
"parameters": kw.get("parameters", {}),
|
||||
"output_schema": output_schema,
|
||||
"input_schema": input_schema,
|
||||
"description": kw.get("description", ""),
|
||||
})
|
||||
|
||||
cls._documentation[handler][route]["methods"].append({
|
||||
"method": method,
|
||||
"status_codes": kw.get("status_codes", {200: "OK"}),
|
||||
"parameters": kw.get("parameters", {}),
|
||||
"output_schema": output_schema,
|
||||
"input_schema": input_schema,
|
||||
"description": kw.get("description", ""),
|
||||
})
|
||||
func = asyncio.coroutine(func)
|
||||
|
||||
@asyncio.coroutine
|
||||
@@ -253,7 +258,7 @@ class Route(object):
|
||||
response = yield from control_schema(request)
|
||||
return response
|
||||
|
||||
cls._routes.append((method, cls._path, vm_concurrency))
|
||||
cls._routes.append((method, route, vm_concurrency))
|
||||
|
||||
return vm_concurrency
|
||||
return register
|
||||
|
||||
Reference in new issue
Block a user