
    Vht                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZmZ d Zd Zd	 Zd
 Z G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona\   
module: manageiq_provider
short_description: Management of provider in ManageIQ
extends_documentation_fragment:
  - community.general.manageiq
  - community.general.attributes

author: Daniel Korn (@dkorn)
description:
  - The manageiq_provider module supports adding, updating, and deleting provider in ManageIQ.
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none

options:
  state:
    type: str
    description:
      - V(absent) - provider should not exist,
      - V(present) - provider should be present,
      - V(refresh) - provider will be refreshed.
    choices: ['absent', 'present', 'refresh']
    default: 'present'
  name:
    type: str
    description: The provider's name.
    required: true
  type:
    type: str
    description: The provider's type.
    choices: ['Openshift', 'Amazon', 'oVirt', 'VMware', 'Azure', 'Director', 'OpenStack', 'GCE']
  zone:
    type: str
    description: The ManageIQ zone name that will manage the provider.
    default: 'default'
  provider_region:
    type: str
    description: The provider region name to connect to (for example AWS region for Amazon).
  host_default_vnc_port_start:
    type: str
    description: The first port in the host VNC range.
  host_default_vnc_port_end:
    type: str
    description: The last port in the host VNC range.
  subscription:
    type: str
    description: Microsoft Azure subscription ID.
  project:
    type: str
    description: Google Compute Engine Project ID.
  azure_tenant_id:
    type: str
    description: Tenant ID. defaults to None.
    aliases: [keystone_v3_domain_id]
  tenant_mapping_enabled:
    type: bool
    default: false
    description: Whether to enable mapping of existing tenants.
  api_version:
    type: str
    description: The OpenStack Keystone API version.
    choices: ['v2', 'v3']

  provider:
    description: Default endpoint connection information, required if state is true.
    type: dict
    suboptions:
      hostname:
        type: str
        description: The provider's API hostname.
        required: true
      port:
        type: int
        description: The provider's API port.
      userid:
        type: str
        description: Provider's API endpoint authentication userid.
      password:
        type: str
        description: Provider's API endpoint authentication password.
      auth_key:
        type: str
        description: Provider's API endpoint authentication bearer token.
      validate_certs:
        description: Whether SSL certificates should be verified for HTTPS requests (deprecated).
        type: bool
        default: true
        aliases: [verify_ssl]
      security_protocol:
        type: str
        description: How SSL certificates should be used for HTTPS requests.
        choices: ['ssl-with-validation', 'ssl-with-validation-custom-ca', 'ssl-without-validation', 'non-ssl']
      certificate_authority:
        type: str
        description: The CA bundle string with custom certificates.
      path:
        type: str
        description:
          - TODO needs documentation.
      project:
        type: str
        description:
          - TODO needs documentation.
      role:
        type: str
        description:
          - TODO needs documentation.
      subscription:
        type: str
        description:
          - TODO needs documentation.
      uid_ems:
        type: str
        description:
          - TODO needs documentation.
  metrics:
    description: Metrics endpoint connection information.
    type: dict
    suboptions:
      hostname:
        type: str
        description: The provider's API hostname.
        required: true
      port:
        type: int
        description: The provider's API port.
      userid:
        type: str
        description: Provider's API endpoint authentication userid.
      password:
        type: str
        description: Provider's API endpoint authentication password.
      auth_key:
        type: str
        description: Provider's API endpoint authentication bearer token.
      validate_certs:
        description: Whether SSL certificates should be verified for HTTPS requests (deprecated).
        type: bool
        default: true
        aliases: [verify_ssl]
      security_protocol:
        type: str
        choices: ['ssl-with-validation', 'ssl-with-validation-custom-ca', 'ssl-without-validation', 'non-ssl']
        description: How SSL certificates should be used for HTTPS requests.
      certificate_authority:
        type: str
        description: The CA bundle string with custom certificates.
      path:
        type: str
        description: Database name for oVirt metrics. Defaults to V(ovirt_engine_history).
      project:
        type: str
        description:
          - TODO needs documentation.
      role:
        type: str
        description:
          - TODO needs documentation.
      subscription:
        type: str
        description:
          - TODO needs documentation.
      uid_ems:
        type: str
        description:
          - TODO needs documentation.
  alerts:
    description: Alerts endpoint connection information.
    type: dict
    suboptions:
      hostname:
        type: str
        description: The provider's API hostname.
        required: true
      port:
        type: int
        description: The provider's API port.
      userid:
        type: str
        description: Provider's API endpoint authentication userid. defaults to None.
      password:
        type: str
        description: Provider's API endpoint authentication password. defaults to None.
      auth_key:
        type: str
        description: Provider's API endpoint authentication bearer token. defaults to None.
      validate_certs:
        type: bool
        description: Whether SSL certificates should be verified for HTTPS requests (deprecated). defaults to True.
        default: true
        aliases: [verify_ssl]
      security_protocol:
        type: str
        choices: ['ssl-with-validation', 'ssl-with-validation-custom-ca', 'ssl-without-validation', 'non-ssl']
        description: How SSL certificates should be used for HTTPS requests. defaults to None.
      certificate_authority:
        type: str
        description: The CA bundle string with custom certificates. defaults to None.
      path:
        type: str
        description:
          - TODO needs documentation.
      project:
        type: str
        description:
          - TODO needs documentation.
      role:
        type: str
        description:
          - TODO needs documentation.
      subscription:
        type: str
        description:
          - TODO needs documentation.
      uid_ems:
        type: str
        description:
          - TODO needs documentation.
  ssh_keypair:
    description: SSH key pair used for SSH connections to all hosts in this provider.
    type: dict
    suboptions:
      hostname:
        type: str
        description: Director hostname.
        required: true
      userid:
        type: str
        description: SSH username.
      auth_key:
        type: str
        description: SSH private key.
      validate_certs:
        description:
          - Whether certificates should be verified for connections.
        type: bool
        default: true
        aliases: [verify_ssl]
      security_protocol:
        type: str
        choices: ['ssl-with-validation', 'ssl-with-validation-custom-ca', 'ssl-without-validation', 'non-ssl']
        description:
          - TODO needs documentation.
      certificate_authority:
        type: str
        description:
          - TODO needs documentation.
      password:
        type: str
        description:
          - TODO needs documentation.
      path:
        type: str
        description:
          - TODO needs documentation.
      project:
        type: str
        description:
          - TODO needs documentation.
      role:
        type: str
        description:
          - TODO needs documentation.
      subscription:
        type: str
        description:
          - TODO needs documentation.
      uid_ems:
        type: str
        description:
          - TODO needs documentation.
      port:
        type: int
        description:
          - TODO needs documentation.
a  
- name: Create a new provider in ManageIQ ('Hawkular' metrics)
  community.general.manageiq_provider:
    name: 'EngLab'
    type: 'OpenShift'
    state: 'present'
    provider:
      auth_key: 'topSecret'
      hostname: 'example.com'
      port: 8443
      validate_certs: true
      security_protocol: 'ssl-with-validation-custom-ca'
      certificate_authority: |
        -----BEGIN CERTIFICATE-----
        FAKECERTsdKgAwI...
        -----END CERTIFICATE-----
    metrics:
      auth_key: 'topSecret'
      role: 'hawkular'
      hostname: 'example.com'
      port: 443
      validate_certs: true
      security_protocol: 'ssl-with-validation-custom-ca'
      certificate_authority: |
        -----BEGIN CERTIFICATE-----
        FAKECERTsdKgAwI...
        -----END CERTIFICATE-----
    manageiq_connection:
      url: 'https://127.0.0.1:80'
      username: 'admin'
      password: 'password'
      validate_certs: true


- name: Update an existing provider named 'EngLab' (defaults to 'Prometheus' metrics)
  community.general.manageiq_provider:
    name: 'EngLab'
    type: 'Openshift'
    state: 'present'
    provider:
      auth_key: 'topSecret'
      hostname: 'next.example.com'
      port: 8443
      validate_certs: true
      security_protocol: 'ssl-with-validation-custom-ca'
      certificate_authority: |
        -----BEGIN CERTIFICATE-----
        FAKECERTsdKgAwI...
        -----END CERTIFICATE-----
    metrics:
      auth_key: 'topSecret'
      hostname: 'next.example.com'
      port: 443
      validate_certs: true
      security_protocol: 'ssl-with-validation-custom-ca'
      certificate_authority: |
        -----BEGIN CERTIFICATE-----
        FAKECERTsdKgAwI...
        -----END CERTIFICATE-----
    manageiq_connection:
      url: 'https://127.0.0.1'
      username: 'admin'
      password: 'password'
      validate_certs: true


- name: Delete a provider in ManageIQ
  community.general.manageiq_provider:
    name: 'EngLab'
    type: 'Openshift'
    state: 'absent'
    manageiq_connection:
      url: 'https://127.0.0.1'
      username: 'admin'
      password: 'password'
      validate_certs: true


- name: Create a new Amazon provider in ManageIQ using token authentication
  community.general.manageiq_provider:
    name: 'EngAmazon'
    type: 'Amazon'
    state: 'present'
    provider:
      hostname: 'amazon.example.com'
      userid: 'hello'
      password: 'world'
    manageiq_connection:
      url: 'https://127.0.0.1'
      token: 'VeryLongToken'
      validate_certs: true


- name: Create a new oVirt provider in ManageIQ
  community.general.manageiq_provider:
    name: 'RHEV'
    type: 'oVirt'
    state: 'present'
    provider:
      hostname: 'rhev01.example.com'
      userid: 'admin@internal'
      password: 'password'
      validate_certs: true
      certificate_authority: |
        -----BEGIN CERTIFICATE-----
        FAKECERTsdKgAwI...
        -----END CERTIFICATE-----
    metrics:
      hostname: 'metrics.example.com'
      path: 'ovirt_engine_history'
      userid: 'user_id_metrics'
      password: 'password_metrics'
      validate_certs: true
      certificate_authority: |
        -----BEGIN CERTIFICATE-----
        FAKECERTsdKgAwI...
        -----END CERTIFICATE-----
    manageiq_connection:
      url: 'https://127.0.0.1'
      username: 'admin'
      password: 'password'
      validate_certs: true

- name: Create a new VMware provider in ManageIQ
  community.general.manageiq_provider:
    name: 'EngVMware'
    type: 'VMware'
    state: 'present'
    provider:
      hostname: 'vcenter.example.com'
      host_default_vnc_port_start: 5800
      host_default_vnc_port_end: 5801
      userid: 'root'
      password: 'password'
    manageiq_connection:
      url: 'https://127.0.0.1'
      token: 'VeryLongToken'
      validate_certs: true

- name: Create a new Azure provider in ManageIQ
  community.general.manageiq_provider:
    name: 'EngAzure'
    type: 'Azure'
    provider_region: 'northeurope'
    subscription: 'e272bd74-f661-484f-b223-88dd128a4049'
    azure_tenant_id: 'e272bd74-f661-484f-b223-88dd128a4048'
    state: 'present'
    provider:
      hostname: 'azure.example.com'
      userid: 'e272bd74-f661-484f-b223-88dd128a4049'
      password: 'password'
    manageiq_connection:
      url: 'https://cf-6af0.rhpds.opentlc.com'
      username: 'admin'
      password: 'password'
      validate_certs: true

- name: Create a new OpenStack Director provider in ManageIQ with rsa keypair
  community.general.manageiq_provider:
    name: 'EngDirector'
    type: 'Director'
    api_version: 'v3'
    state: 'present'
    provider:
      hostname: 'director.example.com'
      userid: 'admin'
      password: 'password'
      security_protocol: 'ssl-with-validation'
      validate_certs: 'true'
      certificate_authority: |
        -----BEGIN CERTIFICATE-----
        FAKECERTsdKgAwI...
        -----END CERTIFICATE-----
    ssh_keypair:
      hostname: director.example.com
      userid: heat-admin
      auth_key: 'SecretSSHPrivateKey'

