add user-agent to Azure API calls (#31872)
* addi Ansible user-agent in Azure API calls * fix import error
This commit is contained in:
parent
905d71d46a
commit
b3cf1e39bb
1 changed files with 81 additions and 59 deletions
|
@ -29,6 +29,10 @@ from os.path import expanduser
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.six.moves import configparser
|
from ansible.module_utils.six.moves import configparser
|
||||||
import ansible.module_utils.six.moves.urllib.parse as urlparse
|
import ansible.module_utils.six.moves.urllib.parse as urlparse
|
||||||
|
try:
|
||||||
|
from ansible.release import __version__ as ansible_version
|
||||||
|
except ImportError:
|
||||||
|
ansible_version = 'unknown'
|
||||||
|
|
||||||
AZURE_COMMON_ARGS = dict(
|
AZURE_COMMON_ARGS = dict(
|
||||||
cli_default_profile=dict(type='bool'),
|
cli_default_profile=dict(type='bool'),
|
||||||
|
@ -64,7 +68,7 @@ AZURE_COMMON_REQUIRED_IF = [
|
||||||
('log_mode', 'file', ['log_path'])
|
('log_mode', 'file', ['log_path'])
|
||||||
]
|
]
|
||||||
|
|
||||||
ANSIBLE_USER_AGENT = 'Ansible-Deploy'
|
ANSIBLE_USER_AGENT = 'Ansible/{0}'.format(ansible_version)
|
||||||
|
|
||||||
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]))")
|
||||||
|
@ -146,20 +150,44 @@ def azure_id_to_dict(id):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
AZURE_EXPECTED_VERSIONS = dict(
|
AZURE_PKG_VERSIONS = {
|
||||||
storage_client_version="1.0.0",
|
StorageManagementClient.__name__: {
|
||||||
compute_client_version="1.0.0",
|
'package_name': 'storage',
|
||||||
network_client_version="1.0.0",
|
'expected_version': '1.0.0',
|
||||||
resource_client_version="1.1.0",
|
'installed_version': storage_client_version
|
||||||
dns_client_version="1.0.1",
|
},
|
||||||
web_client_version="0.32.0"
|
ComputeManagementClient.__name__: {
|
||||||
)
|
'package_name': 'compute',
|
||||||
|
'expected_version': '1.0.0',
|
||||||
|
'installed_version': compute_client_version
|
||||||
|
},
|
||||||
|
NetworkManagementClient.__name__: {
|
||||||
|
'package_name': 'network',
|
||||||
|
'expected_version': '1.0.0',
|
||||||
|
'installed_version': network_client_version
|
||||||
|
},
|
||||||
|
ResourceManagementClient.__name__: {
|
||||||
|
'package_name': 'resource',
|
||||||
|
'expected_version': '1.1.0',
|
||||||
|
'installed_version': resource_client_version
|
||||||
|
},
|
||||||
|
DnsManagementClient.__name__: {
|
||||||
|
'package_name': 'dns',
|
||||||
|
'expected_version': '1.0.1',
|
||||||
|
'installed_version': dns_client_version
|
||||||
|
},
|
||||||
|
WebSiteManagementClient.__name__: {
|
||||||
|
'package_name': 'web',
|
||||||
|
'expected_version': '0.32.0',
|
||||||
|
'installed_version': web_client_version
|
||||||
|
},
|
||||||
|
} if HAS_AZURE else {}
|
||||||
|
|
||||||
|
|
||||||
AZURE_MIN_RELEASE = '2.0.0'
|
AZURE_MIN_RELEASE = '2.0.0'
|
||||||
|
|
||||||
|
|
||||||
class AzureRMModuleBase(object):
|
class AzureRMModuleBase(object):
|
||||||
|
|
||||||
def __init__(self, derived_arg_spec, bypass_checks=False, no_log=False,
|
def __init__(self, derived_arg_spec, bypass_checks=False, no_log=False,
|
||||||
check_invalid_arguments=True, mutually_exclusive=None, required_together=None,
|
check_invalid_arguments=True, mutually_exclusive=None, required_together=None,
|
||||||
required_one_of=None, add_file_common_args=False, supports_check_mode=False,
|
required_one_of=None, add_file_common_args=False, supports_check_mode=False,
|
||||||
|
@ -274,12 +302,17 @@ class AzureRMModuleBase(object):
|
||||||
res = self.exec_module(**self.module.params)
|
res = self.exec_module(**self.module.params)
|
||||||
self.module.exit_json(**res)
|
self.module.exit_json(**res)
|
||||||
|
|
||||||
def check_client_version(self, client_name, client_version, expected_version):
|
def check_client_version(self, client_type):
|
||||||
# Ensure Azure modules are at least 2.0.0rc5.
|
# Ensure Azure modules are at least 2.0.0rc5.
|
||||||
if Version(client_version) < Version(expected_version):
|
package_version = AZURE_PKG_VERSIONS.get(client_type.__name__, None)
|
||||||
self.fail("Installed {0} client version is {1}. The supported version is {2}. Try "
|
if package_version is not None:
|
||||||
"`pip install azure>={3} --upgrade`".format(client_name, client_version, expected_version,
|
client_name = package_version.get('package_name')
|
||||||
AZURE_MIN_RELEASE))
|
client_version = package_version.get('installed_version')
|
||||||
|
expected_version = package_version.get('expected_version')
|
||||||
|
if Version(client_version) < Version(expected_version):
|
||||||
|
self.fail("Installed {0} client version is {1}. The supported version is {2}. Try "
|
||||||
|
"`pip install azure>={3} --upgrade`".format(client_name, client_version, expected_version,
|
||||||
|
AZURE_MIN_RELEASE))
|
||||||
|
|
||||||
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__))
|
||||||
|
@ -677,88 +710,77 @@ class AzureRMModuleBase(object):
|
||||||
|
|
||||||
return self.get_poller_result(poller)
|
return self.get_poller_result(poller)
|
||||||
|
|
||||||
|
def get_mgmt_svc_client(self, client_type, base_url=None, api_version=None):
|
||||||
|
self.log('Getting management service client {0}'.format(client_type.__name__))
|
||||||
|
self.check_client_version(client_type)
|
||||||
|
if api_version:
|
||||||
|
client = client_type(self.azure_credentials,
|
||||||
|
self.subscription_id,
|
||||||
|
api_version=api_version,
|
||||||
|
base_url=base_url)
|
||||||
|
else:
|
||||||
|
client = client_type(self.azure_credentials,
|
||||||
|
self.subscription_id,
|
||||||
|
base_url=base_url)
|
||||||
|
|
||||||
|
client.config.add_user_agent(ANSIBLE_USER_AGENT)
|
||||||
|
|
||||||
|
return client
|
||||||
|
|
||||||
@property
|
@property
|
||||||
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.check_client_version('storage', storage_client_version, AZURE_EXPECTED_VERSIONS['storage_client_version'])
|
self._storage_client = self.get_mgmt_svc_client(StorageManagementClient,
|
||||||
self._storage_client = StorageManagementClient(
|
base_url=self._cloud_environment.endpoints.resource_manager,
|
||||||
self.azure_credentials,
|
api_version='2017-06-01')
|
||||||
self.subscription_id,
|
|
||||||
base_url=self._cloud_environment.endpoints.resource_manager,
|
|
||||||
api_version='2017-06-01'
|
|
||||||
)
|
|
||||||
return self._storage_client
|
return self._storage_client
|
||||||
|
|
||||||
@property
|
@property
|
||||||
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.check_client_version('network', network_client_version, AZURE_EXPECTED_VERSIONS['network_client_version'])
|
self._network_client = self.get_mgmt_svc_client(NetworkManagementClient,
|
||||||
self._network_client = NetworkManagementClient(
|
base_url=self._cloud_environment.endpoints.resource_manager,
|
||||||
self.azure_credentials,
|
api_version='2017-06-01')
|
||||||
self.subscription_id,
|
|
||||||
base_url=self._cloud_environment.endpoints.resource_manager,
|
|
||||||
api_version='2017-06-01'
|
|
||||||
)
|
|
||||||
return self._network_client
|
return self._network_client
|
||||||
|
|
||||||
@property
|
@property
|
||||||
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.check_client_version('resource', resource_client_version, AZURE_EXPECTED_VERSIONS['resource_client_version'])
|
self._resource_client = self.get_mgmt_svc_client(ResourceManagementClient,
|
||||||
self._resource_client = ResourceManagementClient(
|
base_url=self._cloud_environment.endpoints.resource_manager,
|
||||||
self.azure_credentials,
|
api_version='2017-05-10')
|
||||||
self.subscription_id,
|
|
||||||
base_url=self._cloud_environment.endpoints.resource_manager,
|
|
||||||
api_version='2017-05-10'
|
|
||||||
)
|
|
||||||
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.check_client_version('compute', compute_client_version, AZURE_EXPECTED_VERSIONS['compute_client_version'])
|
self._compute_client = self.get_mgmt_svc_client(ComputeManagementClient,
|
||||||
self._compute_client = ComputeManagementClient(
|
base_url=self._cloud_environment.endpoints.resource_manager,
|
||||||
self.azure_credentials,
|
api_version='2017-03-30')
|
||||||
self.subscription_id,
|
|
||||||
base_url=self._cloud_environment.endpoints.resource_manager,
|
|
||||||
api_version='2017-03-30'
|
|
||||||
)
|
|
||||||
return self._compute_client
|
return self._compute_client
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def dns_client(self):
|
def dns_client(self):
|
||||||
self.log('Getting dns client')
|
self.log('Getting dns client')
|
||||||
if not self._dns_client:
|
if not self._dns_client:
|
||||||
self.check_client_version('dns', dns_client_version, AZURE_EXPECTED_VERSIONS['dns_client_version'])
|
self._dns_client = self.get_mgmt_svc_client(DnsManagementClient,
|
||||||
self._dns_client = DnsManagementClient(
|
base_url=self._cloud_environment.endpoints.resource_manager)
|
||||||
self.azure_credentials,
|
|
||||||
self.subscription_id,
|
|
||||||
base_url=self._cloud_environment.endpoints.resource_manager,
|
|
||||||
)
|
|
||||||
return self._dns_client
|
return self._dns_client
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def web_client(self):
|
def web_client(self):
|
||||||
self.log('Getting web client')
|
self.log('Getting web client')
|
||||||
if not self._web_client:
|
if not self._web_client:
|
||||||
self.check_client_version('web', web_client_version, AZURE_EXPECTED_VERSIONS['web_client_version'])
|
self._web_client = self.get_mgmt_svc_client(WebSiteManagementClient, base_url=self.base_url)
|
||||||
self._web_client = WebSiteManagementClient(
|
|
||||||
credentials=self.azure_credentials,
|
|
||||||
subscription_id=self.subscription_id,
|
|
||||||
base_url=self.base_url
|
|
||||||
)
|
|
||||||
return self._web_client
|
return self._web_client
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def containerservice_client(self):
|
def containerservice_client(self):
|
||||||
self.log('Getting container service client')
|
self.log('Getting container service client')
|
||||||
if not self._containerservice_client:
|
if not self._containerservice_client:
|
||||||
self._containerservice_client = ContainerServiceClient(
|
self._containerservice_client = self.get_mgmt_svc_client(ContainerServiceClient)
|
||||||
self.azure_credentials,
|
|
||||||
self.subscription_id
|
|
||||||
)
|
|
||||||
return self._containerservice_client
|
return self._containerservice_client
|
||||||
|
|
Loading…
Reference in a new issue