Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Here is one example of abstraction hiding #63

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 4 additions & 23 deletions lib/Quads.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
import urllib
import json
from subprocess import check_call
from hardware_services.inventory_service import get_inventory_service, set_inventory_service
from hardware_services.network_service import get_network_service, set_network_service
sys.path.append(os.path.dirname(__file__) + "/hardware_services/inventory_drivers/")
sys.path.append(os.path.dirname(__file__) + "/hardware_services/network_drivers/")
from hardware_services.inventory_service import InventoryServiceProxy
from hardware_services.network_service import NetworkServiceProxy


class Quads(object):
Expand All @@ -47,21 +45,8 @@ def __init__(self, config, statedir, movecommand, datearg, syncstate, initialize
self.logger = logging.getLogger("quads.Quads")
self.logger.setLevel(logging.DEBUG)

#EC528 addition - dynamically import driver module and set inventory and network services
inventoryservice = hardwareservice + "InventoryDriver"
networkservice = hardwareservice + "NetworkDriver"

importlib.import_module(inventoryservice)
importlib.import_module(networkservice)

set_inventory_service(getattr(sys.modules[inventoryservice], inventoryservice)())
set_network_service(getattr(sys.modules[networkservice], networkservice)())

self.inventory_service = get_inventory_service()
self.network_service = get_network_service()

self.hardware_service_url = hardwareserviceurl

self.inventory_service = InventoryServiceProxy(hardwareservice, hardwarewserviceurl)
self.network_service = NetworkServiceProxy(hardwareservice, hardwareserviceurl)

self.inventory_service.load_data(self, force, initialize)

Expand Down Expand Up @@ -616,7 +601,3 @@ def quads_get(self, url, params=None):
@classmethod
def quads_delete(self, url):
self.quads_status_code_check(requests.delete(url))




36 changes: 16 additions & 20 deletions lib/hardware_services/inventory_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,24 @@
# will be able to continue with minimal changes (only thing that this will change is where QUADS pulls its data from)
#####################################################################################################################

import sys
import os, sys, importlib
from abc import ABCMeta, abstractmethod


sys.path.append(os.path.dirname(__file__) + "/hardware_services/inventory_drivers/")


class InventoryServiceProxy(object):
def __init__(self, name):
self.name = name
inventoryservice = name + "InventoryDriver"
importlib.import_module()
self.invsrv = getattr(sys.modules[inventoryservice], inventoryservice)()

def __getattr__(self, attr_name):
return getattr(self.invsrv, attr_name)


class InventoryService(object):

__metaclass__ = ABCMeta
Expand Down Expand Up @@ -88,22 +103,3 @@ def sync_state(self, quads):
def write_data(self, quads, doexit):
""" TODO add documentation
"""


_inventory_service = None


def set_inventory_service(inventory_service):

global _inventory_service
if _inventory_service is not None:
sys.exit("Error: _inventory_service already set")

_inventory_service = inventory_service


def get_inventory_service():
return _inventory_service



37 changes: 16 additions & 21 deletions lib/hardware_services/network_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,29 @@
# will be able to continue with minimal changes (only thing that this will change is where QUADS pulls its data from)
#####################################################################################################################

import sys
import os, sys, importlib
from abc import ABCMeta, abstractmethod

class NetworkService(object):

__metaclass__ = ABCMeta


@abstractmethod
def move_hosts(self, **kwargs):
""" TODO add documentation
"""

sys.path.append(os.path.dirname(__file__) + "/hardware_services/network_drivers/")

_network_service = None

class NetworkServiceProxy(object):
def __init__(self, name):
self.name = name
networkservice = name + "NetworkDriver"
importlib.import_module()
self.invsrv = getattr(sys.modules[networkservice], networkservice)()

def set_network_service(network_service):
def __getattr__(self, attr_name):
return getattr(self.netsrv, attr_name)

global _network_service
if _network_service is not None:
sys.exit("Error: _network_service already set")

_network_service = network_service


def get_network_service():
return _network_service

class NetworkService(object):

__metaclass__ = ABCMeta

@abstractmethod
def move_hosts(self, **kwargs):
""" TODO add documentation
"""