Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add wait_for_running option in azure_rm_rediscache module #54976

Merged
merged 5 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 48 additions & 3 deletions lib/ansible/modules/cloud/azure/azure_rm_rediscache.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@
choices:
- primary
- 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:
description:
- Assert the state of the Azure Cache for Redis.
Expand Down Expand Up @@ -209,8 +217,8 @@

try:
from msrestazure.azure_exceptions import CloudError
from msrest.polling import LROPoller
from msrestazure.azure_operation import AzureOperationPoller
from msrest.polling import LROPoller
from msrest.serialization import Model
from azure.mgmt.redis import RedisManagementClient
from azure.mgmt.redis.models import (RedisCreateParameters, RedisUpdateParameters, Sku)
Expand Down Expand Up @@ -366,6 +374,10 @@ def __init__(self):
regenerate_key=dict(
type='dict',
options=regenerate_key_spec
),
wait_for_provisioning=dict(
type='bool',
default='True'
)
)

Expand All @@ -386,6 +398,10 @@ def __init__(self):
self.reboot = 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.results = dict(
Expand Down Expand Up @@ -562,9 +578,12 @@ def create_rediscache(self):
response = self._client.redis.create(resource_group_name=self.resource_group,
name=self.name,
parameters=params)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we shoudl submit a bug to SDK guys, i thought the purpose of Poller was to wait until the resource is completely provisioned

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually the line of checking response is AzureOperationPoller or LORPoller is useful in redis, because redis.create directly return a RedisResource.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for long running operation, it's always better to give user an option instead of directly poll/wait the call to wait for something, given already knowing it's a long run operation.

if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller):
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 exc:
self.log('Error attempting to create the Azure Cache for Redis instance.')
self.fail(
Expand Down Expand Up @@ -598,9 +617,12 @@ def update_rediscache(self):
response = self._client.redis.update(resource_group_name=self.resource_group,
name=self.name,
parameters=params)
if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller):
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 exc:
self.log('Error attempting to update the Azure Cache for Redis instance.')
self.fail(
Expand Down Expand Up @@ -659,6 +681,11 @@ def force_reboot_rediscache(self):
name=self.name,
reboot_type=self.reboot['reboot_type'],
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:
self.log('Error attempting to force reboot the redis cache instance.')
self.fail(
Expand Down Expand Up @@ -718,6 +745,24 @@ def parse_subnet(self):
subnet_id = self.subnet
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():
"""Main execution"""
Expand Down
17 changes: 7 additions & 10 deletions test/integration/targets/azure_rm_rediscache/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
sku:
name: basic
size: C1
wait_for_provisioning: False
check_mode: yes
register: output

Expand All @@ -28,6 +29,7 @@
sku:
name: basic
size: C1
wait_for_provisioning: False
register: output

- name: Assert creating redis cache
Expand Down Expand Up @@ -59,6 +61,7 @@
sku:
name: basic
size: C1
wait_for_provisioning: False
register: output

- name: assert output not changed
Expand Down Expand Up @@ -92,6 +95,7 @@
enable_non_ssl_port: true
tags:
testing: foo
wait_for_provisioning: True
register: output

- name: assert output changed
Expand All @@ -106,7 +110,7 @@
sku:
name: basic
size: C1
enable_non_ssl_port: true
enable_non_ssl_port: True
maxmemory_policy: allkeys_lru
tags:
testing: foo
Expand All @@ -126,21 +130,13 @@
size: C1
tags:
testing: foo
wait_for_provisioning: True
register: output

- assert:
that:
- 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
azure_rm_rediscache:
resource_group: "{{ resource_group }}"
Expand Down Expand Up @@ -201,6 +197,7 @@
subnet:
name: "{{ subnet_name }}"
virtual_network_name: "{{ vnet_name }}"
wait_for_provisioning: False
register: output

- name: Assert creating redis cache
Expand Down