mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
support lro in azure_rm_resource (#49919)
This commit is contained in:
parent
e09196f760
commit
de3d188cdd
4 changed files with 93 additions and 10 deletions
|
@ -2,15 +2,23 @@
|
||||||
#
|
#
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
from ansible.module_utils.ansible_release import __version__ as ANSIBLE_VERSION
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from msrestazure.azure_exceptions import CloudError
|
from msrestazure.azure_exceptions import CloudError
|
||||||
from msrestazure.azure_configuration import AzureConfiguration
|
from msrestazure.azure_configuration import AzureConfiguration
|
||||||
from msrest.service_client import ServiceClient
|
from msrest.service_client import ServiceClient
|
||||||
|
from msrest.pipeline import ClientRawResponse
|
||||||
|
from msrest.polling import LROPoller
|
||||||
|
from msrestazure.polling.arm_polling import ARMPolling
|
||||||
|
import uuid
|
||||||
import json
|
import json
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# This is handled in azure_rm_common
|
# This is handled in azure_rm_common
|
||||||
AzureConfiguration = object
|
AzureConfiguration = object
|
||||||
|
|
||||||
|
ANSIBLE_USER_AGENT = 'Ansible/{0}'.format(ANSIBLE_VERSION)
|
||||||
|
|
||||||
|
|
||||||
class GenericRestClientConfiguration(AzureConfiguration):
|
class GenericRestClientConfiguration(AzureConfiguration):
|
||||||
|
|
||||||
|
@ -25,8 +33,7 @@ class GenericRestClientConfiguration(AzureConfiguration):
|
||||||
|
|
||||||
super(GenericRestClientConfiguration, self).__init__(base_url)
|
super(GenericRestClientConfiguration, self).__init__(base_url)
|
||||||
|
|
||||||
self.add_user_agent('genericrestclient/1.0')
|
self.add_user_agent(ANSIBLE_USER_AGENT)
|
||||||
self.add_user_agent('Azure-SDK-For-Python')
|
|
||||||
|
|
||||||
self.credentials = credentials
|
self.credentials = credentials
|
||||||
self.subscription_id = subscription_id
|
self.subscription_id = subscription_id
|
||||||
|
@ -39,12 +46,17 @@ class GenericRestClient(object):
|
||||||
self._client = ServiceClient(self.config.credentials, self.config)
|
self._client = ServiceClient(self.config.credentials, self.config)
|
||||||
self.models = None
|
self.models = None
|
||||||
|
|
||||||
def query(self, url, method, query_parameters, header_parameters, body, expected_status_codes):
|
def query(self, url, method, query_parameters, header_parameters, body, expected_status_codes, polling_timeout, polling_interval):
|
||||||
# Construct and send request
|
# Construct and send request
|
||||||
operation_config = {}
|
operation_config = {}
|
||||||
|
|
||||||
request = None
|
request = None
|
||||||
|
|
||||||
|
if header_parameters is None:
|
||||||
|
header_parameters = {}
|
||||||
|
|
||||||
|
header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
|
||||||
|
|
||||||
if method == 'GET':
|
if method == 'GET':
|
||||||
request = self._client.get(url, query_parameters)
|
request = self._client.get(url, query_parameters)
|
||||||
elif method == 'PUT':
|
elif method == 'PUT':
|
||||||
|
@ -66,5 +78,20 @@ class GenericRestClient(object):
|
||||||
exp = CloudError(response)
|
exp = CloudError(response)
|
||||||
exp.request_id = response.headers.get('x-ms-request-id')
|
exp.request_id = response.headers.get('x-ms-request-id')
|
||||||
raise exp
|
raise exp
|
||||||
|
elif response.status_code == 202 and polling_timeout > 0:
|
||||||
|
def get_long_running_output(response):
|
||||||
|
return response
|
||||||
|
poller = LROPoller(self._client,
|
||||||
|
ClientRawResponse(None, response),
|
||||||
|
get_long_running_output,
|
||||||
|
ARMPolling(polling_interval, **operation_config))
|
||||||
|
response = self.get_poller_result(poller, polling_timeout)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
def get_poller_result(self, poller, timeout):
|
||||||
|
try:
|
||||||
|
poller.wait(timeout=timeout)
|
||||||
|
return poller.result()
|
||||||
|
except Exception as exc:
|
||||||
|
raise
|
||||||
|
|
|
@ -78,6 +78,18 @@ options:
|
||||||
- If enabled, idempotency check will be done by using GET method first and then comparing with I(body)
|
- If enabled, idempotency check will be done by using GET method first and then comparing with I(body)
|
||||||
default: no
|
default: no
|
||||||
type: bool
|
type: bool
|
||||||
|
polling_timeout:
|
||||||
|
description:
|
||||||
|
- If enabled, idempotency check will be done by using GET method first and then comparing with I(body)
|
||||||
|
default: 0
|
||||||
|
type: int
|
||||||
|
version_added: "2.8"
|
||||||
|
polling_interval:
|
||||||
|
description:
|
||||||
|
- If enabled, idempotency check will be done by using GET method first and then comparing with I(body)
|
||||||
|
default: 60
|
||||||
|
type: int
|
||||||
|
version_added: "2.8"
|
||||||
state:
|
state:
|
||||||
description:
|
description:
|
||||||
- Assert the state of the resource. Use C(present) to create or update resource or C(absent) to delete resource.
|
- Assert the state of the resource. Use C(present) to create or update resource or C(absent) to delete resource.
|
||||||
|
@ -171,6 +183,14 @@ class AzureRMResource(AzureRMModuleBase):
|
||||||
type='bool',
|
type='bool',
|
||||||
default=False
|
default=False
|
||||||
),
|
),
|
||||||
|
polling_timeout=dict(
|
||||||
|
type='int',
|
||||||
|
default=0
|
||||||
|
),
|
||||||
|
polling_interval=dict(
|
||||||
|
type='int',
|
||||||
|
default=60
|
||||||
|
),
|
||||||
state=dict(
|
state=dict(
|
||||||
type='str',
|
type='str',
|
||||||
default='present',
|
default='present',
|
||||||
|
@ -195,6 +215,8 @@ class AzureRMResource(AzureRMModuleBase):
|
||||||
self.method = None
|
self.method = None
|
||||||
self.status_code = []
|
self.status_code = []
|
||||||
self.idempotency = False
|
self.idempotency = False
|
||||||
|
self.polling_timeout = None
|
||||||
|
self.polling_interval = None
|
||||||
self.state = None
|
self.state = None
|
||||||
self.body = None
|
self.body = None
|
||||||
super(AzureRMResource, self).__init__(self.module_arg_spec, supports_tags=False)
|
super(AzureRMResource, self).__init__(self.module_arg_spec, supports_tags=False)
|
||||||
|
@ -249,7 +271,7 @@ class AzureRMResource(AzureRMModuleBase):
|
||||||
response = None
|
response = None
|
||||||
|
|
||||||
if self.idempotency:
|
if self.idempotency:
|
||||||
original = self.mgmt_client.query(self.url, "GET", query_parameters, None, None, [200, 404])
|
original = self.mgmt_client.query(self.url, "GET", query_parameters, None, None, [200, 404], 0, 0)
|
||||||
|
|
||||||
if original.status_code == 404:
|
if original.status_code == 404:
|
||||||
if self.state == 'absent':
|
if self.state == 'absent':
|
||||||
|
@ -262,7 +284,14 @@ class AzureRMResource(AzureRMModuleBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if needs_update:
|
if needs_update:
|
||||||
response = self.mgmt_client.query(self.url, self.method, query_parameters, header_parameters, self.body, self.status_code)
|
response = self.mgmt_client.query(self.url,
|
||||||
|
self.method,
|
||||||
|
query_parameters,
|
||||||
|
header_parameters,
|
||||||
|
self.body,
|
||||||
|
self.status_code,
|
||||||
|
self.polling_timeout,
|
||||||
|
self.polling_interval)
|
||||||
if self.state == 'present':
|
if self.state == 'present':
|
||||||
try:
|
try:
|
||||||
response = json.loads(response.text)
|
response = json.loads(response.text)
|
||||||
|
|
|
@ -190,7 +190,7 @@ class AzureRMResourceFacts(AzureRMModuleBase):
|
||||||
header_parameters = {}
|
header_parameters = {}
|
||||||
header_parameters['Content-Type'] = 'application/json; charset=utf-8'
|
header_parameters['Content-Type'] = 'application/json; charset=utf-8'
|
||||||
|
|
||||||
response = self.mgmt_client.query(self.url, "GET", query_parameters, header_parameters, None, [200, 404])
|
response = self.mgmt_client.query(self.url, "GET", query_parameters, header_parameters, None, [200, 404], 0, 0)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = json.loads(response.text)
|
response = json.loads(response.text)
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
set_fact:
|
set_fact:
|
||||||
nsgname: "{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
|
nsgname: "{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
|
||||||
storageaccountname: "stacc{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
|
storageaccountname: "stacc{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
|
||||||
|
dbname: "mdb{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
|
||||||
run_once: yes
|
run_once: yes
|
||||||
|
|
||||||
- name: Call REST API
|
- name: Call REST API
|
||||||
|
@ -64,11 +65,26 @@
|
||||||
resource_name: "{{ nsgname }}"
|
resource_name: "{{ nsgname }}"
|
||||||
register: output
|
register: output
|
||||||
|
|
||||||
- name: Create storage account for Registry
|
- name: Create storage account that requires LRO polling
|
||||||
azure_rm_storageaccount:
|
azure_rm_resource:
|
||||||
|
polling_timeout: 600
|
||||||
|
polling_interval: 60
|
||||||
|
api_version: '2018-07-01'
|
||||||
resource_group: "{{ resource_group }}"
|
resource_group: "{{ resource_group }}"
|
||||||
name: "{{ storageaccountname }}"
|
provider: Storage
|
||||||
type: Standard_LRS
|
resource_type: storageAccounts
|
||||||
|
resource_name: "{{ storageaccountname }}"
|
||||||
|
body:
|
||||||
|
sku:
|
||||||
|
name: Standard_GRS
|
||||||
|
kind: Storage
|
||||||
|
location: eastus
|
||||||
|
register: output
|
||||||
|
|
||||||
|
- name: Assert that storage was successfully created
|
||||||
|
assert:
|
||||||
|
that: "output['response']['name'] == '{{ storageaccountname }}'"
|
||||||
|
|
||||||
|
|
||||||
- name: Try to storage keys -- special case when subresource part has no name
|
- name: Try to storage keys -- special case when subresource part has no name
|
||||||
azure_rm_resource:
|
azure_rm_resource:
|
||||||
|
@ -85,3 +101,14 @@
|
||||||
- name: Assert that key was returned
|
- name: Assert that key was returned
|
||||||
assert:
|
assert:
|
||||||
that: keys['response']['keys'][0]['value'] | length > 0
|
that: keys['response']['keys'][0]['value'] | length > 0
|
||||||
|
|
||||||
|
- name: Delete storage
|
||||||
|
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
|
||||||
|
resource_name: "{{ storageaccountname }}"
|
||||||
|
|
Loading…
Reference in a new issue