
    VhKf                     N   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mZ d dlmZmZ d d	lmZmZmZmZ d
ZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dddZ)dddd Z*d!d"d#d$d%d&d'd(d)d*d+
Z+d,d-dd.Z,d/ Z-d0 Z.d1 Z/d9d2Z0d3 Z1d4 Z2d5 Z3d6 Z4d7 Z5e6d8k(  r e5        yy):    )absolute_importdivisionprint_functionu  
---
module: idrac_boot
short_description: Configure the boot order settings.
version_added: "6.1.0"
description:
  - This module allows to configure the boot order settings.
extends_documentation_fragment:
  - dellemc.openmanage.idrac_x_auth_options
options:
  boot_options:
    type: list
    elements: dict
    description:
      - Options to enable or disable the boot devices.
      - This is mutually exclusive with I(boot_order), I(boot_source_override_mode), I(boot_source_override_enabled)
        I(boot_source_override_target), and I(uefi_target_boot_source_override).
    suboptions:
      boot_option_reference:
        type: str
        description:
          - FQDD of the boot device.
          - This is mutually exclusive with I(display_name).
      display_name:
        type: str
        description:
          - Display name of the boot source device.
          - This is mutually exclusive with I(boot_option_reference).
      enabled:
        type: bool
        required: true
        description: Enable or disable the boot device.
  boot_order:
    type: list
    elements: str
    description:
      - This option allows to set the boot devices in the required boot order sequences.
      - This is mutually exclusive with I(boot_options).
  boot_source_override_mode:
    type: str
    description:
      - The BIOS boot mode (either Legacy or UEFI) to be used when I(boot_source_override_target)
        boot source is booted from.
      - C(legacy) The system boot in non-UEFI(Legacy) boot mode to the I(boot_source_override_target).
      - C(uefi) The system boot in UEFI boot mode to the I(boot_source_override_target).
      - This is mutually exclusive with I(boot_options).
    choices: [legacy, uefi]
  boot_source_override_enabled:
    type: str
    description:
      - The state of the Boot Source Override feature.
      - C(disabled) The system boots normally.
      - C(once) The system boots (one time) to the I(boot_source_override_target).
      - C(continuous) The system boots to the target specified in the I(boot_source_override_target)
        until this property is set to Disabled.
      - The state is set to C(once) for the one-time boot override and C(continuous) for the
        remain-active-until—canceled override. If the state is set C(once) or C(continuous), the value is reset
        to C(disabled) after the I(boot_source_override_target) actions have completed successfully.
      - Changes to this options do not alter the BIOS persistent boot order configuration.
      - This is mutually exclusive with I(boot_options).
    choices: [continuous, disabled, once]
  boot_source_override_target:
    type: str
    description:
      - The boot source override target device to use during the next boot instead of the normal boot device.
      - C(pxe) performs PXE boot from the primary NIC.
      - C(floppy), C(cd), C(hdd), C(sd_card) performs boot from their devices respectively.
      - C(bios_setup) performs boot into the native BIOS setup.
      - C(utilities) performs boot from the local utilities.
      - C(uefi_target) performs boot from the UEFI device path found in I(uefi_target_boot_source_override).
      - If the I(boot_source_override_target) is set to a value other than C(none) then the
        I(boot_source_override_enabled) is automatically set to C(once).
      - Changes to this options do not alter the BIOS persistent boot order configuration.
      - This is mutually exclusive with I(boot_options).
    choices: [uefi_http, sd_card, uefi_target, utilities, bios_setup, hdd, cd, floppy, pxe, none]
  uefi_target_boot_source_override:
    type: str
    description:
      - The UEFI device path of the device from which to boot when I(boot_source_override_target) is C(uefi_target).
      - I(boot_source_override_enabled) cannot be set to c(continuous) if I(boot_source_override_target)
        set to C(uefi_target) because this settings is defined in UEFI as a one-time-boot setting.
      - Changes to this options do not alter the BIOS persistent boot order configuration.
      - This is required if I(boot_source_override_target) is C(uefi_target).
      - This is mutually exclusive with I(boot_options).
  reset_type:
    type: str
    description:
      - C(none) Host system is not rebooted and I(job_wait) is not applicable.
      - C(force_restart) Forcefully reboot the Host system.
      - C(graceful_restart) Gracefully reboot the Host system.
    choices: [graceful_restart, force_restart, none]
    default: graceful_restart
  job_wait:
    type: bool
    description:
      - Provides the option to wait for job completion.
      - This is applicable when I(reset_type) is C(force_reset) or C(graceful_reset).
    default: true
  job_wait_timeout:
    type: int
    description:
      - The maximum wait time of I(job_wait) in seconds. The job is tracked only for this duration.
      - This option is applicable when I(job_wait) is C(true).
    default: 900
  resource_id:
    type: str
    description: Redfish ID of the resource.
