|
| 1 | +# (c) 2018 Red Hat Inc. |
| 2 | +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 | + |
| 4 | +from __future__ import (absolute_import, division, print_function) |
| 5 | + |
| 6 | +__metaclass__ = type |
| 7 | + |
| 8 | +DOCUMENTATION = """ |
| 9 | +--- |
| 10 | +author: Ansible Networking Team |
| 11 | +httpapi : checkpoint |
| 12 | +short_description: HttpApi Plugin for Checkpoint devices |
| 13 | +description: |
| 14 | + - This HttpApi plugin provides methods to connect to Checkpoint |
| 15 | + devices over a HTTP(S)-based api. |
| 16 | +version_added: "2.8" |
| 17 | +options: |
| 18 | + domain: |
| 19 | + type: str |
| 20 | + description: |
| 21 | + - Specifies the domain of the Check Point device |
| 22 | + vars: |
| 23 | + - name: ansible_checkpoint_domain |
| 24 | +""" |
| 25 | + |
| 26 | +import json |
| 27 | + |
| 28 | +from ansible.module_utils.basic import to_text |
| 29 | +from ansible.errors import AnsibleConnectionFailure |
| 30 | +from ansible.module_utils.six.moves.urllib.error import HTTPError |
| 31 | +from ansible.plugins.httpapi import HttpApiBase |
| 32 | +from ansible.module_utils.connection import ConnectionError |
| 33 | + |
| 34 | +BASE_HEADERS = { |
| 35 | + 'Content-Type': 'application/json', |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +class HttpApi(HttpApiBase): |
| 40 | + def login(self, username, password): |
| 41 | + if username and password: |
| 42 | + cp_domain = self.get_option('domain') |
| 43 | + if cp_domain: |
| 44 | + payload = {'user': username, 'password': password, 'domain': cp_domain} |
| 45 | + else: |
| 46 | + payload = {'user': username, 'password': password} |
| 47 | + url = '/web_api/login' |
| 48 | + response, response_data = self.send_request(url, payload) |
| 49 | + else: |
| 50 | + raise AnsibleConnectionFailure('Username and password are required for login') |
| 51 | + |
| 52 | + try: |
| 53 | + self.connection._auth = {'X-chkp-sid': response_data['sid']} |
| 54 | + self.connection._session_uid = response_data['uid'] |
| 55 | + except KeyError: |
| 56 | + raise ConnectionError( |
| 57 | + 'Server returned response without token info during connection authentication: %s' % response) |
| 58 | + |
| 59 | + def logout(self): |
| 60 | + url = '/web_api/logout' |
| 61 | + |
| 62 | + response, dummy = self.send_request(url, None) |
| 63 | + |
| 64 | + def get_session_uid(self): |
| 65 | + return self.connection._session_uid |
| 66 | + |
| 67 | + def send_request(self, path, body_params): |
| 68 | + data = json.dumps(body_params) if body_params else '{}' |
| 69 | + |
| 70 | + try: |
| 71 | + self._display_request() |
| 72 | + response, response_data = self.connection.send(path, data, method='POST', headers=BASE_HEADERS) |
| 73 | + value = self._get_response_value(response_data) |
| 74 | + |
| 75 | + return response.getcode(), self._response_to_json(value) |
| 76 | + except AnsibleConnectionFailure as e: |
| 77 | + return 404, e.message |
| 78 | + except HTTPError as e: |
| 79 | + error = json.loads(e.read()) |
| 80 | + return e.code, error |
| 81 | + |
| 82 | + def _display_request(self): |
| 83 | + self.connection.queue_message('vvvv', 'Web Services: %s %s' % ('POST', self.connection._url)) |
| 84 | + |
| 85 | + def _get_response_value(self, response_data): |
| 86 | + return to_text(response_data.getvalue()) |
| 87 | + |
| 88 | + def _response_to_json(self, response_text): |
| 89 | + try: |
| 90 | + return json.loads(response_text) if response_text else {} |
| 91 | + # JSONDecodeError only available on Python 3.5+ |
| 92 | + except ValueError: |
| 93 | + raise ConnectionError('Invalid JSON response: %s' % response_text) |
0 commit comments