Stange problem with dynamic dialogs

automate

#1

Hey everyone,

So i’ve been struggeling the whole day with a very weird issue.

I made a simple script that asks a external CMDB which vlan’s a certain customer has.
The intention of this script is to list all the vLAN’s of a specific customer. So he can choose in which vlan he want to provision his vm.

This is the script (the customer_id is a constant atm to make everything simple):

  require 'httparty'
  $i = 0
  vlan = Array.new
  auth = {:username => $evm.object['user'], :password => $evm.object.decrypt('password')}
  $evm.log(:info, "Device42 Entering getSubnets")
  subnets = Array.new
  response = HTTParty.get("#{$evm.object['url']}subnets/?mask_bits_gt=24&customer_id=3", 
	{
		:headers => {'Content-Type' => 'application/x-www-form-urlencoded'},
		:basic_auth => auth,
		:verify => false,
	})
  response.parsed_response["subnets"].each do |a|
	subnets.push(a["subnet_id"])
  end
  $evm.log(:info, "Device42 subnets are #{subnets}")
  
  subnets.each do |subnet|
  response = HTTParty.get("#{$evm.object['url']}subnets/?mask_bits_gt=24&customer_id=3&subnet_id=#{subnet}",
	{
		:headers => {'Content-Type' => 'application/x-www-form-urlencoded'},
		:basic_auth => auth,
		:verify => false,
	})
	  unless response.parsed_response["subnets"].nil?
		vlan[$i] = response.parsed_response["subnets"][0]["parent_vlan_name"]
        $i = $i + 1
	end
  end
  
  $evm.log(:info, "Device42 vlans are #{vlan}")
  
  dialog_field = $evm.object
  dialog_field["sort_by"]    = "value"
  dialog_field["sort_order"] = "ascending"
  dialog_field["data_type"]  = "string"
  dialog_field["required"] = "true"
  dialog_field["values"] = vlan

This is what i can see in the logs, everything seems normal

logs

And i can also see that the values get inserted into dialog_field[“values”]

noglogs

Now here comes the weird thing, the service dialog looks like this:

So instead of the names of my vLAN’s it just says “L” for every vlan the customer has.

Does anyone know why this is happening?

Thanks in advance.


#2

This looks like the code is interpreting values the usual rails way - it expects an array of [value, label] for each item.

So, when it sees that KL3_VL4008 it decides that item[0] == 'K' so K is the value, and item[1] == 'L' so L is the label.

So, instead of vlan[$i] = response.parsed_response["subnets"][0]["parent_vlan_name"], you would need something like

label = value = response.parsed_response["subnets"][0]["parent_vlan_name"]
vlan[$i] = [value, label]

#3

@himdel Thanks, I didn’t expected a reply that fast.

I’m going to give your solution a try but after reading your explanation i’ll definitely think it would work


#4

@himdel I just copied your code and it works like a charm, Thanks man :grinning:


#5

I just found a bug, when i select a vlan to provision the vm it always takes the first one in the list. I think it has something to do with how the values are stored in the array.

This are the logs:

Because when i just code:

unless response.parsed_response["subnets"].nil?
        vlan[$i] = response.parsed_response["subnets"][0]["parent_vlan_name"]
        $i = $i + 1
end
dialog_field["values"] = {vlan[0] => vlan [0], vlan[1] => vlan [1], vlan[2] => vlan [2], vlan[3] => vlan [3]}`

The logs show this:

2018-04-16_17-00-10

But not everyone has the same amount of vLAN’s so i can’t do it like that


#6

I finally got the values stored in the array how i want it:

  require 'httparty'
  vlan_name = Array.new
  vlan_name = {}
  auth = {:username => $evm.object['user'], :password => $evm.object.decrypt('password')}
  $evm.log(:info, "Device42 Entering getSubnets")
  subnets = Array.new
  response = HTTParty.get("#{$evm.object['url']}subnets/?mask_bits_gt=24&customer_id=3", 
	{
		:headers => {'Content-Type' => 'application/x-www-form-urlencoded'},
		:basic_auth => auth,
		:verify => false,
	})
  response.parsed_response["subnets"].each do |a|
	subnets.push(a["subnet_id"])
  end
  $evm.log(:info, "Device42 subnets are #{subnets}")
  
  subnets.each do |subnet|
  response = HTTParty.get("#{$evm.object['url']}subnets/?mask_bits_gt=24&customer_id=3&subnet_id=#{subnet}",
	{
		:headers => {'Content-Type' => 'application/x-www-form-urlencoded'},
		:basic_auth => auth,
		:verify => false,
	})
	  unless response.parsed_response["subnets"].nil?
		label = value = response.parsed_response["subnets"][0]["parent_vlan_name"]
        vlan_name[label] = value
	end
  end
  
  $evm.log(:info, "Device42 vlans are #{vlan_name}")
  
  dialog_field = $evm.object
  dialog_field["sort_by"]    = "value"
  dialog_field["sort_order"] = "ascending"
  dialog_field["data_type"]  = "string"
  dialog_field["required"] = "true"
  dialog_field["values"] = vlan_name

It still always takes the first value to provision the vm, it doesn’t matter which one you select.


#7

I have issues whereby I have to select a dialogue option twice for it to be actually selected. Not sure if this is related to your issue.


#8

Hey @enosullivan, I think it’s a different issue.

I can select the dialogue option I want first try, but after that it always provisions using the first option from the list. So my selected value doesn’t get passed for some reason.

I also thought about what the issue could be on your problem but i’m sad to say that i really don’t have any idea. If u are using ManageIQ, are u using the newest version of it? Because if u aren’t I would definitely give it a try with a newer version.


#9

Hi

There have been several recent fixes for dialog issues, including:


I these should fix the problems that you’re seeing.

Cheers,
pemcg