requirements:
    - "python >= 3.9.6"
author:
    - "Felix Stephen (@felixs88)"
notes:
    - Run this module from a system that has direct access to Dell iDRAC.
    - This module supports C(check_mode).
a  
---
- name: Configure the system boot options settings.
  dellemc.openmanage.idrac_boot:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    boot_options:
      - display_name: Hard drive C
        enabled: true
      - boot_option_reference: NIC.PxeDevice.2-1
        enabled: true

- name: Configure the boot order settings.
  dellemc.openmanage.idrac_boot:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    boot_order:
      - Boot0001
      - Boot0002
      - Boot0004
      - Boot0003

- name: Configure the boot source override mode.
  dellemc.openmanage.idrac_boot:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    boot_source_override_mode: legacy
    boot_source_override_target: cd
    boot_source_override_enabled: once

- name: Configure the UEFI target settings.
  dellemc.openmanage.idrac_boot:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    boot_source_override_mode: uefi
    boot_source_override_target: uefi_target
    uefi_target_boot_source_override: "VenHw(3A191845-5F86-4E78-8FCE-C4CFF59F9DAA)"

- name: Configure the boot source override mode as pxe.
  dellemc.openmanage.idrac_boot:
    idrac_ip: "192.168.0.1"
    idrac_user: "user_name"
    idrac_password: "user_password"
    ca_path: "/path/to/ca_cert.pem"
    boot_source_override_mode: legacy
    boot_source_override_target: pxe
    boot_source_override_enabled: continuous
a  
---
msg:
  description: Successfully updated the boot settings.
  returned: success
  type: str
  sample: Successfully updated the boot settings.
job:
  description: Configured job details.
  returned: success
  type: dict
  sample: {
    "ActualRunningStartTime": "2019-06-19T00:57:24",
    "ActualRunningStopTime": "2019-06-19T01:00:27",
    "CompletionTime": "2019-06-19T01:00:27",
    "Description": "Job Instance",
    "EndTime": "TIME_NA",
    "Id": "JID_609237056489",
    "JobState": "Completed",
    "JobType": "BIOSConfiguration",
    "Message": "Job completed successfully.",
    "MessageArgs": [],
    "MessageId": "PR19",
    "Name": "Configure: BIOS.Setup.1-1",
    "PercentComplete": 100,
    "StartTime": "2019-06-19T00:55:05",
    "TargetSettingsURI": null }
boot:
  description: Configured boot settings details.
  returned: success
  type: dict
  sample: {
    "BootOptions": {
      "Description": "Collection of BootOptions",
      "Members": [{
        "BootOptionEnabled": false,
        "BootOptionReference": "HardDisk.List.1-1",
        "Description": "Current settings of the Legacy Boot option",
        "DisplayName": "Hard drive C:",
        "Id": "HardDisk.List.1-1",
        "Name": "Legacy Boot option",
        "UefiDevicePath": "VenHw(D6C0639F-C705-4EB9-AA4F-5802D8823DE6)"}],
      "Name": "Boot Options Collection"
      },
      "BootOrder": [ "HardDisk.List.1-1"],
      "BootSourceOverrideEnabled": "Disabled",
      "BootSourceOverrideMode": "Legacy",
      "BootSourceOverrideTarget": "None",
      "UefiTargetBootSourceOverride": null }
