|
10 | 10 | # EVM OBJECT
|
11 | 11 | # 'ldap_new_entry_attributes' - Array of LDAP attributes to set on the new LDAP entry for the given VM.
|
12 | 12 | # 'ldap_new_entry_dn' - The DN of the new LDAP entry to create for the given VM.
|
13 |
| -@DEBUG = false |
14 | 13 |
|
15 |
| -# IMPLEMENTERS: Update with business logic |
16 |
| -# |
17 |
| -# Given a VM hostname returns the base attributes required to add a new LDAP entry for that host. |
18 |
| -# |
19 |
| -# @param vm_hostname Hostname of the VM to get the new LDAP entry attributes for |
20 |
| -# @param vm VM to get the new LDAP entry attributes for |
21 |
| -# @param dialog_attributes Dialog attributes which can be used to set the base attributes, |
22 |
| -# required to create the new LDAP entry |
23 |
| -# @param ldap_config LDAP configuration information |
24 |
| -# |
25 |
| -# @return Hash of LDAP attributes to use to create a new LDAP entry for the given VM hostname |
26 |
| -def get_ldap_new_entry_attributes(vm_hostname, vm, dialog_attributes, ldap_config) |
27 |
| - |
28 |
| - # IMPLEMENTERS: Change as necessary. |
29 |
| - # This reference works with Red Hat IdM / FreeIPA. |
30 |
| - # |
31 |
| - krb_principal_domain_name = ldap_config['krb_principal_domain_name'] |
32 |
| - $evm.log(:info, "krb_principal_domain_name=#{krb_principal_domain_name}") if @DEBUG |
33 |
| - return { |
34 |
| - :cn => vm_hostname, |
35 |
| - :fqdn => vm_hostname, |
36 |
| - :objectclass => ['ipaobject', 'ieee802device', 'nshost', 'ipaservice', 'pkiuser', 'ipahost', 'krbprincipal', 'krbprincipalaux', 'ipasshhost', 'top', 'ipaSshGroupOfPubKeys'], |
37 |
| - :krbPrincipalName => "host/#{vm_hostname}@#{krb_principal_domain_name}", |
38 |
| - :ipaUniqueID => 'autogenerate' |
39 |
| - } |
40 |
| -end |
| 14 | +# / Integration / LDAP / Operations / Methods / get_ldap_new_entry_attributes |
| 15 | +module RedHatConsulting_LDAP |
| 16 | + module Integration |
| 17 | + module LDAP |
| 18 | + module Operations |
| 19 | + module Methods |
| 20 | + class GetLDAPNewEntryAttributes |
41 | 21 |
|
42 |
| -# IMPLEMENTERS: Update with business logic |
43 |
| -# |
44 |
| -# Returns the DN for a new LDAP entry for the given VM hostname. |
45 |
| -# |
46 |
| -# @param vm_hostname Hostname of VM to create the DN for the new LDAP entry |
47 |
| -# @param vm VM to create the DN for the new LDAP entry |
48 |
| -# @param dialog_attributes Dialog attributes which can be used to create the DN for the new LDAP entry |
49 |
| -# @param ldap_config LDAP configuration information |
50 |
| -# |
51 |
| -# @return Full DN for a new LDAP entry for the given VM hostname |
52 |
| -def get_ldap_new_entry_dn(vm_hostname, vm, dialog_attributes, ldap_config) |
53 |
| - |
54 |
| - # IMPLEMENTERS: Change as necessary. |
55 |
| - # This reference works with Red Hat IdM / FreeIPA. |
56 |
| - # |
57 |
| - ldap_treebase = ldap_config['ldap_treebase'] |
58 |
| - ldap_hostname_filter = ldap_config['ldap_hostname_filter'] |
59 |
| - $evm.log(:info, "ldap_treebase=#{ldap_treebase}") if @DEBUG |
60 |
| - $evm.log(:info, "ldap_hostname_filter=#{ldap_hostname_filter}") if @DEBUG |
61 |
| - |
62 |
| - ldap_new_entry_dn = "#{ldap_hostname_filter}=#{vm_hostname},#{ldap_treebase}" |
63 |
| - |
64 |
| - return ldap_new_entry_dn |
65 |
| -end |
| 22 | + include RedHatConsulting_Utilities::StdLib::Core |
66 | 23 |
|
67 |
| -# IMPLEMENTERS: DO NOT MODIFY |
68 |
| -# |
69 |
| -# Log an error and exit. |
70 |
| -# |
71 |
| -# @param msg Message to error with |
72 |
| -def error(msg) |
73 |
| - $evm.log(:error, msg) |
74 |
| - $evm.root['ae_result'] = 'error' |
75 |
| - $evm.root['ae_reason'] = msg.to_s |
76 |
| - exit MIQ_STOP |
77 |
| -end |
| 24 | + # IMPLEMENTERS: DO NOT MODIFY |
| 25 | + def initialize(handle = $evm) |
| 26 | + @handle = handle |
| 27 | + @DEBUG = false |
| 28 | + end |
78 | 29 |
|
79 |
| -# IMPLEMENTERS: DO NOT MODIFY |
80 |
| -# |
81 |
| -# There are many ways to attempt to pass parameters in Automate. |
82 |
| -# This function checks all of them in priorty order as well as checking for symbol or string. |
83 |
| -# |
84 |
| -# Order: |
85 |
| -# 1. Current |
86 |
| -# 2. Object |
87 |
| -# 3. Root |
88 |
| -# 4. State |
89 |
| -# |
90 |
| -# @return Value for the given parameter or nil if none is found |
91 |
| -def get_param(param) |
92 |
| - # else check if current has been set for given param |
93 |
| - param_value ||= $evm.current[param.to_sym] |
94 |
| - param_value ||= $evm.current[param.to_s] |
95 |
| - |
96 |
| - # else cehck if current has been set for given param |
97 |
| - param_value ||= $evm.object[param.to_sym] |
98 |
| - param_value ||= $evm.object[param.to_s] |
99 |
| - |
100 |
| - # else check if param on root has been set for given param |
101 |
| - param_value ||= $evm.root[param.to_sym] |
102 |
| - param_value ||= $evm.root[param.to_s] |
103 |
| - |
104 |
| - # check if state has been set for given param |
105 |
| - param_value ||= $evm.get_state_var(param.to_sym) |
106 |
| - param_value ||= $evm.get_state_var(param.to_s) |
107 |
| - |
108 |
| - $evm.log(:info, "{ '#{param}' => '#{param_value}' }") if @DEBUG |
109 |
| - return param_value |
110 |
| -end |
| 30 | + # IMPLEMENTERS: Update with business logic |
| 31 | + # |
| 32 | + # Given a VM hostname returns the base attributes required to add a new LDAP entry for that host. |
| 33 | + # |
| 34 | + # @param vm_hostname Hostname of the VM to get the new LDAP entry attributes for |
| 35 | + # @param vm VM to get the new LDAP entry attributes for |
| 36 | + # @param dialog_attributes Dialog attributes which can be used to set the base attributes, |
| 37 | + # required to create the new LDAP entry |
| 38 | + # @param ldap_config LDAP configuration information |
| 39 | + # |
| 40 | + # @return Hash of LDAP attributes to use to create a new LDAP entry for the given VM hostname |
| 41 | + def get_ldap_new_entry_attributes(vm_hostname, vm, dialog_attributes, ldap_config) |
111 | 42 |
|
112 |
| -# IMPLEMENTERS: DO NOT MODIFY |
113 |
| -# |
114 |
| -# Gets the LDAP connection configuration information. |
115 |
| -LDAP_CONFIG_URI = 'Integration/LDAP/Configuration/default' |
116 |
| -def get_ldap_config() |
117 |
| - return $evm.instantiate(LDAP_CONFIG_URI) |
118 |
| -end |
| 43 | + # IMPLEMENTERS: Change as necessary. |
| 44 | + # This reference works with Red Hat IdM / FreeIPA. |
| 45 | + # |
| 46 | + krb_principal_domain_name = ldap_config['krb_principal_domain_name'] |
| 47 | + log(:info, "krb_principal_domain_name=#{krb_principal_domain_name}") if @DEBUG |
| 48 | + return { |
| 49 | + :cn => vm_hostname, |
| 50 | + :fqdn => vm_hostname, |
| 51 | + :objectclass => ['ipaobject', 'ieee802device', 'nshost', 'ipaservice', 'pkiuser', 'ipahost', 'krbprincipal', 'krbprincipalaux', 'ipasshhost', 'top', 'ipaSshGroupOfPubKeys'], |
| 52 | + :krbPrincipalName => "host/#{vm_hostname}@#{krb_principal_domain_name}", |
| 53 | + :ipaUniqueID => 'autogenerate' |
| 54 | + } |
| 55 | + end |
119 | 56 |
|
120 |
| -# IMPLEMENTERS: DO NOT MODIFY |
121 |
| -# |
122 |
| -# Get the hostname for a given VM. |
123 |
| -# |
124 |
| -# @param vm VM to get the hostname for |
125 |
| -# |
126 |
| -# @return Hostname of the VM or vm.name if a hostname can not be found |
127 |
| -def get_vm_hostname(vm) |
128 |
| - hostname = vm.hardware.hostnames.first rescue nil |
129 |
| - hostname = vm.name if hostname.blank? |
130 |
| - return hostname |
131 |
| -end |
| 57 | + # IMPLEMENTERS: Update with business logic |
| 58 | + # |
| 59 | + # Returns the DN for a new LDAP entry for the given VM hostname. |
| 60 | + # |
| 61 | + # @param vm_hostname Hostname of VM to create the DN for the new LDAP entry |
| 62 | + # @param vm VM to create the DN for the new LDAP entry |
| 63 | + # @param dialog_attributes Dialog attributes which can be used to create the DN for the new LDAP entry |
| 64 | + # @param ldap_config LDAP configuration information |
| 65 | + # |
| 66 | + # @return Full DN for a new LDAP entry for the given VM hostname |
| 67 | + def get_ldap_new_entry_dn(vm_hostname, vm, dialog_attributes, ldap_config) |
| 68 | + |
| 69 | + # IMPLEMENTERS: Change as necessary. |
| 70 | + # This reference works with Red Hat IdM / FreeIPA. |
| 71 | + # |
| 72 | + ldap_treebase = ldap_config['ldap_treebase'] |
| 73 | + ldap_hostname_filter = ldap_config['ldap_hostname_filter'] |
| 74 | + log(:info, "ldap_treebase=#{ldap_treebase}") if @DEBUG |
| 75 | + log(:info, "ldap_hostname_filter=#{ldap_hostname_filter}") if @DEBUG |
| 76 | + |
| 77 | + ldap_new_entry_dn = "#{ldap_hostname_filter}=#{vm_hostname},#{ldap_treebase}" |
| 78 | + |
| 79 | + return ldap_new_entry_dn |
| 80 | + end |
| 81 | + |
| 82 | + def main |
| 83 | + begin |
| 84 | + # IMPLEMENTERS: DO NOT MODIFY |
| 85 | + # |
| 86 | + # get the parameters |
| 87 | + |
| 88 | + # get the VM to get the new LDAP entry attributes for and |
| 89 | + # get the service dialog attributes associated with that VM |
| 90 | + vm, dialog_attributes = get_vm_and_options() |
| 91 | + error("vm parameter not found") if vm.blank? |
| 92 | + log(:info, "vm => #{vm.name}") if @DEBUG |
| 93 | + log(:info, "dialog_attributes => #{dialog_attributes}") if @DEBUG |
| 94 | + |
| 95 | + # IMPLEMENTERS: DO NOT MODIFY |
| 96 | + # |
| 97 | + # determine LDAP treebase and hostname filter |
| 98 | + ldap_config = get_ldap_config() |
| 99 | + error("LDAP Configuration not found") if ldap_config.nil? |
132 | 100 |
|
133 |
| -# IMPLEMENTERS: DO NOT MODIFY |
134 |
| -begin |
135 |
| - # IMPLEMENTERS: DO NOT MODIFY |
136 |
| - # |
137 |
| - # get the parameters |
138 |
| - |
139 |
| - # get the VM to get the new LDAP entry attributes for and |
140 |
| - # get the service dialog attributes associated with that VM |
141 |
| - $evm.log(:info, "$evm.root['vmdb_object_type'] => '#{$evm.root['vmdb_object_type']}'.") if @DEBUG |
142 |
| - case $evm.root['vmdb_object_type'] |
143 |
| - when 'miq_provision' |
144 |
| - miq_provision = $evm.root['miq_provision'] |
145 |
| - vm = miq_provision.vm |
146 |
| - dialog_attributes = miq_provision.options |
147 |
| - when 'vm' |
148 |
| - vm = $evm.root['vm'] |
149 |
| - dialog_attributes = $evm.root.attributes |
150 |
| - else |
151 |
| - error("Can not handle vmdb_object_type: #{$evm.root['vmdb_object_type']}") |
| 101 | + # IMPLEMENTERS: DO NOT MODIFY |
| 102 | + # |
| 103 | + # determine the VM hostname |
| 104 | + vm_hostname = get_vm_hostname(vm) |
| 105 | + log(:info, "vm_hostname => '#{vm_hostname}'") if @DEBUG |
| 106 | + |
| 107 | + # IMPLEMENTERS: DO NOT MODIFY |
| 108 | + # |
| 109 | + # get information about the new LDAP entry |
| 110 | + ldap_new_entry_attributes = get_ldap_new_entry_attributes(vm_hostname, vm, dialog_attributes, ldap_config) |
| 111 | + ldap_new_entry_dn = get_ldap_new_entry_dn(vm_hostname, vm, dialog_attributes, ldap_config) |
| 112 | + |
| 113 | + # IMPLEMENTERS: DO NOT MODIFY |
| 114 | + # |
| 115 | + # return new LDAP entry attributes |
| 116 | + @handle.object['ldap_new_entry_attributes'] = ldap_new_entry_attributes |
| 117 | + log(:info, "$evm.object['ldap_new_entry_attributes']=#{@handle.object['ldap_new_entry_attributes']})") if @DEBUG |
| 118 | + |
| 119 | + # IMPLEMENTERS: DO NOT MODIFY |
| 120 | + # |
| 121 | + # return new LDAP entry DN |
| 122 | + @handle.object['ldap_new_entry_dn'] = ldap_new_entry_dn |
| 123 | + log(:info, "$evm.object['ldap_new_entry_dn']=#{@handle.object['ldap_new_entry_dn']})") if @DEBUG |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + # IMPLEMENTERS: DO NOT MODIFY |
| 128 | + # |
| 129 | + # Get the hostname for a given VM. |
| 130 | + # |
| 131 | + # @param vm VM to get the hostname for |
| 132 | + # |
| 133 | + # @return Hostname of the VM or vm.name if a hostname can not be found |
| 134 | + def get_vm_hostname(vm) |
| 135 | + hostname = vm.hardware.hostnames.first rescue nil |
| 136 | + hostname = vm.name if hostname.blank? |
| 137 | + return hostname |
| 138 | + end |
| 139 | + |
| 140 | + # IMPLEMENTERS: DO NOT MODIFY |
| 141 | + # |
| 142 | + # Gets the LDAP connection configuration information. |
| 143 | + LDAP_CONFIG_URI = 'Integration/LDAP/Configuration/default' |
| 144 | + def get_ldap_config() |
| 145 | + return @handle.instantiate(LDAP_CONFIG_URI) |
| 146 | + end |
| 147 | + |
| 148 | + end |
| 149 | + end |
| 150 | + end |
| 151 | + end |
152 | 152 | end
|
153 |
| - error("vm parameter not found") if vm.blank? |
154 |
| - $evm.log(:info, "vm => #{vm.name}") if @DEBUG |
155 |
| - $evm.log(:info, "dialog_attributes => #{dialog_attributes}") if @DEBUG |
156 |
| - |
157 |
| - # IMPLEMENTERS: DO NOT MODIFY |
158 |
| - # |
159 |
| - # determine LDAP treebase and hostname filter |
160 |
| - ldap_config = get_ldap_config() |
161 |
| - error("LDAP Configuration not found") if ldap_config.nil? |
162 |
| - |
163 |
| - # IMPLEMENTERS: DO NOT MODIFY |
164 |
| - # |
165 |
| - # determine the VM hostname |
166 |
| - vm_hostname = get_vm_hostname(vm) |
167 |
| - $evm.log(:info, "vm_hostname => '#{vm_hostname}'") if @DEBUG |
168 |
| - |
169 |
| - # IMPLEMENTERS: DO NOT MODIFY |
170 |
| - # |
171 |
| - # get information about the new LDAP entry |
172 |
| - ldap_new_entry_attributes = get_ldap_new_entry_attributes(vm_hostname, vm, dialog_attributes, ldap_config) |
173 |
| - ldap_new_entry_dn = get_ldap_new_entry_dn(vm_hostname, vm, dialog_attributes, ldap_config) |
174 |
| - |
175 |
| - # IMPLEMENTERS: DO NOT MODIFY |
176 |
| - # |
177 |
| - # return new LDAP entry attributes |
178 |
| - $evm.object['ldap_new_entry_attributes'] = ldap_new_entry_attributes |
179 |
| - $evm.log(:info, "$evm.object['ldap_new_entry_attributes']=#{$evm.object['ldap_new_entry_attributes']})") if @DEBUG |
180 |
| - |
181 |
| - # IMPLEMENTERS: DO NOT MODIFY |
182 |
| - # |
183 |
| - # return new LDAP entry DN |
184 |
| - $evm.object['ldap_new_entry_dn'] = ldap_new_entry_dn |
185 |
| - $evm.log(:info, "$evm.object['ldap_new_entry_dn']=#{$evm.object['ldap_new_entry_dn']})") if @DEBUG |
| 153 | +end |
| 154 | + |
| 155 | +if __FILE__ == $PROGRAM_NAME |
| 156 | + RedHatConsulting_LDAP::Integration::LDAP::Operations::Methods::GetLDAPNewEntryAttributes.new.main |
186 | 157 | end
|
0 commit comments