add new module: azure_rm_rediscachefirewallrule (#53161)
This commit is contained in:
parent
bc4ef99533
commit
9e42038747
5 changed files with 499 additions and 75 deletions
|
@ -17,9 +17,9 @@ DOCUMENTATION = '''
|
|||
---
|
||||
module: azure_rm_rediscache
|
||||
version_added: "2.8"
|
||||
short_description: Manage Azure Redis Cache instance.
|
||||
short_description: Manage Azure Cache for Redis instance.
|
||||
description:
|
||||
- Create, update and delete instance of Azure Redis Cache.
|
||||
- Create, update and delete instance of Azure Cache for Redis.
|
||||
|
||||
options:
|
||||
resource_group:
|
||||
|
@ -28,17 +28,16 @@ options:
|
|||
required: True
|
||||
name:
|
||||
description:
|
||||
- Unique name of the redis cache to create or update. To create or update a deployment slot, use the {slot} parameter.
|
||||
- Unique name of the Azure Cache for Redis to create or update.
|
||||
required: True
|
||||
|
||||
location:
|
||||
description:
|
||||
- Resource location. If not set, location from the resource group will be used as default.
|
||||
sku:
|
||||
description: Sku info of redis cache.
|
||||
description: Sku info of Azure Cache for Redis.
|
||||
suboptions:
|
||||
name:
|
||||
description: Type of redis cache to deploy
|
||||
description: Type of Azure Cache for Redis to deploy
|
||||
choices:
|
||||
- basic
|
||||
- standard
|
||||
|
@ -46,7 +45,7 @@ options:
|
|||
required: True
|
||||
size:
|
||||
description:
|
||||
- Size of redis cache to deploy.
|
||||
- Size of Azure Cache for Redis to deploy.
|
||||
- When I(sku) is C(basic) or C(standard), allowed values are C0, C1, C2, C3, C4, C5, C6.
|
||||
- When I(sku) is C(premium), allowed values are P1, P2, P3, P4.
|
||||
- Please see U(https://docs.microsoft.com/en-us/rest/api/redis/redis/create#sku) for allowed values.
|
||||
|
@ -97,10 +96,10 @@ options:
|
|||
type: int
|
||||
static_ip:
|
||||
description:
|
||||
- Static IP address. Required when deploying a redis cache inside an existing Azure virtual network.
|
||||
- Static IP address. Required when deploying an Azure Cache for Redis inside an existing Azure virtual network.
|
||||
subnet:
|
||||
description:
|
||||
- Subnet in a virtual network to deploy the redis cache in.
|
||||
- Subnet in a virtual network to deploy the Azure Cache for Redis in.
|
||||
- "It can be resource id of subnet, eg.
|
||||
/subscriptions/{subid}/resourceGroups/{resourceGroupName}/Microsoft.{Network|ClassicNetwork}/VirtualNetworks/vnet1/subnets/subnet1"
|
||||
- It can be a dictionary where contains C(name), C(virtual_network_name) and C(resource_group).
|
||||
|
@ -112,8 +111,8 @@ options:
|
|||
- Dict of tenant settings.
|
||||
state:
|
||||
description:
|
||||
- Assert the state of the redis cache.
|
||||
- Use C(present) to create or update a redis cache and C(absent) to delete it.
|
||||
- Assert the state of the Azure Cache for Redis.
|
||||
- Use C(present) to create or update an Azure Cache for Redis and C(absent) to delete it.
|
||||
default: present
|
||||
choices:
|
||||
- absent
|
||||
|
@ -129,7 +128,7 @@ author:
|
|||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Create a redis cache
|
||||
- name: Create an Azure Cache for Redis
|
||||
azure_rm_rediscache:
|
||||
resource_group: myResourceGroup
|
||||
name: myRedis
|
||||
|
@ -138,7 +137,7 @@ EXAMPLES = '''
|
|||
size: C1
|
||||
|
||||
|
||||
- name: Scale up the redis cache
|
||||
- name: Scale up the Azure Cache for Redis
|
||||
azure_rm_rediscache:
|
||||
resource_group: myResourceGroup
|
||||
name: myRedis
|
||||
|
@ -148,7 +147,7 @@ EXAMPLES = '''
|
|||
tags:
|
||||
testing: foo
|
||||
|
||||
- name: Create redis with subnet
|
||||
- name: Create Azure Cache for Redis with subnet
|
||||
azure_rm_rediscache:
|
||||
resource_group: myResourceGroup
|
||||
name: myRedis
|
||||
|
@ -162,14 +161,14 @@ EXAMPLES = '''
|
|||
|
||||
RETURN = '''
|
||||
id:
|
||||
description: Id of the redis cache.
|
||||
description: Id of the Azure Cache for Redis.
|
||||
returned: always
|
||||
type: str
|
||||
sample: {
|
||||
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Cache/Redis/myRedis"
|
||||
}
|
||||
host_name:
|
||||
description: Host name of the redis cache.
|
||||
description: Host name of the Azure Cache for Redis.
|
||||
returned: state is present
|
||||
type: str
|
||||
sample: {
|
||||
|
@ -242,7 +241,7 @@ class Actions:
|
|||
|
||||
|
||||
class AzureRMRedisCaches(AzureRMModuleBase):
|
||||
"""Configuration class for an Azure RM Redis Cache resource"""
|
||||
"""Configuration class for an Azure RM Cache for Redis resource"""
|
||||
|
||||
def __init__(self):
|
||||
self.module_arg_spec = dict(
|
||||
|
@ -329,8 +328,6 @@ class AzureRMRedisCaches(AzureRMModuleBase):
|
|||
self.state = None
|
||||
self.to_do = Actions.NoAction
|
||||
|
||||
self.frameworks = None
|
||||
|
||||
super(AzureRMRedisCaches, self).__init__(derived_arg_spec=self.module_arg_spec,
|
||||
supports_check_mode=True,
|
||||
supports_tags=True)
|
||||
|
@ -365,7 +362,7 @@ class AzureRMRedisCaches(AzureRMModuleBase):
|
|||
if self.subnet:
|
||||
self.subnet = self.parse_subnet()
|
||||
|
||||
# get existing redis cache
|
||||
# get existing Azure Cache for Redis
|
||||
old_response = self.get_rediscache()
|
||||
|
||||
if old_response:
|
||||
|
@ -374,17 +371,17 @@ class AzureRMRedisCaches(AzureRMModuleBase):
|
|||
if self.state == 'present':
|
||||
# if redis not exists
|
||||
if not old_response:
|
||||
self.log("Redis cache instance doesn't exist")
|
||||
self.log("Azure Cache for Redis instance doesn't exist")
|
||||
|
||||
to_be_updated = True
|
||||
self.to_do = Actions.Create
|
||||
|
||||
if not self.sku:
|
||||
self.fail("Please specify sku to creating new redis cache.")
|
||||
self.fail("Please specify sku to creating new Azure Cache for Redis.")
|
||||
|
||||
else:
|
||||
# redis exists already, do update
|
||||
self.log("Redis cache instance already exists")
|
||||
self.log("Azure Cache for Redis instance already exists")
|
||||
|
||||
update_tags, self.tags = self.update_tags(old_response.get('tags', None))
|
||||
|
||||
|
@ -399,16 +396,16 @@ class AzureRMRedisCaches(AzureRMModuleBase):
|
|||
|
||||
elif self.state == 'absent':
|
||||
if old_response:
|
||||
self.log("Delete Redis cache instance")
|
||||
self.log("Delete Azure Cache for Redis instance")
|
||||
self.results['id'] = old_response['id']
|
||||
to_be_updated = True
|
||||
self.to_do = Actions.Delete
|
||||
else:
|
||||
self.results['changed'] = False
|
||||
self.log("Redis cache {0} not exists.".format(self.name))
|
||||
self.log("Azure Cache for Redis {0} not exists.".format(self.name))
|
||||
|
||||
if to_be_updated:
|
||||
self.log('Need to Create/Update redis cache')
|
||||
self.log('Need to Create/Update Azure Cache for Redis')
|
||||
self.results['changed'] = True
|
||||
|
||||
if self.check_mode:
|
||||
|
@ -426,7 +423,7 @@ class AzureRMRedisCaches(AzureRMModuleBase):
|
|||
|
||||
if self.to_do == Actions.Delete:
|
||||
self.delete_rediscache()
|
||||
self.log('Redis cache instance deleted')
|
||||
self.log('Azure Cache for Redis instance deleted')
|
||||
|
||||
return self.results
|
||||
|
||||
|
@ -461,12 +458,12 @@ class AzureRMRedisCaches(AzureRMModuleBase):
|
|||
|
||||
def create_rediscache(self):
|
||||
'''
|
||||
Creates redis cache instance with the specified configuration.
|
||||
Creates Azure Cache for Redis instance with the specified configuration.
|
||||
|
||||
:return: deserialized redis cache instance state dictionary
|
||||
:return: deserialized Azure Cache for Redis instance state dictionary
|
||||
'''
|
||||
self.log(
|
||||
"Creating redis cache instance {0}".format(self.name))
|
||||
"Creating Azure Cache for Redis instance {0}".format(self.name))
|
||||
|
||||
try:
|
||||
redis_config = dict()
|
||||
|
@ -493,19 +490,19 @@ class AzureRMRedisCaches(AzureRMModuleBase):
|
|||
response = self.get_poller_result(response)
|
||||
|
||||
except CloudError as exc:
|
||||
self.log('Error attempting to create the redis cache instance.')
|
||||
self.log('Error attempting to create the Azure Cache for Redis instance.')
|
||||
self.fail(
|
||||
"Error creating the redis cache instance: {0}".format(str(exc)))
|
||||
"Error creating the Azure Cache for Redis instance: {0}".format(str(exc)))
|
||||
return rediscache_to_dict(response)
|
||||
|
||||
def update_rediscache(self):
|
||||
'''
|
||||
Updates redis cache instance with the specified configuration.
|
||||
Updates Azure Cache for Redis instance with the specified configuration.
|
||||
|
||||
:return: redis cache instance state dictionary
|
||||
:return: Azure Cache for Redis instance state dictionary
|
||||
'''
|
||||
self.log(
|
||||
"Updating redis cache instance {0}".format(self.name))
|
||||
"Updating Azure Cache for Redis instance {0}".format(self.name))
|
||||
|
||||
try:
|
||||
redis_config = dict()
|
||||
|
@ -529,34 +526,34 @@ class AzureRMRedisCaches(AzureRMModuleBase):
|
|||
response = self.get_poller_result(response)
|
||||
|
||||
except CloudError as exc:
|
||||
self.log('Error attempting to update the redis cache instance.')
|
||||
self.log('Error attempting to update the Azure Cache for Redis instance.')
|
||||
self.fail(
|
||||
"Error updating the redis cache instance: {0}".format(str(exc)))
|
||||
"Error updating the Azure Cache for Redis instance: {0}".format(str(exc)))
|
||||
return rediscache_to_dict(response)
|
||||
|
||||
def delete_rediscache(self):
|
||||
'''
|
||||
Deletes specified redis cache instance in the specified subscription and resource group.
|
||||
Deletes specified Azure Cache for Redis instance in the specified subscription and resource group.
|
||||
|
||||
:return: True
|
||||
'''
|
||||
self.log("Deleting the redis cache instance {0}".format(self.name))
|
||||
self.log("Deleting the Azure Cache for Redis instance {0}".format(self.name))
|
||||
try:
|
||||
response = self._client.redis.delete(resource_group_name=self.resource_group,
|
||||
name=self.name)
|
||||
except CloudError as e:
|
||||
self.log('Error attempting to delete the redis cache instance.')
|
||||
self.log('Error attempting to delete the Azure Cache for Redis instance.')
|
||||
self.fail(
|
||||
"Error deleting the redis cache instance: {0}".format(str(e)))
|
||||
"Error deleting the Azure Cache for Redis instance: {0}".format(str(e)))
|
||||
return True
|
||||
|
||||
def get_rediscache(self):
|
||||
'''
|
||||
Gets the properties of the specified redis cache instance.
|
||||
Gets the properties of the specified Azure Cache for Redis instance.
|
||||
|
||||
:return: redis cache instance state dictionary
|
||||
:return: Azure Cache for Redis instance state dictionary
|
||||
'''
|
||||
self.log("Checking if the redis cache instance {0} is present".format(self.name))
|
||||
self.log("Checking if the Azure Cache for Redis instance {0} is present".format(self.name))
|
||||
|
||||
response = None
|
||||
|
||||
|
@ -565,11 +562,11 @@ class AzureRMRedisCaches(AzureRMModuleBase):
|
|||
name=self.name)
|
||||
|
||||
self.log("Response : {0}".format(response))
|
||||
self.log("Redis cache instance : {0} found".format(response.name))
|
||||
self.log("Azure Cache for Redis instance : {0} found".format(response.name))
|
||||
return rediscache_to_dict(response)
|
||||
|
||||
except CloudError as ex:
|
||||
self.log("Didn't find redis cache {0} in resource group {1}".format(
|
||||
self.log("Didn't find Azure Cache for Redis {0} in resource group {1}".format(
|
||||
self.name, self.resource_group))
|
||||
|
||||
return False
|
||||
|
|
|
@ -18,22 +18,22 @@ module: azure_rm_rediscache_info
|
|||
|
||||
version_added: "2.8"
|
||||
|
||||
short_description: Get Azure Redis Cache instance facts
|
||||
short_description: Get Azure Cache for Redis instance facts
|
||||
|
||||
description:
|
||||
- Get facts for Azure Redis Cache instance.
|
||||
- Get facts for Azure Cache for Redis instance.
|
||||
|
||||
options:
|
||||
resource_group:
|
||||
description:
|
||||
- The resource group to search for the desired Azure Redis Cache
|
||||
- The resource group to search for the desired Azure Cache for Redis
|
||||
required: True
|
||||
name:
|
||||
description:
|
||||
- Limit results to a specific Azure Redis Cache.
|
||||
- Limit results to a specific Azure Cache for Redis.
|
||||
return_access_keys:
|
||||
description:
|
||||
- Indicate weather to return access keys of the Redis Cache.
|
||||
- Indicate weather to return access keys of the Azure Cache for Redis.
|
||||
default: False
|
||||
type: bool
|
||||
tags:
|
||||
|
@ -48,43 +48,43 @@ author:
|
|||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Get Redis Cache by name
|
||||
- name: Get Azure Cache for Redis by name
|
||||
azure_rm_rediscache_info:
|
||||
resource_group: myResourceGroup
|
||||
name: myRedis
|
||||
|
||||
- name: Get Redis Cache with access keys by name
|
||||
- name: Get Azure Cache for Redis with access keys by name
|
||||
azure_rm_rediscache_info:
|
||||
resource_group: myResourceGroup
|
||||
name: myRedis
|
||||
return_access_keys: true
|
||||
|
||||
- name: Get Redis Cache in specific resource group
|
||||
- name: Get Azure Cache for Redis in specific resource group
|
||||
azure_rm_rediscache_info:
|
||||
resource_group: myResourceGroup
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
rediscaches:
|
||||
description: List of Azure Redis Cache instances.
|
||||
description: List of Azure Cache for Redis instances.
|
||||
returned: always
|
||||
type: complex
|
||||
contains:
|
||||
resource_group:
|
||||
description:
|
||||
- Name of a resource group where the Azure Redis Cache belongs to.
|
||||
- Name of a resource group where the Azure Cache for Redis belongs to.
|
||||
returned: always
|
||||
type: str
|
||||
sample: myResourceGroup
|
||||
name:
|
||||
description:
|
||||
- Name of the Azure Redis Cache.
|
||||
- Name of the Azure Cache for Redis.
|
||||
returned: always
|
||||
type: str
|
||||
sample: myRedis
|
||||
id:
|
||||
description:
|
||||
- Id of the Azure Redis Cache.
|
||||
- Id of the Azure Cache for Redis.
|
||||
returned: always
|
||||
type: str
|
||||
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Cache/Redis/myRedis
|
||||
|
@ -96,7 +96,7 @@ rediscaches:
|
|||
sample: Creating
|
||||
location:
|
||||
description:
|
||||
- Location of the Azure Redis Cache.
|
||||
- Location of the Azure Cache for Redis.
|
||||
type: str
|
||||
sample: WestUS
|
||||
enable_non_ssl_port:
|
||||
|
@ -115,7 +115,7 @@ rediscaches:
|
|||
type: str
|
||||
sample: standard
|
||||
size:
|
||||
description: Size of the Redis Cache.
|
||||
description: Size of the Azure Cache for Redis.
|
||||
returned: always
|
||||
type: str
|
||||
sample: C1
|
||||
|
@ -126,7 +126,7 @@ rediscaches:
|
|||
sample: 10.75.0.11
|
||||
subnet:
|
||||
description:
|
||||
- The full resource ID of a subnet in a virtual network to deploy the Redis cache in.
|
||||
- The full resource ID of a subnet in a virtual network to deploy the Azure Cache for Redis in.
|
||||
type: str
|
||||
sample:
|
||||
- "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/Microsoft.Network/VirtualNetworks/myVirtualNetwo
|
||||
|
@ -158,7 +158,7 @@ rediscaches:
|
|||
- foo
|
||||
access_keys:
|
||||
description:
|
||||
- Redis cache access keys.
|
||||
- Azure Cache for Redis access keys.
|
||||
type: dict
|
||||
returned: when C(return_access_keys) is true.
|
||||
contains:
|
||||
|
@ -186,7 +186,7 @@ import re
|
|||
|
||||
|
||||
class AzureRMRedisCacheFacts(AzureRMModuleBase):
|
||||
"""Utility class to get Azure Azure Redis cache facts"""
|
||||
"""Utility class to get Azure Cache for Redis facts"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
|
@ -239,7 +239,7 @@ class AzureRMRedisCacheFacts(AzureRMModuleBase):
|
|||
return self.results
|
||||
|
||||
def get_item(self):
|
||||
"""Get a single Azure Redis Cache"""
|
||||
"""Get a single Azure Cache for Redis"""
|
||||
|
||||
self.log('Get properties for {0}'.format(self.name))
|
||||
|
||||
|
@ -257,9 +257,9 @@ class AzureRMRedisCacheFacts(AzureRMModuleBase):
|
|||
return result
|
||||
|
||||
def list_by_resourcegroup(self):
|
||||
"""Get all Azure Azure Redis Cache within a resource group"""
|
||||
"""Get all Azure Cache for Redis within a resource group"""
|
||||
|
||||
self.log('List all Azure Redis Cache within a resource group')
|
||||
self.log('List all Azure Cache for Redis within a resource group')
|
||||
|
||||
try:
|
||||
response = self._client.redis.list_by_resource_group(self.resource_group)
|
||||
|
@ -274,7 +274,7 @@ class AzureRMRedisCacheFacts(AzureRMModuleBase):
|
|||
return results
|
||||
|
||||
def list_keys(self):
|
||||
"""List Azure Redis Cache keys"""
|
||||
"""List Azure Cache for Redis keys"""
|
||||
|
||||
self.log('List keys for {0}'.format(self.name))
|
||||
|
||||
|
@ -289,8 +289,8 @@ class AzureRMRedisCacheFacts(AzureRMModuleBase):
|
|||
|
||||
def serialize_rediscache(self, rediscache):
|
||||
'''
|
||||
Convert a Azure Redis Cache object to dict.
|
||||
:param cdn: Azure Redis Cache object
|
||||
Convert an Azure Cache for Redis object to dict.
|
||||
:param rediscache: Azure Cache for Redis object
|
||||
:return: dict
|
||||
'''
|
||||
new_result = dict(
|
||||
|
|
|
@ -0,0 +1,319 @@
|
|||
#!/usr/bin/python
|
||||
#
|
||||
# Copyright (c) 2019 Yunge Zhu, <yungez@microsoft.com>
|
||||
#
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: azure_rm_rediscachefirewallrule
|
||||
version_added: "2.8"
|
||||
short_description: Manage Azure Cache for Redis Firewall rules.
|
||||
description:
|
||||
- Create, update and delete Azure Cache for Redis Firewall rules.
|
||||
|
||||
options:
|
||||
resource_group:
|
||||
description:
|
||||
- Name of the resource group to which the resource belongs.
|
||||
required: True
|
||||
cache_name:
|
||||
description:
|
||||
- Name of the Azure Cache for Redis.
|
||||
required: True
|
||||
name:
|
||||
description:
|
||||
- Name of the Firewall rule.
|
||||
required: True
|
||||
start_ip_address:
|
||||
description:
|
||||
- The start IP address of the Azure Cache for Redis Firewall rule. Must be IPv4 format.
|
||||
- Required when creating Firewall rule.
|
||||
end_ip_address:
|
||||
description:
|
||||
- The end IP address of the Azure Cache for Redis Firewall rule. Must be IPv4 format.
|
||||
- Required when creating Firewall rule.
|
||||
state:
|
||||
description:
|
||||
- Assert the state of the Firewall rule of Azure Cache for Redis.
|
||||
- Use C(present) to create or update Firewall rule of Azure Cache for Redis and C(absent) to delete it.
|
||||
default: present
|
||||
choices:
|
||||
- absent
|
||||
- present
|
||||
|
||||
extends_documentation_fragment:
|
||||
- azure
|
||||
|
||||
author:
|
||||
- "Yunge Zhu(@yungezz)"
|
||||
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Create a Firewall rule for Azure Cache for Redis
|
||||
azure_rm_rediscachefirewallrule:
|
||||
resource_group: myResourceGroup
|
||||
cache_name: myRedisCache
|
||||
name: myRule
|
||||
start_ip_address: 192.168.1.1
|
||||
end_ip_address: 192.168.1.4
|
||||
|
||||
- name: Update a Firewall rule for Azure Cache for Redis
|
||||
azure_rm_rediscachefirewallrule:
|
||||
resource_group: myResourceGroup
|
||||
cache_name: myRedisCache
|
||||
name: myRule
|
||||
end_ip_address: 192.168.1.5
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
id:
|
||||
description: Id of the Azure Cache for Redis.
|
||||
returned: always
|
||||
type: str
|
||||
sample:
|
||||
"/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Cache/redis/myRedis/firewallRules/myRule"
|
||||
'''
|
||||
|
||||
import time
|
||||
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
|
||||
|
||||
try:
|
||||
from msrestazure.azure_exceptions import CloudError
|
||||
from msrestazure.azure_operation import AzureOperationPoller
|
||||
from msrest.serialization import Model
|
||||
from azure.mgmt.redis import RedisManagementClient
|
||||
except ImportError:
|
||||
# This is handled in azure_rm_common
|
||||
pass
|
||||
|
||||
|
||||
def firewall_rule_to_dict(rule):
|
||||
return dict(
|
||||
id=rule.id,
|
||||
name=rule.name,
|
||||
start_ip_address=rule.start_ip,
|
||||
end_ip_address=rule.end_ip,
|
||||
type=rule.type
|
||||
)
|
||||
|
||||
|
||||
class Actions:
|
||||
NoAction, CreateUpdate, Delete = range(3)
|
||||
|
||||
|
||||
class AzureRMRedisCacheFirewallRule(AzureRMModuleBase):
|
||||
"""Configuration class for an Azure RM Cache for Redis Firewall Rule resource"""
|
||||
|
||||
def __init__(self):
|
||||
self.module_arg_spec = dict(
|
||||
resource_group=dict(
|
||||
type='str',
|
||||
required=True
|
||||
),
|
||||
name=dict(
|
||||
type='str',
|
||||
required=True
|
||||
),
|
||||
cache_name=dict(
|
||||
type='str',
|
||||
required=True
|
||||
),
|
||||
start_ip_address=dict(
|
||||
type='str'
|
||||
),
|
||||
end_ip_address=dict(
|
||||
type='str'
|
||||
),
|
||||
state=dict(
|
||||
type='str',
|
||||
default='present',
|
||||
choices=['present', 'absent']
|
||||
)
|
||||
)
|
||||
|
||||
self._client = None
|
||||
|
||||
self.resource_group = None
|
||||
self.name = None
|
||||
self.cache_name = None
|
||||
|
||||
self.start_ip_address = None
|
||||
self.end_ip_address = None
|
||||
|
||||
self.results = dict(
|
||||
changed=False,
|
||||
id=None
|
||||
)
|
||||
self.state = None
|
||||
|
||||
self.to_do = Actions.NoAction
|
||||
|
||||
super(AzureRMRedisCacheFirewallRule, self).__init__(derived_arg_spec=self.module_arg_spec,
|
||||
supports_check_mode=True,
|
||||
supports_tags=False)
|
||||
|
||||
def exec_module(self, **kwargs):
|
||||
"""Main module execution method"""
|
||||
|
||||
for key in list(self.module_arg_spec.keys()):
|
||||
setattr(self, key, kwargs[key])
|
||||
|
||||
old_response = None
|
||||
response = None
|
||||
|
||||
# get management client
|
||||
self._client = self.get_mgmt_svc_client(RedisManagementClient,
|
||||
base_url=self._cloud_environment.endpoints.resource_manager,
|
||||
api_version='2018-03-01')
|
||||
|
||||
# check if the firewall rule exists
|
||||
old_response = self.get()
|
||||
|
||||
if old_response:
|
||||
self.results['id'] = old_response['id']
|
||||
|
||||
if self.state == 'present':
|
||||
# if firewall rule not exists
|
||||
if not old_response:
|
||||
self.log("Firewall Rule of Azure Cache for Redis doesn't exist")
|
||||
|
||||
self.to_do = Actions.CreateUpdate
|
||||
|
||||
else:
|
||||
# redis exists already, do update
|
||||
self.log("Firewall Rule of Azure Cache for Redis already exists")
|
||||
|
||||
if self.start_ip_address is None:
|
||||
self.start_ip_address = old_response['start_ip_address']
|
||||
if self.end_ip_address is None:
|
||||
self.end_ip_address = old_response['end_ip_address']
|
||||
|
||||
# check if update
|
||||
if self.check_update(old_response):
|
||||
self.to_do = Actions.CreateUpdate
|
||||
|
||||
elif self.state == 'absent':
|
||||
if old_response:
|
||||
self.log("Delete Firewall Rule of Azure Cache for Redis")
|
||||
self.results['id'] = old_response['id']
|
||||
self.to_do = Actions.Delete
|
||||
else:
|
||||
self.results['changed'] = False
|
||||
self.log("Azure Cache for Redis {0} doesn't exist.".format(self.name))
|
||||
|
||||
if self.to_do == Actions.CreateUpdate:
|
||||
self.log('Need to Create/Update Firewall rule of Azure Cache for Redis')
|
||||
self.results['changed'] = True
|
||||
|
||||
if self.check_mode:
|
||||
return self.results
|
||||
|
||||
response = self.create_or_update()
|
||||
self.results['id'] = response['id']
|
||||
|
||||
if self.to_do == Actions.Delete:
|
||||
self.log('Delete Firewall rule of Azure Cache for Redis')
|
||||
self.results['changed'] = True
|
||||
|
||||
if self.check_mode:
|
||||
return self.results
|
||||
|
||||
self.delete()
|
||||
self.log('Firewall rule of Azure Cache for Redis deleted')
|
||||
|
||||
return self.results
|
||||
|
||||
def check_update(self, existing):
|
||||
if self.start_ip_address and self.start_ip_address != existing['start_ip_address']:
|
||||
self.log("start_ip_address diff: origin {0} / update {1}".format(existing['start_ip_address'], self.start_ip_address))
|
||||
return True
|
||||
if self.end_ip_address and self.end_ip_address != existing['end_ip_address']:
|
||||
self.log("end_ip_address diff: origin {0} / update {1}".format(existing['end_ip_address'], self.end_ip_address))
|
||||
return True
|
||||
return False
|
||||
|
||||
def create_or_update(self):
|
||||
'''
|
||||
Creates Firewall rule of Azure Cache for Redis with the specified configuration.
|
||||
|
||||
:return: deserialized Firewall rule of Azure Cache for Redis state dictionary
|
||||
'''
|
||||
self.log(
|
||||
"Creating Firewall rule of Azure Cache for Redis {0}".format(self.name))
|
||||
|
||||
try:
|
||||
response = self._client.firewall_rules.create_or_update(resource_group_name=self.resource_group,
|
||||
cache_name=self.cache_name,
|
||||
rule_name=self.name,
|
||||
start_ip=self.start_ip_address,
|
||||
end_ip=self.end_ip_address)
|
||||
if isinstance(response, AzureOperationPoller):
|
||||
response = self.get_poller_result(response)
|
||||
|
||||
except CloudError as exc:
|
||||
self.log('Error attempting to create/update Firewall rule of Azure Cache for Redis.')
|
||||
self.fail(
|
||||
"Error creating/updating Firewall rule of Azure Cache for Redis: {0}".format(str(exc)))
|
||||
return firewall_rule_to_dict(response)
|
||||
|
||||
def delete(self):
|
||||
'''
|
||||
Deletes specified Firewall rule of Azure Cache for Redis in the specified subscription and resource group.
|
||||
|
||||
:return: True
|
||||
'''
|
||||
self.log("Deleting the Firewall rule of Azure Cache for Redis {0}".format(self.name))
|
||||
try:
|
||||
response = self._client.firewall_rules.delete(resource_group_name=self.resource_group,
|
||||
rule_name=self.name,
|
||||
cache_name=self.cache_name)
|
||||
except CloudError as e:
|
||||
self.log('Error attempting to delete the Firewall rule of Azure Cache for Redis.')
|
||||
self.fail(
|
||||
"Error deleting the Firewall rule of Azure Cache for Redis: {0}".format(str(e)))
|
||||
return True
|
||||
|
||||
def get(self):
|
||||
'''
|
||||
Gets the properties of the specified Firewall rule of Azure Cache for Redis.
|
||||
|
||||
:return: Azure Cache for Redis Firewall Rule instance state dictionary
|
||||
'''
|
||||
self.log("Checking if the Firewall Rule {0} is present".format(self.name))
|
||||
|
||||
response = None
|
||||
|
||||
try:
|
||||
response = self._client.firewall_rules.get(resource_group_name=self.resource_group,
|
||||
rule_name=self.name,
|
||||
cache_name=self.cache_name)
|
||||
|
||||
self.log("Response : {0}".format(response))
|
||||
self.log("Redis Firewall Rule : {0} found".format(response.name))
|
||||
return firewall_rule_to_dict(response)
|
||||
|
||||
except CloudError as ex:
|
||||
self.log("Didn't find Azure Redis Firewall rule {0} in resource group {1}".format(
|
||||
self.name, self.resource_group))
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
"""Main execution"""
|
||||
AzureRMRedisCacheFirewallRule()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -2,3 +2,4 @@ cloud/azure
|
|||
shippable/azure/group2
|
||||
destructive
|
||||
azure_rm_rediscache_facts
|
||||
azure_rm_rediscachefirewallrule
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
redis_name: "redis-{{ resource_group | hash('md5') | truncate(7, True, '') }}-{{ 1000 | random }}"
|
||||
vnet_name: "vnet-{{ resource_group | hash('md5') | truncate(7, True, '') }}-{{ 1000 | random }}"
|
||||
subnet_name: "subnet-{{ resource_group | hash('md5') | truncate(7, True, '') }}-{{ 1000 | random }}"
|
||||
rule_name: "rule1"
|
||||
run_once: yes
|
||||
|
||||
- name: Create a redis cache (Check Mode)
|
||||
|
@ -190,9 +191,115 @@
|
|||
- facts.rediscaches[0].subnet != None
|
||||
- facts.rediscaches[0].access_keys.primary != None
|
||||
|
||||
# - name: Delete the redis cache
|
||||
# azure_rm_rediscache:
|
||||
# resource_group: "{{ resource_group }}"
|
||||
# name: "{{ redis_name }}2"
|
||||
# state: absent
|
||||
# register: output
|
||||
- name: Wait for Redis provisioning to complete
|
||||
azure_rm_rediscache_info:
|
||||
resource_group: "{{ resource_group }}"
|
||||
name: "{{ redis_name }}2"
|
||||
register: facts
|
||||
until: "{{ facts.rediscaches[0]['provisioning_state'] == 'Succeeded' }}"
|
||||
retries: 30
|
||||
delay: 60
|
||||
tags:
|
||||
- long_run
|
||||
|
||||
- name: Create firewall rule (Check mode)
|
||||
azure_rm_rediscachefirewallrule:
|
||||
resource_group: "{{ resource_group }}"
|
||||
cache_name: "{{ redis_name }}2"
|
||||
name: "{{ rule_name }}"
|
||||
start_ip_address: 192.168.1.1
|
||||
end_ip_address: 192.168.1.4
|
||||
check_mode: yes
|
||||
register: output
|
||||
|
||||
- name: Assert check mode creation
|
||||
assert:
|
||||
that:
|
||||
- output.changed
|
||||
|
||||
# Creating firewall rule need Redis status is running, while creating redis Cache costs about 20 mins async operation,
|
||||
# need to poll status from Creating to Running, then able to perform firewall rule creating,
|
||||
# otherwise, will met error:
|
||||
# "Error creating/updating Firewall rule of Azure Cache for Redis: Azure Error: Conflict\nMessage: The resource
|
||||
# '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Cache/Redis/myRedis' is busy processing
|
||||
# a previous update request or is undergoing system maintenance. As such, it is currently unable to accept the update request. Please try again later."
|
||||
# comment out firewall rule tests for test efficiency.
|
||||
- name: Create firewall rule
|
||||
azure_rm_rediscachefirewallrule:
|
||||
resource_group: "{{ resource_group }}"
|
||||
cache_name: "{{ redis_name }}2"
|
||||
name: "{{ rule_name }}"
|
||||
start_ip_address: 192.168.1.1
|
||||
end_ip_address: 192.168.1.4
|
||||
register: output
|
||||
tags:
|
||||
- long_run
|
||||
|
||||
- name: Assert creation
|
||||
assert:
|
||||
that:
|
||||
- output.changed
|
||||
- output.id
|
||||
tags:
|
||||
- long_run
|
||||
|
||||
- name: Update firewall rule idempotence
|
||||
azure_rm_rediscachefirewallrule:
|
||||
resource_group: "{{ resource_group }}"
|
||||
cache_name: "{{ redis_name }}2"
|
||||
name: "{{ rule_name }}"
|
||||
start_ip_address: 192.168.1.1
|
||||
end_ip_address: 192.168.1.4
|
||||
register: output
|
||||
tags:
|
||||
- long_run
|
||||
|
||||
- name: Assert idempotence
|
||||
assert:
|
||||
that:
|
||||
- output.changed == False
|
||||
tags:
|
||||
- long_run
|
||||
|
||||
- name: Update firewall rule
|
||||
azure_rm_rediscachefirewallrule:
|
||||
resource_group: "{{ resource_group }}"
|
||||
cache_name: "{{ redis_name }}2"
|
||||
name: "{{ rule_name }}"
|
||||
end_ip_address: 192.168.1.5
|
||||
register: output
|
||||
tags:
|
||||
- long_run
|
||||
|
||||
- name: Assert updating
|
||||
assert:
|
||||
that:
|
||||
- output.changed
|
||||
tags:
|
||||
- long_run
|
||||
|
||||
- name: Delete firewall rule
|
||||
azure_rm_rediscachefirewallrule:
|
||||
resource_group: "{{ resource_group }}"
|
||||
cache_name: "{{ redis_name }}2"
|
||||
name: "{{ rule_name }}"
|
||||
state: absent
|
||||
register: output
|
||||
tags:
|
||||
- long_run
|
||||
|
||||
- name: Assert deletion
|
||||
assert:
|
||||
that:
|
||||
- output.changed
|
||||
tags:
|
||||
- long_run
|
||||
|
||||
- name: Delete the redis cache
|
||||
azure_rm_rediscache:
|
||||
resource_group: "{{ resource_group }}"
|
||||
name: "{{ redis_name }}2"
|
||||
state: absent
|
||||
register: output
|
||||
tags:
|
||||
- long_run
|
||||
|
|
Loading…
Reference in a new issue