Permalink
Browse files

add new serialized apis to vcap

Change-Id: I461b226c7a89c0f810730c907efce19a639649f8
  • Loading branch information...
1 parent 2bae26e commit b2fece423a7407c16184eba3bf4feadf9899adea @andl andl committed Apr 24, 2012
@@ -62,7 +62,7 @@ GEM
hiredis (0.3.2)
http_parser.rb (0.5.1)
i18n (0.5.0)
- json_pure (1.6.5)
+ json_pure (1.6.6)
mail (2.2.15)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
@@ -131,7 +131,7 @@ GEM
polyglot (>= 0.3.1)
tzinfo (0.3.26)
uuidtools (2.1.2)
- vcap_common (1.0.10)
+ vcap_common (1.0.11)
eventmachine (~> 0.12.11.cloudfoundry.3)
nats (~> 0.4.22.beta.8)
posix-spawn (~> 0.3.6)
@@ -10,9 +10,9 @@ class ServicesController < ApplicationController
before_filter :require_service_auth_token, :only => [:create, :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, :import_from_url, :import_from_data, :job_info]
+ :serialized_url, :create_serialized_url, :import_from_url, :import_from_data, :job_info]
before_filter :require_lifecycle_extension, :only => [:create_snapshot, :enum_snapshots, :snapshot_details,:rollback_snapshot, :delete_snapshot,
- :serialized_url, :import_from_url, :import_from_data, :job_info]
+ :serialized_url, :create_serialized_url, :import_from_url, :import_from_data, :job_info]
rescue_from(JsonMessage::Error) {|e| render :status => 400, :json => {:errors => e.to_s}}
rescue_from(ActiveRecord::RecordInvalid) {|e| render :status => 400, :json => {:errors => e.to_s}}
@@ -243,14 +243,25 @@ def delete_snapshot
render :json => result.extract
end
- # Get the url to download serialized data for an instance
+ # Create serialized url for service snapshot
#
+ def create_serialized_url
+ cfg = ServiceConfig.find_by_user_id_and_name(user.id, params['id'])
+ raise CloudError.new(CloudError::SERVICE_NOT_FOUND) unless cfg
+ raise CloudError.new(CloudError::FORBIDDEN) unless cfg.provisioned_by?(user)
+
+ result = cfg.create_serialized_url params['sid']
+
+ render :json => result.extract
+ end
+
+ # Get the url to download serialized data for an instance
def serialized_url
cfg = ServiceConfig.find_by_user_id_and_name(user.id, params['id'])
raise CloudError.new(CloudError::SERVICE_NOT_FOUND) unless cfg
raise CloudError.new(CloudError::FORBIDDEN) unless cfg.provisioned_by?(user)
- result = cfg.serialized_url
+ result = cfg.serialized_url params['sid']
render :json => result.extract
end
@@ -92,8 +92,8 @@ def unprovision
def handle_lifecycle_error(e)
CloudController.logger.error("Error talking to gateway: #{e}")
CloudController.logger.error(e)
- if e.is_a? VCAP::Services::Api::ServiceGatewayClient::NotFoundResponse
- raise CloudError.new([e.error.code, CloudError::HTTP_NOT_FOUND, e.error.description])
+ if e.is_a? VCAP::Services::Api::ServiceGatewayClient::ErrorResponse
+ raise CloudError.new([e.error.code, e.status, e.error.description])
else
raise CloudError.new(CloudError::SERVICE_GATEWAY_ERROR)
end
@@ -134,9 +134,16 @@ def delete_snapshot(sid)
handle_lifecycle_error(e)
end
- def serialized_url
+ def serialized_url(sid)
client = VCAP::Services::Api::ServiceGatewayClient.new(service.url, service.token, service.timeout)
- client.serialized_url(:service_id => name)
+ client.serialized_url(:service_id => name, :snapshot_id => sid)
+ rescue => e
+ handle_lifecycle_error(e)
+ end
+
+ def create_serialized_url(sid)
+ client = VCAP::Services::Api::ServiceGatewayClient.new(service.url, service.token, service.timeout)
+ client.create_serialized_url(:service_id => name, :snapshot_id => sid)
rescue => e
handle_lifecycle_error(e)
end
@@ -52,13 +52,14 @@
# Brokered Services
get 'brokered_services/poc/offerings' => 'services#list_brokered_services', :as => :service_list_brokered_services
- # Service life cycle apis
+ # Service lifecycle apis
post 'services/v1/configurations/:id/snapshots' => 'services#create_snapshot', :as => :service_create_snapshot, :id => /[^\/]+/
get 'services/v1/configurations/:id/snapshots' => 'services#enum_snapshots', :as => :service_enum_snapshots, :id => /[^\/]+/
get 'services/v1/configurations/:id/snapshots/:sid' => 'services#snapshot_details', :as => :service_snapshot_details, :id => /[^\/]+/, :sid => /[^\/]+/
put 'services/v1/configurations/:id/snapshots/:sid' => 'services#rollback_snapshot', :as => :service_rollback_snapshot, :id => /[^\/]+/, :sid => /[^\/]+/
delete 'services/v1/configurations/:id/snapshots/:sid' => 'services#delete_snapshot', :as => :service_delete_snapshot, :id => /[^\/]+/, :sid => /[^\/]+/
- get 'services/v1/configurations/:id/serialized/url' => 'services#serialized_url', :as => :service_serialized_url, :id => /[^\/]+/
+ post 'services/v1/configurations/:id/serialized/url/snapshots/:sid' => 'services#create_serialized_url', :as => :service_create_serialized_url, :id => /[^\/]+/, :sid => /[^\/]+/
+ get 'services/v1/configurations/:id/serialized/url/snapshots/:sid' => 'services#serialized_url', :as => :service_serialized_url, :id => /[^\/]+/, :sid => /[^\/]+/
put 'services/v1/configurations/:id/serialized/url' => 'services#import_from_url', :as => :service_import_from_url, :id => /[^\/]+/
put 'services/v1/configurations/:id/serialized/data' => 'services#import_from_data', :as => :service_import_from_data, :id => /[^\/]+/
get 'services/v1/configurations/:id/jobs/:job_id' => 'services#job_info', :as => :service_job_info, :id => /[^\/]+/, :job_id => /[^\/]+/
@@ -665,14 +665,14 @@ def unbind_instance(service_id, handle_id, binding_options)
it 'should return not implemented error when lifecycle is disabled' do
begin
origin = AppConfig.delete :service_lifecycle
- %w(create_snapshot enum_snapshots serialized_url import_from_url import_from_data).each do |api|
+ %w(create_snapshot enum_snapshots import_from_url import_from_data).each do |api|
post api.to_sym, :id => 'xxx'
response.status.should == 501
resp = Yajl::Parser.parse(response.body)
resp['description'].include?("not implemented").should == true
end
- %w(snapshot_details rollback_snapshot delete_snapshot).each do |api|
+ %w(snapshot_details rollback_snapshot delete_snapshot serialized_url create_serialized_url ).each do |api|
post api.to_sym, :id => 'xxx', :sid => '1'
response.status.should == 501
resp = Yajl::Parser.parse(response.body)
@@ -921,12 +921,44 @@ def unbind_instance(service_id, handle_id, binding_options)
it 'should return not authorized for unknown users' do
request.env['HTTP_AUTHORIZATION'] = UserToken.create('bar@foo.com').encode
- get :serialized_url, :id => 'xxx'
+ get :serialized_url, :id => 'xxx', :sid => '1'
response.status.should == 403
end
it 'should return not found for unknown ids' do
- get :serialized_url, :id => 'xxx'
+ get :serialized_url, :id => 'xxx', :sid => '1'
+ response.status.should == 404
+ end
+
+ it 'should get serialized url' do
+ url = "http://api.vcap.me"
+ snapshot_id = "abc"
+ serialized_url = VCAP::Services::Api::SerializedURL.new(:url => url)
+ VCAP::Services::Api::ServiceGatewayClient.any_instance.stubs(:serialized_url).with(:service_id => @cfg.name, :snapshot_id => snapshot_id ).returns serialized_url
+
+ get :serialized_url, :id => @cfg.name, :sid => snapshot_id
+ response.status.should == 200
+ resp = Yajl::Parser.parse(response.body)
+ resp["url"].should == url
+ end
+ end
+
+ describe "#create_serialized_url" do
+ before :each do
+ cfg = ServiceConfig.new(:name => 'lifecycle', :alias => 'bar', :service => @svc, :user => @user)
+ cfg.save
+ cfg.should be_valid
+ @cfg = cfg
+ end
+
+ it 'should return not authorized for unknown users' do
+ request.env['HTTP_AUTHORIZATION'] = UserToken.create('bar@foo.com').encode
+ post :create_serialized_url, :id => 'xxx', :sid => '1'
+ response.status.should == 403
+ end
+
+ it 'should return not found for unknown ids' do
+ post :create_serialized_url, :id => 'xxx', :sid => '1'
response.status.should == 404
end
@@ -938,9 +970,10 @@ def unbind_instance(service_id, handle_id, binding_options)
:start_time => "1"
}.to_json
)
- VCAP::Services::Api::ServiceGatewayClient.any_instance.stubs(:serialized_url).with(:service_id => @cfg.name).returns job
+ snapshot_id = "abc"
+ VCAP::Services::Api::ServiceGatewayClient.any_instance.stubs(:create_serialized_url).with(:service_id => @cfg.name, :snapshot_id => snapshot_id).returns job
- get :serialized_url, :id => @cfg.name
+ post :create_serialized_url, :id => @cfg.name, :sid => snapshot_id
response.status.should == 200
resp = Yajl::Parser.parse(response.body)
resp["job_id"].should == "abc"
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- vcap_common (1.0.10)
+ vcap_common (1.0.11)
eventmachine (~> 0.12.11.cloudfoundry.3)
nats (~> 0.4.22.beta.8)
posix-spawn (~> 0.3.6)
@@ -20,8 +20,8 @@ GEM
eventmachine (>= 0.12.9)
escape_utils (0.2.4)
eventmachine (0.12.11.cloudfoundry.3)
- json_pure (1.6.5)
- nats (0.4.22.beta.8)
+ json_pure (1.6.6)
+ nats (0.4.22)
daemons (>= 1.1.4)
eventmachine (>= 0.12.10)
json_pure (>= 1.6.1)
@@ -103,11 +103,16 @@ def delete_snapshot(args)
Job.decode(resp)
end
- def serialized_url(args)
- resp = perform_request(:get, "/gateway/v1/configurations/#{args[:service_id]}/serialized/url")
+ def create_serialized_url(args)
+ resp = perform_request(:post, "/gateway/v1/configurations/#{args[:service_id]}/serialized/url/snapshots/#{args[:snapshot_id]}")
Job.decode(resp)
end
+ def serialized_url(args)
+ resp = perform_request(:get, "/gateway/v1/configurations/#{args[:service_id]}/serialized/url/snapshots/#{args[:snapshot_id]}")
+ SerializedURL.decode(resp)
+ end
+
def import_from_url(args)
resp = perform_request(:put, "/gateway/v1/configurations/#{args[:service_id]}/serialized/url", args[:msg])
Job.decode(resp)
@@ -167,10 +172,10 @@ def perform_request(http_method, path, msg=VCAP::Services::Api::EMPTY_REQUEST)
begin
# try to decode the response
err = ServiceErrorResponse.decode(body)
- raise ErrorResponse.new(code, err)
rescue => e
raise UnexpectedResponse, "Can't decode gateway response. status code:#{code}, response body:#{body}"
end
+ raise ErrorResponse.new(code, err)
end
end
end
@@ -65,8 +65,9 @@ class ServiceGatewayClient
{:code => 40400, :description=> "not found"}.to_json,
{:code => 50300, :description=> "internal"}.to_json,
{:code => 50100, :description=> "not done yet"}.to_json,
+ {:bad_response => "foo"}.to_json,
)
- resp.should_receive(:code).and_return(404, 503, 500)
+ resp.should_receive(:code).and_return(404, 503, 500, 500)
resp.should_receive(:start).any_number_of_times.and_return resp
http_method = :get
@@ -75,6 +76,7 @@ class ServiceGatewayClient
expect {client.perform_request(http_method, path)}.should raise_error(VCAP::Services::Api::ServiceGatewayClient::NotFoundResponse)
expect {client.perform_request(http_method, path)}.should raise_error(VCAP::Services::Api::ServiceGatewayClient::GatewayInternalResponse)
+ expect {client.perform_request(http_method, path)}.should raise_error(VCAP::Services::Api::ServiceGatewayClient::ErrorResponse, /not done yet/)
expect {client.perform_request(http_method, path)}.should raise_error(VCAP::Services::Api::ServiceGatewayClient::UnexpectedResponse)
end
end
@@ -1,7 +1,7 @@
spec = Gem::Specification.new do |s|
s.name = 'vcap_common'
- s.version = '1.0.10'
- s.date = '2011-02-09'
+ s.version = '1.0.11'
+ s.date = '2011-04-25'
s.summary = 'vcap common'
s.homepage = "http://github.com/vmware-ac/core"
s.description = 'common vcap classes/methods'

0 comments on commit b2fece4

Please sign in to comment.