| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_lock_info |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure locks |
| 16 |
description: |
| 17 |
- Create, delete an Azure lock. |
| 18 |
options: |
| 19 |
name: |
| 20 |
description: |
| 21 |
- Name of the lock. |
| 22 |
type: str |
| 23 |
managed_resource_id: |
| 24 |
description: |
| 25 |
- ID of the resource where need to manage the lock. |
| 26 |
- Get this via facts module. |
| 27 |
- Cannot be set mutual with I(resource_group). |
| 28 |
- Manage subscription if both I(managed_resource_id) and I(resource_group) not defined. |
| 29 |
- "'/subscriptions/{subscriptionId}' for subscriptions." |
| 30 |
- "'/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}' for resource groups." |
| 31 |
- "'/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/{namespace}/{resourceType}/{resourceName}' for resources." |
| 32 |
- Can get all locks with 'child scope' for this resource, use I(managed_resource_id) in response for further management. |
| 33 |
type: str |
| 34 |
resource_group: |
| 35 |
description: |
| 36 |
- Resource group name where need to manage the lock. |
| 37 |
- The lock is in the resource group level. |
| 38 |
- Cannot be set mutual with I(managed_resource_id). |
| 39 |
- Query subscription if both I(managed_resource_id) and I(resource_group) not defined. |
| 40 |
- Can get all locks with 'child scope' in this resource group, use the I(managed_resource_id) in response for further management. |
| 41 |
type: str |
| 42 |
|
| 43 |
extends_documentation_fragment: |
| 44 |
- azure.azcollection.azure |
| 45 |
|
| 46 |
author: |
| 47 |
- Yuwei Zhou (@yuwzho) |
| 48 |
|
| 49 |
''' |
| 50 |
|
| 51 |
EXAMPLES = ''' |
| 52 |
- name: Get myLock details of myVM |
| 53 |
azure_rm_lock_info: |
| 54 |
name: myLock |
| 55 |
managed_resource_id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM |
| 56 |
|
| 57 |
- name: List locks of myVM |
| 58 |
azure_rm_lock_info: |
| 59 |
managed_resource_id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM |
| 60 |
|
| 61 |
- name: List locks of myResourceGroup |
| 62 |
azure_rm_lock_info: |
| 63 |
resource_group: myResourceGroup |
| 64 |
|
| 65 |
- name: List locks of myResourceGroup |
| 66 |
azure_rm_lock_info: |
| 67 |
managed_resource_id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/myResourceGroup |
| 68 |
|
| 69 |
- name: List locks of mySubscription |
| 70 |
azure_rm_lock_info: |
| 71 |
|
| 72 |
- name: List locks of mySubscription |
| 73 |
azure_rm_lock_info: |
| 74 |
managed_resource_id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 75 |
''' |
| 76 |
|
| 77 |
RETURN = ''' |
| 78 |
locks: |
| 79 |
description: |
| 80 |
- List of locks dicts. |
| 81 |
returned: always |
| 82 |
type: complex |
| 83 |
contains: |
| 84 |
id: |
| 85 |
description: |
| 86 |
- ID of the Lock. |
| 87 |
returned: always |
| 88 |
type: str |
| 89 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Authorization/locks/myLock" |
| 90 |
name: |
| 91 |
description: |
| 92 |
- Name of the lock. |
| 93 |
returned: always |
| 94 |
type: str |
| 95 |
sample: myLock |
| 96 |
level: |
| 97 |
description: |
| 98 |
- Type level of the lock. |
| 99 |
returned: always |
| 100 |
type: str |
| 101 |
sample: can_not_delete |
| 102 |
notes: |
| 103 |
description: |
| 104 |
- Notes of the lock added by creator. |
| 105 |
returned: always |
| 106 |
type: str |
| 107 |
sample: "This is a lock" |
| 108 |
''' |
| 109 |
|
| 110 |
import json |
| 111 |
import re |
| 112 |
from ansible.module_utils.common.dict_transformations import _camel_to_snake |
| 113 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 114 |
|
| 115 |
try: |
| 116 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 117 |
except ImportError: |
| 118 |
|
| 119 |
pass |
| 120 |
|
| 121 |
|
| 122 |
class AzureRMLockInfo(AzureRMModuleBase): |
| 123 |
|
| 124 |
def __init__(self): |
| 125 |
|
| 126 |
self.module_arg_spec = dict( |
| 127 |
name=dict(type='str'), |
| 128 |
resource_group=dict(type='str'), |
| 129 |
managed_resource_id=dict(type='str') |
| 130 |
) |
| 131 |
|
| 132 |
self.results = dict( |
| 133 |
changed=False, |
| 134 |
locks=[] |
| 135 |
) |
| 136 |
|
| 137 |
mutually_exclusive = [['resource_group', 'managed_resource_id']] |
| 138 |
|
| 139 |
self.name = None |
| 140 |
self.resource_group = None |
| 141 |
self.managed_resource_id = None |
| 142 |
self._mgmt_client = None |
| 143 |
self._query_parameters = {'api-version': '2016-09-01'} |
| 144 |
self._header_parameters = {'Content-Type': 'application/json; charset=utf-8'} |
| 145 |
|
| 146 |
super(AzureRMLockInfo, self).__init__(self.module_arg_spec, |
| 147 |
supports_check_mode=True, |
| 148 |
facts_module=True, |
| 149 |
mutually_exclusive=mutually_exclusive, |
| 150 |
supports_tags=False) |
| 151 |
|
| 152 |
def exec_module(self, **kwargs): |
| 153 |
|
| 154 |
for key in self.module_arg_spec.keys(): |
| 155 |
setattr(self, key, kwargs[key]) |
| 156 |
|
| 157 |
self._mgmt_client = self.get_mgmt_svc_client(GenericRestClient, base_url=self._cloud_environment.endpoints.resource_manager) |
| 158 |
changed = False |
| 159 |
|
| 160 |
scope = self.get_scope() |
| 161 |
url = '/{0}/providers/Microsoft.Authorization/locks'.format(scope) |
| 162 |
if self.name: |
| 163 |
url = '{0}/{1}'.format(url, self.name) |
| 164 |
locks = self.list_locks(url) |
| 165 |
resp = locks.get('value') if 'value' in locks else [locks] |
| 166 |
self.results['locks'] = [self.to_dict(x) for x in resp] |
| 167 |
return self.results |
| 168 |
|
| 169 |
def to_dict(self, lock): |
| 170 |
resp = dict( |
| 171 |
id=lock['id'], |
| 172 |
name=lock['name'], |
| 173 |
level=_camel_to_snake(lock['properties']['level']), |
| 174 |
managed_resource_id=re.sub('/providers/Microsoft.Authorization/locks/.+', '', lock['id']) |
| 175 |
) |
| 176 |
if lock['properties'].get('notes'): |
| 177 |
resp['notes'] = lock['properties']['notes'] |
| 178 |
if lock['properties'].get('owners'): |
| 179 |
resp['owners'] = [x['application_id'] for x in lock['properties']['owners']] |
| 180 |
return resp |
| 181 |
|
| 182 |
def list_locks(self, url): |
| 183 |
try: |
| 184 |
resp = self._mgmt_client.query(url=url, |
| 185 |
method='GET', |
| 186 |
query_parameters=self._query_parameters, |
| 187 |
header_parameters=self._header_parameters, |
| 188 |
body=None, |
| 189 |
expected_status_codes=[200], |
| 190 |
polling_timeout=None, |
| 191 |
polling_interval=None) |
| 192 |
return json.loads(resp.body()) |
| 193 |
except Exception as exc: |
| 194 |
self.fail('Error when finding locks {0}: {1}'.format(url, exc.message)) |
| 195 |
|
| 196 |
def get_scope(self): |
| 197 |
''' |
| 198 |
Get the resource scope of the lock management. |
| 199 |
'/subscriptions/{subscriptionId}' for subscriptions, |
| 200 |
'/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}' for resource groups, |
| 201 |
'/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/{namespace}/{resourceType}/{resourceName}' for resources. |
| 202 |
''' |
| 203 |
if self.managed_resource_id: |
| 204 |
return self.managed_resource_id |
| 205 |
elif self.resource_group: |
| 206 |
return '/subscriptions/{0}/resourcegroups/{1}'.format(self.subscription_id, self.resource_group) |
| 207 |
else: |
| 208 |
return '/subscriptions/{0}'.format(self.subscription_id) |
| 209 |
|
| 210 |
|
| 211 |
def main(): |
| 212 |
AzureRMLockInfo() |
| 213 |
|
| 214 |
|
| 215 |
if __name__ == '__main__': |
| 216 |
main() |