Skip to content

Commit

Permalink
openstack modifications
Browse files Browse the repository at this point in the history
* 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
Thom Mahoney & Eugene Howe authored and lanej committed Dec 10, 2013
1 parent 216cf26 commit 0192e53
Show file tree
Hide file tree
Showing 18 changed files with 261 additions and 59 deletions.
3 changes: 3 additions & 0 deletions lib/fog/openstack/compute.rb
Expand Up @@ -28,6 +28,8 @@ class OpenStack < Fog::Service
collection :addresses collection :addresses
model :security_group model :security_group
collection :security_groups collection :security_groups
model :security_group_rule
collection :security_group_rules
model :key_pair model :key_pair
collection :key_pairs collection :key_pairs
model :tenant model :tenant
Expand Down Expand Up @@ -128,6 +130,7 @@ class OpenStack < Fog::Service
request :create_security_group_rule request :create_security_group_rule
request :delete_security_group request :delete_security_group
request :delete_security_group_rule request :delete_security_group_rule
request :get_security_group_rule


# Key Pair # Key Pair
request :list_key_pairs request :list_key_pairs
Expand Down
1 change: 1 addition & 0 deletions lib/fog/openstack/models/compute/host.rb
Expand Up @@ -10,6 +10,7 @@ class Host < Fog::Model
attribute :host_name attribute :host_name
attribute :service_name attribute :service_name
attribute :details attribute :details
attribute :zone


def initialize(attributes) def initialize(attributes)
attributes["service_name"] = attributes.delete "service" attributes["service_name"] = attributes.delete "service"
Expand Down
22 changes: 18 additions & 4 deletions lib/fog/openstack/models/compute/security_group.rb
Expand Up @@ -3,16 +3,30 @@
module Fog module Fog
module Compute module Compute
class OpenStack class OpenStack

class SecurityGroup < Fog::Model class SecurityGroup < Fog::Model


identity :id identity :id


attribute :name attribute :name
attribute :description attribute :description
attribute :rules attribute :security_group_rules, :aliases => "rules"
attribute :tenant_id 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 def save
requires :name, :description requires :name, :description
Expand All @@ -21,23 +35,23 @@ def save
true true
end end



def destroy def destroy
requires :id requires :id
service.delete_security_group(id) service.delete_security_group(id)
true true
end end


def create_security_group_rule(min, max, ip_protocol = "tcp", cidr = "0.0.0.0/0", group_id = nil) 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 requires :id
service.create_security_group_rule(id, ip_protocol, min, max, cidr, group_id) service.create_security_group_rule(id, ip_protocol, min, max, cidr, group_id)
end end


def delete_security_group_rule(rule_id) 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) service.delete_security_group_rule(rule_id)
true true
end end

end end
end end
end end
Expand Down
32 changes: 32 additions & 0 deletions 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
22 changes: 22 additions & 0 deletions 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
36 changes: 33 additions & 3 deletions lib/fog/openstack/network.rb
Expand Up @@ -118,10 +118,40 @@ class OpenStack < Fog::Service
class Mock class Mock
def self.data def self.data
@data ||= Hash.new do |hash, key| @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] = { 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 => {}, :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 => {}, :floating_ips => {},
:routers => {}, :routers => {},
:lb_pools => {}, :lb_pools => {},
Expand All @@ -140,7 +170,7 @@ def self.data
"subnet" => 10, "subnet" => 10,
"network" => 10, "network" => 10,
"floatingip" => 50, "floatingip" => 50,
"tenant_id" => Fog::Mock.random_hex(8), "tenant_id" => tenant_id,
"router" => 10, "router" => 10,
"port" => 30 "port" => 30
} }
Expand Down
6 changes: 3 additions & 3 deletions lib/fog/openstack/requests/compute/create_security_group.rb
Expand Up @@ -23,10 +23,10 @@ def create_security_group(name, description)


class Mock class Mock
def create_security_group(name, description) 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 tenant_id = Fog::Identity::OpenStack::Mock.data[current_tenant][:tenants].keys.first
security_group_id = Fog::Mock.random_numbers(2).to_i 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, 'tenant_id' => tenant_id,
'rules' => [], 'rules' => [],
'id' => security_group_id, 'id' => security_group_id,
Expand All @@ -42,7 +42,7 @@ def create_security_group(name, description)
'Content-Length' => Fog::Mock.random_numbers(3).to_s, 'Content-Length' => Fog::Mock.random_numbers(3).to_s,
'Date' => Date.new} 'Date' => Date.new}
response.body = { response.body = {
'security_group' => self.data[:security_groups][security_group_id] 'security_group' => self.data[:security_groups][security_group_id.to_s]
} }
response response
end end
Expand Down
Expand Up @@ -47,7 +47,7 @@ def create_security_group_rule(parent_group_id, ip_protocol, from_port, to_port,
'cidr' => cidr '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 = { response.body = {
'security_group_rule' => rule 'security_group_rule' => rule
} }
Expand Down
17 changes: 12 additions & 5 deletions lib/fog/openstack/requests/compute/create_server.rb
Expand Up @@ -13,7 +13,7 @@ def create_server(name, image_ref, flavor_ref, options = {})
} }


