
    VhE                         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_plugcontrol
version_added: "2.8.0"
author: "Western Telematic Inc. (@wtinetworkgear)"
short_description: Get and Set Plug actions on WTI OOB and PDU power devices
description:
    - "Get and Set Plug actions on WTI OOB and PDU devices"
options:
  cpm_action:
    description:
      - This is the Action to send the module.
    type: str
    required: true
    choices: [ "getplugcontrol", "setplugcontrol" ]
  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 or the plug name that is to be manipulated
      - For the plugget command, the plug_id 'all' will return the status of all the plugs the
      - user has rights to access.
    type: str
    required: true
  plug_state:
    description:
      - This is what action to take on the plug.
    type: str
    required: false
    choices: [ "on", "off", "boot", "default" ]
ao  
# Get Plug status for all ports
- name: Get the Plug status for ALL ports of a WTI device
  cpm_plugcontrol:
    cpm_action: "getplugcontrol"
    cpm_url: "rest.wti.com"
    cpm_username: "restpower"
    cpm_password: "restfulpowerpass12"
    use_https: true
    validate_certs: true
    plug_id: "all"

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

# Reboot plug 5
- name: Reboot Plug 5 on a given WTI device
  cpm_plugcontrol:
    cpm_action: "setplugcontrol"
    cpm_url: "rest.wti.com"
    cpm_username: "restpower"
    cpm_password: "restfulpowerpass12"
    use_https: true
    plug_id: "5"
    plug_state: "boot"
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 ]i  }|j                         du r|dt        |      d}n|dt        |      d}| j                   d   |d	t        | j                   d         d}d
|z  }k |S )N plug_id,Tz
{"plug": ""z{"plugname": "
plug_statez,"state": "z%s})paramssplitisdigitr   )	cpmmodule	cpmresult	json_loadplugspassedvals        n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/wti/remote/plugins/modules/cpm_plugcontrol.pyassemble_jsonr#      s    I""9-33C8K 	(KKMT!-6	#GI1:IcNKIL)5.79CSCST`Ca9bcIY'		(     c                     t        t        ddgd      t        dd      t        dd      t        ddd      t        dd      t        g dd	      t        d
d      t        d
d      t        d
d	      	      } t        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}n=|j                  d   dk(  r+t        ||      }|t        |j                  d         d}d}	 t        ||j                  d    |j                  d!   d"d#|z  d$%      }|dk7  rd|d&<   t%        j&                  j)                               |d,<    |j*                  d-i | y # t        $ rB}	t        d'j                  t        |	            d	(      }
 |j                  d-i |
 Y d }	~	d }	~	wt        $ rB}	t        d)j                  t        |	            d	(      }
 |j                  d-i |
 Y d }	~	d }	~	wt         $ rC}	t        d*j                  t        |	            d	(      }
 |j                  d-i |
 Y d }	~	d }	~	wt"        $ rC}	t        d+j                  t        |	            d	(      }
 |j                  d-i |
 Y d }	~	\d }	~	ww xY w).NgetplugcontrolsetplugcontrolT)choicesrequiredstr)typer)   )r+   r)   no_log)onoffbootdefaultFbool)r+   r0   )	
cpm_actioncpm_urlcpm_usernamecpm_passwordr   r   	use_httpsvalidate_certs	use_proxyr   )changeddata)argument_specsupports_check_modez{0}:{1}r4   r5   surrogate_or_strict)errorsr6   zhttps://zhttp://r2   r3   z/api/v2/config/powerplugr   allz?plug=GETPOSTr7   r8   zapplication/jsonzBasic %s)zContent-TypeAuthorization)r:   methodr7   r8   headersr9   z!Received HTTP error for {0} : {1})msgr9   zFailed lookup url for {0} : {1}z6Error validating the servers certificate for {0} : {1}z"Error connecting to  for {0} : {1}r:    )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fullurlrC   responseerM   s              r"   
run_moduler[      sc    !13C DtT%$/ut4utDA%$/ @5Q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#'775=yW`Ia?bcMM)$**,5&-yy9Q/RSG
--
%)9
9/5=yW`Ia?bc&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<   ;I 	M+8J		M+8KM+8LM+(8M&&M+c                      t                y )N)r[   rF   r$   r"   mainr]      s    Lr$   __main__)
__future__r   r   r   r+   __metaclass__ANSIBLE_METADATADOCUMENTATIONEXAMPLESRETURNrI   rN   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#   r[   r]   __name__rF   r$   r"   <module>rj      s~   & A @ k =~!F
   4 C C K S S$BJ zF r$   