error_info:
  description: Details of the HTTP Error.
  returned: on HTTP error
  type: dict
  sample: {
    "error": {
      "code": "Base.1.0.GeneralError",
      "message": "A general error has occurred. See ExtendedInfo for more information.",
      "@Message.ExtendedInfo": [
        {
          "MessageId": "GEN1234",
          "RelatedProperties": [],
          "Message": "Unable to process the request because an error occurred.",
          "MessageArgs": [],
          "Severity": "Critical",
          "Resolution": "Retry the operation. If the issue persists, contact your system administrator."
        }
      ]
    }
  }
N)URLError	HTTPError)ConnectionErrorSSLValidationError)iDRACRedfishAPIIdracAnsibleModule)strip_substr_dictidrac_system_resetget_system_res_idwait_for_idrac_job_completionz/redfish/v1/Systemsz8/redfish/v1/Systems/{0}/BootOptions?$expand=*($levels=1)z?/redfish/v1/Managers/iDRAC.Embedded.1/Jobs?$expand=*($levels=1)z./redfish/v1/Managers/iDRAC.Embedded.1/Jobs/{0}z#/redfish/v1/Systems/{0}/BootSourcesz,/redfish/v1/Systems/{0}/BootSources/SettingszNo changes found to be applied.zChanges found to be applied.zwUnable to complete the request because the BIOS configuration job already exists. Wait for the pending job to complete.z{0} boot_options provided.z"{0} boot order reference provided.z'Successfully updated the boot settings.z#Failed to update the boot settings.z4The system does not support the BootOptions feature.z@The boot settings operation is triggered/submitted successfully.zUnable to communicate with iDRAC {0}. This may be due to one of the following: Incorrect username or password, unreachable iDRAC IP or a failure in TLS/SSL handshake.LegacyUEFI)legacyuefi
ContinuousDisabledOnce
continuousdisabledonceNonePxeFloppyCdHdd	BiosSetup	Utilities
UefiTargetSDCardUefiHttp)
nonepxefloppycdhdd
bios_setup	utilitiesuefi_targetsd_card	uefi_httpGracefulRestartForceRestartgraceful_restartforce_restartr%   c                    |j                  dj                  t        |      d      }|j                  d   }|j	                  dd        |j	                  dd        |j	                  dd        |j                  d      1| j                  j                  d      | j                  t        	       |j                  j                  d
      "|j                  d
   d   d   }d|vr	dt        d<   |S )N{0}/{1}GETBootCertificateszBootOrder@odata.countz0BootSourceOverrideTarget@Redfish.AllowableValuesBootOptionsboot_optionsmsgActionsz#ComputerSystem.Resetz!ResetType@Redfish.AllowableValuesr/   r0   r2   )
invoke_requestformat
SYSTEM_URI	json_datapopgetparams	fail_jsonUNSUPPORTED_MSG
RESET_TYPE)moduleidracres_idresp	resp_data
type_resets         q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/dellemc/openmanage/plugins/modules/idrac_boot.pyget_response_attributesrO   -  s    	 0 0V DeLDv&IMM.$'MM)40MMDdK}}]#+0A0A.0Q0]_-~~)$0^^I./FGHkl
J.-;J)*    c                     | j                  t        j                  |      d      }t        |j                        }g }|d   D ]  }|j                  t        |              ||d<   |S )Nr6   Members)r>   BOOT_OPTIONS_URIr?   r   rA   append)rI   rJ   rK   rL   strip_memberseachs         rN   get_existing_boot_optionsrW   <  sk     0 7 7 ?GD!$..1IM)$ 6.t456(IirP   c                     ddd| j                   j                  d      i f\  }}}}}|E|dk(  s@dt        |   i}t        |||d      \  }}}}t        d   d	k(  rd}|d
k(  rt        d   dk(  rd}||||fS )N FT
reset_typer%   	ResetType)payloadjob_waitr2   r0   r3   r/   )rD   rC   rG   r   )	rH   rI   rJ   	reset_msgtrack_failedresetrZ   job_respdatas	            rN   system_resetrc   F  s    ;=udFMML]L]^jLkmo;o8I|UJjF&:Z
343EeV]alp3q0|Y()^;E(Z8J-KO`-`E,	833rP   c                    g dddg c}}t        j                  d       | j                  t        d      }|j                  d   }|r4t        t        fd|            }t        t        fd|            }|rd	}||fS )
N)	ScheduledNewRunningFBIOSConfiguration
   r6   rR   c                 ,    | j                  d      fv S )NJobTyperC   )djob_type_names    rN   <lambda>z#get_scheduled_job.<locals>.<lambda>Z  s    i0@]O0S rP   c                 *    | j                  d      v S )NJobStaterl   )rm   	job_states    rN   ro   z#get_scheduled_job.<locals>.<lambda>[  s    QUU:->)-K rP   T)timesleepr>   JOB_URIrA   listfilter)rI   rr   is_jobprogress_jobra   job_resp_memberbios_config_jobrn   s    `     @rN   get_scheduled_jobr|   R  s    3	*/1Db'FM<JJrN##GU3H((3Ov&SUdefF#K_]^F<rP   c           
      ~   t        |      \  }}i | j                  d   }}t        | ||      }|d   }	| j                  d   dk(  rd}|r| j                  t               |j                  t        j                  |      d      }
