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