
    Vh=[                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZmZ d dlmZ d dlmZmZ d d	lmZ d
ZdZdZdZdZdZdZdZdZdZd dZd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)e*dk(  r e)        yy)!    )absolute_importdivisionprint_functionaE  
---
module: idrac_attributes
short_description: Configure the iDRAC attributes.
version_added: "6.0.0"
description:
  - This module allows to configure the iDRAC attributes.
extends_documentation_fragment:
  - dellemc.openmanage.idrac_x_auth_options
options:
  idrac_attributes:
    type: dict
    description:
      - "Dictionary of iDRAC attributes and value. The attributes should be
      part of the Integrated Dell Remote Access Controller Attribute Registry.
      To view the list of attributes in Attribute Registry for iDRAC9 and above,
      see, U(https://I(idrac_ip)/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DellAttributes/iDRAC.Embedded.1)
      and U(https://I(idrac_ip)/redfish/v1/Registries/ManagerAttributeRegistry)."
      - "For iDRAC8 based servers, derive the manager attribute name from Server Configuration Profile.
      If the manager attribute name in Server Configuration Profile is <GroupName>.<Instance>#<AttributeName>
      (for Example, 'SNMP.1#AgentCommunity') then the equivalent attribute name for Redfish is
      <GroupName>.<Instance>.<AttributeName> (for Example, 'SNMP.1.AgentCommunity')."
  system_attributes:
    type: dict
    description:
      - "Dictionary of System attributes and value. The attributes should be
      part of the Integrated Dell Remote Access Controller Attribute Registry. To view the list of attributes in Attribute Registry for iDRAC9 and above,
      see, U(https://I(idrac_ip)/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DellAttributes/System.Embedded.1)
      and U(https://I(idrac_ip)/redfish/v1/Registries/ManagerAttributeRegistry)."
      - "For iDRAC8 based servers, derive the manager attribute name from Server Configuration Profile.
      If the manager attribute name in Server Configuration Profile is <GroupName>.<Instance>#<AttributeName>
      (for Example, 'ThermalSettings.1#ThermalProfile') then the equivalent attribute name for Redfish is
      <GroupName>.<Instance>.<AttributeName> (for Example, 'ThermalSettings.1.ThermalProfile')."
  lifecycle_controller_attributes:
    type: dict
    description:
      - "Dictionary of Lifecycle Controller attributes and value. The attributes should be
      part of the Integrated Dell Remote Access Controller Attribute Registry.To view the list of attributes in Attribute Registry for iDRAC9 and above,
      see, U(https://I(idrac_ip)/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DellAttributes/LifecycleController.Embedded.1)
      and U(https://I(idrac_ip)/redfish/v1/Registries/ManagerAttributeRegistry)."
      - "For iDRAC8 based servers, derive the manager attribute name from Server Configuration Profile.
      If the manager attribute name in Server Configuration Profile is <GroupName>.<Instance>#<AttributeName>
      (for Example, 'LCAttributes.1#AutoUpdate') then the equivalent attribute name for Redfish is
      <GroupName>.<Instance>.<AttributeName> (for Example, 'LCAttributes.1.AutoUpdate')."
  resource_id:
    type: str
    description: Redfish ID of the resource.
requirements:
  - "python >= 3.9.6"
author:
  - Husniya Abdul Hameed (@husniya-hameed)
  - Felix Stephen (@felixs88)
notes:
  - Run this module from a system that has direct access to Dell iDRAC.
  - This module supports C(check_mode).
  - For iDRAC8 based servers, the value provided for the attributes are not be validated.
    Ensure appropriate values are passed.
a  
---
- name: Configure iDRAC attributes
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    idrac_attributes:
      SNMP.1.AgentCommunity: public

- name: Configure System attributes
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    system_attributes:
      ThermalSettings.1.ThermalProfile: Sound Cap

- name: Configure Lifecycle Controller attributes
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    lifecycle_controller_attributes:
      LCAttributes.1.AutoUpdate: Enabled

- name: Configure the iDRAC attributes for email alert settings.
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    idrac_attributes:
      EmailAlert.1.CustomMsg: Display Message
      EmailAlert.1.Enable: Enabled
      EmailAlert.1.Address: test@test.com

- name: Configure the iDRAC attributes for SNMP alert settings.
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    idrac_attributes:
      SNMPAlert.1.Destination: 192.168.0.2
      SNMPAlert.1.State: Enabled
      SNMPAlert.1.SNMPv3Username: username

- name: Configure the iDRAC attributes for SMTP alert settings.
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    idrac_attributes:
      RemoteHosts.1.SMTPServerIPAddress: 192.168.0.3
      RemoteHosts.1.SMTPAuthentication: Enabled
      RemoteHosts.1.SMTPPort: 25
      RemoteHosts.1.SMTPUserName: username
      RemoteHosts.1.SMTPPassword: password

- name: Configure the iDRAC attributes for webserver settings.
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    idrac_attributes:
      WebServer.1.SSLEncryptionBitLength: 128-Bit or higher
      WebServer.1.TLSProtocol: TLS 1.1 and Higher

- name: Configure the iDRAC attributes for SNMP settings.
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    idrac_attributes:
      SNMP.1.SNMPProtocol: All
      SNMP.1.AgentEnable: Enabled
      SNMP.1.TrapFormat: SNMPv1
      SNMP.1.AlertPort: 162
      SNMP.1.AgentCommunity: public

- name: Configure the iDRAC LC attributes for collecting system inventory.
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    lifecycle_controller_attributes:
      LCAttributes.1.CollectSystemInventoryOnRestart: Enabled

- name: Configure the iDRAC system attributes for LCD configuration.
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    system_attributes:
      LCD.1.Configuration: Service Tag
      LCD.1.vConsoleIndication: Enabled
      LCD.1.FrontPanelLocking: Full-Access
      LCD.1.UserDefinedString: custom string

- name: Configure the iDRAC attributes for Timezone settings.
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    idrac_attributes:
      Time.1.Timezone: CST6CDT
      NTPConfigGroup.1.NTPEnable: Enabled
      NTPConfigGroup.1.NTP1: 192.168.0.5
      NTPConfigGroup.1.NTP2: 192.168.0.6
      NTPConfigGroup.1.NTP3: 192.168.0.7

- name: Configure all attributes
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    idrac_attributes:
      SNMP.1.AgentCommunity: test
      SNMP.1.AgentEnable: Enabled
      SNMP.1.DiscoveryPort: 161
    system_attributes:
      ServerOS.1.HostName: demohostname
    lifecycle_controller_attributes:
      LCAttributes.1.AutoUpdate: Disabled

- name: Enable idrac basic syslog
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    idrac_attributes:
      SysLog.1.SysLogEnable: Enabled
      SysLog.1.Server1: 192.168.0.2
      SysLog.1.Server2: 192.168.0.3
      SysLog.1.Server3: 192.168.0.4
      SysLog.1.Port: 514

- name: Disable idrac basic syslog
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    idrac_attributes:
      SysLog.1.SysLogEnable: Disabled

- name: Enable idrac secure syslog
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    idrac_attributes:
      SysLog.1.securesyslogenable: Enabled
      SysLog.1.secureserver1: 192.168.0.2
      SysLog.1.secureport: 6511
      SysLog.1.secureclientauth: Anonymous

- name: Disable idrac secure syslog
  dellemc.openmanage.idrac_attributes:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    idrac_attributes:
      SysLog.1.securesyslogenable: Disabled
a  
---
msg:
  type: str
  description: Status of the attribute update operation.
  returned: always
  sample: "Successfully updated the attributes."
invalid_attributes:
  type: dict
  description: Dict of invalid attributes provided.
  returned: on invalid attributes or values.
  sample: {
        "LCAttributes.1.AutoUpdate": "Invalid value for Enumeration.",
        "LCAttributes.1.StorageHealthRollupStatus": "Read only Attribute cannot be modified.",
        "SNMP.1.AlertPort": "Not a valid integer.",
        "SNMP.1.AlertPorty": "Attribute does not exist.",
        "SysLog.1.PowerLogInterval": "Integer out of valid range.",
        "ThermalSettings.1.AirExhaustTemp": "Invalid value for Enumeration."
    }
error_info:
  description: Error information of the operation.
  returned: when attribute value is invalid.
  type: dict
  sample: {
    "error": {
      "@Message.ExtendedInfo": [
        {
          "Message": "The value 'false' for the property LCAttributes.1.BIOSRTDRequested is of a different type than the property can accept.",
          "MessageArgs": [
            "false",
            "LCAttributes.1.BIOSRTDRequested"
          ],
          "MessageArgs@odata.count": 2,
          "MessageId": "Base.1.12.PropertyValueTypeError",
          "RelatedProperties": [
            "#/Attributes/LCAttributes.1.BIOSRTDRequested"
          ],
          "RelatedProperties@odata.count": 1,
          "Resolution": "Correct the value for the property in the request body and resubmit the request if the operation failed.",
          "Severity": "Warning"
        }
      ],
      "code": "Base.1.12.GeneralError",
      "message": "A general error has occurred. See ExtendedInfo for more information"
    }
  }
N)URLError	HTTPError)ConnectionError)iDRACRedfishAPIIdracAnsibleModule)get_manager_res_idz$Successfully updated the attributes.zNo changes found to be applied.zChanges found to be applied.zQApplication of some of the attributes failed due to invalid value or enumeration.zSystem.Embedded.1ziDRAC.Embedded.1zLifecycleController.Embedded.1z/redfish/v1/Managers
Attributesz//redfish/v1/Managers/{manager_id}/Jobs/{job_id}c                     d}d}i }| j                         D ]?  \  }}t        j                  dd|      }|dj                  ||      z  }t	        |      ||<   A |j                  ||      }||fS )Nz%<Component FQDD="{0}">{1}</Component> z\.(?!\d)#z%<Attribute Name="{0}">{1}</Attribute>)itemsresubformatstr)	attrbitefqdd	componentattr	json_datakvkeyroots	            w/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/dellemc/openmanage/plugins/modules/idrac_attributes.pyxml_data_conversionr   D  s    ;IDI   1ff[#q)7>>sAFFQ	#  D$'D?    c                     i }| D ci c]!  }|d   |j                         v s|d   |d   # }}t        |      t        |      k(  s1|j                         D ]  }||vs|j                  dd      }d||<     ||fS c c}w )NNameValuer   .Attribute does not exist.)keyslenreplace)	attributereq_datainvalid_attrr   	data_dictr   act_keys          r   validate_attr_namer.   P  s    L9Bfd6lV^VcVcVeFeftG},fIfy>S]*==? 	DC)#++c3/(CW%	D l"" gs
   A;A;c                    |j                  dddd      }|j                  d   d   }i i i i f\  }}}	}
