Skip to content

Commit 35859a5

Browse files
author
Adityashankar Kini
committed
Merge branch 'teams-api' of https://github.com/draios/python-sdc-client into teams-api
2 parents e6fa0bf + 2cd4e42 commit 35859a5

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

examples/dashboard_save_load.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
#
3+
# Save the first user dashboard to file and then use create_dashboard_from_file()
4+
# to apply the stored dasboard again with a different filter.
5+
#
6+
import os
7+
import sys
8+
import json
9+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
10+
from sdcclient import SdcClient
11+
12+
#
13+
# Parse arguments
14+
#
15+
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'
18+
sys.exit(1)
19+
20+
sdc_token = sys.argv[1]
21+
22+
#
23+
# Instantiate the SDC client
24+
#
25+
sdclient = SdcClient(sdc_token)
26+
27+
#
28+
# Serialize the first user dashboard to disk
29+
#
30+
res = sdclient.get_dashboards()
31+
32+
if not res[0]:
33+
print res[1]
34+
sys.exit(1)
35+
36+
if len(res[1][u'dashboards']) > 0:
37+
with open('dashboard.json', 'w') as outf:
38+
json.dump(res[1][u'dashboards'][0], outf)
39+
else:
40+
print 'the user has no dashboards. Exiting.'
41+
sys.exit(0)
42+
43+
#
44+
# Now create the dashboard from the file. We use a filter for the Cassandra process
45+
# as an example.
46+
#
47+
dashboardFilter = "proc.name = cassandra"
48+
49+
res = sdclient.create_dashboard_from_file('test dasboard from file', 'dashboard.json', dashboardFilter)
50+
51+
if res[0]:
52+
print 'Dashboard created successfully'
53+
else:
54+
print res[1]
55+
sys.exit(1)

sdcclient/_client.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ def filter_fn(panel):
603603
else:
604604
return [False, 'Not found']
605605

606-
def create_dashboard_from_template(self, newdashname, template, scope):
606+
def create_dashboard_from_template(self, newdashname, template, scope=[], shared=False):
607607
if scope is None:
608608
scope = []
609609

@@ -635,7 +635,7 @@ def create_dashboard_from_template(self, newdashname, template, scope):
635635
template['version'] = None
636636
template['schema'] = 1
637637
template['name'] = newdashname
638-
template['isShared'] = False # make sure the dashboard is not shared
638+
template['isShared'] = shared # make sure the dashboard is not shared
639639

640640
#
641641
# Assign the filter and the group ID to each view to point to this service
@@ -701,7 +701,7 @@ def create_dashboard_from_template(self, newdashname, template, scope):
701701
else:
702702
return [True, res.json()]
703703

704-
def create_dashboard_from_view(self, newdashname, viewname, filter):
704+
def create_dashboard_from_view(self, newdashname, viewname, filter, shared=False):
705705
#
706706
# Find our template view
707707
#
@@ -717,9 +717,9 @@ def create_dashboard_from_view(self, newdashname, viewname, filter):
717717
#
718718
# Create the new dashboard
719719
#
720-
return self.create_dashboard_from_template(newdashname, view, filter)
720+
return self.create_dashboard_from_template(newdashname, view, filter, shared)
721721

722-
def create_dashboard_from_dashboard(self, newdashname, templatename, filter):
722+
def create_dashboard_from_dashboard(self, newdashname, templatename, filter, shared=False):
723723
#
724724
# Get the list of dashboards from the server
725725
#
@@ -746,9 +746,9 @@ def create_dashboard_from_dashboard(self, newdashname, templatename, filter):
746746
#
747747
# Create the dashboard
748748
#
749-
return self.create_dashboard_from_template(newdashname, dboard, filter)
749+
return self.create_dashboard_from_template(newdashname, dboard, filter, shared)
750750

751-
def create_dashboard_from_file(self, newdashname, filename, scope):
751+
def create_dashboard_from_file(self, newdashname, filename, filter, shared=False):
752752
#
753753
# Load the Dashboard
754754
#
@@ -761,7 +761,7 @@ def create_dashboard_from_file(self, newdashname, filename, scope):
761761
#
762762
# Create the new dashboard
763763
#
764-
self.create_dashboard_from_template(newdashname, dboard, scope)
764+
return self.create_dashboard_from_template(newdashname, dboard, filter, shared)
765765

766766
def delete_dashboard(self, dashboard):
767767
if 'id' not in dashboard:

0 commit comments

Comments
 (0)