diff --git a/lib/fog/hp/compute_v2.rb b/lib/fog/hp/compute_v2.rb index 87a1e7945..f522f9d50 100644 --- a/lib/fog/hp/compute_v2.rb +++ b/lib/fog/hp/compute_v2.rb @@ -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 diff --git a/lib/fog/hp/models/compute_v2/server.rb b/lib/fog/hp/models/compute_v2/server.rb index 387cebc57..b6a6b7149 100644 --- a/lib/fog/hp/models/compute_v2/server.rb +++ b/lib/fog/hp/models/compute_v2/server.rb @@ -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 diff --git a/lib/fog/hp/requests/compute_v2/add_security_group.rb b/lib/fog/hp/requests/compute_v2/add_security_group.rb new file mode 100644 index 000000000..5d4a336a5 --- /dev/null +++ b/lib/fog/hp/requests/compute_v2/add_security_group.rb @@ -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 diff --git a/lib/fog/hp/requests/compute_v2/remove_security_group.rb b/lib/fog/hp/requests/compute_v2/remove_security_group.rb new file mode 100644 index 000000000..5275c3759 --- /dev/null +++ b/lib/fog/hp/requests/compute_v2/remove_security_group.rb @@ -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 diff --git a/tests/hp/models/compute_v2/server_tests.rb b/tests/hp/models/compute_v2/server_tests.rb index da7a7ccb7..bb00ed6cd 100644 --- a/tests/hp/models/compute_v2/server_tests.rb +++ b/tests/hp/models/compute_v2/server_tests.rb @@ -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 diff --git a/tests/hp/requests/compute_v2/server_security_group_tests.rb b/tests/hp/requests/compute_v2/server_security_group_tests.rb new file mode 100644 index 000000000..2c0f06146 --- /dev/null +++ b/tests/hp/requests/compute_v2/server_security_group_tests.rb @@ -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