mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-01 06:41:26 -07:00
Getting latest API version if not specified in azure_rm_resource (#54022)
This commit is contained in:
parent
0bf14d61d0
commit
5b79631380
3 changed files with 53 additions and 8 deletions
|
@ -30,7 +30,6 @@ options:
|
||||||
api_version:
|
api_version:
|
||||||
description:
|
description:
|
||||||
- Specific API version to be used.
|
- Specific API version to be used.
|
||||||
required: yes
|
|
||||||
provider:
|
provider:
|
||||||
description:
|
description:
|
||||||
- Provider type.
|
- Provider type.
|
||||||
|
@ -163,8 +162,7 @@ class AzureRMResource(AzureRMModuleBase):
|
||||||
default=[]
|
default=[]
|
||||||
),
|
),
|
||||||
api_version=dict(
|
api_version=dict(
|
||||||
type='str',
|
type='str'
|
||||||
required=True
|
|
||||||
),
|
),
|
||||||
method=dict(
|
method=dict(
|
||||||
type='str',
|
type='str',
|
||||||
|
@ -260,6 +258,25 @@ class AzureRMResource(AzureRMModuleBase):
|
||||||
|
|
||||||
if orphan is not None:
|
if orphan is not None:
|
||||||
self.url += '/' + orphan
|
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 = {}
|
||||||
query_parameters['api-version'] = self.api_version
|
query_parameters['api-version'] = self.api_version
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,6 @@ options:
|
||||||
api_version:
|
api_version:
|
||||||
description:
|
description:
|
||||||
- Specific API version to be used.
|
- Specific API version to be used.
|
||||||
required: yes
|
|
||||||
provider:
|
provider:
|
||||||
description:
|
description:
|
||||||
- Provider type, should be specified in no URL is given
|
- Provider type, should be specified in no URL is given
|
||||||
|
@ -121,8 +120,7 @@ class AzureRMResourceFacts(AzureRMModuleBase):
|
||||||
default=[]
|
default=[]
|
||||||
),
|
),
|
||||||
api_version=dict(
|
api_version=dict(
|
||||||
type='str',
|
type='str'
|
||||||
required=True
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
# store the results of the module operation
|
# store the results of the module operation
|
||||||
|
@ -176,6 +174,24 @@ class AzureRMResourceFacts(AzureRMModuleBase):
|
||||||
if orphan is not None:
|
if orphan is not None:
|
||||||
self.url += '/' + orphan
|
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
|
self.results['url'] = self.url
|
||||||
|
|
||||||
query_parameters = {}
|
query_parameters = {}
|
||||||
|
|
|
@ -86,6 +86,19 @@
|
||||||
- output.response[0]['name'] != None
|
- output.response[0]['name'] != None
|
||||||
- output.response | length >= 1
|
- 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
|
- name: Create storage account that requires LRO polling
|
||||||
azure_rm_resource:
|
azure_rm_resource:
|
||||||
polling_timeout: 600
|
polling_timeout: 600
|
||||||
|
@ -123,12 +136,11 @@
|
||||||
assert:
|
assert:
|
||||||
that: keys['response']['keys'][0]['value'] | length > 0
|
that: keys['response']['keys'][0]['value'] | length > 0
|
||||||
|
|
||||||
- name: Delete storage
|
- name: Delete storage - without API version
|
||||||
azure_rm_resource:
|
azure_rm_resource:
|
||||||
polling_timeout: 600
|
polling_timeout: 600
|
||||||
polling_interval: 60
|
polling_interval: 60
|
||||||
method: DELETE
|
method: DELETE
|
||||||
api_version: '2018-07-01'
|
|
||||||
resource_group: "{{ resource_group }}"
|
resource_group: "{{ resource_group }}"
|
||||||
provider: Storage
|
provider: Storage
|
||||||
resource_type: storageAccounts
|
resource_type: storageAccounts
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue