
    VhO)                         d dl mZmZmZ eZdZdZdZd dl	m
Z
mZ d dlmc mZ d dlmZ d dlmZ dd	Zd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_functiona  
module: spectrum_device
short_description: Creates/deletes devices in CA Spectrum
description:
  - This module allows you to create and delete devices in CA Spectrum U(https://www.ca.com/us/products/ca-spectrum.html).
  - Tested on CA Spectrum 9.4.2, 10.1.1 and 10.2.1.
author: "Renato Orgito (@orgito)"
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  device:
    type: str
    aliases: [host, name]
    required: true
    description:
      - IP address of the device.
      - If a hostname is given, it will be resolved to the IP address.
  community:
    type: str
    description:
      - SNMP community used for device discovery.
      - Required when O(state=present).
    required: true
  landscape:
    type: str
    required: true
    description:
      - Landscape handle of the SpectroServer to which add or remove the device.
  state:
    type: str
    description:
      - On V(present) creates the device when it does not exist.
      - On V(absent) removes the device when it exists.
    choices: ['present', 'absent']
    default: 'present'
  url:
    type: str
    aliases: [oneclick_url]
    required: true
    description:
      - HTTP, HTTPS URL of the Oneclick server in the form V((http|https\)://host.domain[:port]).
  url_username:
    type: str
    aliases: [oneclick_user]
    required: true
    description:
      - Oneclick user name.
  url_password:
    type: str
    aliases: [oneclick_password]
    required: true
    description:
      - Oneclick user password.
  use_proxy:
    description:
      - If V(false), it will not use a proxy, even if one is defined in an environment variable on the target hosts.
    default: true
    type: bool
  validate_certs:
    description:
      - If V(false), SSL certificates will not be validated. This should only be used on personally controlled sites using
        self-signed certificates.
    default: true
    type: bool
  agentport:
    type: int
    required: false
    description:
      - UDP port used for SNMP discovery.
    default: 161
notes:
  - The devices will be created inside the I(Universe) container of the specified landscape.
  - All the operations will be performed only on the specified landscape.
aV  
- name: Add device to CA Spectrum
  local_action:
    module: spectrum_device
    device: '{{ ansible_host }}'
    community: secret
    landscape: '0x100000'
    oneclick_url: http://oneclick.example.com:8080
    oneclick_user: username
    oneclick_password: password
    state: present


- name: Remove device from CA Spectrum
  local_action:
    module: spectrum_device
    device: '{{ ansible_host }}'
    landscape: '{{ landscape_handle }}'
    oneclick_url: http://oneclick.example.com:8080
    oneclick_user: username
    oneclick_password: password
    use_proxy: false
    state: absent
z
device:
  description: Device data when O(state=present).
  returned: success
  type: dict
  sample: {'model_handle': '0x1007ab', 'landscape': '0x100000', 'address': '10.10.5.1'}
)gethostbynamegaierrorN)AnsibleModule)	fetch_urlc                 
   ddd}t         j                  d   dz   | z   }t        t         ||||d      \  }}|d   dk(  rt         j                  d	
       |d   dvrt         j                  |d   
       |j	                         S )Nzapplication/xml)zContent-TypeAcceptoneclick_urlz/spectrum/restful/-   )datamethodheaderstimeoutstatusi  z)failed to authenticate to Oneclick servermsg)         r   )moduleparamsr	   	fail_jsonread)resourcexmlr   r   urlresponseinfos          u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/spectrum_device.pyrequestr"      s    )#G
 --
'*>
>
ICvsVW^`aNHdH~HIH~_,T%[)==?    c                     t        | |d      S )NPOSTr   r   r"   )r   r   s     r!   postr(      s    8V44r#   c                     t        | d d      S )NDELETEr&   r'   )r   s    r!   deleter+      s    8h77r#   c                      	 t        t        j                  j                  d            } | S # t        $ r: t        j                  dt        j                  j                  d      z         Y  S w xY w)Ndevicez,failed to resolve device ip address for '%s'r   )r   r   r   getr   r   )	device_ips    r!   get_ipr0      si    k!&--"3"3H"=>	   kKfmmN_N_`hNiijks   (, ?A/.A/c                    d}dt        t        j                  j                  d      d      z  }dt        t        j                  j                  d      d      dz   z  }dj	                  | ||      }t        ||      }t        j                  |      }|j                  d	      d
k(  ryt        d      }|j                  d|      j                  d|      }|j                  d      r(t        j                  d|j                  d      z         |j                  d      }	|j                  d      j                  }
dt        t        |	d      dz  dz        z  }t        |	|
|      }|S )z2Query OneClick for the device using the IP Addressz/models0x%x	landscape      a$  <?xml version="1.0" encoding="UTF-8"?>
        <rs:model-request throttlesize="5"
        xmlns:rs="http://www.ca.com/spectrum/restful/schema/request"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd">
            <rs:target-models>
            <rs:models-search>
                <rs:search-criteria xmlns="http://www.ca.com/spectrum/restful/schema/filter">
                    <action-models>
                        <filtered-models>
                            <and>
                                <equals>
                                    <model-type>SearchManager</model-type>
                                </equals>
                                <greater-than>
                                    <attribute id="0x129fa">
                                        <value>{mh_min}</value>
                                    </attribute>
                                </greater-than>
                                <less-than>
                                    <attribute id="0x129fa">
                                        <value>{mh_max}</value>
                                    </attribute>
                                </less-than>
                            </and>
                        </filtered-models>
                        <action>FIND_DEV_MODELS_BY_IP</action>
                        <attribute id="AttributeID.NETWORK_ADDRESS">
                            <value>{search_ip}</value>
                        </attribute>
                    </action-models>
                </rs:search-criteria>
            </rs:models-search>
            </rs:target-models>
            <rs:requested-attribute id="0x12d7f" /> <!--Network Address-->
        </rs:model-request>
        )	search_ipmh_minmh_max)r   ztotal-models0N2http://www.ca.com/spectrum/restful/schema/responsecazca:model-responsesca:modelerrorzerror checking device: %sr   mhz./*[@id="0x12d7f"]model_handleaddressr3   )intr   r   r.   formatr(   ET
