33from cloudshell .power .pdu .managed_devices .connected_to_pdu_resource import ConnectedToPduResource
44from cloudshell .power .pdu .raritan .device .raritan_rpcapi_pdu_factory import RaritanRpcApiPduFactory
55from cloudshell .power .pdu .raritan .device .factory_context import FactoryContext
6- from cloudshell .power .pdu .raritan .helper import get_outlets_by_address
6+ from cloudshell .power .pdu .raritan .shell_helper import get_outlets_by_address
77
88
99class RaritanHandler :
@@ -14,107 +14,51 @@ def __init__(self, pdu_factory=RaritanRpcApiPduFactory):
1414
1515 @property
1616 def outlets (self ):
17- if not self ._outlets :
18- self ._outlets = self .pdu .get_outlets ()
19- return self ._outlets
17+ return self .pdu .get_outlets ()
2018
2119 def initialize (self , context ):
20+ pass
21+
22+ def initialize_pdu (self , context ):
2223 factory_context = FactoryContext (context )
2324 self .pdu = self ._pdu_factory (factory_context )
2425
2526 def get_inventory (self , context ):
27+ self .initialize_pdu (context )
2628 return self .pdu .get_inventory ()
2729
2830 def power_on (self , context , ports ):
31+ self .initialize_pdu (context )
2932 rr = ConnectedToPduResource (context .remote_endpoints )
30- for o in get_outlets_by_address (ports ):
31- o .power_on
33+ for o in get_outlets_by_address (self . outlets , ports ):
34+ o .power_on ()
3235 return rr .online ()
3336
3437 def power_off (self , context , ports ):
38+ self .initialize_pdu (context )
3539 rr = ConnectedToPduResource (context .remote_endpoints )
36- for o in get_outlets_by_address (ports ):
37- o .power_off
40+ for o in get_outlets_by_address (self . outlets , ports ):
41+ o .power_off ()
3842 return rr .offline ()
3943
4044 def power_cycle (self , context , ports , delay = 0 ):
41- if delay < 0 :
42- delay = 0
45+ self . initialize_pdu ( context )
46+ self . _validate_power_cycle_delay ( delay )
4347 self .power_off (context , ports )
4448 time .sleep (delay )
4549 self .power_on (context , ports )
4650 return 'Power cycle complete'
4751
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')
52+ @staticmethod
53+ def _validate_power_cycle_delay (delay ):
54+ try :
55+ float (delay )
56+ if delay < 0 :
57+ raise ValueError ('Must be non negative number' )
58+ except ValueError :
59+ raise Exception ('Delay represents the seconds between power off and power on. \n '
60+ 'You ran the power cycle command with a delay argument of {0}, '
61+ 'but acceptable values are 0 or a positive numeric value' .format (delay ))
11862
11963
12064
0 commit comments