- name: Create a new OpenStack provider in ManageIQ with amqp metrics
  community.general.manageiq_provider:
    name: 'EngOpenStack'
    type: 'OpenStack'
    api_version: 'v3'
    state: 'present'
    provider_region: 'europe'
    tenant_mapping_enabled: 'False'
    keystone_v3_domain_id: 'mydomain'
    provider:
      hostname: 'openstack.example.com'
      userid: 'admin'
      password: 'password'
      security_protocol: 'ssl-with-validation'
      validate_certs: 'true'
      certificate_authority: |
        -----BEGIN CERTIFICATE-----
        FAKECERTsdKgAwI...
        -----END CERTIFICATE-----
    metrics:
      role: amqp
      hostname: 'amqp.example.com'
      security_protocol: 'non-ssl'
      port: 5666
      userid: admin
      password: password


- name: Create a new GCE provider in ManageIQ
  community.general.manageiq_provider:
    name: 'EngGoogle'
    type: 'GCE'
    provider_region: 'europe-west1'
    project: 'project1'
    state: 'present'
    provider:
      hostname: 'gce.example.com'
      auth_key: 'google_json_key'
      validate_certs: 'false'

)AnsibleModule)ManageIQmanageiq_argument_specc                      t        t        ddddd      t        d      t        d	dd
      t        d      t        d      t        dd      t        d      t        d            S )Nz0ManageIQ::Providers::Openshift::ContainerManagerbearerdefault
prometheusprometheus_alerts)
class_nameauthtypedefault_rolemetrics_rolealerts_rolez)ManageIQ::Providers::Amazon::CloudManager)r   z)ManageIQ::Providers::Redhat::InfraManagermetrics)r   r   r   z)ManageIQ::Providers::Vmware::InfraManagerz(ManageIQ::Providers::Azure::CloudManagerz,ManageIQ::Providers::Openstack::InfraManagerssh_keypair)r   ssh_keypair_rolez,ManageIQ::Providers::Openstack::CloudManagerz)ManageIQ::Providers::Google::CloudManager)	OpenshiftAmazonoVirtVMwareAzureDirector	OpenStackGCEdict     w/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/manageiq_provider.pysupported_providersr$     s    I"%+
 B
 B""

 B
 A
 E*
 E
 B
;   r"   c            
          t        t        dt                     t        dt                     t        dt                     t        dt               d            S )Nr    )typeoptionsF)r&   r'   no_log)providerr   alertsr   )r    endpoint_argument_specr!   r"   r#   endpoint_list_specr,   )  sI    6+A+CD&*@*BC)?)ABf.D.FuU	 r"   c                  (   t        t               t        d      t        d      t        dddg      t               t        g d	      t               t        d
      t        d
      t        d
      t               t               t                     S )NTrequiredint)r&   bool
verify_ssl)r   r&   aliases)zssl-with-validationzssl-with-validation-custom-cazssl-without-validationznon-sslchoices)r(   )rolehostnameportvalidate_certscertificate_authoritysecurity_protocoluseridpasswordauth_keysubscriptionprojectuid_emspathr   r!   r"   r#   r+   r+   2  sr    Vt$uDv~N"f
 vT"T"&V) r"   c                     t        | t              r| D cg c]  }t        |       c}S t        | t              r0| j	                         D ci c]  \  }}|	|t        |       c}}S | S c c}w c c}}w )zP Remove null entries from a hash

    Returns:
        a hash without nulls
    )
isinstancelistdelete_nullsr    items)hikvs       r#   rF   rF   K  sb     !T)*+AQ++!T/0wwyJtq!AM<?"JJH	 ,Js   A,
A1A1c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)ManageIQProviderzK
        Object to execute provider management operations in manageiq.
    c                     || _         | j                   j                  | _        | j                   j                  | _        | j                   j                  | _        y )N)manageiqmoduleapi_urlclient)selfrO   s     r#   __init__zManageIQProvider.__init__^  s<     mm**}},,mm**r"   c                     t               j                         D cg c]  \  }}|d   |k(  s| }}}t        |      dk(  r|d   S yc c}}w )zK Convert class_name to type

        Returns:
            the type
        r      r   N)r$   rG   len)rS   r   rJ   rK   outs        r#   class_name_to_typez#ManageIQProvider.class_name_to_typee  sP     1288:\TQaoQ[>[q\\s8q=q6M	 ]s
   AAc                     | j                   j                  d|      }|s| j                  j                  d|z         |d   S )z Search for zone id by zone name.

        Returns:
            the zone id, or send a module Fail signal if zone not found.
        zonesnamez"zone %s does not exist in manageiqmsgid)rO   find_collection_resource_byrP   	fail_json)rS   r]   zones      r#   zone_idzManageIQProvider.zone_idq  sL     }}88t8LKK!!8DA " C Dzr"   c                 <    | j                   j                  d|      S )zx Search for provider object by name.

        Returns:
            the provider, or None if provider not found.
        	providersr\   )rO   ra   )rS   r]   s     r#   r)   zManageIQProvider.provider~  s     }}8848PPr"   c                    g }t               j                         }t               j                  |i       }|j                  d      }|j                  d      }|D ]  }|j                  |      }|s|j                  d      xs |j                  |dz   d      }	|	dk(  r|j                  d      xs |	}
