
    Vh                           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 d dlmZ dZd	Z G d
 de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionaZ  
module: "ipbase_info"
version_added: "7.0.0"
short_description: "Retrieve IP geolocation and other facts of a host's IP address using the ipbase.com API"
description:
  - Retrieve IP geolocation and other facts of a host's IP address using the ipbase.com API.
author: "Dominik Kukacka (@dominikkukacka)"
extends_documentation_fragment:
  - "community.general.attributes"
  - "community.general.attributes.info_module"
options:
  ip:
    description:
      - The IP you want to get the info for. If not specified the API will detect the IP automatically.
    required: false
    type: str
  apikey:
    description:
      - The API key for the request if you need more requests.
    required: false
    type: str
  hostname:
    description:
      - If the O(hostname) parameter is set to V(true), the API response will contain the hostname of the IP.
    required: false
    type: bool
    default: false
  language:
    description:
      - An ISO Alpha 2 Language Code for localizing the IP data.
    required: false
    type: str
    default: "en"
notes:
  - Check U(https://ipbase.com/) for more information.
a  
- name: "Get IP geolocation information of the primary outgoing IP"
  community.general.ipbase_info:
  register: my_ip_info

- name: "Get IP geolocation information of a specific IP"
  community.general.ipbase_info:
    ip: "8.8.8.8"
  register: my_ip_info


- name: "Get IP geolocation information of a specific IP with all other possible parameters"
  community.general.ipbase_info:
    ip: "8.8.8.8"
    apikey: "xxxxxxxxxxxxxxxxxxxxxx"
    hostname: true
    language: "de"
  register: my_ip_info
a#  
data:
  description: "JSON parsed response from ipbase.com. Please refer to U(https://ipbase.com/docs/info) for the detailed structure
    of the response."
  returned: success
  type: dict
  sample: {
    "ip": "1.1.1.1",
    "hostname": "one.one.one.one",
    "type": "v4",
    "range_type": {
      "type": "PUBLIC",
      "description": "Public address"
    },
    "connection": {
      "asn": 13335,
      "organization": "Cloudflare, Inc.",
      "isp": "APNIC Research and Development",
      "range": "1.1.1.1/32"
    },
    "location": {
      "geonames_id": 5332870,
      "latitude": 34.053611755371094,
      "longitude": -118.24549865722656,
      "zip": "90012",
      "continent": {
        "code": "NA",
        "name": "North America",
        "name_translated": "North America"
      },
      "country": {
        "alpha2": "US",
        "alpha3": "USA",
        "calling_codes": [
          "+1"
        ],
        "currencies": [
          {
            "symbol": "$",
            "name": "US Dollar",
            "symbol_native": "$",
            "decimal_digits": 2,
            "rounding": 0,
            "code": "USD",
            "name_plural": "US dollars"
          }
        ],
        "emoji": "...",
        "ioc": "USA",
        "languages": [
          {
            "name": "English",
            "name_native": "English"
          }
        ],
        "name": "United States",
        "name_translated": "United States",
        "timezones": [
          "America/New_York",
          "America/Detroit",
          "America/Kentucky/Louisville",
          "America/Kentucky/Monticello",
          "America/Indiana/Indianapolis",
          "America/Indiana/Vincennes",
          "America/Indiana/Winamac",
          "America/Indiana/Marengo",
          "America/Indiana/Petersburg",
          "America/Indiana/Vevay",
          "America/Chicago",
          "America/Indiana/Tell_City",
          "America/Indiana/Knox",
          "America/Menominee",
          "America/North_Dakota/Center",
          "America/North_Dakota/New_Salem",
          "America/North_Dakota/Beulah",
          "America/Denver",
          "America/Boise",
          "America/Phoenix",
          "America/Los_Angeles",
          "America/Anchorage",
          "America/Juneau",
          "America/Sitka",
          "America/Metlakatla",
          "America/Yakutat",
          "America/Nome",
          "America/Adak",
          "Pacific/Honolulu"
        ],
        "is_in_european_union": false,
        "fips": "US",
        "geonames_id": 6252001,
        "hasc_id": "US",
        "wikidata_id": "Q30"
      },
      "city": {
        "fips": "644000",
        "alpha2": null,
        "geonames_id": 5368753,
        "hasc_id": null,
        "wikidata_id": "Q65",
        "name": "Los Angeles",
        "name_translated": "Los Angeles"
      },
      "region": {
        "fips": "US06",
        "alpha2": "US-CA",
        "geonames_id": 5332921,
        "hasc_id": "US.CA",
        "wikidata_id": "Q99",
        "name": "California",
        "name_translated": "California"
      }
    },
    "tlds": [
      ".us"
    ],
    "timezone": {
      "id": "America/Los_Angeles",
      "current_time": "2023-05-04T04:30:28-07:00",
      "code": "PDT",
      "is_daylight_saving": true,
      "gmt_offset": -25200
    },
    "security": {
      "is_anonymous": false,
      "is_datacenter": false,
      "is_vpn": false,
      "is_bot": false,
      "is_abuser": true,
      "is_known_attacker": true,
      "is_proxy": false,
      "is_spam": false,
      "is_tor": false,
      "is_icloud_relay": false,
      "threat_score": 100
    },
    "domains": {
      "count": 10943,
      "domains": [
        "eliwise.academy",
        "accountingprose.academy",
        "pistola.academy",
        "1and1-test-ntlds-fr.accountant",
        "omnergy.africa"
      ]
    }
  }
)AnsibleModule)	fetch_url)	urlencodez+ansible-community.general.ipbase_info/0.1.0zhttps://api.ipbase.com/v2/infoc                       e Zd Zd Zd Zd Zy)
IpbaseInfoc                     || _         y )N)module)selfr   s     q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/ipbase_info.py__init__zIpbaseInfo.__init__   s	        c           	         t        | j                  |dddt        d      \  }}|d   dk7  r/| j                  j                  dj	                  |d         	       y 	 |j                         }| j                  j                  |j                  d
            }|S # t        $ r/ | j                  j                  dj	                  |      	       Y y w xY w)NT
   zapplication/json)Acceptz