|	dk(  rd	nd
}|
j                  d   |   }|D cg c];  }|j                  |d         %|j                  d|j                  |d         i      = c} ||iddid}|d
k(  rdt        t        |            D ]E  }|j                  |d   |         ||   j                  d|j                  |d   |         i       G ||d   |<   |j                  t        j                  |      d|      }|j                  dk(  r|j                  d   }|j!                  d      d   }t#        | ||      \  }}}}|r|j                  }|rTt%        |t&        j                  |      || j                  d         \  }}|r| j                  |       |j                  }|S | j                  |       |S c c}w )Nr]   BootSourceOverrideModerZ   r%   Fr;   r6   r   BootSeqUefiBootSeq
AttributesNameEnabled	ApplyTimeOnReset)r   z@Redfish.SettingsApplyTime	BootOrderPATCHrb      Location/job_wait_timeoutr]   wait_timeout)r|   rD   rO   rE   
JOB_EXISTSr>   BOOT_SEQ_URIr?   rA   rC   updaterangelenPATCH_BOOT_SEQ_URIstatus_codeheaderssplitrc   r   
JOB_URI_ID)rH   rI   rJ   r\   rx   ry   job_datar]   rL   override_modeboot_seq_respseq_keyboot_seq_datarV   seq_payloadirK   locationjob_idr`   r_   r^   reset_job_respra   	error_msgs                            rN   configure_boot_optionsr   a  sn   ,U3FLV]]:6hH'v>I67M}}\"f,Z((()<)<V)DeLM(H4i-G!++L9'BM,WDL0I0U 
[[)W[[f67  W")=!9ZegpYqrK-s=)* 	]A{{9[1!45Aa ''GKK	+@VWX@Y4Z([\	] .;L!'* 2 9 9& A7Q\]D3<<
+$R(9EfeU[9\6|Y%//H"?zGXGXY_G`IQMS]][mMn#pHi   Y /))H O +O1Ws   "A H:c                    i | j                   d   }}| j                   d   dk(  rd}|j                  dj                  t        |      d|      }|j                  dk(  rt        | ||      \  }}}	}
|
r|
j                  }t        |      \  }}|rn|rZt        |t        j                  |d	   d
         || j                   d         \  }}|r| j                  |       |j                  }|S | j                  |	       |S )Nr]   rZ   r%   Fr5   r   r      r   Idr   r   r;   )rD   r>   r?   r@   r   rc   rA   r|   r   r   rE   )rH   rI   r\   rJ   r   r]   rK   r`   r_   r^   r   rx   ry   ra   r   s                  rN   apply_boot_settingsr     s   V]]:6hH}}\"f,	 0 0V DgT[\D39EfeU[9\6|Y%//H07&CE:K\K\]ijk]lmq]rKsMUQWQ^Q^_qQr't#) $$$3#-- O   Y /OrP   c                 x   i di i}}| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }t        | ||      }	| |	j                  d      }
|D cg c]	  }||
vs| }}|r&| j                  t        j                  d      |       t        t        |            t        |      k(  sO|d d  }|
D cg c]  }||v s|j                  |       c} | j                  t        j                  d	      |
       t        |      t        |
      k(  s| j                  d       ||
k(  s|d   j                  d|i       |Pt        j                  |      |	j                  d      k(  s)|d   j                  dt        j                  |      i       |Pt        j                  |      |	j                  d      k(  s)|d   j                  dt        j                  |      i       |t        j                  |      |	j                  d      k(  ss|d   j                  dt        j                  |      i       | j                   j                  d      }|dk(  r*||	j                  d      k(  s|d   j                  d|i       | j                  r|d   r| j                  t        d       |S | j                  s| j                  s|d   s| j                  t                |S t#        | |||      }|S c c}w c c}w )Nr7   
boot_orderboot_source_override_modeboot_source_override_enabledboot_source_override_targetr   Invalid)r<   invalid_boot_order	Duplicate)r<   duplicate_boot_orderzZUnable to complete the operation because all boot devices are required for this operation.r;   r~   BootSourceOverrideEnabledBootSourceOverrideTarget uefi_target_boot_source_overrider,   UefiTargetBootSourceOverrideTr<   changed)rD   rC   rO   rE   INVALID_BOOT_OPTr?   r   setremover   BS_OVERRIDE_MODEBS_OVERRIDE_ENABLEDBS_OVERRIDE_TARGET
check_mode	exit_jsonCHANGES_MSGNO_CHANGES_MSGr   )rH   rI   rJ   ra   r\   r   r   override_enabledoverride_targetresponseexist_boot_orderbor   	dup_orderuefi_override_targets                  rN   configure_boot_settingsr     s   VRLgH""<0JMM%%&ABM}}(()GHmm''(EFO&vuf=H#<<4+5TRCS9SbTT!1!8!8!CXjk3z?#s:6"1I,<PbiYb!P!1!8!8!E2;  =:#&6"77 "D E--FO""K#<= !%%m4E]8^^ 8:J:N:N}:]^_#$(()9:hllKf>gg ;=P=T=TUe=fgh"#''8HLLIc<dd :<N<R<RSb<cde%}}001STm+4HHLLYwLx4xFO""$BDX#YZWV_[$7
 O	 

6#4#4gfo^, O 'vugvFO? U
 Qs   	L2"L29	L7L7c           
      &   | j                   j                  d      }g g i i f\  }}}}|t        ||      }|D ]  }	|	j                  d      |	d   n|	j                  d      |	j                  d      dndt        t	        fd|d               }
|
s|j                  |	       od|	d	   i}d|
d
   d   i}|j                  t        t        |j                               t        |j                               z               |	d	   ||
d
   d   <    |r&| j                  t        j                  d      |       t        |      t        |      k(  s&| j                  t        j                  d      |       | j                  r&t        |      du r| j                  t         d       |S | j                  rt#        |      du s| j                  s#t        |      s| j                  t$               |S t'        | |||      }|S t)        | ||      }|S )Nr:   display_nameboot_option_referenceDisplayNameBootOptionReferencec                     |    fv S N )rm   attr_keyattr_vals    rN   ro   z&configure_idrac_boot.<locals>.<lambda>  s    1X;8*+D rP   rR   BootOptionEnabledenabledr   r   r   )r<   invalid_boot_optionsr   )r<   duplicate_boot_optionsTr   Fr;   )rD   rC   rW   rv   rw   rT   boolr   itemsrE   BOOT_OPT_ERROR_MSGr?   r   r   anyr   r   allr   r   r   )rH   rI   rJ   r:   inv_boot_optionsdiff_changer\   ra   boot_option_datarV   reportact_valext_valr   r   s                @@rN   configure_idrac_bootr     s   ==$$^4L792r2~4k7H4UFC  
	;D/3xx/G/StN+Y]YaYabyYzH(,(@(L}RgH&!DFVW`FabcF ''-.Y@.q	:M0NO""4GMMO(<s7==??S(S#TU+/	?q	$(
	; !3!:!:9!E\lm7|s<00!3!:!:;!G`lm[!1T!9d; O C$4$=vGXGXadepaq0
 O .feVWMH O +65&AOrP   c                     dddddddddddddd	gd	gd
ddddddddgdddg ddddg ddddddddg dddddddddddddd
} t        | g dgg dg dgd      }	 t        |j                  d      5 }|j                  j                  d      }|s"t	        |      \  }}|r|j                  |       t        |||      }t        |      }t        ||      }t        |||      }||d<   |rK|j                  d       d!v s!d"|j                  d#      j                         v r|j                  t        |$       |j                  d%   r|j                  d&   d'k(  r,|j                  d       d(k(  s|j                  t        ||)       |j                  t        ||d*       d d d        y # 1 sw Y   y xY w# t        $ r{}	|	j                   d+k(  r2|j                  t"        j%                  |j                  d,                |j                  t'        |	      t)        j*                  |	      d-       Y d }	~	y d }	~	wt,        $ r6 |j                  t"        j%                  |j                  d,         d.       Y y t.        t0        t2        t4        t6        t8        t:        t<        f$ r%}
|j                  t'        |
             Y d }
~
y d }
~
ww xY w)/NFrv   dictstr)requiredtypeTr   )r   r   r   )r   r   )r   r   elementsoptionsmutually_exclusiverequired_one_of)r   r   r   r   r   )r   r   choicesr   )
r.   r-   r,   r+   r*   r)   r(   r'   r&   r%   r2   r1   )r   r   defaultr   )r   r   r   inti  )
r:   r   r   r   r   r   rZ   r]   r   resource_id)r:   r   r   r   r   r   ))r:   r   )r:   r   )r:   r   )r:   r   )r:   r   )r   r,   )r   )argument_specr   r   required_ifsupports_check_mode)req_sessionr   r;   r9   rq   )FailedRebootFailedfailedMessage)r<   jobr]   rZ   r%   RebootCompleted)r<   r   boot)r<   r   r   r   i  idrac_ip)r<   
error_infor   )r<   unreachable)r   r
   rD   rC   r   rE   r   r   rW   rO   lower
FAILED_MSGr   JOB_WAIT_MSGSUCCESS_MSGr   codeAUTH_ERROR_MSGr?   r   jsonloadr   ImportError
ValueErrorRuntimeErrorr	   r   KeyError	TypeError
IndexError)specsrH   rI   rJ   r   ra   job_resp_datar   	boot_attrerres              rN   mainr    s    v66;U)K-2E B(,f=
 $M"M IJ	
 $)&eL27T\^dSe%f5:E4V)X4954Z([ :?,N#(%DV"OQ!&4H).#N$)59/E4  > ?
 b
 !F%V]]= 	_]]&&}5F$5e$<!	$$$3+FE6BH-h7M8G/vFI'7Im$"&&z26PP!2!29!=!C!C!EE  Z] CMM*-|1LPV1V%))*59JJ  \}9 U-iY]^%	_ 	_ 	_&  O88s?!6!6v}}Z7P!QRSX$))C.NN a^226==3LM[_`\3EXy*> %SV$$%sJ   (F= ?D)F1(F= 1F:6F= :F= =	KA1H<<?K=,K)K		K__main__r   )7
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNr  rs   +ansible.module_utils.six.moves.urllib.errorr   r   ansible.module_utils.urlsr   r	   Iansible_collections.dellemc.openmanage.plugins.module_utils.idrac_redfishr
   r   Aansible_collections.dellemc.openmanage.plugins.module_utils.utilsr   r   r   r   r@   rS   ru   r   r   r   r   r   r   r   r   r  r  rF   r  r  r   r   r   rG   rO   rW   rc   r|   r   r   r   r   r  __name__r   rP   rN   <module>r     s5   C Brj7tE
P   K I yn n
 #
M 
K=
4C 2,=
1 7 72
HQk '7 %1zSYZ $Uhd"+K%1hU_a  #4n^bc
	4 $N.(V>F%R zF rP   