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

[cloudstack|server] assign security group

This commit is contained in:
Josh Lane & Jason Hansen 2012-07-18 16:00:32 -07:00 committed by Josh Lane
parent 2488caf8ad
commit 1fb8b19510
10 changed files with 163 additions and 58 deletions

View file

@ -38,6 +38,7 @@ module Fog
request :acquire_ip_address
request :assign_to_load_balancer_rule
request :assign_virtual_machine
request :attach_volume
request :authorize_security_group_egress
request :authorize_security_group_ingress
@ -53,6 +54,7 @@ module Fog
request :create_snapshot_policy
request :create_user
request :create_volume
request :create_zone
request :delete_account
request :delete_domain
request :delete_load_balancer_rule

View file

@ -13,20 +13,6 @@ module Fog
attribute :ingress_rules, :aliases => "ingressrule", :type => :array
attribute :egress_rules, :aliases => "egressrule", :type => :array
def save
requires :name
options = {
'name' => self.name,
'account' => self.account,
'description' => self.description,
'projectid' => self.project_id,
'domainid' => self.domain_id,
}
data = connection.create_security_group(options)
merge_attributes(data['createsecuritygroupresponse']['securitygroup'])
end
def destroy
requires :id
connection.delete_security_group('id' => self.id)
@ -41,6 +27,20 @@ module Fog
attributes[:ingress_rules] || []
end
def save
requires :name
options = {
'name' => self.name,
'account' => self.account,
'description' => self.description,
'projectid' => self.project_id,
'domainid' => self.domain_id,
}
data = connection.create_security_group(options)
merge_attributes(data['createsecuritygroupresponse']['securitygroup'])
end
def rules
connection.security_group_rules.all("security_group_id" => self.id)
end

View file

@ -6,8 +6,8 @@ module Fog
class Server < Fog::Compute::Server
identity :id, :aliases => 'id'
attribute :name
attribute :account
attribute :domain
attribute :account_name, :aliases => 'account'
attribute :domain_name, :aliases => 'domain'
attribute :created
attribute :state
attribute :haenable
@ -33,10 +33,25 @@ module Fog
attribute :guest_os_id, :aliases => 'guestosid'
attribute :root_device_id, :aliases => 'rootdeviceid'
attribute :root_device_type, :aliases => 'rootdevicetype'
attribute :security_group, :aliases => 'securitygroup'
attribute :nics, :aliases => 'nic'
attribute :security_group_list, :type => :array, :aliases => 'securitygroup'
attribute :nics, :type => :array, :aliases => 'nic'
attr_accessor :network_ids, :disk_offering_id, :ip_address, :ip_to_network_list
attr_writer :security_group_ids
def addresses
nics.map{|nic| Address.new(nic)}
end
def destroy
requires :id
data = connection.destroy_virtual_machine("id" => id)
connection.jobs.new(data["destroyvirtualmachineresponse"])
end
def flavor
connection.flavors.get(self.flavor_id)
end
def ready?
state == 'Running'
@ -48,6 +63,18 @@ module Fog
connection.jobs.new(data["rebootvirtualmachineresponse"])
end
def security_groups=(security_groups)
self.security_group_ids= Array(security_groups).map(&:id)
end
def security_group_ids
@security_group_ids || (self.security_group_list || []).map{|sg| sg["id"]}
end
def security_groups
security_group_ids.map{|id| self.connection.security_groups.get(id)}
end
def save
requires :image_id, :flavor_id, :zone_id
@ -62,27 +89,26 @@ module Fog
'hostid' => host_id,
'ipaddress' => ip_address,
'iptonetworklist' => ip_to_network_list,
'projectid' => project_id
'projectid' => project_id,
}
options.merge!('networkids' => network_ids) if network_ids
options.merge!('securitygroupids' => security_group_ids) if security_group_ids
data = connection.deploy_virtual_machine(options)
merge_attributes(data['deployvirtualmachineresponse'])
end
def addresses
nics.map{|nic| Address.new(nic)}
end
def flavor
connection.flavors.get(self.flavor_id)
end
def destroy
def start
requires :id
connection.destroy_virtual_machine(:id => id)
true
data = connection.start_virtual_machine("id" => self.id)
connection.jobs.new(data["startvirtualmachineresponse"])
end
def stop(force=false)
requires :id
data = connection.stop_virtual_machine("id" => self.id, "force" => force)
connection.jobs.new(data["stopvirtualmachineresponse"])
end
end # Server
end # Cloudstack

