1- import os
2- import inject
31import time
42
5- from cloudshell .configuration .cloudshell_snmp_configuration import SNMP_HANDLER
6- from cloudshell .shell .core .driver_context import AutoLoadDetails , AutoLoadResource , AutoLoadAttribute
7- from cloudshell .shell .core .context_utils import get_attribute_by_name
8- from cloudshell .api .cloudshell_api import CloudShellAPISession
9- from cloudshell .power .pdu .raritan .api .raritan_rpc_api import RaritanRpcApi
103from cloudshell .power .pdu .managed_devices .connected_to_pdu_resource import ConnectedToPduResource
11-
12- from raritan import rpc
13- from raritan .rpc import pdumodel
4+ from cloudshell . power . pdu . raritan . device . raritan_rpcapi_pdu_factory import RaritanRpcApiPduFactory
5+ from cloudshell . power . pdu . raritan . device . factory_context import FactoryContext
6+ from cloudshell . power . pdu . raritan .helper import get_outlets_by_address
147
158
169class RaritanHandler :
17- def __init__ (self ):
18- self ._snmp = None
19- self ._agent = None
20- self ._pdu = None
21- self ._user = None
22- self ._password = None
10+ def __init__ (self , pdu_factory = RaritanRpcApiPduFactory ):
11+ self .pdu = None
2312 self ._outlets = []
24-
25- @property
26- def snmp (self ):
27- if self ._snmp is None :
28- self ._snmp = SNMP_HANDLER ()
29- path = os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' , 'mibs' ))
30- self ._snmp .update_mib_sources (path )
31- return self ._snmp
32-
33- @property
34- def pdu (self ):
35- if self ._pdu is None :
36- self ._pdu = pdumodel .Pdu ('model/pdu/0' , self .agent )
37- return self ._pdu
13+ self ._pdu_factory = pdu_factory
3814
3915 @property
4016 def outlets (self ):
41- # uses raritan RPC api
4217 if not self ._outlets :
43- self ._outlets = self .pdu .getOutlets ()
18+ self ._outlets = self .pdu .get_outlets ()
4419 return self ._outlets
4520
4621 def initialize (self , context ):
47- user = get_attribute_by_name ('User' , context )
48- password = self ._get_decrypted_password (context )
49- rari_api = RaritanRpcApi (context .resource .address , user , password )
50- self ._pdu = rari_api .pdu_handler ()
22+ factory_context = FactoryContext (context )
23+ self .pdu = self ._pdu_factory (factory_context )
24+
25+ def get_inventory (self , context ):
26+ return self .pdu .get_inventory ()
5127
5228 def power_on (self , context , ports ):
5329 rr = ConnectedToPduResource (context .remote_endpoints )
54- from debug_utils import debugger
55- debugger .attach_debugger ()
56- result = True
57- for o in self ._get_outlets_by_address (ports ):
58- o .setPowerState (pdumodel .Outlet .PowerState .PS_ON )
59- result = o .getState ()
60- print result
30+ for o in get_outlets_by_address (ports ):
31+ o .power_on
6132 return rr .online ()
6233
6334 def power_off (self , context , ports ):
6435 rr = ConnectedToPduResource (context .remote_endpoints )
65- for o in self . _get_outlets_by_address (ports ):
66- o .setPowerState ( pdumodel . Outlet . PowerState . PS_OFF )
36+ for o in get_outlets_by_address (ports ):
37+ o .power_off
6738 return rr .offline ()
6839
6940 def power_cycle (self , context , ports , delay = 0 ):
@@ -74,45 +45,76 @@ def power_cycle(self, context, ports, delay=0):
7445 self .power_on (context , ports )
7546 return 'Power cycle complete'
7647
77- def get_inventory (self , context ):
78- metadata = self .pdu .getMetaData ()
79- resources = self ._autoload_resources_by_rpc ()
80- attributes = [AutoLoadAttribute ('' , 'Firmware Version' , metadata .fwRevision ),
81- AutoLoadAttribute ('' , 'Vendor' , metadata .nameplate .manufacturer ),
82- AutoLoadAttribute ('' , 'Model' , metadata .nameplate .model )]
83- result = AutoLoadDetails (resources , attributes )
84- return result
85-
86- def _autoload_resources_by_rpc (self ):
87- resources = [AutoLoadResource ('Generic Power Socket' ,
88- 'Socket ' + str (x + 1 ),
89- str (x + 1 ))
90- for x , val in enumerate (self .outlets )]
91- return resources
92-
93- def _autoload_resources_by_snmp (self ):
94- outlets = self ._get_outlets ()
95- resources = [AutoLoadResource ('Generic Socket' ,
96- 'Socket ' + outlets [outlet ]['outletLabel' ],
97- outlets [outlet ]['outletLabel' ])
98- for outlet in self .outlets ]
99- return resources
100-
101- def _get_outlets (self ):
102- return self .snmp .get_table ('PDU2-MIB' , 'outletConfigurationTable' )
103-
104- def _get_outlets_by_address (self , ports ):
105- # ports: ['192.168.30.128/4']
106-
107- def socket (x ):
108- n = int (x .split ('/' )[- 1 ])
109- return self .outlets [n - 1 ]
110-
111- return [socket (x ) for x in ports ]
112-
113- def _get_decrypted_password (self , context ):
114- password = get_attribute_by_name ('Password' , context )
115- api = inject .instance ('api' )
116- return api .DecryptPassword (password ).Value
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+ # SNMP legacy code
96+ #
97+ # import os
98+ # from cloudshell.configuration.cloudshell_snmp_configuration import SNMP_HANDLER
99+ #
100+ # @property
101+ # def snmp(self):
102+ # if self._snmp is None:
103+ # self._snmp = SNMP_HANDLER()
104+ # path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'mibs'))
105+ # self._snmp.update_mib_sources(path)
106+ # return self._snmp
107+ #
108+ # def _autoload_resources_by_snmp(self):
109+ # outlets = self._get_outlets()
110+ # resources = [AutoLoadResource('Generic Socket',
111+ # 'Socket ' + outlets[outlet]['outletLabel'],
112+ # outlets[outlet]['outletLabel'])
113+ # for outlet in self.outlets]
114+ # return resources
115+ #
116+ # def _get_outlets(self):
117+ # return self.snmp.get_table('PDU2-MIB', 'outletConfigurationTable')
118+
117119
118120
0 commit comments