Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
[CC] add update_snapshot_name api support
Browse files Browse the repository at this point in the history
update_snapshot_name api enable user to add a brief,
human readable name to each service snapshot.

Change-Id: I902507996fd5301a981f98bb004b71651af57dbb
  • Loading branch information
Andrew Liu committed Aug 21, 2012
1 parent 6411e33 commit ad452a9
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cloud_controller/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ gem 'rails', '~> 3.0.14'
gem 'nats', :require => 'nats/client'

# VCAP common components
gem 'vcap_common', :require => ['vcap/common', 'vcap/component'], :git => 'https://github.com/cloudfoundry/vcap-common.git', :ref => '1dca468ec0'
gem 'vcap_common', :require => ['vcap/common', 'vcap/component'], :git => 'https://github.com/cloudfoundry/vcap-common.git', :ref => '2d4b47ea'
gem 'vcap_logging', :require => ['vcap/logging'], :git => 'https://github.com/cloudfoundry/common.git', :ref => 'e36886a1'
gem 'vcap_staging', '~> 0.1.62', :git => 'https://github.com/cloudfoundry/vcap-staging.git', :ref => '8d9da6cd'
gem 'cf-uaa-client', '~> 1.0', :git => 'https://github.com/cloudfoundry/uaa.git', :ref => 'b19eb89b'
Expand Down
12 changes: 6 additions & 6 deletions cloud_controller/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ GIT

GIT
remote: https://github.com/cloudfoundry/vcap-common.git
revision: 1dca468ec017ea5d296aeac1aa2fb7a70affba63
ref: 1dca468ec0
revision: 2d4b47ea52487897fbf4d9292a1f14cfc4f2d5b8
ref: 2d4b47ea
specs:
vcap_common (2.0.4)
vcap_common (2.0.6)
em-http-request (~> 1.0.0.beta3)
eventmachine
httpclient
membrane (~> 0.0.2)
mime-types
multipart-post
nats (~> 0.4.22.beta.8)
nats (~> 0.4.24)
posix-spawn (~> 0.3.6)
thin (~> 1.3.1)
thin
yajl-ruby (~> 0.8.3)

GIT
Expand Down Expand Up @@ -122,7 +122,7 @@ GEM
abstract (>= 1.0.0)
hiredis (0.3.2)
http_parser.rb (0.5.1)
httpclient (2.2.5)
httpclient (2.2.7)
i18n (0.5.0)
interact (0.4.3)
json (1.7.3)
Expand Down
62 changes: 46 additions & 16 deletions cloud_controller/app/controllers/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,39 @@ class ServicesController < ApplicationController

before_filter :validate_content_type, :except => [:import_from_data]
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]
before_filter :require_lifecycle_extension, :only => [:create_snapshot, :enum_snapshots, :snapshot_details,:rollback_snapshot, :delete_snapshot,
:serialized_url, :create_serialized_url, :import_from_url, :import_from_data, :job_info]
before_filter :require_user, :only =>
[
:provision,
:bind,
:bind_external,
:unbind,
:unprovision,
:create_snapshot,
:enum_snapshots,
:snapshot_details,
:update_snapshot_name,
:rollback_snapshot,
:delete_snapshot,
: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,
:update_snapshot_name,
:rollback_snapshot,
:delete_snapshot,
:serialized_url,
:create_serialized_url,
:import_from_url,
:import_from_data,
:job_info,
]
before_filter :unify_provider, :only => [:get, :delete, :update_handle, :list_handles]

rescue_from(JsonMessage::Error) {|e| render :status => 400, :json => {:errors => e.to_s}}
Expand Down Expand Up @@ -225,7 +253,6 @@ def provision
def unprovision
cfg = ServiceConfig.find_by_user_id_and_alias(user.id, params[:id])
raise CloudError.new(CloudError::SERVICE_NOT_FOUND) unless cfg
raise CloudError.new(CloudError::FORBIDDEN) unless cfg.provisioned_by?(user)

cfg.unprovision

Expand All @@ -237,7 +264,6 @@ def unprovision
def create_snapshot
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_snapshot

Expand All @@ -249,7 +275,6 @@ def create_snapshot
def enum_snapshots
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.enum_snapshots

Expand All @@ -261,19 +286,30 @@ def enum_snapshots
def snapshot_details
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.snapshot_details params['sid']

render :json => result.extract
end

# Update name of a snapshot
#
def update_snapshot_name
req = VCAP::Services::Api::UpdateSnapshotNameRequest.decode(request_body)

