
    Vh*%                         d dl mZmZmZ eZddgddZdZdZdZ	d d	l
Z
d d	lZd d
lmZ d dlmZmZmZ d dlmZmZ d dlmZmZmZ d Zd Zd Zedk(  r e        y	y	)    )absolute_importdivisionprint_functionz1.1preview	community)metadata_versionstatussupported_bya
  
---
module: cpm_plugconfig
version_added: "2.8.0"
author: "Western Telematic Inc. (@wtinetworkgear)"
short_description: Get and Set Plug Parameters on WTI OOB and PDU power devices
description:
    - "Get and Set Plug Parameters on WTI OOB and PDU devices"
options:
    cpm_action:
        description:
            - This is the Action to send the module.
        type: str
        required: true
        choices: [ "getplugconfig", "setplugconfig" ]
    cpm_url:
        description:
            - This is the URL of the WTI device to send the module.
        type: str
        required: true
    cpm_username:
        description:
            - This is the Username of the WTI device to send the module.
        type: str
        required: true
    cpm_password:
        description:
        - This is the Password of the WTI device to send the module.
        type: str
        required: true
    use_https:
        description:
            - Designates to use an https connection or http connection.
        type: bool
        required: false
        default: true
    validate_certs:
        description:
            - If false, SSL certificates will not be validated. This should only be used
            - on personally controlled sites using self-signed certificates.
        type: bool
        required: false
        default: true
    use_proxy:
        description: Flag to control if the lookup will observe HTTP proxy environment variables when present.
        type: bool
        required: false
        default: false
    plug_id:
        description:
            - This is the plug number that is to be manipulated
            - For the getplugconfig command, the plug_id 'all' will return the status of all the plugs the
            - user has rights to access.
        type: str
        required: true
    plug_name:
        description:
            - The new name of the Plug.
        type: str
        required: false
    plug_bootdelay:
        description:
            - On a reboot command, this is the time when a plug will turn on power, after it has been turned off.
            - 0='0.5 Secs', 1='1 Sec', 2='2 Sec', 3='5 Sec', 4='15 Sec', 5='30 Sec', 6='1 Min', 7='2 Mins',
             - 8='3 Mins', 9='5 Mins'.
        type: int
        required: false
        choices: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
    plug_default:
        description:
            - What the Plugs default state is when the device starts. 0 - Off, 1 - On.
        type: int
        required: false
        choices: [ 0, 1 ]
    plug_bootpriority:
        description:
            - Prioritizes which plug gets its state changed first. The lower the number the higher the priority.
            - Valid value can from 1 to the maximum number of plugs of the WTI unit.
        type: int
        required: false
a  
# Get Plug parameters for all ports
- name: Get the Plug parameters for ALL ports of a WTI Power device
  cpm_plugconfig:
    cpm_action: "getplugconfig"
    cpm_url: "rest.wti.com"
    cpm_username: "restpower"
    cpm_password: "restfulpowerpass12"
    use_https: true
    validate_certs: true
    plug_id: "all"

# Get Plug parameters for port 2
- name: Get the Plug parameters for the given port of a WTI Power device
  cpm_plugconfig:
    cpm_action: "getplugconfig"
    cpm_url: "rest.wti.com"
    cpm_username: "restpower"
    cpm_password: "restfulpowerpass12"
    use_https: true
    validate_certs: false
    plug_id: "2"

# Configure plug 5
- name: Configure parameters for Plug 5 on a given WTI Power device
  cpm_plugconfig:
    cpm_action: "setplugconfig"
    cpm_url: "rest.wti.com"
    cpm_username: "restpower"
    cpm_password: "restfulpowerpass12"
    use_https: true
    plug_id: "5"
    plug_name: "NewPlugNameFive"
    plug_bootdelay: "3"
    plug_default: "0"
    plug_bootpriority: "1"
zk
data:
    description: The output JSON returned from the commands sent
    returned: always
    type: str
N)AnsibleModule)to_textto_bytes	to_native)	HTTPErrorURLError)open_urlConnectionErrorSSLValidationErrorc                 :   d}| j                   d   j                  d      }|D ]  }t        |      dk(  rd}nd|z  }|dt        |      d}| j                   d	   |d
t        | j                   d	         d}| j                   d   |dt        | j                   d         d}| j                   d   |dt        | j                   d         d}| j                   d   |dt        | j                   d         d}d|z  } t        |      dkD  rd|z  }|S )N plug_id,r   z{"plugs": [z%s,z
{"plug": ""	plug_namez,"plugname": "plug_bootdelayz,"bootdelay": "plug_defaultz,"default": "plug_bootpriorityz,"bootpriority": "z%s}z%s]})paramssplitlenr   )	cpmmodule	cpmresult	json_loadplugspassedvals        m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/wti/remote/plugins/modules/cpm_plugconfig.pyassemble_jsonr&      s4   I""9-33C8K (y>Q%I+I)2IcNC	K(41:IiFVFVWbFc<deI,-92;YyGWGWXhGi=jkIN+7099YEUEUVdEe;fgI/0<5>	)JZJZ[nJo@pqIY'	#(& 9~i(	    c                  >   t        t        ddgd      t        dd      t        dd      t        ddd      t        dd      t        dd      t        d	dd g d
      t        d	dd ddg      t        d	dd       t        dd      t        dd      t        dd            } t        ddd      }t        | d      }|j                  r|S t        t	        j
                  t        dj                  t        |j                  d         t        |j                  d               d                  }|j                  d   du rd}nd}d }|j                  d   dk(  r]|t        |j                  d         d}|j                  d    j                         d!k7  r|d"t        |j                  d          }d#}nB|j                  d   dk(  r0t        ||      }||d$<   |t        |j                  d         d}d%}	 t        ||j                  d&   |j                  d'   d(d)|z  d*+      }|d#k7  rd|d,<   t%        j&                  j)                               |d2<    |j*                  d3i | y # t        $ rB}	t        d-j                  t        |	            d.      }
 |j                  d3i |
 Y d }	~	d }	~	wt        $ rB}	t        d/j                  t        |	            d.      }
 |j                  d3i |
 Y d }	~	d }	~	wt         $ rC}	t        d0j                  t        |	            d.      }
 |j                  d3i |
 Y d }	~	d }	~	wt"        $ rC}	t        d1j                  t        |	            d.      }
 |j                  d3i |
 Y d }	~	\d }	~	ww xY w)4NgetplugconfigsetplugconfigT)choicesrequiredstr)typer,   )r.   r,   no_logFint)
r                           	   )r.   r,   defaultr+   r   r1   )r.   r,   r:   bool)r.   r:   )
cpm_actioncpm_urlcpm_usernamecpm_passwordr   r   r   r   r   	use_httpsvalidate_certs	use_proxyr   )changeddatadebug)argument_specsupports_check_modez{0}:{1}r>   r?   surrogate_or_strict)errorsr@   zhttps://zhttp://r<   r=   z/api/v2/config/powerplugconfigr   allz?plug=GETrE   POSTrA   rB   zapplication/jsonzBasic %s)zContent-TypeAuthorization)rD   methodrA   rB   headersrC   z!Received HTTP error for {0} : {1})msgrC   zFailed lookup url for {0} : {1}z6Error validating the servers certificate for {0} : {1}z"Error connecting to  for {0} : {1}rD    )dictr   
check_moder   base64	b64encoder   formatr   r   lowerr&   r   r   	fail_jsonr   r   r   jsonloadsread	exit_json)module_argsresultmoduleauthprotocolPayloadfullurlrN   responseerX   s              r%   
run_modulerf      s    / BTR%$/ut4utDA%$/EE2NlmuudQPQFSEE4HFD16FE2K F $OF6##HY-=-=iVdHe>fhqrxrr  AO  sP  iQ  .R/%1 2 3D }}[!T)Gl#6;CYv}}]fOgEhiMM)$**,5&-yy9Q/RSG
--
%
8/!w;CYv}}]fOgEhi&G'&QWQ^Q^_oQp  }C  }J  }J  KV  }W5GZdgkZk$lneO $F9 ZZ0F6NFv  &@GGQZ[\Q]^hmn	%9% &>EEgyYZ|\fkl	%9% &W^^_fhqrshtu  @E  F	%9%% &AHHR[\]R^_ino	%9%%&s<    ;I4 4	N=8J::N8LN8MN8NNc                      t                y )N)rf   rQ   r'   r%   mainrh     s    Lr'   __main__)
__future__r   r   r   r.   __metaclass__ANSIBLE_METADATADOCUMENTATIONEXAMPLESRETURNrT   rY   ansible.module_utils.basicr   ansible.module_utils._textr   r   r   +ansible.module_utils.six.moves.urllib.errorr   r   ansible.module_utils.urlsr   r   r   r&   rf   rh   __name__rQ   r'   r%   <module>ru      s   & A @ k Pd$L
   4 C C K S S:GT zF r'   