Merge pull request #15536 from chouseknecht/azure_common

Set the name of the user_agent in each mangement client.
pull/12534/merge
Chris Houseknecht 9 years ago
commit a6e83599d6

@ -36,6 +36,7 @@ AZURE_COMMON_ARGS = dict(
tenant=dict(type='str', no_log=True), tenant=dict(type='str', no_log=True),
ad_user=dict(type='str', no_log=True), ad_user=dict(type='str', no_log=True),
password=dict(type='str', no_log=True), password=dict(type='str', no_log=True),
# debug=dict(type='bool', default=False),
) )
AZURE_CREDENTIAL_ENV_MAPPING = dict( AZURE_CREDENTIAL_ENV_MAPPING = dict(
@ -57,6 +58,8 @@ AZURE_COMMON_REQUIRED_IF = [
('log_mode', 'file', ['log_path']) ('log_mode', 'file', ['log_path'])
] ]
ANSIBLE_USER_AGENT = 'Ansible-Deploy'
CIDR_PATTERN = re.compile("(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1" CIDR_PATTERN = re.compile("(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1"
"[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))") "[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))")
@ -143,6 +146,7 @@ class AzureRMModuleBase(object):
self._compute_client = None self._compute_client = None
self.check_mode = self.module.check_mode self.check_mode = self.module.check_mode
self.facts_module = facts_module self.facts_module = facts_module
self.debug = self.module.params.get('debug')
# authenticate # authenticate
self.credentials = self._get_credentials(self.module.params) self.credentials = self._get_credentials(self.module.params)
@ -177,22 +181,25 @@ class AzureRMModuleBase(object):
def exec_module(self, **kwargs): def exec_module(self, **kwargs):
self.fail("Error: {0} failed to implement exec_module method.".format(self.__class__.__name__)) self.fail("Error: {0} failed to implement exec_module method.".format(self.__class__.__name__))
def fail(self, msg): def fail(self, msg, **kwargs):
''' '''
Shortcut for calling module.fail() Shortcut for calling module.fail()
:param msg: Errot message text. :param msg: Error message text.
:param kwargs: Any key=value pairs
:return: None :return: None
''' '''
self.module.fail_json(msg=msg) self.module.fail_json(msg=msg, **kwargs)
def log(self, msg, pretty_print=False): def log(self, msg, pretty_print=False):
pass pass
# log_file = open('azure_rm.log', 'a') # Use only during module development
# if pretty_print: # if self.debug:
# log_file.write(json.dumps(msg, indent=4, sort_keys=True)) # log_file = open('azure_rm.log', 'a')
# else: # if pretty_print:
# log_file.write(msg + u'\n') # log_file.write(json.dumps(msg, indent=4, sort_keys=True))
# else:
# log_file.write(msg + u'\n')
def validate_tags(self, tags): def validate_tags(self, tags):
''' '''
@ -398,7 +405,7 @@ class AzureRMModuleBase(object):
serializer = Serializer() serializer = Serializer()
return serializer.body(obj, class_name) return serializer.body(obj, class_name)
def get_poller_result(self, poller): def get_poller_result(self, poller, wait=20):
''' '''
Consistent method of waiting on and retrieving results from Azure's long poller Consistent method of waiting on and retrieving results from Azure's long poller
@ -406,7 +413,7 @@ class AzureRMModuleBase(object):
:return object resulting from the original request :return object resulting from the original request
''' '''
try: try:
delay = 20 delay = wait
while not poller.done(): while not poller.done():
self.log("Waiting for {0} sec".format(delay)) self.log("Waiting for {0} sec".format(delay))
poller.wait(timeout=delay) poller.wait(timeout=delay)
@ -589,8 +596,9 @@ class AzureRMModuleBase(object):
def storage_client(self): def storage_client(self):
self.log('Getting storage client...') self.log('Getting storage client...')
if not self._storage_client: if not self._storage_client:
self._storage_client = StorageManagementClient( config = StorageManagementClientConfiguration(self.azure_credentials, self.subscription_id)
StorageManagementClientConfiguration(self.azure_credentials, self.subscription_id)) config.add_user_agent(ANSIBLE_USER_AGENT)
self._storage_client = StorageManagementClient(config)
self._register('Microsoft.Storage') self._register('Microsoft.Storage')
return self._storage_client return self._storage_client
@ -598,8 +606,9 @@ class AzureRMModuleBase(object):
def network_client(self): def network_client(self):
self.log('Getting network client') self.log('Getting network client')
if not self._network_client: if not self._network_client:
self._network_client = NetworkManagementClient( config = NetworkManagementClientConfiguration(self.azure_credentials, self.subscription_id)
NetworkManagementClientConfiguration(self.azure_credentials, self.subscription_id)) config.add_user_agent(ANSIBLE_USER_AGENT)
self._network_client = NetworkManagementClient(config)
self._register('Microsoft.Network') self._register('Microsoft.Network')
return self._network_client return self._network_client
@ -607,15 +616,17 @@ class AzureRMModuleBase(object):
def rm_client(self): def rm_client(self):
self.log('Getting resource manager client') self.log('Getting resource manager client')
if not self._resource_client: if not self._resource_client:
self._resource_client = ResourceManagementClient( config = ResourceManagementClientConfiguration(self.azure_credentials, self.subscription_id)
ResourceManagementClientConfiguration(self.azure_credentials, self.subscription_id)) config.add_user_agent(ANSIBLE_USER_AGENT)
self._resource_client = ResourceManagementClient(config)
return self._resource_client return self._resource_client
@property @property
def compute_client(self): def compute_client(self):
self.log('Getting compute client') self.log('Getting compute client')
if not self._compute_client: if not self._compute_client:
self._compute_client = ComputeManagementClient( config = ComputeManagementClientConfiguration(self.azure_credentials, self.subscription_id)
ComputeManagementClientConfiguration(self.azure_credentials, self.subscription_id)) config.add_user_agent(ANSIBLE_USER_AGENT)
self._compute_client = ComputeManagementClient(config)
self._register('Microsoft.Compute') self._register('Microsoft.Compute')
return self._compute_client return self._compute_client

Loading…
Cancel
Save