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

openstack modifications

* add model and collection for security group rules
* add mock data for networks
* returns address in create_server mock
* proper security group rule mocks
* proper security group mocks
This commit is contained in:
Thom Mahoney & Eugene Howe 2013-12-06 15:32:52 -08:00 committed by Brian Nelson
parent 2a215ac166
commit a4186438c0
18 changed files with 261 additions and 59 deletions

View file

@ -28,6 +28,8 @@ module Fog
collection :addresses
model :security_group
collection :security_groups
model :security_group_rule
collection :security_group_rules
model :key_pair
collection :key_pairs
model :tenant
@ -128,6 +130,7 @@ module Fog
request :create_security_group_rule
request :delete_security_group
request :delete_security_group_rule
request :get_security_group_rule
# Key Pair
request :list_key_pairs

View file

@ -10,6 +10,7 @@ module Fog
attribute :host_name
attribute :service_name
attribute :details
attribute :zone
def initialize(attributes)
attributes["service_name"] = attributes.delete "service"

View file

@ -3,16 +3,30 @@ require 'fog/core/model'
module Fog
module Compute
class OpenStack
class SecurityGroup < Fog::Model
identity :id
attribute :name
attribute :description
attribute :rules
attribute :security_group_rules, :aliases => "rules"
attribute :tenant_id
def security_group_rules
Fog::Compute::OpenStack::SecurityGroupRules.new(:service => service).load(attributes[:security_group_rules])
end
def rules
Fog::Logger.deprecation('#rules is deprecated. Use #security_group_rules instead')
attributes[:security_group_rules]
end
# no one should be calling this because it doesn't do anything
# useful but we deprecated the rules attribute and need to maintain the API
def rules=(new_rules)
Fog::Logger.deprecation('#rules= is deprecated. Use the Fog::Compute::Openstack::SecurityGroupRules collection to create new rules.')
attributes[:security_group_rules] = new_rules
end
def save
requires :name, :description
@ -21,7 +35,6 @@ module Fog
true
end
def destroy
requires :id
service.delete_security_group(id)
@ -29,15 +42,16 @@ module Fog
end
def create_security_group_rule(min, max, ip_protocol = "tcp", cidr = "0.0.0.0/0", group_id = nil)
Fog::Logger.deprecation('#create_security_group_rule is deprecated. Use the Fog::Compute::Openstack::SecurityGroupRules collection to create new rules.')
requires :id
service.create_security_group_rule(id, ip_protocol, min, max, cidr, group_id)
end
def delete_security_group_rule(rule_id)
Fog::Logger.deprecation('#create_security_group_rule is deprecated. Use the Fog::Compute::Openstack::SecurityGroupRule objects to destroy rules.')
service.delete_security_group_rule(rule_id)
true
end
end
end
end

View file

@ -0,0 +1,32 @@
require 'fog/core/model'
module Fog
module Compute
class OpenStack
class SecurityGroupRule < Fog::Model
identity :id
attribute :from_port
attribute :group
attribute :ip_protocol
attribute :to_port
attribute :parent_group_id
attribute :ip_range
def save
requires :ip_protocol, :from_port, :to_port, :parent_group_id
cidr = ip_range && ip_range["cidr"]
if rule = service.create_security_group_rule(parent_group_id, ip_protocol, from_port, to_port, cidr, group).data[:body]
merge_attributes(rule["security_group_rule"])
end
end
def destroy
requires :id
service.delete_security_group_rule(id)
true
end
end
end
end
end

View file

@ -0,0 +1,22 @@
require 'fog/core/collection'
require 'fog/openstack/models/compute/security_group_rule'
module Fog
module Compute
class OpenStack
class SecurityGroupRules < Fog::Collection
model Fog::Compute::OpenStack::SecurityGroupRule
def get(security_group_rule_id)
if security_group_rule_id
body = service.get_security_group_rule(security_group_rule_id).body
new(body['security_group_rule'])
end
rescue Fog::Compute::OpenStack::NotFound
nil
end
end
end
end
end

View file

