1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Getting latest API version if not specified in azure_rm_resource (#54022)

This commit is contained in:
Zim Kalinowski 2019-03-20 14:38:18 +08:00 committed by GitHub
parent 0bf14d61d0
commit 5b79631380
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 8 deletions

View file

@ -30,7 +30,6 @@ options:
api_version:
description:
- Specific API version to be used.
required: yes
provider:
description:
- Provider type.
@ -163,8 +162,7 @@ class AzureRMResource(AzureRMModuleBase):
default=[]
),
api_version=dict(
type='str',
required=True
type='str'
),
method=dict(
type='str',
@ -260,6 +258,25 @@ class AzureRMResource(AzureRMModuleBase):
if orphan is not None:
self.url += '/' + orphan
# if api_version was not specified, get latest one
if not self.api_version:
try:
# extract provider and resource type
if "/providers/" in self.url:
provider = self.url.split("/providers/")[1].split("/")[0]
resourceType = self.url.split(provider + "/")[1].split("/")[0]
url = "/subscriptions/" + self.subscription_id + "/providers/" + provider
api_versions = json.loads(self.mgmt_client.query(url, "GET", {'api-version': '2015-01-01'}, None, None, [200], 0, 0).text)
for rt in api_versions['resourceTypes']:
if rt['resourceType'].lower() == resourceType.lower():
self.api_version = rt['apiVersions'][0]
break
if not self.api_version:
self.fail("Couldn't find api version for {0}/{1}".format(provider, resourceType))
except Exception as exc:
self.fail("Failed to obtain API version: {0}".format(str(exc)))
query_parameters = {}
query_parameters['api-version'] = self.api_version

View file

@ -30,7 +30,6 @@ options:
api_version:
description:
- Specific API version to be used.
required: yes
provider:
description:
- Provider type, should be specified in no URL is given
@ -121,8 +120,7 @@ class AzureRMResourceFacts(AzureRMModuleBase):
default=[]
),
api_version=dict(
type='str',
required=True
type='str'
)
)
# store the results of the module operation
@ -176,6 +174,24 @@ class AzureRMResourceFacts(AzureRMModuleBase):
if orphan is not None:
self.url += '/' + orphan
# if api_version was not specified, get latest one
if not self.api_version:
try:
# extract provider and resource type
if "/providers/" in self.url:
provider = self.url.split("/providers/")[1].split("/")[0]
resourceType = self.url.split(provider + "/")[1].split("/")[0]
url = "/subscriptions/" + self.subscription_id + "/providers/" + provider
api_versions = json.loads(self.mgmt_client.query(url, "GET", {'api-version': '2015-01-01'}, None, None, [200], 0, 0).text)
for rt in api_versions['resourceTypes']:
if rt['resourceType'].lower() == resourceType.lower():
self.api_version = rt['apiVersions'][0]
break
if not self.api_version:
self.fail("Couldn't find api version for {0}/{1}".format(provider, resourceType))
except Exception as exc:
self.fail("Failed to obtain API version: {0}".format(str(exc)))
self.results['url'] = self.url
query_parameters = {}

View file

@ -86,6 +86,19 @@
- output.response[0]['name'] != None
- output.response | length >= 1
- name: Try to query a list - same without API version
azure_rm_resource_facts:
resource_group: "{{ resource_group }}"
provider: network
resource_type: networksecuritygroups
register: output
- name: Assert value was returned
assert:
that:
- not output.changed
- output.response[0]['name'] != None
- output.response | length >= 1
- name: Create storage account that requires LRO polling
azure_rm_resource:
polling_timeout: 600
@ -123,12 +136,11 @@
assert:
that: keys['response']['keys'][0]['value'] | length > 0
- name: Delete storage
- name: Delete storage - without API version
azure_rm_resource:
polling_timeout: 600
polling_interval: 60
method: DELETE
api_version: '2018-07-01'
resource_group: "{{ resource_group }}"
provider: Storage
resource_type: storageAccounts