Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One way is to only keep track of Server resources and just implement start/stop/install_pkg as custom endpoints.</p> <p>You would implement the Server Resource under Tastypie to have endpoints for each server using regular ORM based resources. That implements standard REST operations on the resource, GET to read the server info, POST to create new ones if you say you have admin user that needs to add another server to administer it with services, PUT/PATCH to update existing servers.</p> <p>Then you would extend ServerResource to include additional endpoints by:</p> <ol> <li>Using <a href="http://django-tastypie.readthedocs.org/en/latest/resources.html#Resource.prepend_urls"><code>prepend_urls</code></a> to define 3 custom endpoints (start, stop, install_pkg)</li> <li>Implement each endpoint in its function under the ServerResource, taking care to consider security, limit operations (do you want GET or POST invoked on them?)</li> </ol> <hr> <p>Another way is to create ServiceResource as a Non ORM Resource which will be in many to many relationship with the Server Resource. This is more tricky but arguably much cleaner design.</p> <p>Your ServiceResource would have 2 custom endpoints just like in the previous scenario (start/stop) but to install a service on a server you would send a PUT/PATCH to the ServerResource that you want to associate with that service.</p> <p>For more specific answer please state more specific questions.</p> <hr> <p>See Tastypie cookbook for some solid details on how to proceed:</p> <p>This <a href="http://django-tastypie.readthedocs.org/en/v0.9.11/cookbook.html#adding-search-functionality">search example</a> uses the <code>override_urls</code> which is what you want to use in 0.9.11 and has similar behavior to <code>prepend_urls</code> in 0.9.12 which is not released yet. Depending on which version you are, you need to create 3 custom endpoints:</p> <pre><code> def override_urls(self): return [ url(r"^(?P&lt;resource_name&gt;%s)/(?P&lt;pk&gt;\w[\w/-]*)/%start%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('start'), name="api_start"), url(r"^(?P&lt;resource_name&gt;%s)/(?P&lt;pk&gt;\w[\w/-]*)/%stop%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('stop'), name="api_stop"), url(r"^(?P&lt;resource_name&gt;%s)/(?P&lt;pk&gt;\w[\w/-]*)/%install_pkg%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('install_pkg'), name="api_install_pkg"), ] </code></pre> <p>Then you need to define the 3 handlers similar to the search example on the ServerResource. I will give you a starting point for the start function. This is an example of a synchronous operation. If you want it to be asynchronous you will need a lot more, but its a starting point.</p> <pre><code>from tastypie.http import Http def start(self, request, **kwargs): self.method_check(request, allowed=['post']) self.is_authenticated(request) self.throttle_check(request) try: output = function_that_starts_server(kwargs['pk']) except FailedException as failure: return self.create_response(request, { 'status' : 'failure', 'reason' : failure }, Http self.log_throttled_access(request) return self.create_response(request, { 'status' : 'success', 'log' : output }) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload