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:
parent
2488caf8ad
commit
1fb8b19510
10 changed files with 163 additions and 58 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -4,39 +4,54 @@ module Fog
|
|||
module Compute
|
||||
class Cloudstack
|
||||
class Server < Fog::Compute::Server
|
||||
identity :id, :aliases => 'id'
|
||||
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
|
||||
attribute :memory
|
||||
attribute :display_name, :aliases => 'displayname'
|
||||
attribute :domain_id, :aliases => 'domainid'
|
||||
attribute :host_id, :aliases => 'hostid'
|
||||
attribute :host_name, :aliases => 'hostname'
|
||||
attribute :project_id, :aliases => 'projectid'
|
||||
attribute :zone_id, :aliases => 'zoneid'
|
||||
attribute :zone_name, :aliases => 'zonename'
|
||||
attribute :image_id, :aliases => ['templateid', :template_id]
|
||||
attribute :image_name, :aliases => ['templatename', :template_name]
|
||||
attribute :templated_display_text, :aliases => 'templatedisplaytext'
|
||||
attribute :password_enabled, :aliases => 'passwordenabled'
|
||||
attribute :flavor_id, :aliases => ['serviceofferingid', :service_offering_id]
|
||||
attribute :flavor_name, :aliases => ['serviceofferingname', :service_offering_name]
|
||||
attribute :cpu_number, :aliases => 'cpunumber'
|
||||
attribute :cpu_speed, :aliases => 'cpuspeed'
|
||||
attribute :cpu_used, :aliases => 'cpuused'
|
||||
attribute :network_kbs_read, :aliases => 'networkkbsread'
|
||||
attribute :network_kbs_write, :aliases => 'networkkbswrite'
|
||||
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 :display_name, :aliases => 'displayname'
|
||||
attribute :domain_id, :aliases => 'domainid'
|
||||
attribute :host_id, :aliases => 'hostid'
|
||||
attribute :host_name, :aliases => 'hostname'
|
||||
attribute :project_id, :aliases => 'projectid'
|
||||
attribute :zone_id, :aliases => 'zoneid'
|
||||
attribute :zone_name, :aliases => 'zonename'
|
||||
attribute :image_id, :aliases => ['templateid', :template_id]
|
||||
attribute :image_name, :aliases => ['templatename', :template_name]
|
||||
attribute :templated_display_text, :aliases => 'templatedisplaytext'
|
||||
attribute :password_enabled, :aliases => 'passwordenabled'
|
||||
attribute :flavor_id, :aliases => ['serviceofferingid', :service_offering_id]
|
||||
attribute :flavor_name, :aliases => ['serviceofferingname', :service_offering_name]
|
||||
attribute :cpu_number, :aliases => 'cpunumber'
|
||||
attribute :cpu_speed, :aliases => 'cpuspeed'
|
||||
attribute :cpu_used, :aliases => 'cpuused'
|
||||
attribute :network_kbs_read, :aliases => 'networkkbsread'
|
||||
attribute :network_kbs_write, :aliases => 'networkkbswrite'
|
||||
attribute :guest_os_id, :aliases => 'guestosid'
|
||||
attribute :root_device_id, :aliases => 'rootdeviceid'
|
||||
attribute :root_device_type, :aliases => 'rootdevicetype'
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
20
lib/fog/cloudstack/requests/compute/create_zone.rb
Normal file
20
lib/fog/cloudstack/requests/compute/create_zone.rb
Normal 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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
18
tests/cloudstack/compute/models/server_tests.rb
Normal file
18
tests/cloudstack/compute/models/server_tests.rb
Normal 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
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue