Skip to content

Commit

Permalink
'refactoring'
Browse files Browse the repository at this point in the history
  • Loading branch information
nahumtimerman committed Jul 11, 2016
1 parent d53284c commit 1306526
Show file tree
Hide file tree
Showing 10 changed files with 357 additions and 440 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/nahum_t.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

530 changes: 185 additions & 345 deletions .idea/workspace.xml

Large diffs are not rendered by default.

10 changes: 0 additions & 10 deletions cloudshell/power/pdu/raritan/api/raritan_rpc_api.py

This file was deleted.

23 changes: 23 additions & 0 deletions cloudshell/power/pdu/raritan/device/factory_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from cloudshell.shell.core.context_utils import get_attribute_by_name
import inject


class FactoryContext:
def __init__(self, context):
api = inject.instance('api')
password = get_attribute_by_name('Password', context)
self._password = api.DecryptPassword(password).Value
self._user = get_attribute_by_name('User', context)
self._host = context.resource.address

@property
def user(self):
return self._user

@property
def password(self):
return self._password

@property
def host(self):
return self._host
31 changes: 31 additions & 0 deletions cloudshell/power/pdu/raritan/device/raritan_rpcapi_pdu_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from raritan import rpc
from raritan.rpc import pdumodel

from cloudshell.shell.core.driver_context import AutoLoadDetails, AutoLoadResource, AutoLoadAttribute
from cloudshell.power.pdu.raritan.device.rpcapi_outlet import RPCAPIOutlet
from cloudshell.power.pdu.device.pdu_factory import PDUFactory


class RaritanRpcApiPduFactory(PDUFactory):
def __init__(self, context):
self._agent = rpc.Agent("https", context.host, context.user, context.password)
self._pdu_handler = pdumodel.Pdu('model/pdu/0', self._agent)

def get_outlets(self):
return [RPCAPIOutlet(x) for x in self._pdu_handler.getOutlets()]

def get_inventory(self):
metadata = self._pdu_handler.getMetaData()
resources = self._autoload_resources_by_rpc()
attributes = [AutoLoadAttribute('', 'Firmware Version', metadata.fwRevision),
AutoLoadAttribute('', 'Vendor', metadata.nameplate.manufacturer),
AutoLoadAttribute('', 'Model', metadata.nameplate.model)]
result = AutoLoadDetails(resources, attributes)
return result

def _autoload_resources_by_rpc(self):
resources = [AutoLoadResource('Generic Power Socket',
'Socket ' + str(x+1),
str(x+1))
for x, val in enumerate(self.get_outlets())]
return resources
20 changes: 20 additions & 0 deletions cloudshell/power/pdu/raritan/device/rpcapi_outlet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from raritan.rpc import pdumodel
from cloudshell.power.pdu.device.outlet import Outlet


class RPCAPIOutlet(Outlet):
def __init__(self, outlet_handler):
self._handler = outlet_handler

def power_on(self):
self._handler.setPowerState(POWERED_ON)
if self._handler.getState().powerState != POWERED_ON:
Exception('Ports were not powered on')

def power_off(self):
self._handler.setPowerState(POWERED_OFF)
if self._handler.getState().powerState != POWERED_OFF:
Exception('Ports were not powered off')

POWERED_ON = pdumodel.Outlet.PowerState.PS_ON
POWERED_OFF = pdumodel.Outlet.PowerState.PS_OFF
10 changes: 10 additions & 0 deletions cloudshell/power/pdu/raritan/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


def get_outlets_by_address(outlets, ports):
# ports: ['192.168.30.128/4', '192.168.30.128/6']

def socket(port):
n = int(port.split('/')[-1])
return outlets[n-1]

return [socket(x) for x in ports]
172 changes: 87 additions & 85 deletions cloudshell/power/pdu/raritan/raritan_handler.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,40 @@
import os
import inject
import time

from cloudshell.configuration.cloudshell_snmp_configuration import SNMP_HANDLER
from cloudshell.shell.core.driver_context import AutoLoadDetails, AutoLoadResource, AutoLoadAttribute
from cloudshell.shell.core.context_utils import get_attribute_by_name
from cloudshell.api.cloudshell_api import CloudShellAPISession
from cloudshell.power.pdu.raritan.api.raritan_rpc_api import RaritanRpcApi
from cloudshell.power.pdu.managed_devices.connected_to_pdu_resource import ConnectedToPduResource

