|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +JupiterOne Sync Job Workflow Example |
| 4 | +
|
| 5 | +This example demonstrates the core synchronization job workflow: |
| 6 | +1. Start a sync job |
| 7 | +2. Upload entities batch |
| 8 | +3. Finalize the sync job |
| 9 | +
|
| 10 | +This is a standalone script that can be run independently to test |
| 11 | +the sync job functionality. |
| 12 | +""" |
| 13 | + |
| 14 | +import os |
| 15 | +import sys |
| 16 | +from jupiterone.client import JupiterOneClient |
| 17 | + |
| 18 | +def main(): |
| 19 | + """Main function to demonstrate sync job workflow.""" |
| 20 | + |
| 21 | + # Initialize JupiterOne client |
| 22 | + # You can set these as environment variables or replace with your values |
| 23 | + api_token = os.getenv('JUPITERONE_API_TOKEN') |
| 24 | + account_id = os.getenv('JUPITERONE_ACCOUNT_ID') |
| 25 | + |
| 26 | + if not api_token or not account_id: |
| 27 | + print("Error: Please set JUPITERONE_API_TOKEN and JUPITERONE_ACCOUNT_ID environment variables") |
| 28 | + print("Example:") |
| 29 | + print(" export JUPITERONE_API_TOKEN='your-api-token'") |
| 30 | + print(" export JUPITERONE_ACCOUNT_ID='your-account-id'") |
| 31 | + sys.exit(1) |
| 32 | + |
| 33 | + # Create JupiterOne client |
| 34 | + j1 = JupiterOneClient(api_token=api_token, account_id=account_id) |
| 35 | + |
| 36 | + print("=== JupiterOne Sync Job Workflow Example ===\n") |
| 37 | + |
| 38 | + # You'll need to replace this with an actual integration instance ID |
| 39 | + instance_id = os.getenv('JUPITERONE_INSTANCE_ID') |
| 40 | + if not instance_id: |
| 41 | + print("Error: Please set JUPITERONE_INSTANCE_ID environment variable") |
| 42 | + print("Example:") |
| 43 | + print(" export JUPITERONE_INSTANCE_ID='your-integration-instance-id'") |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | + try: |
| 47 | + # Step 1: Start sync job |
| 48 | + print("1. Starting synchronization job...") |
| 49 | + sync_job = j1.start_sync_job( |
| 50 | + instance_id=instance_id, |
| 51 | + sync_mode="PATCH", |
| 52 | + source="integration-external" |
| 53 | + ) |
| 54 | + |
| 55 | + sync_job_id = sync_job['job'].get('id') |
| 56 | + print(f"✓ Started sync job: {sync_job_id}") |
| 57 | + print(f" Status: {sync_job['job']['status']}") |
| 58 | + print() |
| 59 | + |
| 60 | + # Step 2: Upload entities batch |
| 61 | + print("2. Uploading entities batch...") |
| 62 | + |
| 63 | + # Sample entities payload |
| 64 | + entities_payload = [ |
| 65 | + { |
| 66 | + "_key": "example-server-001", |
| 67 | + "_type": "example_server", |
| 68 | + "_class": "Host", |
| 69 | + "displayName": "Example Server 001", |
| 70 | + "hostname": "server-001.example.com", |
| 71 | + "ipAddress": "192.168.1.100", |
| 72 | + "operatingSystem": "Linux", |
| 73 | + "tag.Environment": "development", |
| 74 | + "tag.Team": "engineering", |
| 75 | + "tag.Purpose": "web_server" |
| 76 | + }, |
| 77 | + { |
| 78 | + "_key": "example-server-002", |
| 79 | + "_type": "example_server", |
| 80 | + "_class": "Host", |
| 81 | + "displayName": "Example Server 002", |
| 82 | + "hostname": "server-002.example.com", |
| 83 | + "ipAddress": "192.168.1.101", |
| 84 | + "operatingSystem": "Linux", |
| 85 | + "tag.Environment": "staging", |
| 86 | + "tag.Team": "engineering", |
| 87 | + "tag.Purpose": "database_server" |
| 88 | + }, |
| 89 | + { |
| 90 | + "_key": "example-database-001", |
| 91 | + "_type": "example_database", |
| 92 | + "_class": "Database", |
| 93 | + "displayName": "Example Database 001", |
| 94 | + "databaseName": "app_db", |
| 95 | + "engine": "postgresql", |
| 96 | + "version": "13.4", |
| 97 | + "tag.Environment": "development", |
| 98 | + "tag.Team": "data" |
| 99 | + } |
| 100 | + ] |
| 101 | + |
| 102 | + # Upload entities |
| 103 | + upload_result = j1.upload_entities_batch_json( |
| 104 | + instance_job_id=sync_job_id, |
| 105 | + entities_list=entities_payload |
| 106 | + ) |
| 107 | + print(f"✓ Uploaded {len(entities_payload)} entities successfully") |
| 108 | + print(f" Upload result: {upload_result}") |
| 109 | + print() |
| 110 | + |
| 111 | + # Step 3: Finalize sync job |
| 112 | + print("3. Finalizing synchronization job...") |
| 113 | + finalize_result = j1.finalize_sync_job(instance_job_id=sync_job_id) |
| 114 | + |
| 115 | + finalize_job_id = finalize_result['job'].get('id') |
| 116 | + print(f"✓ Finalized sync job: {finalize_job_id}") |
| 117 | + print(f" Status: {finalize_result['job']['status']}") |
| 118 | + |
| 119 | + # Check final status |
| 120 | + if finalize_result['job']['status'] == 'COMPLETED': |
| 121 | + print("✓ Sync job completed successfully!") |
| 122 | + elif finalize_result['job']['status'] == 'FAILED': |
| 123 | + error_msg = finalize_result['job'].get('error', 'Unknown error') |
| 124 | + print(f"✗ Sync job failed: {error_msg}") |
| 125 | + else: |
| 126 | + print(f"ℹ Sync job status: {finalize_result['job']['status']}") |
| 127 | + |
| 128 | + print("\n=== Sync Job Workflow Complete ===") |
| 129 | + |
| 130 | + except Exception as e: |
| 131 | + print(f"✗ Error during sync job workflow: {e}") |
| 132 | + sys.exit(1) |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + main() |
| 136 | + |
0 commit comments