Skip to content

Commit

Permalink
Issue #15 - Add dynamic dialog methods for creating a dialog of LDAP …
Browse files Browse the repository at this point in the history
…entries
  • Loading branch information
CFME committed Aug 18, 2017
1 parent 89fc127 commit 50ac3ec
Show file tree
Hide file tree
Showing 13 changed files with 392 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object:
schema:
- field:
aetype: attribute
name: ldap_entry_attribute_name
name: ldap_treebase
display_name:
datatype: string
priority: 1
Expand All @@ -31,12 +31,132 @@ object:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: ldap_filter_attribute
display_name:
datatype: string
priority: 2
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: ldap_filter_value
display_name:
datatype: string
priority: 3
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: ldap_search_scope
display_name:
datatype: integer
priority: 4
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: dialog_value_ldap_entry_attribute_name
display_name:
datatype: string
priority: 5
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: dialog_description_ldap_entry_attribute_name
display_name:
datatype: string
priority: 6
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: method
name: validate_attributes
display_name:
datatype: string
priority: 7
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error: skip_to_state(next_state => "execute")
max_retries:
max_time:
- field:
aetype: state
name: get_ldap_entries
display_name:
datatype: string
priority: 2
priority: 8
owner:
default_value:
substitute: true
Expand All @@ -56,7 +176,7 @@ object:
name: execute
display_name:
datatype: string
priority: 3
priority: 9
owner:
default_value:
substitute: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Returns a dynamic drop down dialog with all of the given LDAP entries using the value of the given attribute names for the drop down values and descriptions.
#
# EXPECTED
# EVM STATE || EVM CURRENT || EVM OBJECT || EVM ROOT
# ldap_entries - LDAP entries to display in the dynamic drop down
# dialog_value_ldap_entry_attribute_name - LDAP entry attribute to use as the value for the dynamic drop down
# dialog_description_ldap_entry_attribute_name - LDAP entry attribute to use as the description for the dynamic drop down
# Optional. Defaults to `dialog_value_ldap_entry_attribute_name` if not specifed.
#
@DEBUG = false

require 'rubygems'
require 'net/ldap'

# Log an error and exit.
#
# @param msg Message to error with
def error(msg)
$evm.log(:error, msg)
$evm.root['ae_result'] = 'error'
$evm.root['ae_reason'] = msg.to_s
exit MIQ_STOP
end

# There are many ways to attempt to pass parameters in Automate.
# This function checks all of them in priorty order as well as checking for symbol or string.
#
# Order:
# 1. Current
# 2. Object
# 3. Root
# 4. State
#
# @return Value for the given parameter or nil if none is found
def get_param(param)
# else check if current has been set for given param
param_value ||= $evm.current[param.to_sym]
param_value ||= $evm.current[param.to_s]

# else cehck if current has been set for given param
param_value ||= $evm.object[param.to_sym]
param_value ||= $evm.object[param.to_s]

# else check if param on root has been set for given param
param_value ||= $evm.root[param.to_sym]
param_value ||= $evm.root[param.to_s]

# check if state has been set for given param
param_value ||= $evm.get_state_var(param.to_sym)
param_value ||= $evm.get_state_var(param.to_s)

$evm.log(:info, "get_param: { '#{param}' => '#{param_value}' }") if @DEBUG
return param_value
end

begin
# get the parameters
ldap_entries = get_param(:ldap_entries)

values = {}
if ldap_entries
dialog_value_ldap_entry_attribute_name = get_param(:dialog_value_ldap_entry_attribute_name)
error("dialog_value_ldap_entry_attribute_name parameter not found") if dialog_value_ldap_entry_attribute_name.nil?

dialog_description_ldap_entry_attribute_name = get_param(:dialog_description_ldap_entry_attribute_name) || dialog_value_ldap_entry_attribute_name

