mirror of
https://github.com/github/octodns.git
synced 2024-05-11 05:55:00 +00:00
Use uuid4 for zone name in GoogleCloudProvider
use uuid4().hex to ensure unique zone_name generation and thereby streamline with the other providers.
This commit is contained in:
@@ -5,10 +5,10 @@
|
|||||||
from __future__ import absolute_import, division, print_function, \
|
from __future__ import absolute_import, division, print_function, \
|
||||||
unicode_literals
|
unicode_literals
|
||||||
|
|
||||||
import re
|
|
||||||
import shlex
|
import shlex
|
||||||
import time
|
import time
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
from google.cloud import dns
|
from google.cloud import dns
|
||||||
|
|
||||||
@@ -128,21 +128,8 @@ class GoogleCloudProvider(BaseProvider):
|
|||||||
"""
|
"""
|
||||||
# Zone name must begin with a letter, end with a letter or digit,
|
# Zone name must begin with a letter, end with a letter or digit,
|
||||||
# and only contain lowercase letters, digits or dashes
|
# and only contain lowercase letters, digits or dashes
|
||||||
zone_name = re.sub("[^a-z0-9-]", "",
|
zone_name = '{}-{}'.format(
|
||||||
dns_name[:-1].replace('.', "-"))
|
dns_name[:-1].replace('.', '-'), uuid4().hex)
|
||||||
|
|
||||||
# Check if there is another zone in google cloud which has the same
|
|
||||||
# name as the new one
|
|
||||||
while zone_name in [z.name for z in self.gcloud_zones.values()]:
|
|
||||||
# If there is a zone in google cloud alredy, then try suffixing the
|
|
||||||
# name with a -i where i is a number which keeps increasing until
|
|
||||||
# a free name has been reached.
|
|
||||||
m = re.match("^(.+)-([0-9]+$)", zone_name)
|
|
||||||
if m:
|
|
||||||
i = int(m.group(2)) + 1
|
|
||||||
zone_name = "{}-{!s}".format(m.group(1), i)
|
|
||||||
else:
|
|
||||||
zone_name += "-2"
|
|
||||||
|
|
||||||
gcloud_zone = self.gcloud_client.zone(
|
gcloud_zone = self.gcloud_client.zone(
|
||||||
name=zone_name,
|
name=zone_name,
|
||||||
|
|||||||
@@ -427,29 +427,3 @@ class TestGoogleCloudProvider(TestCase):
|
|||||||
|
|
||||||
mock_zone.create.assert_called()
|
mock_zone.create.assert_called()
|
||||||
provider.gcloud_client.zone.assert_called()
|
provider.gcloud_client.zone.assert_called()
|
||||||
provider.gcloud_client.zone.assert_called_once_with(
|
|
||||||
dns_name=u'nonexistant.zone.mock', name=u'nonexistant-zone-moc')
|
|
||||||
|
|
||||||
def test__create_zone_with_duplicate_names(self):
|
|
||||||
|
|
||||||
def _create_dummy_zone(name, dns_name):
|
|
||||||
return DummyGoogleCloudZone(name=name, dns_name=dns_name)
|
|
||||||
|
|
||||||
provider = self._get_provider()
|
|
||||||
|
|
||||||
provider.gcloud_client = Mock()
|
|
||||||
provider.gcloud_client.zone = Mock(side_effect=_create_dummy_zone)
|
|
||||||
provider.gcloud_client.list_zones = Mock(
|
|
||||||
return_value=DummyIterator([]))
|
|
||||||
|
|
||||||
_gcloud_zones = {
|
|
||||||
'unit-tests': DummyGoogleCloudZone("a.unit-tests.", "unit-tests")
|
|
||||||
}
|
|
||||||
|
|
||||||
provider._gcloud_zones = _gcloud_zones
|
|
||||||
|
|
||||||
test_zone_1 = provider._create_gcloud_zone("unit.tests.")
|
|
||||||
self.assertEqual(test_zone_1.name, "unit-tests-2")
|
|
||||||
|
|
||||||
test_zone_2 = provider._create_gcloud_zone("unit.tests.")
|
|
||||||
self.assertEqual(test_zone_2.name, "unit-tests-3")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user