|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Script to copy (non-API) target with settings/integrations |
| 4 | +
|
| 5 | +This example is for python 3.6 |
| 6 | +""" |
| 7 | + |
| 8 | +import requests |
| 9 | + |
| 10 | +API_BASE_URL = "https://api.probely.com" |
| 11 | +API_TOKEN = "" |
| 12 | +TARGET_ID = "" |
| 13 | + |
| 14 | +if not API_TOKEN: |
| 15 | + print('Missing API_TOKEN') |
| 16 | + exit(1) |
| 17 | + |
| 18 | +if not TARGET_ID: |
| 19 | + print('Missing TARGET_ID') |
| 20 | + exit(1) |
| 21 | + |
| 22 | +session = requests.Session() |
| 23 | +session.headers.update( |
| 24 | + { |
| 25 | + "Authorization": f"JWT {API_TOKEN}", |
| 26 | + } |
| 27 | +) |
| 28 | + |
| 29 | +response = session.get(f"{API_BASE_URL}/targets/{TARGET_ID}") |
| 30 | +assert response.status_code == 200 |
| 31 | +data = response.json() |
| 32 | +for field in ('id', 'connected_target', 'highs', 'mediums', 'lows'): |
| 33 | + data.pop(field) |
| 34 | + |
| 35 | +data['site']['name'] = data['site']['name'] + ' (Copy)' |
| 36 | +response = session.post(f"{API_BASE_URL}/targets/?skip_reachability_check=true", json=data) |
| 37 | +assert response.status_code == 201, response.json() |
| 38 | +copied_target_id = response.json()['id'] |
| 39 | + |
| 40 | +response = session.get(f"{API_BASE_URL}/integrations/") |
| 41 | +assert response.status_code == 200 |
| 42 | + |
| 43 | +installed_integrations = [ |
| 44 | + integration for integration, installed |
| 45 | + in response.json()["installed"].items() if installed] |
| 46 | + |
| 47 | +for integration in installed_integrations: |
| 48 | + response = session.get( |
| 49 | + f"{API_BASE_URL}/targets/{TARGET_ID}/" |
| 50 | + f"integrations/{integration.replace('_', '-')}/") |
| 51 | + assert response.status_code == 200 |
| 52 | + |
| 53 | + data = response.json() |
| 54 | + |
| 55 | + response = session.patch( |
| 56 | + f"{API_BASE_URL}/targets/{copied_target_id}/" |
| 57 | + f"integrations/{integration.replace('_', '-')}/", |
| 58 | + json=data) |
| 59 | + assert response.status_code == 200 |
0 commit comments