from raritan import rpc
from raritan.rpc import pdumodel
from cloudshell.power.pdu.raritan.device.raritan_rpcapi_pdu_factory import RaritanRpcApiPduFactory
from cloudshell.power.pdu.raritan.device.factory_context import FactoryContext
from cloudshell.power.pdu.raritan.helper import get_outlets_by_address


class RaritanHandler:
def __init__(self):
self._snmp = None
self._agent = None
self._pdu = None
self._user = None
self._password = None
def __init__(self, pdu_factory=RaritanRpcApiPduFactory):
self.pdu = None
self._outlets = []

@property
def snmp(self):
if self._snmp is None:
self._snmp = SNMP_HANDLER()
path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'mibs'))
self._snmp.update_mib_sources(path)
return self._snmp

@property
def pdu(self):
if self._pdu is None:
self._pdu = pdumodel.Pdu('model/pdu/0', self.agent)
return self._pdu
self._pdu_factory = pdu_factory

@property
def outlets(self):
# uses raritan RPC api
if not self._outlets:
self._outlets = self.pdu.getOutlets()
self._outlets = self.pdu.get_outlets()
return self._outlets

def initialize(self, context):
user = get_attribute_by_name('User', context)
password = self._get_decrypted_password(context)
rari_api = RaritanRpcApi(context.resource.address, user, password)
self._pdu = rari_api.pdu_handler()
factory_context = FactoryContext(context)
self.pdu = self._pdu_factory(factory_context)

def get_inventory(self, context):
return self.pdu.get_inventory()

def power_on(self, context, ports):
rr = ConnectedToPduResource(context.remote_endpoints)
from debug_utils import debugger
debugger.attach_debugger()
result = True
for o in self._get_outlets_by_address(ports):
o.setPowerState(pdumodel.Outlet.PowerState.PS_ON)
result = o.getState()
print result
for o in get_outlets_by_address(ports):
o.power_on
return rr.online()

def power_off(self, context, ports):
rr = ConnectedToPduResource(context.remote_endpoints)
for o in self._get_outlets_by_address(ports):
o.setPowerState(pdumodel.Outlet.PowerState.PS_OFF)
for o in get_outlets_by_address(ports):
o.power_off
return rr.offline()

def power_cycle(self, context, ports, delay=0):
Expand All @@ -74,45 +45,76 @@ def power_cycle(self, context, ports, delay=0):
self.power_on(context, ports)
return 'Power cycle complete'

def get_inventory(self, context):
metadata = self.pdu.getMetaData()
resources = self._autoload_resources_by_rpc()
attributes = [AutoLoadAttribute('', 'Firmware Version', metadata.fwRevision),
AutoLoadAttribute('', 'Vendor', metadata.nameplate.manufacturer),
AutoLoadAttribute('', 'Model', metadata.nameplate.model)]
result = AutoLoadDetails(resources, attributes)
return result

def _autoload_resources_by_rpc(self):
resources = [AutoLoadResource('Generic Power Socket',
'Socket ' + str(x+1),
str(x+1))
for x, val in enumerate(self.outlets)]
return resources

def _autoload_resources_by_snmp(self):
outlets = self._get_outlets()
resources = [AutoLoadResource('Generic Socket',
'Socket ' + outlets[outlet]['outletLabel'],
outlets[outlet]['outletLabel'])
for outlet in self.outlets]
return resources

def _get_outlets(self):
return self.snmp.get_table('PDU2-MIB', 'outletConfigurationTable')

def _get_outlets_by_address(self, ports):
# ports: ['192.168.30.128/4']

def socket(x):
n = int(x.split('/')[-1])
return self.outlets[n-1]

return [socket(x) for x in ports]

def _get_decrypted_password(self, context):
password = get_attribute_by_name('Password', context)
api = inject.instance('api')
return api.DecryptPassword(password).Value















































# SNMP legacy code
#
# import os
# from cloudshell.configuration.cloudshell_snmp_configuration import SNMP_HANDLER
#
# @property
# def snmp(self):
# if self._snmp is None:
# self._snmp = SNMP_HANDLER()
# path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'mibs'))
# self._snmp.update_mib_sources(path)
# return self._snmp
#
# def _autoload_resources_by_snmp(self):
# outlets = self._get_outlets()
# resources = [AutoLoadResource('Generic Socket',
# 'Socket ' + outlets[outlet]['outletLabel'],
# outlets[outlet]['outletLabel'])
# for outlet in self.outlets]
# return resources
#
# def _get_outlets(self):
# return self.snmp.get_table('PDU2-MIB', 'outletConfigurationTable')



Binary file not shown.

0 comments on commit 1306526

Please sign in to comment.