|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# -*- coding: utf-8 -*- |
| 4 | +# (c) Christian Meißner 2021 |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +from __future__ import absolute_import, division, print_function |
| 20 | +__metaclass__ = type |
| 21 | + |
| 22 | +DOCUMENTATION = ''' |
| 23 | +--- |
| 24 | +module: location |
| 25 | +version_added: 1.4.0 |
| 26 | +short_description: Manage locations |
| 27 | +description: |
| 28 | + - create, update and delete locations |
| 29 | +author: |
| 30 | + - "Christian Meißner (@cmeissner)" |
| 31 | +options: |
| 32 | + name: |
| 33 | + description: Name of the given location |
| 34 | + type: str |
| 35 | + required: true |
| 36 | + description: |
| 37 | + description: Description of the given location |
| 38 | + type: str |
| 39 | + required: false |
| 40 | + address: |
| 41 | + description: |
| 42 | + - Address of the given location |
| 43 | + - if I(resolv_location) is set to True, this address will be used to resolve the latitude and longitude |
| 44 | + - Mutually exclusive with latitude and longitude |
| 45 | + type: str |
| 46 | + required: false |
| 47 | + latitude: |
| 48 | + description: |
| 49 | + - Latitude of the given location |
| 50 | + - if I(resolv_location) is set to True, this latitude will be used to resolve the address |
| 51 | + - This parameter is mutually exclusive with address |
| 52 | + - This parameter is required if I(longitude) is given |
| 53 | + type: float |
| 54 | + required: false |
| 55 | + longitude: |
| 56 | + description: |
| 57 | + - Longitude of the given location |
| 58 | + - if I(resolv_location) is set to True, this longitude will be used to resolve the address |
| 59 | + - This parameter is mutually exclusive with address |
| 60 | + - This parameter is required if I(latitude) is given |
| 61 | + type: float |
| 62 | + required: false |
| 63 | + resolv_location: |
| 64 | + description: |
| 65 | + - Resolve the given location |
| 66 | + - Requires network connectivity to https://nominatim.org/ to resolve the given address |
| 67 | + - If address can not be resolved, latitude and longitude will not be set |
| 68 | + type: bool |
| 69 | + required: false |
| 70 | + default: no |
| 71 | +requirements: |
| 72 | + - geopy |
| 73 | +extends_documentation_fragment: |
| 74 | + - codeaffen.phpipam.phpipam |
| 75 | + - codeaffen.phpipam.phpipam.entity_state |
| 76 | +''' |
| 77 | + |
| 78 | +EXAMPLES = ''' |
| 79 | +- name: "Create with address" |
| 80 | + codeaffen.phpipam.location: |
| 81 | + username: "admin" |
| 82 | + password: "s3cr3t" |
| 83 | + server_url: "https://ipam.example.com" |
| 84 | + name: "my location" |
| 85 | + description: "my location description" |
| 86 | + address: "my location address" |
| 87 | + state: present |
| 88 | +
|
| 89 | +- name: "Create location with geo coordinates" |
| 90 | + codeaffen.phpipam.location: |
| 91 | + username: "admin" |
| 92 | + password: "s3cr3t" |
| 93 | + server_url: "https://ipam.example.com" |
| 94 | + name: "my location" |
| 95 | + description: "my location description" |
| 96 | + latitude: 123.456 |
| 97 | + longitude: 123.456 |
| 98 | + state: present |
| 99 | +
|
| 100 | +- name: "Remove location" |
| 101 | + codeaffen.phpipam.location: |
| 102 | + username: "admin" |
| 103 | + password: "s3cr3t" |
| 104 | + server_url: "https://ipam.example.com" |
| 105 | + name: "my location" |
| 106 | + state: absent |
| 107 | +''' |
| 108 | + |
| 109 | +import traceback |
| 110 | +from ansible_collections.codeaffen.phpipam.plugins.module_utils.phpipam_helper import PhpipamEntityAnsibleModule |
| 111 | + |
| 112 | +try: |
| 113 | + from geopy.geocoders import Nominatim |
| 114 | + from geopy.point import Point |
| 115 | + HAS_GEOPY = True |
| 116 | +except ImportError: |
| 117 | + HAS_GEOPY = False |
| 118 | + GEOPY_IMP_ERR = traceback.format_exc() |
| 119 | + |
| 120 | + |
| 121 | +class PhpipamToolsLocationsModule(PhpipamEntityAnsibleModule): |
| 122 | + pass |
| 123 | + |
| 124 | + |
| 125 | +def main(): |
| 126 | + module = PhpipamToolsLocationsModule( |
| 127 | + phpipam_spec=dict( |
| 128 | + id=dict(type='int', invisible=True, phpipam_name='id'), |
| 129 | + name=dict(type='str', required=True), |
| 130 | + description=dict(type='str', required=False), |
| 131 | + address=dict(type='str', required=False), |
| 132 | + latitude=dict(type='float', required=False, phpipam_name='lat'), |
| 133 | + longitude=dict(type='float', required=False, phpipam_name='long'), |
| 134 | + resolv_location=dict(type='bool', required=False, default=False, api_invisible=True), |
| 135 | + ), |
| 136 | + mutually_exclusive=[['address', 'latitude'], ['address', 'longitude']], |
| 137 | + required_together=[['latitude', 'longitude']], |
| 138 | + ) |
| 139 | + |
| 140 | + module_params = module.phpipam_params |
| 141 | + |
| 142 | + if module_params['resolv_location']: |
| 143 | + if not HAS_GEOPY: |
| 144 | + module.fail_json(msg='geopy is required for resolv_location', exception=GEOPY_IMP_ERR) |
| 145 | + |
| 146 | + geolocator = Nominatim(user_agent='ansible-phpipam-module') |
| 147 | + if 'address' in module_params and module_params['resolv_location']: |
| 148 | + location = geolocator.geocode(module_params['address']) |
| 149 | + if location: |
| 150 | + module_params['latitude'] = str(location.latitude) |
| 151 | + module_params['longitude'] = str(location.longitude) |
| 152 | + elif 'latitude' in module_params and 'longitude' in module_params: |
| 153 | + location = geolocator.reverse(Point(float(module_params['latitude']), float(module_params['longitude']))) |
| 154 | + if location: |
| 155 | + module_params['address'] = location.address |
| 156 | + module_params['latitude'] = str(location.latitude) |
| 157 | + module_params['longitude'] = str(location.longitude) |
| 158 | + |
| 159 | + with module.api_connection(): |
| 160 | + module.run() |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + main() |
0 commit comments