User-Agent)forcetimeoutheadersstatus   z?The API request to ipbase.com returned an error status code {0})msgutf8z0Failed to parse the ipbase.com response: {0} {1})	r   r   
USER_AGENT	fail_jsonformatread	from_jsondecode
ValueError)r   urlresponseinfocontentresults         r   _get_url_datazIpbaseInfo._get_url_data   s    "KK,($ >S KK!!&g&n&nost|o}&~!"--/..w~~f/EF   4%%$fS'2 & 44s   :B 5CCc                 Z   | j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }t        }i }|r||d<   |r||d<   |rd|d<   |r||d<   |r|dt        |      z   z  }| j	                  |      S )Nipapikeyhostnamelanguage   ?)r   paramsBASE_URLr   r'   )r   r)   r*   r+   r,   r"   r/   s          r   r$   zIpbaseInfo.info  s    [[%##H-;;%%j1;;%%j1F4L%F8!"F:!)F:36***C!!#&&r   N)__name__
__module____qualname__r   r'   r$    r   r   r
   r
      s    0'r   r
   c                      t        t        ddd      t        ddd      t        dddd      t        dddd            } t        | d	      }t        |      } |j                  d
i |j	                          y )NstrF)typerequiredno_logTbool)r7   r8   r9   defaulten)r)   r*   r+   r,   )argument_specsupports_check_moder4   )dictr   r
   	exit_jsonr$   )module_argsr   ipbases      r   mainrC     sw    UU59t<6E%O55M	K ! F
 FF%v{{}%r   __main__N)
__future__r   r   r   r7   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   ansible.module_utils.urlsr   +ansible.module_utils.six.moves.urllib.parser   r   r0   objectr
   rC   r1   r4   r   r   <module>rN      sh    A @#J(S
j 5 / A ;
+6' 6'r&" zF r   