n|	}
|j	                  |	|j                  d      |j                  d      d	d
g|j                  dd         |j                  d      |j                  d      |j                  d      d|
|j                  d      |j                  d      |j                  d      xs |dd        |S )z Build "connection_configurations" objects from
        requested endpoints provided by user

        Returns:
            the user requested provider endpoints list
        r)   r>   r6   _roler   r   r7   r8   r   rV   r9   Tr;   r:   rB   )r6   r7   r8   r2   r;   r:   rB   r<   r=   )r   r<   r=   r>   )endpointauthentication)r,   keysr$   getappend)rS   provider_type	endpointsconnection_configurationsendpoint_keysprovider_defaultsri   default_auth_keyendpoint_keyr6   r   s              r#   build_connection_configurationsz0ManageIQProvider.build_connection_configurations  sk    %'!*,113/155mRH ==,#<<
3 * 	L }}\2H||F+g/@/D/D\T[E[]f/g9$044Z@HDH#H *00 $$,LL$< (V 4'(!fX\\:JD-Q&R-5\\:M-N19>U1V (V 4! %-"*,,x"8$,LL$<$,LL$<$P@P	'2 	: )(r"   c           	         	 | j                   d|d   }| j                  j                  |d      }t        d
d         S # t        $ r8}| j                  j                  d|d   dt        |             Y d	}~Ld	}~ww xY w)z| Deletes a provider from manageiq.

        Returns:
            a short message describing the operation executed.
        /providers/r`   deleteactionzfailed to delete provider r]   : r^   NTmessagechangedr_   rQ   rR   post	ExceptionrP   rb   strr    )rS   r)   urlresultes        r#   delete_providerz ManageIQProvider.delete_provider  s    	g'+||Xd^DC[[%%c(%;F DfY&788  	gKK!!HU[L\^abc^d&e!ff	gs   1A 	B.A??Bc                 b   | j                   d|d   }t        |d|i|||||	|
|||      }t        |      }	 | j                  j	                  |d|      }t        dd|d   d      S # t
        $ r8}| j                  j                  d|d   dt        |      	       Y d
}~Rd
}~ww xY w)zy Edit a provider from manageiq.

        Returns:
            a short message describing the operation executed.
        rw   r`   )r]   rc   provider_regionrp   host_default_vnc_port_starthost_default_vnc_port_endr?   r@   rA   tenant_mapping_enabledapi_versionedit)rz   resourcezfailed to update provider r]   r{   r^   NTz"successfully updated the provider r}   )	rQ   r    rF   rR   r   r   rP   rb   r   )rS   r)   r]   rn   ro   rd   r   r   r   r?   r@   rA   r   r   r   r   r   r   s                     r#   edit_providerzManageIQProvider.edit_provider  s     $(<<$@+&/(C&?%#9#
*  )	g[[%%c&8%LF >Fv>NPVWY 	Y  	gKK!!HU[L\^abc^d&e!ff	gs   A- -	B.6.B))B.c                 p   t        |d|i|||||	|
|||      }t        |      }	 d| j                  z  } | j                  j                  |fdt               |   d   i|}t        d
d|dd         S # t        $ r5}| j                  j                  d|dt        |             Y d	}~Od	}~ww xY w)z| Creates the provider in manageiq.

        Returns:
            a short message describing the operation executed.
        r`   )r]   rc   r   r   r   r?   r@   rA   r   r   rp   z%s/providersr&   r   zfailed to create provider r{   r^   NTz"successfully created the provider resultsr}   )
r    rF   rQ   rR   r   r$   r   rP   rb   r   )rS   r]   rn   ro   rd   r   r   r   r?   r@   rA   r   r   r   r   r   r   s                    r#   create_providerz ManageIQProvider.create_provider  s     +(C&?%#9#&/
  )	[ DLL1C%T[[%%co0C0Em0TUa0bofnoF >BF9DUVX 	X  	[KK!!DRUVWRX&Y!ZZ	[s   <A7 7	B5 +B00B5c           	         	 | j                   d|d   }| j                  j                  |d      }t        d	d
|z        S # t        $ r5}| j                  j                  d|dt        |             Y d}~Id}~ww xY w)zt Trigger provider refresh.

        Returns:
            a short message describing the operation executed.
        rw   r`   refreshry   zfailed to refresh provider r{   r^   NTzrefreshing provider %sr}   r   )rS   r)   r]   r   r   r   s         r#   r   zManageIQProvider.refresh  s    	\'+||Xd^DC[[%%c)%<F (4/1 	1  	\KK!!TSVWXSY&Z![[	\s   1A 	B+A<<BN)__name__
