Skip to content

Commit 6bfd743

Browse files
omerazrDavide Schiera
authored andcommitted
Clean up the rest of the examples (#81)
1 parent 0ef0f54 commit 6bfd743

17 files changed

+207
-205
lines changed

examples/add_notification_email.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# Parse arguments
1313
#
1414
if len(sys.argv) != 3:
15-
print 'usage: %s <sysdig-token> email' % sys.argv[0]
16-
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
15+
print('usage: %s <sysdig-token> email' % sys.argv[0])
16+
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
1717
sys.exit(1)
1818

1919
sdc_token = sys.argv[1]
@@ -27,13 +27,13 @@
2727
#
2828
# Post the event
2929
#
30-
res = sdclient.add_email_notification_recipient(email)
30+
ok, res = sdclient.add_email_notification_recipient(email)
3131

3232
#
3333
# Return the result
3434
#
35-
if res[0]:
36-
print 'Recipient added successfully'
35+
if ok:
36+
print('Recipient added successfully')
3737
else:
38-
print res[1]
38+
print(res)
3939
sys.exit(1)

examples/add_users_to_secure.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
# Parse arguments
2424
#
2525
if len(sys.argv) != 2:
26-
print 'usage: %s <sysdig-token>' % sys.argv[0]
27-
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
26+
print('usage: %s <sysdig-token>' % sys.argv[0])
27+
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
2828
sys.exit(1)
2929

3030
sdc_token = sys.argv[1]
@@ -44,36 +44,36 @@
4444
#
4545
sdclient = SdcClient(sdc_token, sdc_url='https://app.sysdigcloud.com')
4646

47-
res = sdclient.list_memberships(SECURE_TEAM_NAME)
47+
ok, res = sdclient.list_memberships(SECURE_TEAM_NAME)
4848

49-
if res[0] == False:
50-
print 'Unable to get memberships for ' + SECURE_TEAM_NAME + ' team: ', res[1]
49+
if not ok:
50+
print('Unable to get memberships for ' + SECURE_TEAM_NAME + ' team: ', res)
5151
sys.exit(1)
52-
memberships = res[1]
52+
memberships = res
5353

54-
res = sdclient.get_users()
54+
ok, res = sdclient.get_users()
5555

56-
if res[0] == False:
57-
print 'Unable to get users: ', res[1]
56+
if not ok:
57+
print('Unable to get users: ', res)
5858
sys.exit(1)
59-
all_users = res[1]
59+
all_users = res
6060

6161
#
6262
# The memberships passed into edit_team() are based on username
6363
# rather than ID, so convert the IDs.
6464
#
6565
for user in all_users:
6666
if user['username'] in memberships:
67-
print 'Will preserve existing membership for: ' + user['username']
67+
print('Will preserve existing membership for: ' + user['username'])
6868
else:
69-
print 'Will add new member: ' + user['username']
69+
print('Will add new member: ' + user['username'])
7070
memberships[user['username']] = SECURE_TEAM_ROLE
7171

72-
res = sdclient.save_memberships(SECURE_TEAM_NAME, memberships=memberships)
73-
if res[0] == False:
74-
print 'Could not edit team:', res[1], '. Exiting.'
72+
ok, res = sdclient.save_memberships(SECURE_TEAM_NAME, memberships=memberships)
73+
if not ok:
74+
print('Could not edit team:', res, '. Exiting.')
7575
sys.exit(1)
7676
else:
77-
print 'Finished syncing memberships of "' + SECURE_TEAM_NAME + '" team'
77+
print('Finished syncing memberships of "' + SECURE_TEAM_NAME + '" team')
7878

7979
sys.exit(0)

examples/dashboard_save_load.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Save the first user dashboard to file and then use create_dashboard_from_file()
44
# to apply the stored dasboard again with a different filter.
5-
#
5+
#
66
import os
77
import sys
88
import json
@@ -13,8 +13,8 @@
1313
# Parse arguments
1414
#
1515
if len(sys.argv) != 2:
16-
print 'usage: %s <sysdig-token>' % sys.argv[0]
17-
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
16+
print('usage: %s <sysdig-token>' % sys.argv[0])
17+
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
1818
sys.exit(1)
1919

2020
sdc_token = sys.argv[1]
@@ -27,17 +27,17 @@
2727
#
2828
# Serialize the first user dashboard to disk
2929
#
30-
res = sdclient.get_dashboards()
30+
ok, res = sdclient.get_dashboards()
3131

32-
if not res[0]:
33-
print res[1]
32+
if not ok:
33+
print(res)
3434
sys.exit(1)
3535

36-
if len(res[1][u'dashboards']) > 0:
36+
if len(res[u'dashboards']) > 0:
3737
with open('dashboard.json', 'w') as outf:
38-
json.dump(res[1][u'dashboards'][0], outf)
38+
json.dump(res[u'dashboards'][0], outf)
3939
else:
40-
print 'the user has no dashboards. Exiting.'
40+
print('the user has no dashboards. Exiting.')
4141
sys.exit(0)
4242

4343
#
@@ -46,10 +46,10 @@
4646
#
4747
dashboardFilter = "proc.name = cassandra"
4848

49-
res = sdclient.create_dashboard_from_file('test dasboard from file', 'dashboard.json', dashboardFilter)
49+
ok, res = sdclient.create_dashboard_from_file('test dasboard from file', 'dashboard.json', dashboardFilter)
5050

51-
if res[0]:
52-
print 'Dashboard created successfully'
51+
if ok:
52+
print('Dashboard created successfully')
5353
else:
54-
print res[1]
54+
print(res)
5555
sys.exit(1)

examples/download_dashboards.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@
1010
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
1111
from sdcclient import SdcClient
1212

13+
1314
def zipdir(path, ziph):
1415
# ziph is zipfile handle
1516
for root, dirs, files in os.walk(path):
1617
for file in files:
1718
ziph.write(os.path.join(root, file))
1819

20+
1921
def cleanup_dir(path):
2022
if os.path.exists(path) == False:
2123
return
2224
if os.path.isdir(path) == False:
23-
print 'Provided path is not a directory'
25+
print('Provided path is not a directory')
2426
sys.exit(-1)
2527

2628
for file in os.listdir(path):
@@ -29,17 +31,18 @@ def cleanup_dir(path):
2931
if os.path.isfile(file_path):
3032
os.unlink(file_path)
3133
else:
32-
print 'Cannot clean the provided directory due to delete failure on %s' % file_path
34+
print('Cannot clean the provided directory due to delete failure on %s' % file_path)
3335
except Exception as e:
3436
print(e)
3537
os.rmdir(path)
3638

39+
3740
#
3841
# Parse arguments
3942
#
4043
if len(sys.argv) != 3:
41-
print 'usage: %s <sysdig-token> <file-name>' % sys.argv[0]
42-
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
44+
print('usage: %s <sysdig-token> <file-name>' % sys.argv[0])
45+
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
4346
sys.exit(1)
4447

4548
sdc_token = sys.argv[1]
@@ -54,15 +57,13 @@ def cleanup_dir(path):
5457
#
5558
# Fire the request.
5659
#
57-
res = sdclient.get_dashboards()
60+
ok, res = sdclient.get_dashboards()
5861

5962
#
6063
# Show the list of dashboards
6164
#
62-
if res[0]:
63-
data = res[1]
64-
else:
65-
print res[1]
65+
if not ok:
66+
print(res)
6667
sys.exit(1)
6768

6869

@@ -74,11 +75,11 @@ def cleanup_dir(path):
7475
os.makedirs(sysdig_dashboard_dir)
7576

7677

77-
for db in data['dashboards']:
78+
for db in res['dashboards']:
7879
file_path = os.path.join(sysdig_dashboard_dir, str(db['id']))
7980
f = open(file_path, 'w')
8081
f.write(json.dumps(db))
81-
print "Name: %s, # Charts: %d" % (db['name'], len(db['items']))
82+
print("Name: %s, # Charts: %d" % (db['name'], len(db['items'])))
8283
f.close()
8384

8485
zipf = zipfile.ZipFile(dashboard_state_file, 'w', zipfile.ZIP_DEFLATED)

examples/flip_alerts_enabled.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@
88
import getopt
99
import os
1010
import sys
11-
import json
1211
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
1312
from sdcclient import SdcClient
1413

14+
1515
#
1616
# Parse arguments
1717
#
1818
def usage():
19-
print ('usage: %s [-a|--alert <name>] <sysdig-token>' % sys.argv[0])
20-
print ('-a|--alert: Comma seperated list of alerts')
21-
print ('You can find your token at https://app.sysdigcloud.com/#/settings/user')
19+
print('usage: %s [-a|--alert <name>] <sysdig-token>' % sys.argv[0])
20+
print('-a|--alert: Comma seperated list of alerts')
21+
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
2222
sys.exit(1)
2323

24+
2425
try:
25-
opts, args = getopt.getopt(sys.argv[1:],"a:",["alert="])
26+
opts, args = getopt.getopt(sys.argv[1:], "a:", ["alert="])
2627
except getopt.GetoptError:
2728
usage()
2829

@@ -41,32 +42,32 @@ def usage():
4142
#
4243
sdclient = SdcClient(sdc_token)
4344

44-
res = sdclient.get_alerts()
45-
if not res[0]:
46-
print (res[1])
45+
ok, res = sdclient.get_alerts()
46+
if not ok:
47+
print(res)
4748
sys.exit(1)
4849

4950
alert_found = False
50-
for alert in res[1]['alerts']:
51+
for alert in res['alerts']:
5152
if alert['name'] in alert_list:
5253
alert_found = True
53-
print ("Updating \'" + alert['name'] + "\'. Enabled status before change:")
54-
print (alert['enabled'])
54+
print("Updating \'" + alert['name'] + "\'. Enabled status before change:")
55+
print(alert['enabled'])
5556
if alert['enabled'] == True:
5657
alert['enabled'] = False
5758
else:
5859
alert['enabled'] = True
59-
res_update = sdclient.update_alert(alert)
60+
ok, res_update = sdclient.update_alert(alert)
6061

61-
if not res_update[0]:
62-
print (res_update[1])
62+
if not ok:
63+
print(res_update)
6364
sys.exit(1)
6465

6566
# Validate and print the results
66-
print ('Alert status after modification:')
67-
print (alert['enabled'])
68-
print (' ')
67+
print('Alert status after modification:')
68+
print(alert['enabled'])
69+
print(' ')
6970

7071
if not alert_found:
71-
print ('Alert to be updated not found')
72-
sys.exit(1)
72+
print('Alert to be updated not found')
73+
sys.exit(1)

examples/get_agents_config.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
# Parse arguments
1616
#
1717
if len(sys.argv) != 2:
18-
print 'usage: %s <sysdig-token>' % sys.argv[0]
19-
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
18+
print('usage: %s <sysdig-token>' % sys.argv[0])
19+
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
2020
sys.exit(1)
2121

2222
sdc_token = sys.argv[1]
@@ -29,18 +29,18 @@
2929
#
3030
# Get the configuration
3131
#
32-
res = sdclient.get_agents_config()
32+
ok, res = sdclient.get_agents_config()
3333

3434
#
3535
# Return the result
3636
#
37-
if res[0]:
38-
if not("files" in res[1]) or len(res[1]["files"]) == 0:
39-
print "No current auto configuration"
37+
if ok:
38+
if not("files" in res) or len(res["files"]) == 0:
39+
print("No current auto configuration")
4040
else:
41-
print "Current contents of config file:"
42-
print "--------------------------------"
43-
print res[1]["files"][0]["content"]
44-
print "--------------------------------"
41+
print("Current contents of config file:")
42+
print("--------------------------------")
43+
print(res["files"][0]["content"])
44+
print("--------------------------------")
4545
else:
46-
print res[1]
46+
print(res)

examples/get_secure_default_falco_rules_files.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,22 @@
1212
import sys
1313
import pprint
1414
import getopt
15-
import shutil
1615
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
1716
from sdcclient import SdSecureClient
1817

18+
1919
#
2020
# Parse arguments
2121
#
2222
def usage():
23-
print 'usage: %s [-s|--save <path>] <sysdig-token>' % sys.argv[0]
24-
print '-s|--save: save the retrieved files to a set of files below <path> using save_default_rules_files().'
25-
print 'You can find your token at https://secure.sysdig.com/#/settings/user'
23+
print('usage: %s [-s|--save <path>] <sysdig-token>' % sys.argv[0])
24+
print('-s|--save: save the retrieved files to a set of files below <path> using save_default_rules_files().')
25+
print('You can find your token at https://secure.sysdig.com/#/settings/user')
2626
sys.exit(1)
2727

28+
2829
try:
29-
opts, args = getopt.getopt(sys.argv[1:],"s:",["save="])
30+
opts, args = getopt.getopt(sys.argv[1:], "s:", ["save="])
3031
except getopt.GetoptError:
3132
usage()
3233

@@ -51,20 +52,20 @@ def usage():
5152
#
5253
# Get the configuration
5354
#
54-
res = sdclient.get_default_falco_rules_files()
55+
ok, res = sdclient.get_default_falco_rules_files()
5556

5657
#
5758
# Return the result
5859
#
59-
if res[0]:
60+
if ok:
6061
if save_dir == "":
6162
pp = pprint.PrettyPrinter(indent=4)
62-
pp.pprint(res[1])
63+
pp.pprint(res)
6364
else:
64-
print "Saving falco rules files below {}...".format(save_dir)
65-
sres = sdclient.save_default_falco_rules_files(res[1], save_dir)
66-
if not sres[0]:
67-
print sres[1]
65+
print("Saving falco rules files below {}...".format(save_dir))
66+
ok, sres = sdclient.save_default_falco_rules_files(res, save_dir)
67+
if not ok:
68+
print(sres)
6869
else:
69-
print res[1]
70+
print(res)
7071
sys.exit(1)

0 commit comments

Comments
 (0)