@ -118,10 +118,40 @@ module Fog
class Mock
def self.data
@data ||= Hash.new do |hash, key|
network_id = Fog::UUID.uuid
subnet_id = Fog::UUID.uuid
tenant_id = Fog::Mock.random_hex(8)
hash[key] = {
:networks => {},
:networks => {
network_id => {
'id' => network_id,
'name' => 'Public',
'subnets' => [subnet_id],
'shared' => true,
'status' => 'ACTIVE',
'tenant_id' => tenant_id,
'provider_network_type' => 'vlan',
'router:external' => false,
'admin_state_up' => true,
}
},
:ports => {},
:subnets => {},
:subnets => {
subnet_id => {
'id' => subnet_id,
'name' => "Public",
'network_id' => network_id,
'cidr' => "192.168.0.0/22",
'ip_version' => 4,
'gateway_ip' => Fog::Mock.random_ip,
'allocation_pools' => [],
'dns_nameservers' => [Fog::Mock.random_ip, Fog::Mock.random_ip],
'host_routes' => [Fog::Mock.random_ip],
'enable_dhcp' => true,
'tenant_id' => tenant_id,
}
},
:floating_ips => {},
:routers => {},
:lb_pools => {},
@ -140,7 +170,7 @@ module Fog
"subnet" => 10,
"network" => 10,
"floatingip" => 50,
"tenant_id" => Fog::Mock.random_hex(8),
"tenant_id" => tenant_id,
"router" => 10,
"port" => 30
}

View file

@ -23,10 +23,10 @@ module Fog
class Mock
def create_security_group(name, description)
Fog::Identity.new(:provider => 'OpenStack')
Fog::Identity::OpenStack.new(:openstack_auth_url => credentials[:openstack_auth_url])
tenant_id = Fog::Identity::OpenStack::Mock.data[current_tenant][:tenants].keys.first
security_group_id = Fog::Mock.random_numbers(2).to_i
self.data[:security_groups][security_group_id] = {
self.data[:security_groups][security_group_id.to_s] = {
'tenant_id' => tenant_id,
'rules' => [],
'id' => security_group_id,
@ -42,7 +42,7 @@ module Fog
'Content-Length' => Fog::Mock.random_numbers(3).to_s,
'Date' => Date.new}
response.body = {
'security_group' => self.data[:security_groups][security_group_id]
'security_group' => self.data[:security_groups][security_group_id.to_s]
}
response
end

View file

@ -47,7 +47,7 @@ module Fog
'cidr' => cidr
}
}
self.data[:security_groups][parent_group_id]['rules'].push(rule)
self.data[:security_groups][parent_group_id.to_s]['rules'].push(rule)
response.body = {
'security_group_rule' => rule
}

View file

