B
    `"                 @   sn   d dl mZmZmZ eZdZdZd dlm	Z	m
ZmZ yd dlmZ W n ek
rX   Y nX G dd deZ
dS )	    )absolute_importdivisionprint_functiona_  
    name: openshift
    plugin_type: inventory
    author:
      - Chris Houseknecht <@chouseknecht>

    short_description: OpenShift inventory source

    description:
      - Fetch containers, services and routes for one or more clusters
      - Groups by cluster name, namespace, namespace_services, namespace_pods, namespace_routes, and labels
      - Uses openshift.(yml|yaml) YAML configuration file to set parameter values.

    options:
      plugin:
        description: token that ensures this is a source file for the 'openshift' plugin.
        required: True
        choices: ['openshift']
      connections:
          description:
          - Optional list of cluster connection settings. If no connections are provided, the default
            I(~/.kube/config) and active context will be used, and objects will be returned for all namespaces
            the active user is authorized to access.
          suboptions:
              name:
                  description:
                  - Optional name to assign to the cluster. If not provided, a name is constructed from the server
                    and port.
              kubeconfig:
                  description:
                  - Path to an existing Kubernetes config file. If not provided, and no other connection
                    options are provided, the OpenShift client will attempt to load the default
                    configuration file from I(~/.kube/config.json). Can also be specified via K8S_AUTH_KUBECONFIG
                    environment variable.
              context:
                  description:
                  - The name of a context found in the config file. Can also be specified via K8S_AUTH_CONTEXT environment
                    variable.
              host:
                  description:
                  - Provide a URL for accessing the API. Can also be specified via K8S_AUTH_HOST environment variable.
              api_key:
                  description:
                  - Token used to authenticate with the API. Can also be specified via K8S_AUTH_API_KEY environment
                    variable.
              username:
                  description:
                  - Provide a username for authenticating with the API. Can also be specified via K8S_AUTH_USERNAME
                    environment variable.
              password:
                  description:
                  - Provide a password for authenticating with the API. Can also be specified via K8S_AUTH_PASSWORD
                    environment variable.
              client_cert:
                  description:
                  - Path to a certificate used to authenticate with the API. Can also be specified via K8S_AUTH_CERT_FILE
                    environment variable.
                  aliases: [ cert_file ]
              client_key:
                  description:
                  - Path to a key file used to authenticate with the API. Can also be specified via K8S_AUTH_KEY_FILE
                    environment variable.
                  aliases: [ key_file ]
              ca_cert:
                  description:
                  - Path to a CA certificate used to authenticate with the API. Can also be specified via
                    K8S_AUTH_SSL_CA_CERT environment variable.
                  aliases: [ ssl_ca_cert ]
              validate_certs:
                  description:
                  - "Whether or not to verify the API server's SSL certificates. Can also be specified via
                    K8S_AUTH_VERIFY_SSL environment variable."
                  type: bool
                  aliases: [ verify_ssl ]
              namespaces:
                  description:
                  - List of namespaces. If not specified, will fetch all containers for all namespaces user is authorized
                    to access.

    requirements:
    - "python >= 2.7"
    - "openshift >= 0.6"
    - "PyYAML >= 3.11"
as  
# File must be named openshift.yaml or openshift.yml

# Authenticate with token, and return all pods and services for all namespaces
plugin: community.okd.openshift
connections:
  - host: https://192.168.64.4:8443
    api_key: xxxxxxxxxxxxxxxx
    verify_ssl: false

# Use default config (~/.kube/config) file and active context, and return objects for a specific namespace
plugin: community.okd.openshift
connections:
  - namespaces:
    - testing

# Use a custom config file, and a specific context.
plugin: community.okd.openshift
connections:
  - kubeconfig: /path/to/config
    context: 'awx/192-168-64-4:8443/developer'
)K8sInventoryExceptionInventoryModuleformat_dynamic_api_exc)DynamicApiErrorc                   s0   e Zd ZdZdZdZ fddZdd Z  ZS )r   zcommunity.okd.openshiftzcommunity.okd.ococc                s   t t| | |rt|ts&tdx|D ]`}| jf |}|d| |j	j
}|drf|d }n
| |}x|D ]}| ||| qvW q,W n<|  }| |j	j
}| |}x|D ]}| ||| qW d S )Nz#Expecting connections to be a list.name
namespaces)superr   fetch_objects
isinstancelistr   Zget_api_clientgetZget_default_host_nameconfigurationhostZget_available_namespacesget_routes_for_namespace)selfconnections
connectionclientr
   r   	namespace)	__class__ l/home/dcms/DCMS/lib/python3.7/site-packages/ansible_collections/community/okd/plugins/inventory/openshift.pyr      s"    







zInventoryModule.fetch_objectsc          
   C   sL  |j jddd}y|j|d}W n> tk
r^ } z | j| tdt| W d d }~X Y nX d|}d|}| j	| | j	| | j
|| | j	| | j
|| x|jD ]}	|	jj}
|	jjsi n
t|	jj}| j|
 |	jjrFx<|	jjD ]0\}}d||}| j	| | j
||
 qW t|	jj}ni }| j
||
 | j|
d	| | j|
d
| | j|
d|	jj | j|
dd | j|
d|	jj | j|
d|	jj | j|
d|	jj |	jjr| j|
d|	jj |	jjr| j|
d|	jj t|	jjdr|	jjjr| j|
dt|	jj qW d S )Nzroute.openshift.io/v1ZRoute)api_versionkind)r   zError fetching Routes list: %sznamespace_{0}z
{0}_routeszlabel_{0}_{1}labelsannotationsZcluster_nameZobject_typerouteZ	self_linkZresource_versionuidr   path
targetPortport)	resourcesr   r   Zdisplaydebugr   r   formatZ	inventory	add_groupZ	add_childitemsmetadatar
   r   dictZadd_hostr   Zset_variableZclusterNameZselfLinkZresourceVersionr!   specr   r"   hasattrr$   r#   )r   r   r
   r   Zv1_routeobjexcZnamespace_groupZnamespace_routes_groupr    Z
route_nameZroute_annotationskeyvalueZ
group_nameZroute_labelsr   r   r   r      sL    "




z(InventoryModule.get_routes_for_namespace)	__name__
__module____qualname__NAMEZconnection_plugin	transportr   r   __classcell__r   r   )r   r   r   |   s
   r   N)
__future__r   r   r   type__metaclass__ZDOCUMENTATIONZEXAMPLESZ9ansible_collections.kubernetes.core.plugins.inventory.k8sr   r   ZK8sInventoryModuler   Zopenshift.dynamic.exceptionsr   ImportErrorr   r   r   r   <module>   s   U