|
13 | 13 | description:
|
14 | 14 | - This HttpApi plugin provides methods to connect to Checkpoint
|
15 | 15 | devices over a HTTP(S)-based api.
|
16 |
| -version_added: "1.0.0" |
| 16 | +version_added: "2.8.0" |
17 | 17 | options:
|
| 18 | + cptarget: |
| 19 | + type: str |
| 20 | + description: |
| 21 | + - target gateway |
| 22 | + vars: |
| 23 | + - name: ansible_checkpoint_target |
18 | 24 | domain:
|
19 | 25 | type: str
|
20 | 26 | description:
|
|
30 | 36 | from ansible.module_utils.six.moves.urllib.error import HTTPError
|
31 | 37 | from ansible.plugins.httpapi import HttpApiBase
|
32 | 38 | from ansible.module_utils.connection import ConnectionError
|
| 39 | +from ansible.parsing.dataloader import DataLoader |
| 40 | +from ansible.inventory.manager import InventoryManager |
33 | 41 |
|
34 | 42 | BASE_HEADERS = {
|
35 | 43 | 'Content-Type': 'application/json',
|
|
38 | 46 |
|
39 | 47 |
|
40 | 48 | class HttpApi(HttpApiBase):
|
| 49 | + def __init__(self, connection): |
| 50 | + super(HttpApi, self).__init__(connection) |
| 51 | + self.connection = connection |
| 52 | + self.mgmt_proxy_enabled = False |
| 53 | + |
| 54 | + loader = DataLoader() |
| 55 | + # Initialize InventoryManager |
| 56 | + inventory = InventoryManager(loader=loader, sources=['/etc/ansible/hosts']) |
| 57 | + # Get host |
| 58 | + host = inventory.get_host('mgmt_proxy') |
| 59 | + # Get variable |
| 60 | + try: |
| 61 | + proxy_enabled = host.vars['enabled'] |
| 62 | + if proxy_enabled == True: |
| 63 | + self.mgmt_proxy_enabled = True |
| 64 | + except Exception as e: |
| 65 | + pass |
| 66 | + |
| 67 | + |
41 | 68 | def login(self, username, password):
|
| 69 | + payload = {} |
| 70 | + url = '/gaia_api/login' |
42 | 71 | if username and password:
|
43 | 72 | payload = {'user': username, 'password': password}
|
44 |
| - url = '/gaia_api/login' |
45 |
| - response, response_data = self.send_request(url, payload) |
46 | 73 | else:
|
47 | 74 | raise AnsibleConnectionFailure('Username and password are required for login')
|
| 75 | + if self.mgmt_proxy_enabled == True: |
| 76 | + url = '/web_api/login' |
| 77 | + response, response_data = self.send_request(url, payload) |
48 | 78 |
|
49 | 79 | try:
|
50 | 80 | self.connection._auth = {'X-chkp-sid': response_data['sid']}
|
51 | 81 | except KeyError:
|
52 | 82 | raise ConnectionError(
|
53 |
| - 'Server returned response without token info during connection authentication: %s' % response) |
| 83 | + 'Server returned response without token info during connection authentication: %s' % response_data) |
54 | 84 |
|
55 | 85 | def logout(self):
|
56 | 86 | url = '/gaia_api/logout'
|
57 |
| - |
| 87 | + if self.mgmt_proxy_enabled == True: |
| 88 | + url = '/web_api/logout' |
58 | 89 | response, dummy = self.send_request(url, None)
|
59 | 90 |
|
60 | 91 | def get_session_uid(self):
|
61 | 92 | return self.connection._session_uid
|
62 | 93 |
|
63 | 94 | def send_request(self, path, body_params):
|
| 95 | + # we only replace gaia_ip/ with web_api/gaia-api/ if target is set and path contains for gaia_ip/ |
| 96 | + cp_api_target = self.get_option('cptarget') |
| 97 | + if 'gaia_api/' in path: # Avoid login/logut requests in case of web_api |
| 98 | + if self.mgmt_proxy_enabled == True: |
| 99 | + if cp_api_target != None: |
| 100 | + body_params['target'] = cp_api_target |
| 101 | + path = path.replace("gaia_api/", "web_api/gaia-api/") |
64 | 102 | data = json.dumps(body_params) if body_params else '{}'
|
65 | 103 |
|
66 | 104 | try:
|
|
0 commit comments