From 0b116a57b96c66e4e051e980d372820618937260 Mon Sep 17 00:00:00 2001 From: Robert Reichel Date: Tue, 2 Feb 2021 12:18:40 -0500 Subject: [PATCH] Modify Azure DNS Client logic to lazy load Lazy loading the Azure DNS client ensures that the necessary network calls only occur when it is time to actually do something with the client. --- octodns/provider/azuredns.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/octodns/provider/azuredns.py b/octodns/provider/azuredns.py index a1ef6fe..888f5e3 100644 --- a/octodns/provider/azuredns.py +++ b/octodns/provider/azuredns.py @@ -323,6 +323,22 @@ class AzureProvider(BaseProvider): SUPPORTS = set(('A', 'AAAA', 'CAA', 'CNAME', 'MX', 'NS', 'PTR', 'SRV', 'TXT')) + def _get_dns_client(self): + if not self._dns_client_handle: + # Not initialized yet, we need to do that. + credentials = ServicePrincipalCredentials( + self._dns_client_client_id, + secret=self._dns_client_key, + tenant=self._dns_client_directory_id + ) + self._dns_client_handle = DnsManagementClient( + credentials, + self._dns_client_subscription_id + ) + + return self._dns_client_handle + + def __init__(self, id, client_id, key, directory_id, sub_id, resource_group, *args, **kwargs): self.log = logging.getLogger('AzureProvider[{}]'.format(id)) @@ -330,10 +346,13 @@ class AzureProvider(BaseProvider): 'key=***, directory_id:%s', id, client_id, directory_id) super(AzureProvider, self).__init__(id, *args, **kwargs) - credentials = ServicePrincipalCredentials( - client_id, secret=key, tenant=directory_id - ) - self._dns_client = DnsManagementClient(credentials, sub_id) + # Store necessary initialization params + self._dns_client_handle = None + self._dns_client_client_id = client_id + self._dns_client_key = key + self._dns_client_directory_id = directory_id + self._dns_client_subscription_id = sub_id + self._dns_client = property(_get_dns_client, _set_dns_client) self._resource_group = resource_group self._azure_zones = set()