Skip to content

Commit

Permalink
Add an api for CC, which returns a specific service offering
Browse files Browse the repository at this point in the history
Get /services/v1/offerings/:label

Change-Id: I47e66278d0782e095b553cafbc904591e25b5571
  • Loading branch information
Haipeng Wu committed May 22, 2012
1 parent 2abb03a commit af46278
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cloud_controller/app/controllers/services_controller.rb
Expand Up @@ -7,7 +7,7 @@ class ServicesController < ApplicationController
include ServicesHelper

before_filter :validate_content_type
before_filter :require_service_auth_token, :only => [:create, :delete, :update_handle, :list_handles, :list_brokered_services]
before_filter :require_service_auth_token, :only => [:create, :get, :delete, :update_handle, :list_handles, :list_brokered_services]
before_filter :require_user, :only => [:provision, :bind, :bind_external, :unbind, :unprovision,
:create_snapshot, :enum_snapshots, :snapshot_details,:rollback_snapshot, :delete_snapshot,
:serialized_url, :create_serialized_url, :import_from_url, :import_from_data, :job_info]
Expand Down Expand Up @@ -141,6 +141,15 @@ def list_brokered_services
render :json => {:brokered_services => result}
end

# Get a service offering on the CC
#
def get
svc = Service.find_by_label(params[:label])
raise CloudError.new(CloudError::SERVICE_NOT_FOUND) unless svc
raise CloudError.new(CloudError::FORBIDDEN) unless svc.verify_auth_token(@service_auth_token)
render :json => svc.hash_to_service_offering
end

# Unregister a service offering with the CC
#
def delete
Expand Down
17 changes: 17 additions & 0 deletions cloud_controller/app/models/service.rb
Expand Up @@ -160,4 +160,21 @@ def verify_auth_token(token)
(self.token == token)
end
end

def hash_to_service_offering
svc_offering = {
:label => self.label,
:url => self.url
}
svc_offering[:description] = self.description if self.description
svc_offering[:info_url] = self.info_url if self.info_url
svc_offering[:tags] = self.tags if self.tags
svc_offering[:plans] = self.plans if self.plans
svc_offering[:plan_options] = self.plan_options if self.plan_options
svc_offering[:binding_options] = self.binding_options if self.binding_options
svc_offering[:acls] = self.acls if self.acls
svc_offering[:active] = self.active if self.active
svc_offering[:timeout] = self.timeout if self.timeout
return svc_offering
end
end
1 change: 1 addition & 0 deletions cloud_controller/config/routes.rb
Expand Up @@ -39,6 +39,7 @@

post 'services/v1/offerings' => 'services#create', :as => :service_create
delete 'services/v1/offerings/:label' => 'services#delete', :as => :service_delete, :label => /[^\/]+/
get 'services/v1/offerings/:label' => 'services#get', :as => :service_get, :label => /[^\/]+/
get 'services/v1/offerings/:label/handles' => 'services#list_handles', :as => :service_list_handles, :label => /[^\/]+/
post 'services/v1/offerings/:label/handles/:id' => 'services#update_handle', :as => :service_update_handle, :label => /[^\/]+/
post 'services/v1/configurations' => 'services#provision', :as => :service_provision
Expand Down
31 changes: 31 additions & 0 deletions cloud_controller/spec/controllers/services_controller_spec.rb
Expand Up @@ -221,6 +221,37 @@ def unbind_instance(service_id, handle_id, binding_options)
end
end

describe '#get' do
before :each do
@svc = Service.create(
:label => 'foo-bar',
:url => 'http://www.google.com',
:plans => ['free', 'nonfree'],
:token => 'foobar')
@svc.should be_valid
end

it 'should return not found for unknown services' do
get :get, :label => 'xxx'
response.status.should == 404
end

it 'should return not authorized on token mismatch' do
request.env['HTTP_X_VCAP_SERVICE_TOKEN'] = 'xxx'
get :get, :label => 'foo-bar'
response.status.should == 403
end

it 'should return the specific service offering' do
get :get, :label => 'foo-bar'
response.status.should == 200
resp = Yajl::Parser.parse(response.body)
resp["label"].should == 'foo-bar'
resp["url"].should == 'http://www.google.com'
resp["plans"].should == ['free', 'nonfree']
end
end

describe '#list_handles' do
it 'should return not found for unknown services' do
get :list_handles, :label => 'foo-bar'
Expand Down

0 comments on commit af46278

Please sign in to comment.