__module____qualname____doc__rT   rY   rd   r)   ru   r   r   r   r   r!   r"   r#   rM   rM   Y  s:    +
Q-)^9)YV"XH1r"   rM   c                  `   d } g }t        t        g dd      t        d      t        d      t               t               t               t               t               t        dg	      t        d
d      t        ddg      t        t        t               j                                           }|j	                  t                      |j	                  t                      t        |dddgfdddgfgddgg      }|j                  d   }|j                  d   }|j                  d   }|j                  }|j                  d   }|j                  d   }	|j                  d   }
|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }t        |      }t        |      }|j                  |      }|d k(  r$|r|j                  |      }nt        d
d!|z  "      }|dk(  r|r|j                  |      } |r|s|j                  |d         }|s|j                  j!                  d#$       |t               j                         vr|j                  j!                  d%|z  $       |j#                  d      r|j%                  ||      }|r|j'                  ||||| ||	|
|||||      }n|j)                  |||| ||	|
|||||      }|dk(  r%|r|j+                  ||      }nt        d
d!|z  "      } |j,                  d&i  y )'N)absentpresentr   r   )r5   r   Tr.   r   )r   keystone_v3_domain_id)r3   Fr1   )r   r&   v2v3r4   )stater]   rc   r   r   r   r?   r@   azure_tenant_idr   r   r&   r   r)   r   r]   r   r   )argument_specrequired_ifrequired_togetherrc   r&   r   r?   r   r@   r   r   r   z'provider %s: does not exist in manageiqr}   z(missing required argument: provider_typer^   z!provider_type %s is not supportedr!   )r    rE   r$   rk   updater	   r,   r   paramsr   rM   r)   r   rd   rY   rP   rb   rl   ru   r   r   r   	exit_json)rd   ro   r   rP   r]   	zone_namern   raw_endpointsr   r   r   r?   rA   r@   r   r   r   rO   manageiq_providerr)   res_argss                        r#   mainr   #  sg   GI;YO4 )$$(F"&&V&=%>?#E?$.$2499;<=M /12+-.#i*.i&*, +,GH
F == Df%IMM&)MMMMmm$56O"(--0M"N &.I J==0Lmm-.GmmI&G#]]+CD--.KMM'"EH(2 ))$/H (88BH =FHH
 	'//	:G M-@@&AQRM $$..> / @  3 5 : : <<$$..7=I / K
 Z()II-YfgI (66x}V_ahjy7RTm7CWgWmoz|H
 )88}iY`bq9TVo9EwPWYoq|~H
 	(004@H=FHH F x r"   __main__N)
__future__r   r   r   r&   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Cansible_collections.community.general.plugins.module_utils.manageiqr   r	   r$   r,   r+   rF   objectrM   r   r   r!   r"   r#   <module>r      ss    A @UnYv
 5 p!H2G1v G1Tk!\ zF r"   