Skip to content

Clean up the rest of the examples #81

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions examples/add_notification_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# Parse arguments
#
if len(sys.argv) != 3:
print 'usage: %s <sysdig-token> email' % sys.argv[0]
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
print('usage: %s <sysdig-token> email' % sys.argv[0])
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)

sdc_token = sys.argv[1]
Expand All @@ -27,13 +27,13 @@
#
# Post the event
#
res = sdclient.add_email_notification_recipient(email)
ok, res = sdclient.add_email_notification_recipient(email)

#
# Return the result
#
if res[0]:
print 'Recipient added successfully'
if ok:
print('Recipient added successfully')
else:
print res[1]
print(res)
sys.exit(1)
32 changes: 16 additions & 16 deletions examples/add_users_to_secure.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
# Parse arguments
#
if len(sys.argv) != 2:
print 'usage: %s <sysdig-token>' % sys.argv[0]
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
print('usage: %s <sysdig-token>' % sys.argv[0])
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)

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

res = sdclient.list_memberships(SECURE_TEAM_NAME)
ok, res = sdclient.list_memberships(SECURE_TEAM_NAME)

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

res = sdclient.get_users()
ok, res = sdclient.get_users()

if res[0] == False:
print 'Unable to get users: ', res[1]
if not ok:
print('Unable to get users: ', res)
sys.exit(1)
all_users = res[1]
all_users = res

#
# The memberships passed into edit_team() are based on username
# rather than ID, so convert the IDs.
#
for user in all_users:
if user['username'] in memberships:
print 'Will preserve existing membership for: ' + user['username']
print('Will preserve existing membership for: ' + user['username'])
else:
print 'Will add new member: ' + user['username']
print('Will add new member: ' + user['username'])
memberships[user['username']] = SECURE_TEAM_ROLE

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

sys.exit(0)
26 changes: 13 additions & 13 deletions examples/dashboard_save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Save the first user dashboard to file and then use create_dashboard_from_file()
# to apply the stored dasboard again with a different filter.
#
#
import os
import sys
import json
Expand All @@ -13,8 +13,8 @@
# Parse arguments
#
if len(sys.argv) != 2:
print 'usage: %s <sysdig-token>' % sys.argv[0]
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
print('usage: %s <sysdig-token>' % sys.argv[0])
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)

sdc_token = sys.argv[1]
Expand All @@ -27,17 +27,17 @@
#
# Serialize the first user dashboard to disk
#
res = sdclient.get_dashboards()
ok, res = sdclient.get_dashboards()

if not res[0]:
print res[1]
if not ok:
print(res)
sys.exit(1)

if len(res[1][u'dashboards']) > 0:
if len(res[u'dashboards']) > 0:
with open('dashboard.json', 'w') as outf:
json.dump(res[1][u'dashboards'][0], outf)
json.dump(res[u'dashboards'][0], outf)
else:
print 'the user has no dashboards. Exiting.'
print('the user has no dashboards. Exiting.')
sys.exit(0)

#
Expand All @@ -46,10 +46,10 @@
#
dashboardFilter = "proc.name = cassandra"

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

if res[0]:
print 'Dashboard created successfully'
if ok:
print('Dashboard created successfully')
else:
print res[1]
print(res)
sys.exit(1)
23 changes: 12 additions & 11 deletions examples/download_dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
from sdcclient import SdcClient


def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))


def cleanup_dir(path):
if os.path.exists(path) == False:
return
if os.path.isdir(path) == False:
print 'Provided path is not a directory'
print('Provided path is not a directory')
sys.exit(-1)

for file in os.listdir(path):
Expand All @@ -29,17 +31,18 @@ def cleanup_dir(path):
if os.path.isfile(file_path):
os.unlink(file_path)
else:
print 'Cannot clean the provided directory due to delete failure on %s' % file_path
print('Cannot clean the provided directory due to delete failure on %s' % file_path)
except Exception as e:
print(e)
os.rmdir(path)


#
# Parse arguments
#
if len(sys.argv) != 3:
print 'usage: %s <sysdig-token> <file-name>' % sys.argv[0]
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
print('usage: %s <sysdig-token> <file-name>' % sys.argv[0])
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)

sdc_token = sys.argv[1]
Expand All @@ -54,15 +57,13 @@ def cleanup_dir(path):
#
# Fire the request.
#
res = sdclient.get_dashboards()
ok, res = sdclient.get_dashboards()

