mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 17:09:03 +00:00
Merge pull request #41220 from rallytime/bp-40246
Back-port #40246 to 2016.11
This commit is contained in:
commit
75942235f0
@ -47,38 +47,13 @@ Example:
|
|||||||
# Import Python Libs
|
# Import Python Libs
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
from distutils.version import LooseVersion as _LooseVersion # pylint: disable=import-error,no-name-in-module
|
|
||||||
|
|
||||||
import salt.modules.libcloud_dns as libcloud_dns_module
|
|
||||||
|
|
||||||
# Import salt libs
|
# Import salt libs
|
||||||
import salt.utils
|
import salt.utils
|
||||||
import logging
|
import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Import third party libs
|
|
||||||
REQUIRED_LIBCLOUD_VERSION = '1.0.0'
|
|
||||||
try:
|
|
||||||
#pylint: disable=unused-import
|
|
||||||
import libcloud
|
|
||||||
from libcloud.dns.providers import get_driver
|
|
||||||
#pylint: enable=unused-import
|
|
||||||
if hasattr(libcloud, '__version__') and _LooseVersion(libcloud.__version__) < _LooseVersion(REQUIRED_LIBCLOUD_VERSION):
|
|
||||||
raise ImportError()
|
|
||||||
logging.getLogger('libcloud').setLevel(logging.CRITICAL)
|
|
||||||
HAS_LIBCLOUD = True
|
|
||||||
except ImportError:
|
|
||||||
HAS_LIBCLOUD = False
|
|
||||||
|
|
||||||
|
|
||||||
def __virtual__():
|
def __virtual__():
|
||||||
'''
|
|
||||||
Only load if libcloud libraries exist.
|
|
||||||
'''
|
|
||||||
if not HAS_LIBCLOUD:
|
|
||||||
msg = ('A apache-libcloud library with version at least {0} was not '
|
|
||||||
'found').format(REQUIRED_LIBCLOUD_VERSION)
|
|
||||||
return (False, msg)
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -86,17 +61,6 @@ def __init__(opts):
|
|||||||
salt.utils.compat.pack_dunder(__name__)
|
salt.utils.compat.pack_dunder(__name__)
|
||||||
|
|
||||||
|
|
||||||
def _get_driver(profile):
|
|
||||||
config = __salt__['config.option']('libcloud_dns')[profile]
|
|
||||||
cls = get_driver(config['driver'])
|
|
||||||
key = config.get('key')
|
|
||||||
secret = config.get('secret', None)
|
|
||||||
secure = config.get('secure', True)
|
|
||||||
host = config.get('host', None)
|
|
||||||
port = config.get('port', None)
|
|
||||||
return cls(key, secret, secure, host, port)
|
|
||||||
|
|
||||||
|
|
||||||
def state_result(result, message):
|
def state_result(result, message):
|
||||||
return {'result': result, 'comment': message}
|
return {'result': result, 'comment': message}
|
||||||
|
|
||||||
@ -114,14 +78,14 @@ def zone_present(domain, type, profile):
|
|||||||
:param profile: The profile key
|
:param profile: The profile key
|
||||||
:type profile: ``str``
|
:type profile: ``str``
|
||||||
'''
|
'''
|
||||||
zones = libcloud_dns_module.list_zones(profile)
|
zones = __salt__['libcloud_dns.list_zones'](profile)
|
||||||
if not type:
|
if not type:
|
||||||
type = 'master'
|
type = 'master'
|
||||||
matching_zone = [z for z in zones if z.domain == domain]
|
matching_zone = [z for z in zones if z.domain == domain]
|
||||||
if len(matching_zone) > 0:
|
if len(matching_zone) > 0:
|
||||||
return state_result(True, "Zone already exists")
|
return state_result(True, "Zone already exists")
|
||||||
else:
|
else:
|
||||||
result = libcloud_dns_module.create_zone(domain, profile, type)
|
result = __salt__['libcloud_dns.create_zone'](domain, profile, type)
|
||||||
return state_result(result, "Created new zone")
|
return state_result(result, "Created new zone")
|
||||||
|
|
||||||
|
|
||||||
@ -135,12 +99,12 @@ def zone_absent(domain, profile):
|
|||||||
:param profile: The profile key
|
:param profile: The profile key
|
||||||
:type profile: ``str``
|
:type profile: ``str``
|
||||||
'''
|
'''
|
||||||
zones = libcloud_dns_module.list_zones(profile)
|
zones = __salt__['libcloud_dns.list_zones'](profile)
|
||||||
matching_zone = [z for z in zones if z.domain == domain]
|
matching_zone = [z for z in zones if z.domain == domain]
|
||||||
if len(matching_zone) == 0:
|
if len(matching_zone) == 0:
|
||||||
return state_result(True, "Zone already absent")
|
return state_result(True, "Zone already absent")
|
||||||
else:
|
else:
|
||||||
result = libcloud_dns_module.delete_zone(matching_zone[0].id, profile)
|
result = __salt__['libcloud_dns.delete_zone'](matching_zone[0].id, profile)
|
||||||
return state_result(result, "Deleted zone")
|
return state_result(result, "Deleted zone")
|
||||||
|
|
||||||
|
|
||||||
@ -166,18 +130,18 @@ def record_present(name, zone, type, data, profile):
|
|||||||
:param profile: The profile key
|
:param profile: The profile key
|
||||||
:type profile: ``str``
|
:type profile: ``str``
|
||||||
'''
|
'''
|
||||||
zones = libcloud_dns_module.list_zones(profile)
|
zones = __salt__['libcloud_dns.list_zones'](profile)
|
||||||
try:
|
try:
|
||||||
matching_zone = [z for z in zones if z.domain == zone][0]
|
matching_zone = [z for z in zones if z.domain == zone][0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return state_result(False, "Could not locate zone")
|
return state_result(False, "Could not locate zone")
|
||||||
records = libcloud_dns_module.list_records(matching_zone.id, profile)
|
records = __salt__['libcloud_dns.list_records'](matching_zone.id, profile)
|
||||||
matching_records = [record for record in records
|
matching_records = [record for record in records
|
||||||
if record.name == name and
|
if record.name == name and
|
||||||
record.type == type and
|
record.type == type and
|
||||||
record.data == data]
|
record.data == data]
|
||||||
if len(matching_records) == 0:
|
if len(matching_records) == 0:
|
||||||
result = libcloud_dns_module.create_record(
|
result = __salt__['libcloud_dns.create_record'](
|
||||||
name, matching_zone.id,
|
name, matching_zone.id,
|
||||||
type, data, profile)
|
type, data, profile)
|
||||||
return state_result(result, "Created new record")
|
return state_result(result, "Created new record")
|
||||||
@ -207,12 +171,12 @@ def record_absent(name, zone, type, data, profile):
|
|||||||
:param profile: The profile key
|
:param profile: The profile key
|
||||||
:type profile: ``str``
|
:type profile: ``str``
|
||||||
'''
|
'''
|
||||||
zones = libcloud_dns_module.list_zones(profile)
|
zones = __salt__['libcloud_dns.list_zones'](profile)
|
||||||
try:
|
try:
|
||||||
matching_zone = [z for z in zones if z.domain == zone][0]
|
matching_zone = [z for z in zones if z.domain == zone][0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return state_result(False, "Zone could not be found")
|
return state_result(False, "Zone could not be found")
|
||||||
records = libcloud_dns_module.list_records(matching_zone.id, profile)
|
records = __salt__['libcloud_dns.list_records'](matching_zone.id, profile)
|
||||||
matching_records = [record for record in records
|
matching_records = [record for record in records
|
||||||
if record.name == name and
|
if record.name == name and
|
||||||
record.type == type and
|
record.type == type and
|
||||||
@ -220,7 +184,7 @@ def record_absent(name, zone, type, data, profile):
|
|||||||
if len(matching_records) > 0:
|
if len(matching_records) > 0:
|
||||||
result = []
|
result = []
|
||||||
for record in matching_records:
|
for record in matching_records:
|
||||||
result.append(libcloud_dns_module.delete_record(
|
result.append(__salt__['libcloud_dns.delete_record'](
|
||||||
matching_zone.id,
|
matching_zone.id,
|
||||||
record.id,
|
record.id,
|
||||||
profile))
|
profile))
|
||||||
|
@ -10,8 +10,6 @@ from __future__ import absolute_import
|
|||||||
from salttesting import skipIf
|
from salttesting import skipIf
|
||||||
from tests.unit import ModuleTestCase, hasDependency
|
from tests.unit import ModuleTestCase, hasDependency
|
||||||
from salttesting.mock import (
|
from salttesting.mock import (
|
||||||
patch,
|
|
||||||
MagicMock,
|
|
||||||
NO_MOCK,
|
NO_MOCK,
|
||||||
NO_MOCK_REASON
|
NO_MOCK_REASON
|
||||||
)
|
)
|
||||||
@ -21,7 +19,6 @@ from salt.states import libcloud_dns
|
|||||||
ensure_in_syspath('../../')
|
ensure_in_syspath('../../')
|
||||||
|
|
||||||
SERVICE_NAME = 'libcloud_dns'
|
SERVICE_NAME = 'libcloud_dns'
|
||||||
libcloud_dns.__salt__ = {}
|
|
||||||
libcloud_dns.__utils__ = {}
|
libcloud_dns.__utils__ = {}
|
||||||
|
|
||||||
|
|
||||||
@ -48,33 +45,46 @@ def get_mock_driver():
|
|||||||
return MockDNSDriver()
|
return MockDNSDriver()
|
||||||
|
|
||||||
|
|
||||||
class MockDnsModule(object):
|
test_records = {
|
||||||
test_records = {
|
"zone1": [TestRecord(0, "www", "A", "127.0.0.1")]
|
||||||
"zone1": [TestRecord(0, "www", "A", "127.0.0.1")]
|
}
|
||||||
}
|
|
||||||
|
|
||||||
def list_zones(self, profile):
|
|
||||||
return [TestZone("zone1", "test.com")]
|
|
||||||
|
|
||||||
def list_records(self, zone_id, profile):
|
def list_zones(profile):
|
||||||
return MockDnsModule.test_records[zone_id]
|
return [TestZone("zone1", "test.com")]
|
||||||
|
|
||||||
def create_record(self, *args):
|
|
||||||
return True
|
|
||||||
|
|
||||||
def delete_record(self, *args):
|
def list_records(zone_id, profile):
|
||||||
return True
|
return test_records[zone_id]
|
||||||
|
|
||||||
def create_zone(self, *args):
|
|
||||||
return True
|
|
||||||
|
|
||||||
def delete_zone(self, *args):
|
def create_record(*args):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def delete_record(*args):
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def create_zone(*args):
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def delete_zone(*args):
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
libcloud_dns.__salt__ = {
|
||||||
|
'libcloud_dns.list_zones': list_zones,
|
||||||
|
'libcloud_dns.list_records': list_records,
|
||||||
|
'libcloud_dns.create_record': create_record,
|
||||||
|
'libcloud_dns.delete_record': delete_record,
|
||||||
|
'libcloud_dns.create_zone': create_zone,
|
||||||
|
'libcloud_dns.delete_zone': delete_zone
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||||
@patch('salt.states.libcloud_dns._get_driver',
|
|
||||||
MagicMock(return_value=MockDNSDriver()))
|
|
||||||
class LibcloudDnsModuleTestCase(ModuleTestCase):
|
class LibcloudDnsModuleTestCase(ModuleTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
hasDependency('libcloud', fake_module=False)
|
hasDependency('libcloud', fake_module=False)
|
||||||
@ -92,52 +102,34 @@ class LibcloudDnsModuleTestCase(ModuleTestCase):
|
|||||||
|
|
||||||
self.setup_loader()
|
self.setup_loader()
|
||||||
self.loader.set_result(libcloud_dns, 'config.option', get_config)
|
self.loader.set_result(libcloud_dns, 'config.option', get_config)
|
||||||
libcloud_dns.libcloud_dns_module = MockDnsModule()
|
|
||||||
|
|
||||||
def test_module_creation(self, *args):
|
|
||||||
client = libcloud_dns._get_driver('test')
|
|
||||||
self.assertFalse(client is None)
|
|
||||||
|
|
||||||
def test_init(self):
|
|
||||||
with patch('salt.utils.compat.pack_dunder', return_value=False) as dunder:
|
|
||||||
libcloud_dns.__init__(None)
|
|
||||||
dunder.assert_called_with('salt.states.libcloud_dns')
|
|
||||||
|
|
||||||
def test_present_record_exists(self):
|
def test_present_record_exists(self):
|
||||||
"""
|
"""
|
||||||
Try and create a record that already exists
|
Try and create a record that already exists
|
||||||
"""
|
"""
|
||||||
with patch.object(MockDnsModule, 'create_record', MagicMock(return_value=True)) as create_patch:
|
result = libcloud_dns.record_present("www", "test.com", "A", "127.0.0.1", "test")
|
||||||
result = libcloud_dns.record_present("www", "test.com", "A", "127.0.0.1", "test")
|
self.assertTrue(result)
|
||||||
self.assertTrue(result)
|
|
||||||
self.assertFalse(create_patch.called)
|
|
||||||
|
|
||||||
def test_present_record_does_not_exist(self):
|
def test_present_record_does_not_exist(self):
|
||||||
"""
|
"""
|
||||||
Try and create a record that already exists
|
Try and create a record that already exists
|
||||||
"""
|
"""
|
||||||
with patch.object(MockDnsModule, 'create_record') as create_patch:
|
result = libcloud_dns.record_present("mail", "test.com", "A", "127.0.0.1", "test")
|
||||||
result = libcloud_dns.record_present("mail", "test.com", "A", "127.0.0.1", "test")
|
self.assertTrue(result)
|
||||||
self.assertTrue(result)
|
|
||||||
create_patch.assert_called_with('mail', "zone1", "A", "127.0.0.1", "test")
|
|
||||||
|
|
||||||
def test_absent_record_exists(self):
|
def test_absent_record_exists(self):
|
||||||
"""
|
"""
|
||||||
Try and deny a record that already exists
|
Try and deny a record that already exists
|
||||||
"""
|
"""
|
||||||
with patch.object(MockDnsModule, 'delete_record', MagicMock(return_value=True)) as create_patch:
|
result = libcloud_dns.record_absent("www", "test.com", "A", "127.0.0.1", "test")
|
||||||
result = libcloud_dns.record_absent("www", "test.com", "A", "127.0.0.1", "test")
|
self.assertTrue(result)
|
||||||
self.assertTrue(result)
|
|
||||||
create_patch.assert_called_with('zone1', 0, 'test')
|
|
||||||
|
|
||||||
def test_absent_record_does_not_exist(self):
|
def test_absent_record_does_not_exist(self):
|
||||||
"""
|
"""
|
||||||
Try and deny a record that already exists
|
Try and deny a record that already exists
|
||||||
"""
|
"""
|
||||||
with patch.object(MockDnsModule, 'delete_record') as create_patch:
|
result = libcloud_dns.record_absent("mail", "test.com", "A", "127.0.0.1", "test")
|
||||||
result = libcloud_dns.record_absent("mail", "test.com", "A", "127.0.0.1", "test")
|
self.assertTrue(result)
|
||||||
self.assertTrue(result)
|
|
||||||
self.assertFalse(create_patch.called)
|
|
||||||
|
|
||||||
def test_present_zone_not_found(self):
|
def test_present_zone_not_found(self):
|
||||||
"""
|
"""
|
||||||
@ -159,39 +151,29 @@ class LibcloudDnsModuleTestCase(ModuleTestCase):
|
|||||||
"""
|
"""
|
||||||
Assert that a zone is present (that did not exist)
|
Assert that a zone is present (that did not exist)
|
||||||
"""
|
"""
|
||||||
with patch.object(MockDnsModule, 'create_zone') as create_patch:
|
result = libcloud_dns.zone_present('testing.com', 'master', 'test1')
|
||||||
result = libcloud_dns.zone_present('testing.com', 'master', 'test1')
|
self.assertTrue(result)
|
||||||
self.assertTrue(result)
|
|
||||||
self.assertTrue(create_patch.called)
|
|
||||||
create_patch.assert_called_with('testing.com', 'test1', 'master')
|
|
||||||
|
|
||||||
def test_zone_already_present(self):
|
def test_zone_already_present(self):
|
||||||
"""
|
"""
|
||||||
Assert that a zone is present (that did exist)
|
Assert that a zone is present (that did exist)
|
||||||
"""
|
"""
|
||||||
with patch.object(MockDnsModule, 'create_zone') as create_patch:
|
result = libcloud_dns.zone_present('test.com', 'master', 'test1')
|
||||||
result = libcloud_dns.zone_present('test.com', 'master', 'test1')
|
self.assertTrue(result)
|
||||||
self.assertTrue(result)
|
|
||||||
self.assertFalse(create_patch.called)
|
|
||||||
|
|
||||||
def test_zone_absent(self):
|
def test_zone_absent(self):
|
||||||
"""
|
"""
|
||||||
Assert that a zone that did exist is absent
|
Assert that a zone that did exist is absent
|
||||||
"""
|
"""
|
||||||
with patch.object(MockDnsModule, 'delete_zone') as create_patch:
|
result = libcloud_dns.zone_absent('test.com', 'test1')
|
||||||
result = libcloud_dns.zone_absent('test.com', 'test1')
|
self.assertTrue(result)
|
||||||
self.assertTrue(result)
|
|
||||||
self.assertTrue(create_patch.called)
|
|
||||||
create_patch.assert_called_with('zone1', 'test1')
|
|
||||||
|
|
||||||
def test_zone_already_absent(self):
|
def test_zone_already_absent(self):
|
||||||
"""
|
"""
|
||||||
Assert that a zone that did not exist is absent
|
Assert that a zone that did not exist is absent
|
||||||
"""
|
"""
|
||||||
with patch.object(MockDnsModule, 'delete_zone') as create_patch:
|
result = libcloud_dns.zone_absent('testing.com', 'test1')
|
||||||
result = libcloud_dns.zone_absent('testing.com', 'test1')
|
self.assertTrue(result)
|
||||||
self.assertTrue(result)
|
|
||||||
self.assertFalse(create_patch.called)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from unit import run_tests
|
from unit import run_tests
|
||||||
|
Loading…
Reference in New Issue
Block a user