add wait_for_running option in azure_rm_rediscache module (#54976)

This commit is contained in:
Yunge Zhu 2019-04-11 11:17:31 +08:00 committed by GitHub
parent e12846f540
commit ed1334fbe3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 13 deletions

View file

@ -131,6 +131,14 @@ options:
choices: choices:
- primary - primary
- secondary - secondary
wait_for_provisioning:
description:
- Wait till the Azure Cache for Redis instance provisioning_state is Succeeded.
- It takes several minutes for Azure Cache for Redis to be provisioned ready for use after creating/updating/rebooting.
- Set this option to true to wait for provisioning_state. Set to false if you don't care about provisioning_state.
- Poll wait timeout is 60 minutes.
type: bool
default: True
state: state:
description: description:
- Assert the state of the Azure Cache for Redis. - Assert the state of the Azure Cache for Redis.
@ -209,8 +217,8 @@ from ansible.module_utils.azure_rm_common import AzureRMModuleBase
try: try:
from msrestazure.azure_exceptions import CloudError from msrestazure.azure_exceptions import CloudError
from msrest.polling import LROPoller
from msrestazure.azure_operation import AzureOperationPoller from msrestazure.azure_operation import AzureOperationPoller
from msrest.polling import LROPoller
from msrest.serialization import Model from msrest.serialization import Model
from azure.mgmt.redis import RedisManagementClient from azure.mgmt.redis import RedisManagementClient
from azure.mgmt.redis.models import (RedisCreateParameters, RedisUpdateParameters, Sku) from azure.mgmt.redis.models import (RedisCreateParameters, RedisUpdateParameters, Sku)
@ -366,6 +374,10 @@ class AzureRMRedisCaches(AzureRMModuleBase):
regenerate_key=dict( regenerate_key=dict(
type='dict', type='dict',
options=regenerate_key_spec options=regenerate_key_spec
),
wait_for_provisioning=dict(
type='bool',
default='True'
) )
) )
@ -386,6 +398,10 @@ class AzureRMRedisCaches(AzureRMModuleBase):
self.reboot = None self.reboot = None
self.regenerate_key = None self.regenerate_key = None
self.wait_for_provisioning = None
self.wait_for_provisioning_polling_interval_in_seconds = 30
self.wait_for_provisioning_polling_times = 120
self.tags = None self.tags = None
self.results = dict( self.results = dict(
@ -562,9 +578,12 @@ class AzureRMRedisCaches(AzureRMModuleBase):
response = self._client.redis.create(resource_group_name=self.resource_group, response = self._client.redis.create(resource_group_name=self.resource_group,
name=self.name, name=self.name,
parameters=params) parameters=params)
if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller): if isinstance(response, AzureOperationPoller) or isinstance(response, LROPoller):
response = self.get_poller_result(response) response = self.get_poller_result(response)
if self.wait_for_provisioning:
self.wait_for_redis_running()
except CloudError as exc: except CloudError as exc:
self.log('Error attempting to create the Azure Cache for Redis instance.') self.log('Error attempting to create the Azure Cache for Redis instance.')
self.fail( self.fail(
@ -598,9 +617,12 @@ class AzureRMRedisCaches(AzureRMModuleBase):
response = self._client.redis.update(resource_group_name=self.resource_group, response = self._client.redis.update(resource_group_name=self.resource_group,
name=self.name, name=self.name,
parameters=params) parameters=params)
if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller): if isinstance(response, AzureOperationPoller) or isinstance(response, LROPoller):
response = self.get_poller_result(response) response = self.get_poller_result(response)
if self.wait_for_provisioning:
self.wait_for_redis_running()
except CloudError as exc: except CloudError as exc:
self.log('Error attempting to update the Azure Cache for Redis instance.') self.log('Error attempting to update the Azure Cache for Redis instance.')
self.fail( self.fail(
@ -659,6 +681,11 @@ class AzureRMRedisCaches(AzureRMModuleBase):
name=self.name, name=self.name,
reboot_type=self.reboot['reboot_type'], reboot_type=self.reboot['reboot_type'],
shard_id=self.reboot.get('shard_id')) shard_id=self.reboot.get('shard_id'))
if isinstance(response, AzureOperationPoller) or isinstance(response, LROPoller):
response = self.get_poller_result(response)
if self.wait_for_provisioning:
self.wait_for_redis_running()
except CloudError as e: except CloudError as e:
self.log('Error attempting to force reboot the redis cache instance.') self.log('Error attempting to force reboot the redis cache instance.')
self.fail( self.fail(
@ -718,6 +745,24 @@ class AzureRMRedisCaches(AzureRMModuleBase):
subnet_id = self.subnet subnet_id = self.subnet
return subnet_id return subnet_id
def wait_for_redis_running(self):
try:
response = self._client.redis.get(resource_group_name=self.resource_group, name=self.name)
status = response.provisioning_state
polling_times = 0
while polling_times < self.wait_for_provisioning_polling_times:
if status.lower() != "succeeded":
polling_times += 1
time.sleep(self.wait_for_provisioning_polling_interval_in_seconds)
response = self._client.redis.get(resource_group_name=self.resource_group, name=self.name)
status = response.provisioning_state
else:
return True
self.fail("Azure Cache for Redis is not running after 60 mins.")
except CloudError as e:
self.fail("Failed to get Azure Cache for Redis: {0}".format(str(e)))
def main(): def main():
"""Main execution""" """Main execution"""

View file

@ -13,6 +13,7 @@
sku: sku:
name: basic name: basic
size: C1 size: C1
wait_for_provisioning: False
check_mode: yes check_mode: yes
register: output register: output
@ -28,6 +29,7 @@
sku: sku:
name: basic name: basic
size: C1 size: C1
wait_for_provisioning: False
register: output register: output
- name: Assert creating redis cache - name: Assert creating redis cache
@ -59,6 +61,7 @@
sku: sku:
name: basic name: basic
size: C1 size: C1
wait_for_provisioning: False
register: output register: output
- name: assert output not changed - name: assert output not changed
@ -92,6 +95,7 @@
enable_non_ssl_port: true enable_non_ssl_port: true
tags: tags:
testing: foo testing: foo
wait_for_provisioning: True
register: output register: output
- name: assert output changed - name: assert output changed
@ -106,7 +110,7 @@
sku: sku:
name: basic name: basic
size: C1 size: C1
enable_non_ssl_port: true enable_non_ssl_port: True
maxmemory_policy: allkeys_lru maxmemory_policy: allkeys_lru
tags: tags:
testing: foo testing: foo
@ -126,21 +130,13 @@
size: C1 size: C1
tags: tags:
testing: foo testing: foo
wait_for_provisioning: True
register: output register: output
- assert: - assert:
that: that:
- output.changed - output.changed
- name: Wait for Redis provisioning to complete
azure_rm_rediscache_facts:
resource_group: "{{ resource_group }}"
name: "{{ redis_name }}"
register: facts
until: facts.rediscaches[0]['provisioning_state'] == 'Succeeded'
retries: 30
delay: 60
- name: Force reboot redis cache - name: Force reboot redis cache
azure_rm_rediscache: azure_rm_rediscache:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
@ -201,6 +197,7 @@
subnet: subnet:
name: "{{ subnet_name }}" name: "{{ subnet_name }}"
virtual_network_name: "{{ vnet_name }}" virtual_network_name: "{{ vnet_name }}"
wait_for_provisioning: False
register: output register: output
- name: Assert creating redis cache - name: Assert creating redis cache