cfg = ServiceConfig.find_by_user_id_and_name(user.id, params['id'])
raise CloudError.new(CloudError::SERVICE_NOT_FOUND) unless cfg

result = cfg.update_snapshot_name params['sid'], req

render :json => result.extract
end

# Rollback to a snapshot
#
def rollback_snapshot
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.rollback_snapshot params['sid']

Expand All @@ -285,7 +321,6 @@ def rollback_snapshot
def delete_snapshot
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.delete_snapshot params['sid']

Expand All @@ -297,7 +332,6 @@ def delete_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']

Expand All @@ -308,7 +342,6 @@ def create_serialized_url
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 params['sid']

Expand All @@ -322,7 +355,6 @@ def import_from_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.import_from_url req

Expand All @@ -342,7 +374,6 @@ def import_from_data
# Check the service and user's permission
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)

# Check whether has upload_token
upload_token = AppConfig[:service_lifecycle][:upload_token]
Expand Down Expand Up @@ -388,7 +419,6 @@ def import_from_data
def job_info
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.job_info params['job_id']

Expand Down
7 changes: 7 additions & 0 deletions cloud_controller/app/models/service_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ def snapshot_details(sid)
handle_lifecycle_error(e)
end

def update_snapshot_name(sid, req)
client = VCAP::Services::Api::ServiceGatewayClient.new(service.url, service.token, service.timeout)
client.update_snapshot_name(:service_id => name, :snapshot_id => sid, :msg => req)
rescue => e
handle_lifecycle_error(e)
end

def rollback_snapshot(sid)
client = VCAP::Services::Api::ServiceGatewayClient.new(service.url, service.token, service.timeout)
client.rollback_snapshot(:service_id => name, :snapshot_id => sid)
Expand Down
1 change: 1 addition & 0 deletions cloud_controller/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
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 => /[^\/]+/
post 'services/v1/configurations/:id/snapshots/:sid/name'=> 'services#update_snapshot_name', :as => :service_update_snapshot_name, :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 => /[^\/]+/
post 'services/v1/configurations/:id/serialized/url/snapshots/:sid' => 'services#create_serialized_url', :as => :service_create_serialized_url, :id => /[^\/]+/, :sid => /[^\/]+/
Expand Down
40 changes: 38 additions & 2 deletions cloud_controller/spec/controllers/services_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@
resp['description'].include?("not implemented").should == true
end

%w(snapshot_details rollback_snapshot delete_snapshot serialized_url create_serialized_url ).each do |api|
%w(snapshot_details update_snapshot_name 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)
Expand All @@ -1008,6 +1008,7 @@
response.status.should == 501
resp = Yajl::Parser.parse(response.body)
resp['description'].include?("not implemented").should == true

ensure
AppConfig[:service_lifecycle] = origin
end
Expand Down Expand Up @@ -1110,7 +1111,8 @@
{
:snapshot_id => "abc",
:date => "1",
:size => 123
:size => 123,
:name => "foo",
}.to_json
)
VCAP::Services::Api::ServiceGatewayClient.any_instance.stubs(:snapshot_details).with(:service_id => @cfg.name, :snapshot_id => snapshot.snapshot_id).returns snapshot
Expand All @@ -1135,6 +1137,40 @@
end
end

describe "#update_snapshot_name" 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 :update_snapshot_name, :id => 'xxx' , :sid => 'yyy'
response.status.should == 403
end

it 'should return not found for unknown ids' do
post_msg :update_snapshot_name, :id => 'xxx', :sid => 'yyy' do
VCAP::Services::Api::UpdateSnapshotNameRequest.new(:name => "new name")
end
response.status.should == 404
end

it 'should update snapshot name' do
empty_response = VCAP::Services::Api::EMPTY_REQUEST
VCAP::Services::Api::ServiceGatewayClient.any_instance.expects(:update_snapshot_name).with(anything).returns empty_response

post_msg :update_snapshot_name, :id => @cfg.name, :sid => "1" do
VCAP::Services::Api::UpdateSnapshotNameRequest.new(:name => "new name")
end
response.status.should == 200
resp = Yajl::Parser.parse(response.body)
resp.should == {}
end
end

describe "#rollback_snapshot" do
before :each do
cfg = ServiceConfig.new(:name => 'lifecycle', :alias => 'bar', :service => @svc, :user => @user)
Expand Down

0 comments on commit ad452a9

Please sign in to comment.