#
# Show the list of dashboards
#
if res[0]:
data = res[1]
else:
print res[1]
if not ok:
print(res)
sys.exit(1)


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


for db in data['dashboards']:
for db in res['dashboards']:
file_path = os.path.join(sysdig_dashboard_dir, str(db['id']))
f = open(file_path, 'w')
f.write(json.dumps(db))
print "Name: %s, # Charts: %d" % (db['name'], len(db['items']))
print("Name: %s, # Charts: %d" % (db['name'], len(db['items'])))
f.close()

zipf = zipfile.ZipFile(dashboard_state_file, 'w', zipfile.ZIP_DEFLATED)
Expand Down
39 changes: 20 additions & 19 deletions examples/flip_alerts_enabled.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
import getopt
import os
import sys
import json
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
from sdcclient import SdcClient


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


try:
opts, args = getopt.getopt(sys.argv[1:],"a:",["alert="])
opts, args = getopt.getopt(sys.argv[1:], "a:", ["alert="])
except getopt.GetoptError:
usage()

Expand All @@ -41,32 +42,32 @@ def usage():
#
sdclient = SdcClient(sdc_token)

res = sdclient.get_alerts()
if not res[0]:
print (res[1])
ok, res = sdclient.get_alerts()
if not ok:
print(res)
sys.exit(1)

alert_found = False
for alert in res[1]['alerts']:
for alert in res['alerts']:
if alert['name'] in alert_list:
alert_found = True
print ("Updating \'" + alert['name'] + "\'. Enabled status before change:")
print (alert['enabled'])
print("Updating \'" + alert['name'] + "\'. Enabled status before change:")
print(alert['enabled'])
if alert['enabled'] == True:
alert['enabled'] = False
else:
alert['enabled'] = True
res_update = sdclient.update_alert(alert)
ok, res_update = sdclient.update_alert(alert)

if not res_update[0]:
print (res_update[1])
if not ok:
print(res_update)
sys.exit(1)

# Validate and print the results
print ('Alert status after modification:')
print (alert['enabled'])
print (' ')
print('Alert status after modification:')
print(alert['enabled'])
print(' ')

if not alert_found:
print ('Alert to be updated not found')
sys.exit(1)
print('Alert to be updated not found')
sys.exit(1)
22 changes: 11 additions & 11 deletions examples/get_agents_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
# Parse arguments
#
if len(sys.argv) != 2:
print 'usage: %s <sysdig-token>' % sys.argv[0]
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
print('usage: %s <sysdig-token>' % sys.argv[0])
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)

sdc_token = sys.argv[1]
Expand All @@ -29,18 +29,18 @@
#
# Get the configuration
#
res = sdclient.get_agents_config()
ok, res = sdclient.get_agents_config()

#
# Return the result
#
if res[0]:
if not("files" in res[1]) or len(res[1]["files"]) == 0:
print "No current auto configuration"
if ok:
if not("files" in res) or len(res["files"]) == 0:
print("No current auto configuration")
else:
print "Current contents of config file:"
print "--------------------------------"
print res[1]["files"][0]["content"]
print "--------------------------------"
print("Current contents of config file:")
print("--------------------------------")
print(res["files"][0]["content"])
print("--------------------------------")
else:
print res[1]
print(res)
27 changes: 14 additions & 13 deletions examples/get_secure_default_falco_rules_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@
import sys
import pprint
import getopt
import shutil
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
from sdcclient import SdSecureClient


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


try:
opts, args = getopt.getopt(sys.argv[1:],"s:",["save="])
opts, args = getopt.getopt(sys.argv[1:], "s:", ["save="])
except getopt.GetoptError:
usage()

Expand All @@ -51,20 +52,20 @@ def usage():
#
# Get the configuration
#
res = sdclient.get_default_falco_rules_files()
ok, res = sdclient.get_default_falco_rules_files()

#
# Return the result
#
if res[0]:
if ok:
if save_dir == "":
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(res[1])
pp.pprint(res)
else:
print "Saving falco rules files below {}...".format(save_dir)
sres = sdclient.save_default_falco_rules_files(res[1], save_dir)
if not sres[0]:
print sres[1]
print("Saving falco rules files below {}...".format(save_dir))
ok, sres = sdclient.save_default_falco_rules_files(res, save_dir)
if not ok:
print(sres)
else:
print res[1]
print(res)
sys.exit(1)
Loading