1+ # -*- coding: utf-8 -*-
2+
3+ from suds .client import Client
4+ from suds .sax .text import Text
5+ from suds .transport .http import HttpAuthenticated
6+ import logging as log
7+ import sys
8+
9+ class SSRS ():
10+ '''
11+ Create a SOAP connection to a SSRS (Microsoft Reporting Services)
12+
13+ Example of usage on SSRS 2008:
14+ RS = SSRS(host='http://localhost/ReportinServices/ReportService2010.asmx?wsdl', user:'user@domain.com', key_password='myfreakingpassword')
15+
16+ '''
17+
18+ def __init__ (self , host = 'http://localhost/reportserver/ReportService2010.asmx?wsdl' , user = 'SEPTODONT\a dmin' , key_password = 'admin' , verbose = True ):
19+
20+ self .verbose = verbose
21+ credentials = dict (username = user , password = key_password )
22+ credentials = HttpAuthenticated (** credentials )
23+ log .basicConfig (filename = 'SSRS.log' , level = log .INFO )
24+
25+ try :
26+ self .client = Client (host , transport = credentials )
27+
28+ except BaseException as e :
29+
30+ msg = "Could not connect to %s with user %s: %s" % (host , user , str (e ))
31+ log .error (msg )
32+
33+ if self .verbose :
34+ print (msg )
35+
36+ exit ()
37+
38+
39+ def ListMethods (self ):
40+ '''
41+ Return the list of methods available for your SSRS version
42+ '''
43+ try :
44+ list_of_methods = [method for method in self .client .wsdl .services [0 ].ports [0 ].methods ]
45+ return list_of_methods
46+
47+ except BaseException as e :
48+
49+ msg = "ListMethod() Could not retrieve the methods: %s" % (str (e ))
50+ log .error (msg )
51+
52+ if self .verbose :
53+ print (msg )
54+
55+ return False
56+
57+
58+ def ListDirItems (self , dir = r'/' , recursive = False ):
59+ '''
60+ List all itens in a folder.
61+ if specified the <recursive> parameter, subfolders will be scanned too
62+ '''
63+
64+ try :
65+ it = self .client .service .ListChildren (dir , recursive )
66+ it = it .CatalogItem
67+
68+ except BaseException as e :
69+ msg = "ListDirItems() Could not retrieve the Objects: %s" % (str (e ))
70+ log .error (msg )
71+
72+ if self .verbose :
73+ print (msg )
74+
75+ return False
76+
77+ catalog_dict = {}
78+
79+ for item in it :
80+ catalog_dict [item ['ID' ]] = {
81+ 'Name' : item ['Name' ],
82+ 'Path' : item ['Path' ],
83+ 'TypeName' : item ['TypeName' ],
84+ 'CreationDate' : item ['CreationDate' ],
85+ 'ModifiedDate' : item ['ModifiedDate' ],
86+ 'CreatedBy' : item ['CreatedBy' ],
87+ 'ModifiedBy' : item ['ModifiedBy' ],
88+ 'ItemMetadata' : item ['ItemMetadata' ],
89+ }
90+
91+ return catalog_dict
92+
93+
94+ def Find (self , text , objtype = None ):
95+ '''
96+ Find objects on SSRS
97+ '''
98+
99+ try :
100+ it = self .ListDirItems (recursive = True )
101+ if isinstance (it , Text ):
102+ return dict (it )
103+
104+
105+ except BaseException as e :
106+ msg = "Find() Could not retrieve the Objects: %s" % (str (e ))
107+ log .error (msg )
108+
109+ if self .verbose :
110+ print (msg )
111+
112+ return False
113+
114+ catalog_dict = {}
115+ for key , value in it .items () :
116+ for word in value .values ():
117+ if text in str (word ):
118+ if objtype == None :
119+ catalog_dict [key ] = value
120+
121+ else :
122+ if it [key ]['TypeName' ] == objtype :
123+ catalog_dict [key ] = value
124+
125+ return catalog_dict
126+
127+
128+ def GetParameters (self , path ):
129+ '''
130+ Retrieve parameters from an SSRS Report
131+ '''
132+
133+ try :
134+ it = self .client .service .GetItemParameters (path , None , True , None , None )
135+ if isinstance (it , Text ):
136+ return dict (it )
137+
138+ except BaseException as e :
139+ msg = "GetParameters() Could not retrieve the parameters: %s" % str (e )
140+ log .error (msg )
141+
142+ if self .verbose :
143+ print (msg )
144+
145+ return False
146+
147+ param_dict = {}
148+ for item in it .ItemParameter :
149+ param_dict [item ['Name' ]] = {
150+ 'ParameterTypeName' : item ['ParameterTypeName' ],
151+ 'Nullable' : item ['Nullable' ],
152+ 'AllowBlank' : item ['AllowBlank' ],
153+ 'MultiValue' : item ['MultiValue' ],
154+ 'QueryParameter' : item ['QueryParameter' ],
155+ 'Prompt' : item ['Prompt' ],
156+ 'PromptUser' : item ['PromptUser' ],
157+ 'ValidValuesQueryBased' : item ['ValidValuesQueryBased' ],
158+ 'DefaultValuesQueryBased' : item ['DefaultValuesQueryBased' ],
159+ }
160+
161+ return param_dict
162+
163+
164+
165+
166+
0 commit comments