|D ]  }|r=|j                  d      t        k(  r%t	        |d	   |      \  }}|r|
j                  |       |r=|j                  d      t        k(  r%t	        |d	   |      \  }}|r|
j                  |       |s|j                  d      t        k(  st	        |d	   |      \  }	}|s|
j                  |        |
r| j                  d
|
       t        t        |j                               t        |j                               z        xsz t        t        |j                               t        |j                               z        xs< t        t        |	j                               t        |j                               z        g}| j                  r%t        |      du r| j                  t        d       y | j                  rt!        |      du s| j                  s%t!        |      du r| j                  t"               y y y )NJSONDefaultz iDRAC,System,LifecycleControllerT)export_format
export_usetargetjob_waitSystemConfiguration
ComponentsFQDDr   Attributes have invalid values.)msginvalid_attributesr:   changedFr:   )
export_scpr   get
MANAGER_IDr.   update	SYSTEM_IDLC_ID	fail_jsonboolsetr   
check_modeany	exit_jsonCHANGES_MSGallNO_CHANGES_MSG)moduleidrac
idrac_jsonsys_jsonlc_jsonscp_responsecompexist_idrac	exist_sysexist_lcinvalidcmpr+   diff_changes                 r   get_check_moder[   [  s   ##&Y+MX\ $ ^L!!"78FD02BB-KHg -#''&/Z7(:3|;Lj(Y%K|,94&8\9JH&U#I||,swwv%/%7L8I7%S"Hl|,- >SZ[K--/03z7G7G7I3JJK FIOO-.X^^5E1FFGFHNN,-GMMO0DDEGK S-5[$7


K 0E 9""s;'75'@^, (A"r    c                    | j                   j                  dd      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }d}d}i i i }}
}	|t        |t              \  }}	||z  }|t        |t              \  }}
||z  }|t        |t
              \  }}||z  }t        | ||	|
