Skip to content

Commit

Permalink
Merge pull request #5 from CenturyLinkLabs/feature/metadata
Browse files Browse the repository at this point in the history
Add adapter version and metadata endpoint.
  • Loading branch information
dpetersen committed Nov 13, 2014
2 parents c4830cc + 387bb39 commit 65659f7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 30 deletions.
3 changes: 3 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
require 'app/utils'

module FleetAdapter
VERSION = ENV["ADAPTER_VERSION"]

class App < Sinatra::Application
configure do
disable :method_override
disable :static
end

use FleetAdapter::Routes::Services
use FleetAdapter::Routes::Metadata
end
end

Expand Down
1 change: 1 addition & 0 deletions app/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module FleetAdapter
module Routes
autoload :Base, 'app/routes/base'
autoload :Services, 'app/routes/services'
autoload :Metadata, 'app/routes/metadata'
end
end
16 changes: 16 additions & 0 deletions app/routes/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module FleetAdapter
module Routes
class Metadata < Base
namespace '/' + API_VERSION do
get '/metadata' do
json(
{
version: FleetAdapter::VERSION,
type: "Fleet"
}
)
end
end
end
end
end
62 changes: 32 additions & 30 deletions app/routes/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,46 @@ module FleetAdapter
module Routes
class Services < Base

post "/#{API_VERSION}/services" do
sorted_services = ServiceSorter.sort(@payload)
services = Service.create_all(sorted_services)
services.each(&:start)
status 201
json services.map { |service| { id: service.id } }
end
namespace '/' + API_VERSION do
post '/services' do
sorted_services = ServiceSorter.sort(@payload)
services = Service.create_all(sorted_services)
services.each(&:start)
status 201
json services.map { |service| { id: service.id } }
end

get "/#{API_VERSION}/services/:id" do
service = Service.find(params[:id])
get '/services/:id' do
service = Service.find(params[:id])

result = {
id: service.id,
'actualState' => service.status
}
result = {
id: service.id,
'actualState' => service.status
}

json result
end
json result
end

put "/#{API_VERSION}/services/:id" do
service = Service.find(params[:id])
put '/services/:id' do
service = Service.find(params[:id])

case @payload[:desiredState]
when 'started'
service.start
status 204
when 'stopped'
service.stop
status 204
else
status 400
end
end

case @payload[:desiredState]
when 'started'
service.start
status 204
when 'stopped'
service.stop
delete '/services/:id' do
Service.find(params[:id]).destroy
status 204
else
status 400
end
end

delete "/#{API_VERSION}/services/:id" do
Service.find(params[:id]).destroy
status 204
end
end
end
end
21 changes: 21 additions & 0 deletions spec/api/metadata_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative '../spec_helper'

describe 'Metadata Endpoint' do
describe 'GET /v1/metadata' do
let(:response) { get '/v1/metadata' }
subject(:hash) { JSON.parse(response.body) }
before { stub_const("FleetAdapter::VERSION", "2.0") }

it "has a 200 response code" do
expect(response.status).to eq(200)
end

it "has a JSON Content-Type" do
expect(response.headers["Content-Type"]).to eq("application/json")
end

its(:keys) { should eq([ "version", "type" ]) }
its(["version"]) { should eq("2.0") }
its(["type"]) { should eq("Fleet") }
end
end

0 comments on commit 65659f7

Please sign in to comment.