1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[hp|compute_v2] Add request and server model methods to add security groups to server after creation, along with tests.

This commit is contained in:
Rupak Ganguly 2013-08-06 10:45:52 -04:00
parent 5cf78d9d6c
commit 386682e0b6
6 changed files with 142 additions and 0 deletions

View file

@ -32,6 +32,7 @@ module Fog
collection :volume_attachments
request_path 'fog/hp/requests/compute_v2'
request :add_security_group
request :allocate_address
request :associate_address
request :attach_volume
@ -73,6 +74,7 @@ module Fog
request :reboot_server
request :rebuild_server
request :release_address
request :remove_security_group
#request :resize_server
#request :revert_resized_server
request :server_action

View file

@ -239,6 +239,18 @@ module Fog
true
end
def add_security_group(sg_name)
requires :id
service.add_security_group(id, sg_name)
true
end
def remove_security_group(sg_name)
requires :id
service.remove_security_group(id, sg_name)
true
end
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
requires :flavor_id, :name

View file

@ -0,0 +1,40 @@
module Fog
module Compute
class HPV2
class Real
# Add an existing security group to an existing server
#
# ==== Parameters
# * 'server_id'<~String> - UUId of server
# * 'sg_name'<~String> - Name of security group to add to the server
#
def add_security_group(server_id, sg_name)
body = { 'addSecurityGroup' => { 'name' => sg_name }}
server_action(server_id, body)
end
end
class Mock
def add_security_group(server_id, sg_name)
response = Excon::Response.new
if server = self.data[:servers][server_id]
data = {"name" => "#{sg_name}"}
if server['security_groups']
server['security_groups'] << data
else
server['security_groups'] = data
end
response.status = 202
else
raise Fog::Compute::HPV2::NotFound
end
response
end
end
end
end
end

View file

@ -0,0 +1,37 @@
module Fog
module Compute
class HPV2
class Real
# Remove an existing security group from an existing server
#
# ==== Parameters
# * 'server_id'<~String> - UUId of server
# * 'sg_name'<~String> - Name of security group to remove from the server
#
def remove_security_group(server_id, sg_name)
body = { 'removeSecurityGroup' => { 'name' => sg_name }}
server_action(server_id, body)
end
end
class Mock
def remove_security_group(server_id, sg_name)
response = Excon::Response.new
if server = self.data[:servers][server_id]
data = server['security_groups'].reject {|sg| sg['name'] == sg_name}
self.data[:servers][server_id]['security_groups'] = data
response.status = 202
else
raise Fog::Compute::HPV2::NotFound
end
response
end
end
end
end
end

View file

@ -30,6 +30,14 @@ Shindo.tests("Fog::Compute::HPV2 | server model", ['hp', 'v2', 'compute']) do
@server.rebuild(@base_image_id, 'fogrebuildserver')
end
tests('#add_security_group("default")').succeeds do
@server.add_security_group('default')
end
tests('#remove_security_group("default")').succeeds do
@server.remove_security_group('default')
end
@server.destroy
end

View file

@ -0,0 +1,43 @@
Shindo.tests("Fog::Compute::HPV2 | server security group requests", ['hp', 'v2', 'compute']) do
service = Fog::Compute.new(:provider => 'HP', :version => :v2)
@base_image_id = ENV['BASE_IMAGE_ID'] || '7f60b54c-cd15-433f-8bed-00acbcd25a17'
tests('success') do
@server_name = 'fogsecgrouptests'
@server_id = nil
# create a server without a sec group
data = service.create_server(@server_name, 100, @base_image_id).body['server']
@server_id = data['id']
# now add the 'default' sec group to the server
tests("#add_security_group(#{@server_id}, 'default')").succeeds do
service.add_security_group(@server_id, 'default')
end
# now remove the 'default' sec group to the server
tests("#remove_security_group(#{@server_id}, 'default')").succeeds do
service.remove_security_group(@server_id, 'default')
end
service.delete_server(@server_id)
end
tests('failure') do
tests("#add_security_group(0, 'default')").raises(Fog::Compute::HPV2::NotFound) do
service.add_security_group(0, 'default')
end
tests("#remove_security_group(0, 'default')").raises(Fog::Compute::HPV2::NotFound) do
service.remove_security_group(0, 'default')
end
end
end