Skip to content

Commit fb7f375

Browse files
authored
Merge pull request #63 from JupiterOne/fix-example-scripts
fix example scripts, improve test suites, add type hints for method inputs, improve input validation
2 parents d14aa56 + b4d7f41 commit fb7f375

21 files changed

+478
-238
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ entity = j1.create_entity(
128128
entity_key='my-unique-key',
129129
entity_type='my_type',
130130
entity_class='MyClass',
131-
properties=properties,
132-
timestamp=int(time.time()) * 1000 # Optional, defaults to current datetime
131+
properties=properties
133132
)
134133
print(entity['entity'])
135134

examples/02_entity_management.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,10 @@ def create_entity_examples(j1):
7676
'backupRetentionPeriod': 7,
7777
'tag.Environment': 'production',
7878
'tag.Team': 'data',
79-
'metadata': {
80-
'createdBy': 'terraform',
81-
'lastBackup': '2024-01-01T00:00:00Z',
82-
'maintenanceWindow': 'sun:03:00-sun:04:00'
83-
}
84-
},
85-
timestamp=int(time.time()) * 1000
79+
'createdBy': 'terraform',
80+
'lastBackup': '2024-01-01T00:00:00Z',
81+
'maintenanceWindow': 'sun:03:00-sun:04:00'
82+
}
8683
)
8784
print(f"Created complex entity: {complex_entity['entity']['_id']}\n")
8885

@@ -122,16 +119,12 @@ def update_entity_examples(j1, entity_id):
122119
entity_id=entity_id,
123120
properties={
124121
'isActive': False,
125-
'maintenanceWindow': {
126-
'start': '2024-01-01T00:00:00Z',
127-
'end': '2024-01-01T04:00:00Z',
128-
'reason': 'scheduled_maintenance'
129-
},
130-
'metadata': {
131-
'maintenancePerformedBy': 'admin@company.com',
132-
'maintenanceType': 'security_patches',
133-
'estimatedDuration': '4 hours'
134-
}
122+
'maintenanceWindowStart': '2024-01-01T00:00:00Z',
123+
'maintenanceWindowEnd': '2024-01-01T04:00:00Z',
124+
'maintenanceReason': 'scheduled_maintenance',
125+
'maintenancePerformedBy': 'admin@company.com',
126+
'maintenanceType': 'security_patches',
127+
'estimatedDuration': '4 hours'
135128
}
136129
)
137130
print(f"Updated with complex properties\n")

examples/03_relationship_management.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,9 @@ def create_relationship_examples(j1, from_entity_id, to_entity_id):
100100
'version': '2.1.0',
101101
'installPath': '/usr/local/bin/software',
102102
'permissions': ['read', 'execute'],
103-
'metadata': {
104-
'installer': 'package_manager',
105-
'verified': True,
106-
'checksum': 'sha256:abc123...'
107-
},
103+
'installer': 'package_manager',
104+
'verified': True,
105+
'checksum': 'sha256:abc123...',
108106
'tag.InstallationType': 'automated',
109107
'tag.Verified': 'true'
110108
}
@@ -142,11 +140,9 @@ def update_relationship_examples(j1, relationship_id, from_entity_id, to_entity_
142140
'lastModified': int(time.time()) * 1000,
143141
'modifiedBy': 'security_team',
144142
'expiresOn': int(time.time() + 86400) * 1000, # 24 hours from now
145-
'auditLog': {
146-
'previousLevel': 'write',
147-
'reason': 'promotion_requested',
148-
'approvedBy': 'security_manager'
149-
}
143+
'previousLevel': 'write',
144+
'promotionReason': 'promotion_requested',
145+
'approvedBy': 'security_manager'
150146
}
151147
)
152148
print(f"Updated with complex properties\n")

examples/09_custom_file_transfer_example.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
import sys
2121

2222
# Add the parent directory to the path so we can import the jupiterone client
23-
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
23+
try:
24+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
25+
except NameError:
26+
# Handle case when __file__ is not available (e.g., when exec'd)
27+
sys.path.append('..')
2428

2529
from jupiterone.client import JupiterOneClient
2630

examples/J1QLdeferredResponse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# JupiterOne GraphQL API headers
1414
j1_graphql_headers = {
1515
'Content-Type': 'application/json',
16-
'Authorization': 'Bearer ' + token,
17-
'Jupiterone-Account': acct
16+
'Authorization': 'Bearer ' + (token or ''),
17+
'Jupiterone-Account': acct or ''
1818
}
1919

2020
gql_query = """

examples/bulk_upload.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
token = os.environ.get("JUPITERONE_TOKEN")
66
url = "https://graphql.us.jupiterone.io"
77

8+
# Check if credentials are available
9+
if not account or not token:
10+
print("Error: JUPITERONE_ACCOUNT and JUPITERONE_TOKEN environment variables must be set")
11+
print("This example script requires valid JupiterOne credentials to run")
12+
exit(1)
13+
814
j1 = JupiterOneClient(account=account, token=token, url=url)
915

10-
instance_id = "e7113c37-1ea8-4d00-9b82-c24952e70916"
16+
instance_id = "<Integration Instance ID>"
1117

1218
sync_job = j1.start_sync_job(
1319
instance_id=instance_id,

examples/examples.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
token = os.environ.get("JUPITERONE_TOKEN")
99
url = "https://graphql.us.jupiterone.io"
1010

11+
# Check if credentials are available
12+
if not account or not token:
13+
print("Error: JUPITERONE_ACCOUNT and JUPITERONE_TOKEN environment variables must be set")
14+
print("This example script requires valid JupiterOne credentials to run")
15+
exit(1)
16+
1117
j1 = JupiterOneClient(account=account, token=token, url=url)
1218

1319
# query_v1
@@ -31,8 +37,7 @@
3137
entity_key='jupiterone-api-client-python:{}'.format(num1),
3238
entity_type='python_client_create_entity',
3339
entity_class='Record',
34-
properties=properties,
35-
timestamp=int(time.time()) * 1000 # Optional, defaults to current datetime
40+
properties=properties
3641
)
3742
print("create_entity()")
3843
print(create_r)
@@ -63,8 +68,7 @@
6368
entity_key='jupiterone-api-client-python:{}'.format(num2),
6469
entity_type='python_client_create_entity',
6570
entity_class='Record',
66-
properties=properties,
67-
timestamp=int(time.time()) * 1000 # Optional, defaults to current datetime
71+
properties=properties
6872
)
6973
print("create_entity()")
7074
print(create_r_2)

jupiterone/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
from .client import JupiterOneClient
22
from .errors import (
33
JupiterOneClientError,
4-
JupiterOneApiError
4+
JupiterOneApiError,
5+
JupiterOneApiRetryError
56
)
7+
8+
__all__ = [
9+
"JupiterOneClient",
10+
"JupiterOneClientError",
11+
"JupiterOneApiError",
12+
"JupiterOneApiRetryError"
13+
]

0 commit comments

Comments
 (0)