Skip to content

Commit

Permalink
Migrate all the bound BindProvider calls to dynamically loaded methods
Browse files Browse the repository at this point in the history
- adds the load_class call to common to return a pointer to the class
  passed in
- refactors out BindProvider to be Provider, to adhere to the spec

preliminary work for #12
  • Loading branch information
Charles Butler committed Jun 10, 2015
1 parent 4e94c8a commit 1e3c35a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 46 deletions.
2 changes: 1 addition & 1 deletion contrib/bind/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from common import resolve_hostname_to_ip


class BindProvider(object):
class Provider(object):

def config_changed(self, domain='example.com'):
zp = ZoneParser(domain)
Expand Down
14 changes: 13 additions & 1 deletion contrib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,22 @@ def existing_nameservers():

def load_module(full_class_string):
"""
dynamically load a class from a string
dynamically load a module from a string
"""

class_data = full_class_string.split(".")
module_path = ".".join(class_data[:-1])

return importlib.import_module(module_path)

def load_class(full_class_string):
"""
dynamically load a class from a string
"""

class_data = full_class_string.split(".")
module_path = ".".join(class_data[:-1])
class_str = class_data[-1]

module = importlib.import_module(module_path)
return getattr(module, class_str)
10 changes: 5 additions & 5 deletions contrib/tests/test_bind_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import sys

from bind.provider import BindProvider
from bind.provider import Provider


class TestBindProvider(unittest.TestCase):
Expand All @@ -13,7 +13,7 @@ class TestBindProvider(unittest.TestCase):
@patch('bind.provider.unit_get')
def test_first_setup(self, ugm, spcom):
spcom.return_value = '10.0.0.1'
bp = BindProvider()
bp = Provider()
parser = MagicMock()
bp.first_setup(parser)
ugm.assert_called_once()
Expand All @@ -24,7 +24,7 @@ def test_first_setup(self, ugm, spcom):
@patch('bind.provider.ZoneParser.dict_to_zone')
@patch('bind.provider.ZoneParser.save')
def test_add_record(self, zps, zpm):
bp = BindProvider()
bp = Provider()
bp.reload_config = Mock()
bp.add_record({'rr': 'A', 'alias': 'foo', 'addr': '127.0.0.1'})
zps.assert_called_once()
Expand All @@ -35,7 +35,7 @@ def test_add_record(self, zps, zpm):
@patch('os.path.exists')
def test_config_changed(self, osem, zpsm):
osem.return_value = False
bp = BindProvider()
bp = Provider()
bp.reload_config = Mock()
bp.first_setup = Mock()
bp.config_changed()
Expand All @@ -45,7 +45,7 @@ def test_config_changed(self, osem, zpsm):

@patch('bind.provider.ZoneParser')
def test_remove_record(self, zpm):
bp = BindProvider()
bp = Provider()
bp.reload_config = Mock()
zpm.zone.remove = Mock()
zpm.save = Mock()
Expand Down
12 changes: 7 additions & 5 deletions hooks/config-changed
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.environ['CHARM_DIR'],
'contrib')))

from charmhelpers.core.hookenv import config
from common import sanity_check
from bind.provider import BindProvider
from common import sanity_check, load_class

#from bind.provider import BindProvider


class ConfigChanged(object):
Expand All @@ -15,10 +16,11 @@ class ConfigChanged(object):
if not sanity_check():
return 0
self.config = config()
if self.config['provider'] == 'bind':
bp = BindProvider()
bp.config_changed(self.config['domain'])

class_string = "{}.provider.Provider".format(self.config['provider'])
provider = load_class(class_string)
p = provider()
p.config_changed(self.config['domain'])

if __name__ == '__main__':
c = ConfigChanged()
19 changes: 7 additions & 12 deletions hooks/programmable-multiple-relation-changed
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@
import os
import sys
# Add charmhelpers to the system path.
try:
sys.path.insert(0, os.path.abspath(os.path.join(os.environ['CHARM_DIR'],
'contrib')))
except:
sys.path.insert(0, os.path.abspath(os.path.join('..', 'contrib')))


from bind.provider import BindProvider
sys.path.insert(0, os.path.abspath(os.path.join(os.environ['CHARM_DIR'],
'contrib')))

from charmhelpers.core.hookenv import (
config,
Expand All @@ -21,7 +15,7 @@ from charmhelpers.core.hookenv import (

from common import trim_empty_array_elements as trim
from common import resolve_hostname_to_ip as getip

from common import load_class

class ProgrammableChanged(object):

Expand All @@ -43,9 +37,10 @@ class ProgrammableChanged(object):
if not domain:
domain = config()['domain']

if self.config['provider'] == 'bind':
bp = BindProvider()
bp.add_record(resources, domain)
class_string = "{}.provider.Provider".format(self.config['provider'])
provider = load_class(class_string)
p = provider()
p.add_record(resources, domain)


if __name__ == '__main__':
Expand Down
16 changes: 6 additions & 10 deletions hooks/programmable-relation-changed
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@
import os
import sys
# Add charmhelpers to the system path.
try:
sys.path.insert(0, os.path.abspath(os.path.join(os.environ['CHARM_DIR'],
sys.path.insert(0, os.path.abspath(os.path.join(os.environ['CHARM_DIR'],
'contrib')))
except:
sys.path.insert(0, os.path.abspath(os.path.join('..', 'contrib')))


from bind.provider import BindProvider

from charmhelpers.core.hookenv import (
config,
Expand All @@ -19,6 +13,7 @@ from charmhelpers.core.hookenv import (
relation_id,
)
from common import resolve_hostname_to_ip as getip
from common import load_class


class ProgrammableChanged(object):
Expand All @@ -41,9 +36,10 @@ class ProgrammableChanged(object):

parsed = {'domain': domain, 'alias': alias, 'addr': addr, 'rr': rr}

if self.config['provider'] == 'bind':
bp = BindProvider()
bp.add_record(parsed, domain)
class_string = '{}.provider.Provider'.format(self.config['provider'])
provider = load_class(class_string)
p = provider()
p.add_record(parsed, domain)


if __name__ == '__main__':
Expand Down
21 changes: 9 additions & 12 deletions hooks/programmable-relation-departed
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#!/usr/bin/python
import os
import sys
# Add charmhelpers to the system path.
try:
sys.path.insert(0, os.path.abspath(os.path.join(os.environ['CHARM_DIR'],
'contrib')))
except:
sys.path.insert(0, os.path.abspath(os.path.join('..', 'contrib')))

sys.path.insert(0, os.path.abspath(os.path.join(os.environ['CHARM_DIR'],
'contrib')))

from bind.provider import BindProvider
#from bind.provider import BindProvider

from charmhelpers.core.hookenv import (
log,
Expand All @@ -18,6 +14,8 @@ from charmhelpers.core.hookenv import (
relation_get,
)

from common import load_class

class ProgrammableBroken(object):

def __init__(self):
Expand All @@ -33,11 +31,10 @@ class ProgrammableBroken(object):

parsed = {'domain': domain, 'alias': alias, 'addr': addr, 'rr': rr}

if self.config['provider'] == 'bind':
bp = BindProvider()
bp.remove_record(parsed, domain)


class_string = "{}.provider.Provider".format(self.config['provider'])
provider = load_class(class_string)
p = provider()
p.remove_record(parsed, domain)

if __name__ == '__main__':
c = ProgrammableBroken()

0 comments on commit 1e3c35a

Please sign in to comment.