fromstringdictfindr   text)r/   r   landscape_minlandscape_maxr   resultroot	namespacemodelrA   model_addressmodel_landscaper-   s                r!   
get_devicerR      sR   HS!2!2;!?DDMc&--"3"3K"@"EPQM$H FY}]FSI L ($F== Dxx3&LMI II*I6;;J	REyy8599W;MMN 99T?LJJ3499M s3|R#8H#Dx#OPPO!!#F
 Mr#   c            
         t               } t        |       }|rt        j                  d|       t        j                  rOt        d | dt        t        j                  j                  d      d      z        }t        j                  d|       d| z   d	z   t        j                  j                  d
      z   }|dt        j                  j                  d      z   z  }t        j                  j                  dd       r/|dt        t        j                  j                  dd            z   z  }t        |      }t        j                  |      }|j                  d      dk7  r%t        j                  |j                  d             t        d      }|j                  d|      }|j                  d      }dt        t        |d      dz  dz        z  }t        || |      }t        j                  d|       y )NF)changedr-   r2   r3   r4   r@   Tzmodel?ipaddress=z&commstring=	communityz&landscapeid=	agentportz&agentport=   r>   Successzerror-messager   r:   r;   r=   r?   r5   )r0   rR   r   	exit_json
check_moderG   rC   r   r.   strr(   rE   rF   r   rH   )	r/   r-   r   rL   rM   rN   rO   rA   rQ   s	            r!   
add_devicer\      s   I	"Fv6s6==#4#4[#A2FFH 	f5!I->ARARS^A__H&--"3"3K"@@@H}}d+MC(9(9+s(K$LLL(^F== DxxI%TXXo67LMIIIj),E99T?Ls3|R#8H#Dx#OPPO!!F T&1r#   c                     t               } t        |       }|t        j                  d       t        j                  rt        j                  d       d|d   z   }t        |      }t        j                  |      }t        d      }|j                  d|      j                  }|d	k7  r7|j                  d
|      j                  }t        j                  |d|       t        j                  d       y )NF)rT   Tz/model/rA   r:   r;   zca:errorrX   zca:error-message r   )r0   rR   r   rY   rZ   r+   rE   rF   rG   rH   rI   r   )r/   r-   r   rL   rM   rN   r>   error_messages           r!   remove_devicer`     s    I	"F~'&6.11HHF== DLMIIIj),11E			"4i@EE}=>
T"r#   c                     t        t        t        dddg      t        d      t        ddgd      t        dd	      t        d
d      t        ddg      t        ddg      t        dddg      t        dd      t        dd      
      dddgfgd      at        j                  j	                  d      dk(  rt                y t                y )NThostname)requiredaliases)rd   presentabsent)choicesdefault)rd   no_logrC   rW   )typeri   r   oneclick_useroneclick_password)rd   rj   re   bool)
r-   r3   staterU   rV   r   url_usernameurl_password	use_proxyvalidate_certsro   rU   )argument_specrequired_ifsupports_check_mode)r   rG   r   r   r.   r\   r`    r#   r!   mainrx   5  s    /?@D)	84iHD6s3d^,<=to5FGtDCVBWX5VT:
 y;-89 F" }}!Y.r#   __main__)NN)N)
__future__r   r   r   rk   __metaclass__DOCUMENTATIONEXAMPLESRETURNsocketr   r   xml.etree.ElementTreeetreeElementTreerE   ansible.module_utils.basicr   ansible.module_utils.urlsr	   r"   r(   r+   r0   rR   r\   r`   rx   __name__rw   r#   r!   <module>r      sz    A @N`2
 + " " 4 /&58HV&2R#22 zF r#   