|       |j                  |      }|j                  |d	d
      }|j                  d   j                  d      d   }t        j                  ||      }|j                  ||      }|S )Nr5   Tidrac_attributessystem_attributeslifecycle_controller_attributesz.<SystemConfiguration>{0}</SystemConfiguration>r   )r   ALLF)import_bufferr4   r5   Location/)
manager_idjob_id)r5   )paramsr@   r   rA   rC   rD   r[   r   
import_scpheaderssplitJOB_URIwait_for_job_completion)rN   rO   res_idr5   
idrac_attrsystem_attrlc_attrr   r   idrac_json_datasystem_json_datalc_json_dataidrac_xml_payloadsystem_xml_payloadlc_xml_payloadpayloadresprf   job_urijob_resps                       r   scp_idrac_attributesr{   y  sY   }}  T2H""#56J--##$78Kmm ABG?DI68"b|%O-@R\-]*?&&	/B;U^/_,,''	':7'O$^#	65/3C\Skk)$G'%%PD\\*%++C04Fnnvn>G,,Wx,HHOr    c                    i }d}| j                  |j                  |      d      }|j                         D ]P  }|j                  t           j                  |      |j                  |      k7  s8|j                  |      ||<   |dz  }R ||fS )Nr   GET   )invoke_requestr@   r&   r   ATTR)rO   idrac_idr   uri_dictresponse_attrdiffresponser   s           r   get_response_attrr     s    MD##HLL$:EBHYY[ d#''*dhhqk9#xx{M!AID	
 r    c                 T   i }	 | j                  dd      }|j                  j                  dg       }|rb|d   j                  d      }| j                  |d      }|j                  j                  d      j                  d      }t        d |D              }|S # t        $ r i }Y |S w xY w)	Nz//redfish/v1/Registries/ManagerAttributeRegistryr}   rb   rd   UriRegistryEntriesr   c              3   *   K   | ]  }|d    |f  yw)AttributeNameN ).0xs     r   	<genexpr>z*get_attributes_registry.<locals>.<genexpr>  s     DQ!O,a0Ds   )r   r   r@   dict	Exception)rO   reggyrx   loc_listreg_json_urireg_resp	attr_lists          r   get_attributes_registryr     s    E	##$UW\]>>%%j"5#B<++E2L++L%@H **../@AEElSID)DDE L  Ls   BB B'&B'c                    i }|j                         D ]  \  }}|| v r| j                  |      }|j                  d      rd||<   2|j                  d      }|dk(  r8d}|j                  dg       D ]  }||j                  d      k(  sd} n |sd	||<   |d