View file

@ -26,12 +26,11 @@ module Fog
'domainid' => domain_id,
'guestcidraddress' => guest_cidr_address,
'internaldns2' => internaldns2,
'securitygroupenabled' => security_group_enabled,
'securitygroupenabled' => security_groups_enabled,
}
data = connection.create_zone(options)
merge_attributes(data['createzoneresponse'])
end
end # Zone
end # Cloudstack
end # Compute

View file

@ -0,0 +1,18 @@
module Fog
module Compute
class Cloudstack
class Real
def assign_virtual_machine(options={})
options.merge!('command' => 'assignVirtualMachine')
request(options)
end
end
class Mock
# Fog::Compute::Cloudstack::Error: Failed to move vm VM is Running, unable to move the vm VM[User|e845934a-e44f-43da-aabf-05c90c651756]
#def assign_virtual_machine(options={})
#end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Lists zones.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/listZones.html]
def create_zone(options={})
options.merge!(
'command' => 'createZone'
)
request(options)
end
end # Real
end # Cloudstack
end # Compute
end # Fog

View file

@ -19,9 +19,31 @@ module Fog
class Mock
def destroy_virtual_machine(options={})
identity = options[:id]
identity = options["id"]
server = self.data[:servers][identity]
unless server
raise Fog::Compute::Cloudstack::BadRequest.new("Unable to execute API command attachserver due to invalid value. Object servers(uuid: #{identity}) does not exist.")
end
job_id = Fog::Cloudstack.uuid
job = {
"cmd" => "com.cloud.api.commands.DestroyVirtualMachineCmd",
"created" => Time.now.iso8601,
"jobid" => job_id,
"jobstatus" => 1,
"jobprocstatus" => 0,
"jobresultcode" => 0,
"jobresulttype" => "object",
"jobresult" =>
{"virtualmachine" => server}
}
self.data[:jobs][job_id]= job
self.data[:servers].delete(identity)
{"destroyvirtualmachineresponse" => {"jobid" => job_id}}
end
end
end # Cloudstack

View file

@ -1,5 +1,5 @@
def security_group_tests(connection, params, mocks_implemented = true)
model_tests(connection.security_groups, params, mocks_implemented) do
model_tests(connection.security_groups, params[:security_group_attributes], mocks_implemented) do
if Fog.mocking? && !mocks_implemented
pending
else
@ -11,6 +11,6 @@ end
provider, config = :cloudstack, compute_providers[:cloudstack]
Shindo.tests("Fog::Compute[:#{provider}] | security_groups", [provider.to_s]) do
security_group_tests(Fog::Compute[:cloudstack], config[:security_group_attributes], config[:mocked])
security_group_tests(Fog::Compute[:cloudstack], config, config[:mocked])
end

View file

@ -0,0 +1,18 @@
provider, config = :cloudstack, compute_providers[:cloudstack]
Shindo.tests("Fog::Compute[:#{provider}] | servers + security_groups", [provider.to_s]) do
connection = Fog::Compute[provider]
@security_group = connection.security_groups.create(config[:security_group_attributes])
@server = connection.servers.create(config[:server_attributes].merge(:security_groups => [@security_group]))
tests('#security_group').succeeds do
@server.wait_for { ready? }
@server.security_groups.map(&:id) == [@security_group.id]
end
tests('#destroy').succeeds do
@server.destroy.wait_for { ready? }
@security_group.destroy
end
end

View file

@ -41,7 +41,7 @@ def compute_providers
hash[k]= Fog.credentials["cloudstack_#{k}".to_sym]
end
end,
:security_group_attributes => {:name => "somesecuritygroup"},
:security_group_attributes => {:name => "cloudstack.sg.#{Time.now.to_i}"},
:security_group_rule_attributes => {
:cidr => '0.0.0.0/0',
:start_port => 123,