vanilla_options = ['metadata', 'accessIPv4', 'accessIPv6', vanilla_options = ['metadata', 'accessIPv4', 'accessIPv6',
'availability_zone', 'user_data', 'key_name', 'availability_zone', 'user_data', 'key_name',
'adminPass', 'config_drive', 'min_count', 'max_count', 'adminPass', 'config_drive', 'min_count', 'max_count',
'return_reservation_id' 'return_reservation_id'
] ]
Expand Down Expand Up @@ -107,7 +107,6 @@ def create_server(name, image_ref, flavor_ref, options = {})
response.body["user"]["id"] response.body["user"]["id"]
end end



mock_data = { mock_data = {
'addresses' => {}, 'addresses' => {},
'flavor' => {"id" => flavor_ref, "links"=>[{"href"=>"http://nova1:8774/admin/flavors/1", "rel"=>"bookmark"}]}, 'flavor' => {"id" => flavor_ref, "links"=>[{"href"=>"http://nova1:8774/admin/flavors/1", "rel"=>"bookmark"}]},
Expand All @@ -127,10 +126,18 @@ def create_server(name, image_ref, flavor_ref, options = {})
'config_drive' => options['config_drive'] || '', '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 = {} response_data = {}
if options['return_reservation_id'] == 'True' then if options['return_reservation_id'] == 'True' then
response_data = { 'reservation_id' => "r-#{Fog::Mock.random_numbers(6).to_s}" } response_data = { 'reservation_id' => "r-#{Fog::Mock.random_numbers(6).to_s}" }
else else
response_data = { response_data = {
'adminPass' => 'password', 'adminPass' => 'password',
'id' => server_id, 'id' => server_id,
Expand All @@ -156,12 +163,12 @@ def create_server(name, image_ref, flavor_ref, options = {})
self.data[:last_modified][:servers][server_id] = Time.now self.data[:last_modified][:servers][server_id] = Time.now
self.data[:servers][server_id] = mock_data self.data[:servers][server_id] = mock_data
if options['return_reservation_id'] == 'True' then if options['return_reservation_id'] == 'True' then
response.body = response_data response.body = response_data
else else
response.body = { 'server' => response_data } response.body = { 'server' => response_data }
end end
response response
end end
end end
end end
end end
Expand Down
Expand Up @@ -15,7 +15,7 @@ def delete_security_group(security_group_id)


class Mock class Mock
def delete_security_group(security_group_id) 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 = Excon::Response.new
response.status = 202 response.status = 202
Expand Down
Expand Up @@ -15,6 +15,8 @@ def delete_security_group_rule(security_group_rule_id)


class Mock class Mock
def delete_security_group_rule(security_group_rule_id) 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 = Excon::Response.new
response.status = 202 response.status = 202
response.headers = { response.headers = {
Expand Down
38 changes: 14 additions & 24 deletions lib/fog/openstack/requests/compute/get_security_group.rb
Expand Up @@ -15,32 +15,22 @@ def get_security_group(security_group_id)


class Mock class Mock
def get_security_group(security_group_id) def get_security_group(security_group_id)
security_group = self.data[:security_groups][security_group_id.to_s]
response = Excon::Response.new response = Excon::Response.new
response.status = 200 if security_group
response.headers = { response.status = 200
"X-Compute-Request-Id" => "req-63a90344-7c4d-42e2-936c-fd748bced1b3", response.headers = {
"Content-Type" => "application/json", "X-Compute-Request-Id" => "req-63a90344-7c4d-42e2-936c-fd748bced1b3",
"Content-Length" => "167", "Content-Type" => "application/json",
"Date" => Date.new "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"
} }
} response.body = {
"security_group" => security_group
}
else
raise Fog::Compute::OpenStack::NotFound, "Security group #{security_group_id} does not exist"
end
response response
end end
end # mock end # mock
Expand Down
38 changes: 38 additions & 0 deletions 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

5 comments on commit 0192e53

@thommahoney
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link
Member

@geemus geemus commented on 0192e53 Dec 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

@geemus geemus commented on 0192e53 Dec 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

@geemus geemus commented on 0192e53 Dec 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.