@ -13,7 +13,7 @@ module Fog
}
vanilla_options = ['metadata', 'accessIPv4', 'accessIPv6',
'availability_zone', 'user_data', 'key_name',
'availability_zone', 'user_data', 'key_name',
'adminPass', 'config_drive', 'min_count', 'max_count',
'return_reservation_id'
]
@ -107,7 +107,6 @@ module Fog
response.body["user"]["id"]
end
mock_data = {
'addresses' => {},
'flavor' => {"id" => flavor_ref, "links"=>[{"href"=>"http://nova1:8774/admin/flavors/1", "rel"=>"bookmark"}]},
@ -127,10 +126,18 @@ module Fog
'config_drive' => options['config_drive'] || '',
}
if nics = options['nics']
nics.each do |nic|
mock_data["addresses"].merge!(
"Public" => [{ 'addr' => Fog::Mock.random_ip }]
)
end
end
response_data = {}
if options['return_reservation_id'] == 'True' then
response_data = { 'reservation_id' => "r-#{Fog::Mock.random_numbers(6).to_s}" }
else
else
response_data = {
'adminPass' => 'password',
'id' => server_id,
@ -156,12 +163,12 @@ module Fog
self.data[:last_modified][:servers][server_id] = Time.now
self.data[:servers][server_id] = mock_data
if options['return_reservation_id'] == 'True' then
response.body = response_data
response.body = response_data
else
response.body = { 'server' => response_data }
end
response
end
end
end
end
end

View file

@ -15,7 +15,7 @@ module Fog
class Mock
def delete_security_group(security_group_id)
self.data[:security_groups].delete security_group_id
self.data[:security_groups].delete security_group_id.to_s
response = Excon::Response.new
response.status = 202

View file

@ -15,6 +15,8 @@ module Fog
class Mock
def delete_security_group_rule(security_group_rule_id)
security_group = self.data[:security_groups].values.detect{|sg| sg["rules"].detect{ |sgr| sgr["id"].to_s == security_group_rule_id.to_s }}
security_group["rules"].reject! { |sgr| sgr["id"] == security_group_rule_id }
response = Excon::Response.new
response.status = 202
response.headers = {

View file

@ -15,32 +15,22 @@ module Fog
class Mock
def get_security_group(security_group_id)
security_group = self.data[:security_groups][security_group_id.to_s]
response = Excon::Response.new
response.status = 200
response.headers = {
"X-Compute-Request-Id" => "req-63a90344-7c4d-42e2-936c-fd748bced1b3",
"Content-Type" => "application/json",
"Content-Length" => "167",
"Date" => Date.new
}
response.body = {
"security_group" => {
"rules" => [{
"from_port" => 44,
"group" => {},
"ip_protocol" => "tcp",
"to_port" => 55,
"parent_group_id" => 1,
"ip_range" => {
"cidr" => "10.10.10.10/24"
}, "id"=>1
}],
"tenant_id" => "d5183375ab0343f3a0b4b05f547aefc2",
"id"=>security_group_id,
"name"=>"default",
"description"=>"default"
if security_group
response.status = 200
response.headers = {
"X-Compute-Request-Id" => "req-63a90344-7c4d-42e2-936c-fd748bced1b3",
"Content-Type" => "application/json",
"Content-Length" => "167",
"Date" => Date.new
}
}
response.body = {
"security_group" => security_group
}
else
raise Fog::Compute::OpenStack::NotFound, "Security group #{security_group_id} does not exist"
end
response
end
end # mock

View file

@ -0,0 +1,38 @@
module Fog
module Compute
class OpenStack
class Real
def get_security_group_rule(security_group_rule_id)
request(
:expects => [200],
:method => 'GET',
:path => "os-security-group-rules/#{security_group_rule_id}"
)
end
end
class Mock
def get_security_group_rule(security_group_rule_id)
security_group_rule = nil
self.data[:security_groups].detect{|id, sg| security_group_rule = sg["rules"].detect{ |sgr| sgr["id"].to_s == security_group_rule_id.to_s }}
response = Excon::Response.new
if security_group_rule
response.status = 200
response.headers = {
"X-Compute-Request-Id" => "req-63a90344-7c4d-42e2-936c-fd748bced1b3",
"Content-Type" => "application/json",
"Content-Length" => "167",
"Date" => Date.new
}
response.body = {
"security_group_rule" => security_group_rule
}
else
raise Fog::Compute::OpenStack::NotFound, "Security group rule #{security_group_rule_id} does not exist"
end
response
end
end # mock
end # openstack
end #compute
end #fog

View file

@ -14,12 +14,12 @@ module Fog
end
class Mock
def list_hosts
response = Excon::Response.new
response.status = 200
response.body = { "hosts" => [
{"host_name" => "host.test.net", "service"=>"compute"}
{"host_name" => "host.test.net", "service"=>"compute", "zone" => "az1"}
]
}
response
@ -29,4 +29,4 @@ module Fog
end # mock
end # openstack
end # compute
end # fog
end # fog

View file

@ -9,7 +9,7 @@ module Fog
:openstack_service_type, :openstack_service_name, :openstack_tenant,
:openstack_api_key, :openstack_username,
:current_user, :current_tenant,
:openstack_endpoint_type
:openstack_endpoint_type, :openstack_region
model_path 'fog/openstack/models/volume'
@ -125,6 +125,7 @@ module Fog
@openstack_must_reauthenticate = false
@openstack_service_type = options[:openstack_service_type] || ['volume']
@openstack_service_name = options[:openstack_service_name]
@openstack_region = options[:openstack_region]
@openstack_endpoint_type = options[:openstack_endpoint_type] || 'adminURL'
@connection_options = options[:connection_options] || {}
@ -188,6 +189,7 @@ module Fog
def authenticate
if !@openstack_management_url || @openstack_must_reauthenticate
options = {
:openstack_region => @openstack_region,
:openstack_tenant => @openstack_tenant,
:openstack_api_key => @openstack_api_key,
:openstack_username => @openstack_username,

View file

@ -0,0 +1,54 @@
Shindo.tests("Fog::Compute[:openstack] | security_group", ['openstack']) do
tests('success') do
begin
fog = Fog::Compute[:openstack]
security_group = fog.security_groups.create(
:name => 'my_group',
:description => 'my group'
)
tests('#create').succeeds do
security_group = fog.security_groups.create(
:name => 'my_group',
:description => 'my group'
)
returns('my_group') { security_group.name }
returns('my group') { security_group.description }
returns([]) { security_group.security_group_rules }
returns(true) { security_group.tenant_id != nil }
end
tests('#rules').succeeds do
tests("#create").succeeds do
rules_count = security_group.security_group_rules.count
rule = security_group.security_group_rules.create(
:parent_group_id => security_group.id,
:ip_protocol => 'tcp',
:from_port => 1234,
:to_port => 1234,
:ip_range => { "cidr" => "0.0.0.0/0" }
)
returns(true) { security_group.security_group_rules.count == (rules_count + 1) }
security_group_rule = security_group.security_group_rules.detect { |r| r.id == rule.id }
returns(true) { security_group_rule.attributes == rule.attributes }
end
tests("#destroy").succeeds do
rule = security_group.security_group_rules.create(
:parent_group_id => security_group.id,
:ip_protocol => 'tcp',
:from_port => 1234,
:to_port => 1234,
:ip_range => { "cidr" => "0.0.0.0/0" }
)
rule.destroy
returns(true) { rule.reload == nil }
end
end
ensure
security_group.destroy if security_group
end
end
end

View file

@ -10,12 +10,12 @@ Shindo.tests('Fog::Compute[:openstack] | security group requests', ['openstack']
}
@security_group_rule_format = {
"id" => Integer,
"from_port" => Integer,
"to_port" => Integer,
"ip_protocol" => String,
"group" => Hash,
"ip_range" => Hash,
"id" => Integer,
"from_port" => Integer,
"to_port" => Integer,
"ip_protocol" => String,
"group" => Hash,
"ip_range" => Hash,
"parent_group_id" => Integer
}
@ -38,6 +38,13 @@ Shindo.tests('Fog::Compute[:openstack] | security group requests', ['openstack']
Fog::Compute[:openstack].get_security_group(group_id).body
end
tests('#get_security_group_rule').formats({"security_group_rule" => @security_group_rule_format}) do
parent_group_id = Fog::Compute[:openstack].list_security_groups.body['security_groups'].last['id']
Fog::Compute[:openstack].create_security_group_rule(parent_group_id, "tcp", 2222, 3333, "20.20.20.20/24").body
rule_id = Fog::Compute[:openstack].list_security_groups.body['security_groups'].last['rules'].first['id']
Fog::Compute[:openstack].get_security_group_rule(rule_id).body
end
tests('#delete_security_group_rule(security_group_rule_id)').succeeds do
security_group_rule_id = Fog::Compute[:openstack].list_security_groups.body['security_groups'].last['rules'].last['id']
Fog::Compute[:openstack].delete_security_group_rule(security_group_rule_id)

View file

@ -1,13 +1,13 @@
Shindo.tests('Fog::Network[:openstack] | network requests', ['openstack']) do
@network_format = {
'id' => String,
'name' => String,
'subnets' => Array,
'shared' => Fog::Boolean,
'status' => String,
'admin_state_up' => Fog::Boolean,
'tenant_id' => String
'id' => String,
'name' => String,
'subnets' => Array,
'shared' => Fog::Boolean,
'status' => String,
'admin_state_up' => Fog::Boolean,
'tenant_id' => String,
}
@network_format_extensions = {
@ -98,7 +98,7 @@ Shindo.tests('Fog::Network[:openstack] | network requests', ['openstack']) do
Fog::Network[:openstack].delete_network(0)
end
end
# Cleaning up the mess
Fog::Network[:openstack].networks.each do |n|
Fog::Network[:openstack].delete_network(n.id)