k(  s	 t        |      }	|j                  d      |	cxk  r|j                  d      k  rn d||<   d||<    |S # t        $ r d||<   Y w xY w)NReadonlyz'Read only Attribute cannot be modified.TypeEnumerationFr#   ValueDisplayNameTzInvalid value for Enumeration.Integer
LowerBound
UpperBoundzInteger out of valid range.zNot a valid integer.r%   )r   r@   intr   )
registry	attr_dictrX   r   r   val_dicttypefoundvalis
             r   validate_vs_registryr     s   G! 51=||AH||J'F
||F+=(!E'||GR8 "(: ;;$(E!" !%E
9$GF !)\ :a ]8<<P\C] ])FGAJ4GAJ152 N % <%;
<s   C  C10C1c                    d}i }i }i }i }| j                  dj                  t        |      d      }|j                  j	                  di       j	                  di       j	                  di       j	                  d      }	|	rK|	D ],  }
|
j	                  d      }|j                  d	      d
   }|||<   . |j                  j	                  d      }|j                  j	                  d      }|j                  j	                  d      }i }t        |       }|5t        | t        ||      \  }}|j                  t        ||             ||z  }|5t        | t        ||      \  }}|j                  t        ||             ||z  }|5t        | t        ||      \  }}|j                  t        ||             ||z  }|r|j                  dd|       nt        || |      }|j                   dk(  rddg}|j                  d   }|j                  d   }|dk(  r|j                  t"               nJ|dk(  r|j                  t$        d       n-||v r|j'                  t(               n|j'                  |       |||||fS )Nr   z{0}/{1}r}   LinksOemDellDellAttributesz	@odata.idrc   rd   r]   r^   r_   Tr9   )failedr:   r;      z?Unable to complete application of configuration profile values.zGImport of Server Configuration Profile operation completed with errors.Message	MessageIdSYS069r>   SYS053r<   )r   r   MANAGERS_URIr   r@   rj   rg   r   r   rA   rB   r   rC   rD   rJ   r{   status_coderM   SUCCESS_MSGrE   ATTR_FAIL_MSG)rO   rN   rm   r   r   idrac_response_attrsystem_response_attrlc_response_attrr   dell_attributesitemuriattr_idrn   ro   rp   rX   attr_registryr   rz   	error_msgmessage
message_ids                          r   fetch_idrac_uri_attrr     s   DH##I$4$4\6$JERH((,,Wb9==eRHLLVUWX\\]mnO# 	$D((;'CiinR(G #HW	$ ]]&&'9:
mm''(;<--##$EF/6!%6uj*V^%_"A"NN/?RSTAID"&7y+W_&`#A#NN/?STUAID"3E5'8"TANN/?OPQAIDD.Odkl'v>3&ZbdI((3G!++K8JX%  ^ 4x'  [$ ?I%  ] 3  W -.0DFVVVr    c                     |s| j                  t               y |r%| j                  r| j                  t        d       y y y )Nr>   Tr<   )rJ   rM   rH   rK   )rN   r   s     r   process_check_moder     s;    ^,	&##[$7 $r    c                 ,   i }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }	|>|<| j                  |j                  t              dt        |i      }
|
j
                  |d<   |>|<| j                  |j                  t              dt        |i      }|j
                  |d<   |	>|<| j                  |j                  t              dt        |	i      }|j
                  |d<   |S )	Nr]   r^   r_   PATCH)dataiDRACSystemzLifecycle Controller)rg   r@   r   rA   r   r   rC   rD   )rO   rN   r   r   r   r   rx   idrac_payloadsystem_payload