values = {}
ldap_entries.each do |entry|
value = entry[dialog_value_ldap_entry_attribute_name][0]
description = entry[dialog_description_ldap_entry_attribute_name][0]
values[value] = description
end
$evm.log(:info, "values => #{values}") if @DEBUG
values[nil] = ''
end

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"] = values
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
object_type: method
version: 1.0
object:
attributes:
name: get_ldap_entries
display_name:
description:
scope: instance
language: ruby
location: inline
inputs: []
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# EXPECTED
# EVM STATE || EVM CURRENT || EVM OBJECT || EVM ROOT
# ldap_entry_attribute_name - LDAP entry attribute name to get the value for
# dialog_value_ldap_entry_attribute_name - LDAP entry attribute name to get the value for
#
# EVM ROOT
# dialog_ldap_entries_attributes - Dialog element that has all of the existing LDAP entry attributes in YAML format.
Expand Down Expand Up @@ -61,7 +61,7 @@ def get_param(param)
$evm.log(:info, "$evm.object => #{$evm.object}") if @DEBUG
$evm.object.attributes.each { |k,v| $evm.log(:info, " #{k} => #{v}") } if @DEBUG

ldap_entry_attribute_name = get_param(:ldap_entry_attribute_name)
ldap_entry_attribute_name = get_param(:dialog_value_ldap_entry_attribute_name)
error("ldap_entry_attribute_name parameter not set") if ldap_entry_attribute_name.blank?

ldap_entries_attributes = $evm.root['dialog_ldap_entries_attributes']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Helper method used to skip to a given state.
# Great for error or exception handling.
#
# EXPECTED
# INPUT PARAMETERS
# next_state - The next state to skip to
#
@DEBUG = false

begin
# set the tags and attributes for the LDAP sync failure
next_state = $evm.inputs['next_state']

# depending on if this method was entered by on_error or on_exit
# the new result needs to be updated correctly
case $evm.root['ae_status_state']
when 'on_error'
new_result = 'continue'
when 'on_exit'
new_result = 'skip'
else
new_result = 'skip'
end
$evm.log(:info, "{ $evm.root['ae_status_state'] => #{$evm.root['ae_status_state']}, $evm.root['ae_result'] => #{$evm.root['ae_result']}, new_result => #{new_result} }") if @DEBUG

# Set attributes to skip to specified next state
$evm.log(:info, "Skip to State: #{next_state}")
$evm.root['ae_result'] = new_result
$evm.root['ae_next_state'] = next_state
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
object_type: method
version: 1.0
object:
attributes:
name: skip_to_state
display_name:
description:
scope: instance
language: ruby
location: inline
inputs: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Validates attributes before getting LDAP entries.
#
# NOTE: Intended to be overriden by implimenters.
#
@DEBUG = false

require 'rubygems'
require 'net/ldap'

# Log an error and exit.
#
# @param msg Message to error with
def error(msg)
$evm.log(:error, msg)
$evm.root['ae_result'] = 'error'
$evm.root['ae_reason'] = msg.to_s
end

# There are many ways to attempt to pass parameters in Automate.
# This function checks all of them in priorty order as well as checking for symbol or string.
#
# Order:
# 1. Current
# 2. Object
# 3. Root
# 4. State
#
# @return Value for the given parameter or nil if none is found
def get_param(param)
# else check if current has been set for given param
param_value ||= $evm.current[param.to_sym]
param_value ||= $evm.current[param.to_s]

# else cehck if current has been set for given param
param_value ||= $evm.object[param.to_sym]
param_value ||= $evm.object[param.to_s]

# else check if param on root has been set for given param
param_value ||= $evm.root[param.to_sym]
param_value ||= $evm.root[param.to_s]

# check if state has been set for given param
param_value ||= $evm.get_state_var(param.to_sym)
param_value ||= $evm.get_state_var(param.to_s)

$evm.log(:info, "get_param: { '#{param}' => '#{param_value}' }") if @DEBUG
return param_value
end

begin
# IMPLEMENTER TODO:
# Any needed parameter validation.
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
object_type: method
version: 1.0
object:
attributes:
name: validate_ldap_attributes
display_name:
description:
scope: instance
language: ruby
location: inline
inputs: []
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object:
inherits:
description:
fields:
- ldap_entry_attribute_name:
- dialog_value_ldap_entry_attribute_name:
value: description
- execute:
value: get_ldap_entry_attribute
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object:
inherits:
description:
fields:
- ldap_entry_attribute_name:
- dialog_value_ldap_entry_attribute_name:
value: l
- execute:
value: get_ldap_entry_attribute
Loading

0 comments on commit 50ac3ec

Please sign in to comment.