Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple network ids and network names #148

Merged
merged 4 commits into from
Apr 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ to update UUIDs in your Vagrantfile. If both are specified, the id parameter tak
* `instance_ready_timeout` - The number of seconds to wait for the instance
to become "ready" in Cloudstack. Defaults to 120 seconds.
* `domain_id` - Domain id to launch the instance into
* `network_id` - Network uuid that the instance should use
* `network_name` - Network name that the instance should use
* `network_id` - Network uuid(s) that the instance should use
- `network_id` is single value (e.g. `"AAAA"`) or multiple values (e.g. `["AAAA", "BBBB"]`)
* `network_name` - Network name(s) that the instance should use
- `network_name` is single value (e.g. `"AAAA"`) or multiple values (e.g. `["AAAA", "BBBB"]`)
* `project_id` - Project uuid that the instance should belong to
* `service_offering_id`- Service offering uuid to use for the instance
* `service_offering_name`- Service offering name to use for the instance
Expand Down
33 changes: 26 additions & 7 deletions lib/vagrant-cloudstack/action/run_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def call(env)
sanitize_domain_config

@zone = CloudstackResource.new(@domain_config.zone_id, @domain_config.zone_name, 'zone')
@network = CloudstackResource.new(@domain_config.network_id, @domain_config.network_name, 'network')
@networks = CloudstackResource.create_list(@domain_config.network_id, @domain_config.network_name, 'network')
@service_offering = CloudstackResource.new(@domain_config.service_offering_id, @domain_config.service_offering_name, 'service_offering')
@disk_offering = CloudstackResource.new(@domain_config.disk_offering_id, @domain_config.disk_offering_name, 'disk_offering')
@template = CloudstackResource.new(@domain_config.template_id, @domain_config.template_name || @env[:machine].config.vm.box, 'template')
Expand All @@ -48,8 +48,8 @@ def call(env)

if cs_zone.network_type.downcase == 'basic'
# No network specification in basic zone
@env[:ui].warn(I18n.t('vagrant_cloudstack.basic_network', :zone_name => @zone.name)) if @network.id || @network.name
@network = CloudstackResource.new(nil, nil, 'network')
@env[:ui].warn(I18n.t('vagrant_cloudstack.basic_network', :zone_name => @zone.name)) if !@networks.empty? && (@networks[0].id || @networks[0].name)
@networks = [CloudstackResource.new(nil, nil, 'network')]

# No portforwarding in basic zone, so none of the below
@domain_config.pf_ip_address = nil
Expand All @@ -58,7 +58,9 @@ def call(env)
@domain_config.pf_public_rdp_port = nil
@domain_config.pf_public_port_randomrange = nil
else
@resource_service.sync_resource(@network)
@networks.each do |network|
@resource_service.sync_resource(network)
end
end

if cs_zone.security_groups_enabled
Expand Down Expand Up @@ -91,7 +93,9 @@ def call(env)
@env[:ui].info(" -- Template: #{@template.name} (#{@template.id})")
@env[:ui].info(" -- Project UUID: #{@domain_config.project_id}") unless @domain_config.project_id.nil?
@env[:ui].info(" -- Zone: #{@zone.name} (#{@zone.id})")
@env[:ui].info(" -- Network: #{@network.name} (#{@network.id})") unless @network.id.nil?
@networks.each do |network|
@env[:ui].info(" -- Network: #{network.name} (#{network.id})")
end
@env[:ui].info(" -- Keypair: #{@domain_config.keypair}") if @domain_config.keypair
@env[:ui].info(' -- User Data: Yes') if @domain_config.user_data
@security_groups.each do |security_group|
Expand Down Expand Up @@ -121,6 +125,21 @@ def call(env)
def sanitize_domain_config
# Accept a single entry as input, convert it to array
@domain_config.pf_trusted_networks = [@domain_config.pf_trusted_networks] if @domain_config.pf_trusted_networks

if @domain_config.network_id.nil?
# Use names if ids are not present
@domain_config.network_id = []

