Skip to content

Commit 1306526

Browse files
committed
'refactoring'
1 parent d53284c commit 1306526

10 files changed

Lines changed: 357 additions & 440 deletions

File tree

.idea/dictionaries/nahum_t.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 185 additions & 345 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloudshell/power/pdu/raritan/api/raritan_rpc_api.py

Lines changed: 0 additions & 10 deletions
This file was deleted.
File renamed without changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from cloudshell.shell.core.context_utils import get_attribute_by_name
2+
import inject
3+
4+
5+
class FactoryContext:
6+
def __init__(self, context):
7+
api = inject.instance('api')
8+
password = get_attribute_by_name('Password', context)
9+
self._password = api.DecryptPassword(password).Value
10+
self._user = get_attribute_by_name('User', context)
11+
self._host = context.resource.address
12+
13+
@property
14+
def user(self):
15+
return self._user
16+
17+
@property
18+
def password(self):
19+
return self._password
20+
21+
@property
22+
def host(self):
23+
return self._host
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from raritan import rpc
2+
from raritan.rpc import pdumodel
3+
4+
from cloudshell.shell.core.driver_context import AutoLoadDetails, AutoLoadResource, AutoLoadAttribute
5+
from cloudshell.power.pdu.raritan.device.rpcapi_outlet import RPCAPIOutlet
6+
from cloudshell.power.pdu.device.pdu_factory import PDUFactory
7+
8+
9+
class RaritanRpcApiPduFactory(PDUFactory):
10+
def __init__(self, context):
11+
self._agent = rpc.Agent("https", context.host, context.user, context.password)
12+
self._pdu_handler = pdumodel.Pdu('model/pdu/0', self._agent)
13+
14+
def get_outlets(self):
15+
return [RPCAPIOutlet(x) for x in self._pdu_handler.getOutlets()]
16+
17+
def get_inventory(self):
18+
metadata = self._pdu_handler.getMetaData()
19+
resources = self._autoload_resources_by_rpc()
20+
attributes = [AutoLoadAttribute('', 'Firmware Version', metadata.fwRevision),
21+
AutoLoadAttribute('', 'Vendor', metadata.nameplate.manufacturer),
22+
AutoLoadAttribute('', 'Model', metadata.nameplate.model)]
23+
result = AutoLoadDetails(resources, attributes)
24+
return result
25+
26+
def _autoload_resources_by_rpc(self):
27+
resources = [AutoLoadResource('Generic Power Socket',
28+
'Socket ' + str(x+1),
29+
str(x+1))
30+
for x, val in enumerate(self.get_outlets())]
31+
return resources
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from raritan.rpc import pdumodel
2+
from cloudshell.power.pdu.device.outlet import Outlet
3+
4+
5+
class RPCAPIOutlet(Outlet):
6+
def __init__(self, outlet_handler):
7+
self._handler = outlet_handler
8+
9+
def power_on(self):
10+
self._handler.setPowerState(POWERED_ON)
11+
if self._handler.getState().powerState != POWERED_ON:
12+
Exception('Ports were not powered on')
13+
14+
def power_off(self):
15+
self._handler.setPowerState(POWERED_OFF)
16+
if self._handler.getState().powerState != POWERED_OFF:
17+
Exception('Ports were not powered off')
18+
19+
POWERED_ON = pdumodel.Outlet.PowerState.PS_ON
20+
POWERED_OFF = pdumodel.Outlet.PowerState.PS_OFF
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
3+
def get_outlets_by_address(outlets, ports):
4+
# ports: ['192.168.30.128/4', '192.168.30.128/6']
5+
6+
def socket(port):
7+
n = int(port.split('/')[-1])
8+
return outlets[n-1]
9+
10+
return [socket(x) for x in ports]
Lines changed: 87 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,40 @@
1-
import os
2-
import inject
31
import 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
103
from 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

169
class 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

-5.01 KB
Binary file not shown.

0 commit comments

Comments
 (0)