Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit 0192e53ef7acbfa8a08ac2b98d0d920efd575294 1 parent 216cf26
Thom Mahoney & Eugene Howe authored lanej committed
View
3  lib/fog/openstack/compute.rb
@@ -28,6 +28,8 @@ class OpenStack < Fog::Service
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 @@ class OpenStack < Fog::Service
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
1  lib/fog/openstack/models/compute/host.rb
@@ -10,6 +10,7 @@ class Host < Fog::Model
attribute :host_name
attribute :service_name
attribute :details
+ attribute :zone
def initialize(attributes)
attributes["service_name"] = attributes.delete "service"
View
22 lib/fog/openstack/models/compute/security_group.rb
@@ -3,16 +3,30 @@
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 @@ def save
true
end
-
def destroy
requires :id
service.delete_security_group(id)
@@ -29,15 +42,16 @@ def destroy
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
32 lib/fog/openstack/models/compute/security_group_rule.rb
@@ -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
22 lib/fog/openstack/models/compute/security_group_rules.rb
@@ -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
36 lib/fog/openstack/network.rb
@@ -118,10 +118,40 @@ class OpenStack < Fog::Service
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 @@ def self.data
"subnet" => 10,
"network" => 10,
"floatingip" => 50,
- "tenant_id" => Fog::Mock.random_hex(8),
+ "tenant_id" => tenant_id,
"router" => 10,
"port" => 30
}
View
6 lib/fog/openstack/requests/compute/create_security_group.rb
@@ -23,10 +23,10 @@ def create_security_group(name, description)
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 @@ def create_security_group(name, description)
'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
2  lib/fog/openstack/requests/compute/create_security_group_rule.rb
@@ -47,7 +47,7 @@ def create_security_group_rule(parent_group_id, ip_protocol, from_port, to_port,
'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
17 lib/fog/openstack/requests/compute/create_server.rb
@@ -13,7 +13,7 @@ def create_server(name, image_ref, flavor_ref, options = {})
}
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 @@ def create_server(name, image_ref, flavor_ref, options = {})
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 @@ def create_server(name, image_ref, flavor_ref, options = {})
'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 @@ def create_server(name, image_ref, flavor_ref, options = {})
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
2  lib/fog/openstack/requests/compute/delete_security_group.rb
@@ -15,7 +15,7 @@ def delete_security_group(security_group_id)
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
2  lib/fog/openstack/requests/compute/delete_security_group_rule.rb
@@ -15,6 +15,8 @@ def delete_security_group_rule(security_group_rule_id)
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
38 lib/fog/openstack/requests/compute/get_security_group.rb
@@ -15,32 +15,22 @@ def get_security_group(security_group_id)
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
38 lib/fog/openstack/requests/compute/get_security_group_rule.rb
@@ -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
6 lib/fog/openstack/requests/compute/list_hosts.rb
@@ -14,12 +14,12 @@ def list_hosts
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 @@ def list_hosts
end # mock
end # openstack
end # compute
-end # fog
+end # fog
View
4 lib/fog/openstack/volume.rb
@@ -9,7 +9,7 @@ class OpenStack < Fog::Service
: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 @@ def initialize(options={})
@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 @@ def request(params)
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
54 tests/openstack/models/compute/security_group_tests.rb
@@ -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
19 tests/openstack/requests/compute/security_group_tests.rb
@@ -10,12 +10,12 @@
}
@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 @@
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
16 tests/openstack/requests/network/network_tests.rb
@@ -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 @@
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)

5 comments on commit 0192e53

@thommahoney

@burns you are correct. We're using string keys for the security group mock data now. So that seed data does need to change.

@thommahoney

@burns master seems to be green. Let me know if you want me to look into a specific failure. https://travis-ci.org/fog/fog/builds/15552477

@geemus
Owner

Yeah, any insight would be awesome (I was just trying to look in to it and nothing was jumping out at me as obvious in terms of why it would sometimes be failing on us).

@geemus
Owner

Just did a bit of work around it here: b056df5

Not sure it will fix it (since I'm not sure why it was breaking), but it at least streamlines and removes some of the failure vectors.

@geemus
Owner

Looks like it might be fixed, fwiw. Now a different error is breaking the build anyway. Will continue to monitor.

Please sign in to comment.
Something went wrong with that request. Please try again.