if @domain_config.network_name.nil?
@domain_config.network_name = []
else
@domain_config.network_name = Array(@domain_config.network_name)
end
else
# Use ids if present
@domain_config.network_id = Array(@domain_config.network_id)
@domain_config.network_name = []
end
end

def configure_networking
Expand Down Expand Up @@ -205,7 +224,7 @@ def create_vm
:image_id => @template.id
}

options['network_ids'] = @network.id unless @network.id.nil?
options['network_ids'] = @networks.map(&:id).compact.join(",") unless @networks.empty?
options['security_group_ids'] = @security_groups.map{|security_group| security_group.id}.join(',') unless @security_groups.empty?
options['project_id'] = @domain_config.project_id unless @domain_config.project_id.nil?
options['key_name'] = @domain_config.keypair unless @domain_config.keypair.nil?
Expand All @@ -228,7 +247,7 @@ def create_vm
# XXX FIXME vpc?
if e.message =~ /subnet ID/
raise Errors::FogError,
:message => "Subnet ID not found: #{@network.id}"
:message => "Subnet ID not found: #{@networks.map(&:id).compact.join(",")}"
end

raise
Expand Down
8 changes: 4 additions & 4 deletions lib/vagrant-cloudstack/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ class Config < Vagrant.plugin("2", :config)
# @return [String]
attr_accessor :domain_id

# Network uuid that the instance should use
# Network uuid(s) that the instance should use
#
# @return [String]
# @return [String,Array]
attr_accessor :network_id

# Network name that the instance should use
# Network name(s) that the instance should use
#
# @return [String]
# @return [String,Array]
attr_accessor :network_name

# Network Type
Expand Down
18 changes: 18 additions & 0 deletions lib/vagrant-cloudstack/model/cloudstack_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ def is_name_undefined?
def to_s
"#{kind} - #{id || '<unknown id>'}:#{name || '<unknown name>'}"
end

def self.create_list(ids, names, kind)
return create_id_list(ids, kind) unless ids.empty?
return create_name_list(names, kind) unless names.empty?
[]
end

def self.create_id_list(ids, kind)
ids.each_with_object([]) do |id, resources|
resources << CloudstackResource.new(id, nil, kind)
end
end

def self.create_name_list(names, kind)
names.each_with_object([]) do |name, resources|
resources << CloudstackResource.new(nil, name, kind)
end
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
require 'coveralls'
require 'rspec/its'

Dir["#{__dir__}/vagrant-cloudstack/support/**/*.rb"].each { |f| require f }

SimpleCov.start
Coveralls.wear!
22 changes: 22 additions & 0 deletions spec/vagrant-cloudstack/model/cloudstack_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,26 @@
it { expect(CloudstackResource.new(nil, 'name', 'kind').is_name_undefined?).to be_eql false }
it { expect(CloudstackResource.new('', 'name', 'kind').is_name_undefined?).to be_eql false }
end

describe '#create_id_list' do
subject { CloudstackResource.create_id_list(ids, kind) }

let(:kind) { 'network' }
let(:ids) { %w(id1 id2) }

its(:count) { should eq 2 }
its([0]) { should be_a_resource('id1', nil, kind) }
its([1]) { should be_a_resource('id2', nil, kind) }
end

describe '#create_name_list' do
subject { CloudstackResource.create_name_list(names, kind) }

let(:kind) { 'network' }
let(:names) { %w(name1 name2) }

its(:count) { should eq 2 }
its([0]) { should be_a_resource(nil, 'name1', kind) }
its([1]) { should be_a_resource(nil, 'name2', kind) }
end
end
6 changes: 6 additions & 0 deletions spec/vagrant-cloudstack/support/be_a_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RSpec::Matchers.define :be_a_resource do |id, name, kind|
match do |actual|
actual.is_a?(VagrantPlugins::Cloudstack::Model::CloudstackResource) &&
actual.id == id && actual.name == name && actual.kind == kind
end
end