Skip to content

Commit

Permalink
bug 1614567: Allow user to select Network Port for provisioning
Browse files Browse the repository at this point in the history
fixes https://bugzilla.redhat.com/show_bug.cgi?id=1614567

Allow user to select Network Port or Cloud Network when provisioning
an OpenStack cloud instance.
  • Loading branch information
sseago committed Feb 19, 2019
1 parent 77c5817 commit 2295d60
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 11 deletions.
Expand Up @@ -11,7 +11,12 @@ def configure_network_adapters
networks = Array(options[:networks])

# Set the first nic to whatever was selected in the dialog if not set by automate
networks[0] ||= {:network_id => cloud_network.id} if cloud_network
if (cloud_network_selection_method == "network" && cloud_network) || (cloud_network_selection_method == "port" && network_port)
entry_from_dialog = {}
entry_from_dialog[:network_id] = cloud_network.id if cloud_network_selection_method == "network"
entry_from_dialog[:port_id] = network_port.id if cloud_network_selection_method == "port"
networks[0] ||= entry_from_dialog
end

options[:networks] = convert_networks_to_openstack_nics(networks)
end
Expand All @@ -20,6 +25,12 @@ def configure_network_adapters
private

def convert_networks_to_openstack_nics(networks)
networks.delete_blanks.collect { |nic| {"net_id" => CloudNetwork.find_by(:id => nic[:network_id]).ems_ref} }
networks.delete_blanks.collect do |nic|
if nic[:network_id]
{"net_id" => CloudNetwork.find_by(:id => nic[:network_id]).ems_ref}
elsif nic[:port_id]
{"port_id" => NetworkPort.find_by(:id => nic[:port_id]).ems_ref}
end
end.compact
end
end
Expand Up @@ -2,4 +2,12 @@ module ManageIQ::Providers::Openstack::CloudManager::Provision::OptionsHelper
def cloud_tenant
@cloud_tenant ||= CloudTenant.find_by(:id => get_option(:cloud_tenant))
end

def network_port
@network_port ||= NetworkPort.find_by(:id => get_option(:network_port))
end

def cloud_network_selection_method
@cloud_network_selection_method ||= get_option(:cloud_network_selection_method)
end
end
Expand Up @@ -83,18 +83,27 @@ def allowed_floating_ip_addresses(_options = {})
end
end

def validate_cloud_network(field, values, dlg, fld, value)
return nil if allowed_cloud_networks.length <= 1
validate_placement(field, values, dlg, fld, value)
end

def allowed_cloud_networks(_options = {})
return {} unless (src = provider_or_tenant_object)
targets = get_targets_for_source(src, :cloud_filter, CloudNetwork, 'all_cloud_networks')
targets = filter_cloud_networks(targets)
allowed_ci(:cloud_network, [:availability_zone], targets.map(&:id))
end

def allowed_network_ports(_options = {})
return {} unless (src = provider_or_tenant_object)
cloud_networks = get_targets_for_source(src, :cloud_filter, CloudNetwork, 'all_cloud_networks')
network_ports = []
cloud_networks.each do |cloud_network|
cloud_network.cloud_subnets.each do |cloud_subnet|
network_ports.push(*cloud_subnet.network_ports.where(:device_owner => [nil, ""]).to_a)
end
end
network_ports.each_with_object({}) do |port, h|
h[port.id] = "#{port.name} (#{port.fixed_ip_addresses.first})"
end
end

private

def dialog_name_from_automate(message = 'get_dialog_name')
Expand Down
Expand Up @@ -2,6 +2,12 @@
module ManageIQ::Providers::Openstack::CloudManager::ProvisionWorkflow::DialogFieldValidation
def validate_cloud_network(field, values, dlg, fld, value)
return nil if allowed_cloud_networks.length <= 1
return nil unless get_value(values[:cloud_network_selection_method]) == 'network'
validate_placement(field, values, dlg, fld, value)
end

def validate_network_port(field, values, dlg, fld, value)
return nil unless get_value(values[:cloud_network_selection_method]) == 'port'
validate_placement(field, values, dlg, fld, value)
end
end
18 changes: 18 additions & 0 deletions content/miq_dialogs/miq_provision_openstack_dialogs_template.yaml
Expand Up @@ -164,6 +164,15 @@
:required: true
:display: :edit
:data_type: :integer
:cloud_network_selection_method:
:values:
network: Cloud Network
port: Network Port
:description: Network Selection Method
:required: false
:display: :edit
:default: network
:data_type: :string
:cloud_network:
:values_from:
:method: :allowed_cloud_networks
Expand All @@ -173,6 +182,15 @@
:required_method: :validate_cloud_network
:display: :edit
:data_type: :integer
:network_port:
:values_from:
:method: :allowed_network_ports
:description: Network Port
:auto_select_single: false
:required: true
:required_method: :validate_network_port
:display: :edit
:data_type: :integer
:security_groups:
:values_from:
:method: :allowed_security_groups
Expand Down
Expand Up @@ -6,17 +6,28 @@
@vm = FactoryBot.create(:vm_openstack)
@net1 = FactoryBot.create(:cloud_network)
@net2 = FactoryBot.create(:cloud_network)
@port = FactoryBot.create(:network_port_openstack)

@task = FactoryBot.create(:miq_provision_openstack,
:source => @template,
:destination => @vm,
:state => 'pending',
:status => 'Ok',
:options => {
:src_vm_id => @template.id,
:cloud_network => [@net1.id, @net1.name]
}
)
:src_vm_id => @template.id,
:cloud_network_selection_method => "network",
:cloud_network => [@net1.id, @net1.name]
})
@port_task = FactoryGirl.create(:miq_provision_openstack,
:source => @template,
:destination => @vm,
:state => 'pending',
:status => 'Ok',
:options => {
:src_vm_id => @template.id,
:cloud_network_selection_method => "port",
:network_port => [@port.id, @port.name]
})
allow(@task).to receive_messages(:miq_request => double("MiqRequest").as_null_object)
end

Expand All @@ -26,6 +37,12 @@
expect(@task.options[:networks]).to eq([{"net_id" => @net1.ems_ref}])
end

it "sets nic from dialog specifying network port" do
@port_task.configure_network_adapters

expect(@port_task.options[:networks]).to eq([{"port_id" => @port.ems_ref}])
end

it "sets nic from dialog with additional nic from automate" do
@task.options[:networks] = [nil, {:network_id => @net2.id}]

Expand Down

0 comments on commit 2295d60

Please sign in to comment.