lc_payloadidrac_responsesystem_responselc_responses                r   update_idrac_attributesr     s   DMM%%&89M]]&&':;N""#DEJ %8%D--hll:.FW[]jVk-l&00W!&:&F..x||I/FW[]kVl.m(22X"2">**8<<+>tU_N`*a'2'<'<#$Kr    c            	          ddddddddddddd} t        | dgd      }	 t        |j                  d	      5 }|j                  j                  d
      }|st	        |      }t        |||      \  }}}}}t        ||       t        ||||||       |j                  t        d       d d d        y # 1 sw Y   y xY w# t        $ r9}	|j                  t        |	      t        j                  |	             Y d }	~	y d }	~	wt        $ r&}	|j                  t        |	      d       Y d }	~	y d }	~	wt         t"        t$        t&        t(        t*        t,        f$ r9}	|j                  t        |	      t        j                  |	             Y d }	~	y d }	~	ww xY w)NFr   )requiredr   r   )r]   r^   r_   resource_id)r]   r^   r_   T)argument_specrequired_one_ofsupports_check_mode)req_sessionr   r<   )r:   
error_info)r:   unreachable)r
   r	   rg   r@   r   r   r   r   rJ   r   r   rE   r   jsonloadr   IOError
ValueError	TypeErrorr   AttributeError
IndexErrorKeyError)
specsrN   rO   rm   r   r   r   r   r   errs
             r   mainr     sz   ).?*/@8=v+N$)59	E  ef F
BV]]= 	<]]&&}5F+E2Znotv|  E  [FWD(/1EGWvt,#E68=PRfhxyd;	< 	< 	<  BSX$))C.AA 9SX488ZO^ZYab BSX$))C.AABsM   B? A/B3*B? 3B<8B? <B? ?	F/C<<FD))+F/FF__main__)N)+
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   r   +ansible.module_utils.six.moves.urllib.errorr   r   ansible.module_utils.urlsr   Iansible_collections.dellemc.openmanage.plugins.module_utils.idrac_redfishr	   r
   Aansible_collections.dellemc.openmanage.plugins.module_utils.utilsr   r   rM   rK   r   rC   rA   rD   r   r   rk   r   r.   r[   r{   r   r   r   r   r   r   r   __name__r   r    r   <module>r      s    C B9vrh.
`  	 K 5 y ` 52,c	
(%
;	#-<4	</Wd8"B< zF r    