Skip to content

Instantly share code, notes, and snippets.

@crohr
Created June 15, 2010 15:17
Show Gist options
  • Save crohr/439249 to your computer and use it in GitHub Desktop.
Save crohr/439249 to your computer and use it in GitHub Desktop.
##
# :method: get(/grid5000/sites/:site_uid/deployments[.:format])
#
# :call-seq:
# GET /grid5000/sites/:site_uid/deployments[.:format]
#
# Get the list of deployments launched on a specific site.
#
# == URI parameters
# <tt>site_uid</tt>:: the UID of the site (e.g. "rennes").
#
# == Query parameters
# <tt>limit</tt>:: max number of deployments to return
# <tt>offset</tt>:: number of deployments to skip
# By default, you'll get all the deployments ordered by their creation date DESC.
# You may filter them using the following query parameters:
# <tt>status</tt>:: see only the deployments which have the specified status
# (choose a value among <tt>terminated</tt>, <tt>error</tt>, <tt>processing</tt>)
# <tt>user_uid</tt>:: see only the deployments of the user <tt>user_uid</tt>.
#
# == Supported Content-Types
# <tt>application/json</tt>:: JSON (catch-all mime-type)
# <tt>application/vnd.fr.grid5000.api.Collection+json;level=1</tt>:: JSON description of a collection of items
#
# == Status codes
# <tt>200</tt>:: OK.
# <tt>406</tt>:: Returns 406 if the requested format is not available.
# The response body will contain a comma-separated list of supported mime types.
# <tt>500</tt>:: Internal Server Error.
#
# == Usage
# Get the last 50 deployments submitted on the <tt>rennes</tt> site:
# GET /grid5000/sites/rennes/deployments?limit=50
# Accept: application/json
#
# Get all the deployments submitted by the <tt>crohr</tt> user on the <tt>rennes</tt> site:
# GET /grid5000/sites/rennes/deployments?user_uid=crohr
# Accept: application/json
#
# Get all the deployments on the <tt>orsay</tt> site that are still being processed:
# GET /grid5000/sites/orsay/deployments?status=processing
# Accept: application/vnd.fr.grid5000.api.Collection+json
#
#
get '/grid5000/sites/:site_uid/deployments' do |site_uid|
provides :json, :collection_json_v1
offset = (params[:offset] || 0).to_i
limit = (params[:limit] || options.deployments_collection_limit).to_i
reverse = (params[:reverse] || false)
direction = reverse ? :desc : :asc
# filters
deployments = Grid5000::Deployment.order(:created_at.send(direction))
deployments = deployments.filter(
:status => params[:status].downcase
) if params[:status]
deployments = deployments.filter(
:user_uid => params[:user_uid]
) if params[:user_uid]
# Count the total number of deployments validating the filters,
# then limit
total = deployments.count
deployments = deployments.limit(limit, offset)
output = {
:offset => offset,
:total => total,
:items => deployments.all,
:links => [
{
:rel => "self",
:type => media_type(:collection_json_v1),
:href => uri_for("/grid5000/sites/#{site_uid}/deployments")
},
{
:rel => "parent",
:type => media_type(:site_json_v1),
:href => uri_for("/grid5000/sites/#{site_uid}")
}
]
}
output[:items].map!{ |item|
item['links'] = generate_links_for_deployment(item); item}
# pagination
if output[:total] > output[:items].length
params[:offset] = limit+offset
output[:links].push({
:rel => "next",
:type => media_type(:collection_json_v1),
:href => uri_for("/grid5000/sites/#{site_uid}/deployments")})
end
headers['Allow'] = 'GET,POST'
status 200
parser.dump(output, :format => :pretty)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment