| 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_storageshare |
| 14 |
version_added: "1.8.0" |
| 15 |
short_description: Manage Azure storage file share |
| 16 |
description: |
| 17 |
- Create, update or delete a storage file share in existing storage account. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- Name of the resource group to use. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Name of the storage file share to delete or create. |
| 27 |
type: str |
| 28 |
required: true |
| 29 |
account_name: |
| 30 |
description: |
| 31 |
- Name of the parent storage account for the storage file share. |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
access_tier: |
| 35 |
description: |
| 36 |
- The access tier determines the price and in some cases also the performance of a file share. TransactionOptimized if not set. |
| 37 |
type: str |
| 38 |
choices: |
| 39 |
- TransactionOptimized |
| 40 |
- Hot |
| 41 |
- Cool |
| 42 |
- Premium |
| 43 |
metadata: |
| 44 |
description: |
| 45 |
- A name-value pair to associate with the container as metadata. |
| 46 |
type: dict |
| 47 |
state: |
| 48 |
description: |
| 49 |
- State of the storage file share. Use 'present' to create or update a storage file share and use 'absent' to delete a file share. |
| 50 |
default: present |
| 51 |
type: str |
| 52 |
choices: |
| 53 |
- absent |
| 54 |
- present |
| 55 |
quota: |
| 56 |
description: |
| 57 |
- The maximum size of the file share, in gigabytes. Must be greater than 0, and less than or equal to 5TB (5120). |
| 58 |
For large file shares, the maximum size is 102400. By default 102400 |
| 59 |
type: int |
| 60 |
enabled_protocols: |
| 61 |
description: |
| 62 |
- The authentication protocol that is used for the file share. |
| 63 |
- Can only be specified when creating a share. |
| 64 |
type: str |
| 65 |
choices: |
| 66 |
- SMB |
| 67 |
- NFS |
| 68 |
root_squash: |
| 69 |
description: |
| 70 |
- The property is for NFS share only. |
| 71 |
- The default is C(NoRootSquash). |
| 72 |
type: str |
| 73 |
choices: |
| 74 |
- NoRootSquash |
| 75 |
- RootSquash |
| 76 |
- AllSquash |
| 77 |
|
| 78 |
extends_documentation_fragment: |
| 79 |
- azure.azcollection.azure |
| 80 |
|
| 81 |
author: |
| 82 |
- Andrii Bilorus (@ewscat) |
| 83 |
''' |
| 84 |
|
| 85 |
EXAMPLES = ''' |
| 86 |
--- |
| 87 |
- name: Create storage share |
| 88 |
azure_rm_storageshare: |
| 89 |
name: testShare |
| 90 |
resource_group: myResourceGroup |
| 91 |
account_name: testStorageAccount |
| 92 |
state: present |
| 93 |
access_tier: Cool |
| 94 |
quota: 2048 |
| 95 |
metadata: |
| 96 |
key1: value1 |
| 97 |
key2: value2 |
| 98 |
|
| 99 |
- name: Create share with enalbed protocols |
| 100 |
azure_rm_storageshare: |
| 101 |
name: "{{ share_name }}" |
| 102 |
resource_group: "{{ resource_group }}" |
| 103 |
account_name: "{{ storage_account }}" |
| 104 |
access_tier: "{{ access_tier }}" |
| 105 |
root_squash: RootSquash |
| 106 |
enabled_protocols: NFS |
| 107 |
|
| 108 |
- name: Delete storage share |
| 109 |
azure_rm_storageshare: |
| 110 |
name: testShare |
| 111 |
resource_group: myResourceGroup |
| 112 |
account_name: testStorageAccount |
| 113 |
state: absent |
| 114 |
''' |
| 115 |
|
| 116 |
RETURN = ''' |
| 117 |
state: |
| 118 |
description: |
| 119 |
- Facts about the current state of the storage file file share. |
| 120 |
returned: always |
| 121 |
type: complex |
| 122 |
contains: |
| 123 |
id: |
| 124 |
description: |
| 125 |
- Resource ID of the storage file share |
| 126 |
sample: "/subscriptions/9e700857-1631-4d8a-aed5-908520ede375/resourceGroups/myResourceGroup/providers/Microsoft.Storage/ |
| 127 |
storageAccounts/mystorageaccount/fileServices/default/shares/myshare" |
| 128 |
returned: always |
| 129 |
type: str |
| 130 |
name: |
| 131 |
description: |
| 132 |
- Name of the file share |
| 133 |
sample: myshare |
| 134 |
returned: always |
| 135 |
type: str |
| 136 |
type: |
| 137 |
description: |
| 138 |
- The type of the resource |
| 139 |
sample: "Microsoft.Storage/storageAccounts/fileServices/shares" |
| 140 |
returned: always |
| 141 |
type: str |
| 142 |
etag: |
| 143 |
description: |
| 144 |
- Resource Etag |
| 145 |
sample: "0x8D75E4BA3E275F1" |
| 146 |
returned: always |
| 147 |
type: str |
| 148 |
last_modified_time: |
| 149 |
description: |
| 150 |
- Returns the date and time the file share was last modified |
| 151 |
sample: "2021-08-23T08:17:35+00:00" |
| 152 |
returned: always |
| 153 |
type: str |
| 154 |
metadata: |
| 155 |
description: |
| 156 |
- A name-value pair to associate with the file share as metadata |
| 157 |
sample: '{"key1": "value1"}' |
| 158 |
returned: always |
| 159 |
type: dict |
| 160 |
share_quota: |
| 161 |
description: |
| 162 |
- The maximum size of the file share, in gigabytes |
| 163 |
sample: 102400 |
| 164 |
returned: always |
| 165 |
type: int |
| 166 |
access_tier: |
| 167 |
description: |
| 168 |
- Access tier for specific file share |
| 169 |
sample: 'TransactionOptimized' |
| 170 |
returned: always |
| 171 |
type: str |
| 172 |
access_tier_change_time: |
| 173 |
description: |
| 174 |
- Indicates the last modification time for file share access tier |
| 175 |
sample: "2021-08-23T08:17:35+00:00" |
| 176 |
returned: always |
| 177 |
type: str |
| 178 |
''' |
| 179 |
|
| 180 |
try: |
| 181 |
from azure.core.exceptions import ResourceNotFoundError |
| 182 |
except ImportError: |
| 183 |
|
| 184 |
pass |
| 185 |
|
| 186 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 187 |
|
| 188 |
|
| 189 |
class Actions: |
| 190 |
''' |
| 191 |
Action list that can be executed with storage file share |
| 192 |
''' |
| 193 |
NoAction, Create, Update, Delete = range(4) |
| 194 |
|
| 195 |
|
| 196 |
class AzureRMStorageShare(AzureRMModuleBase): |
| 197 |
''' |
| 198 |
Configuration class for an Azure RM Storage file share resource |
| 199 |
''' |
| 200 |
|
| 201 |
def __init__(self): |
| 202 |
|
| 203 |
self.module_arg_spec = dict( |
| 204 |
resource_group=dict(type='str', required=True), |
| 205 |
name=dict(type='str', required=True), |
| 206 |
account_name=dict(type='str', required=True), |
| 207 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 208 |
access_tier=dict(type='str', default=None, |
| 209 |
choices=['TransactionOptimized', 'Hot', 'Cool', 'Premium']), |
| 210 |
quota=dict(type='int', default=None), |
| 211 |
metadata=dict(type='dict', default=None), |
| 212 |
root_squash=dict(type='str', choices=['NoRootSquash', 'RootSquash', 'AllSquash']), |
| 213 |
enabled_protocols=dict(type='str', choices=['SMB', 'NFS']), |
| 214 |
) |
| 215 |
self.results = dict( |
| 216 |
changed=False, |
| 217 |
state=dict() |
| 218 |
) |
| 219 |
|
| 220 |
self.resource_group = None |
| 221 |
self.name = None |
| 222 |
self.account_name = None |
| 223 |
self.state = None |
| 224 |
self.quota = None |
| 225 |
self.metadata = None |
| 226 |
self.root_squash = None |
| 227 |
self.enabled_protocols = None |
| 228 |
|
| 229 |
self.to_do = Actions.NoAction |
| 230 |
|
| 231 |
super(AzureRMStorageShare, self).__init__(self.module_arg_spec, |
| 232 |
supports_check_mode=True, |
| 233 |
supports_tags=False) |
| 234 |
|
| 235 |
def exec_module(self, **kwargs): |
| 236 |
''' |
| 237 |
Main module execution method |
| 238 |
''' |
| 239 |
|
| 240 |
for key in list(self.module_arg_spec.keys()): |
| 241 |
setattr(self, key, kwargs[key]) |
| 242 |
|
| 243 |
self.log('Fetching storage file share {0}'.format(self.name)) |
| 244 |
response = None |
| 245 |
old_response = self.get_share() |
| 246 |
|
| 247 |
if old_response is None: |
| 248 |
if self.state == "present": |
| 249 |
self.to_do = Actions.Create |
| 250 |
else: |
| 251 |
if self.state == 'absent': |
| 252 |
self.to_do = Actions.Delete |
| 253 |
else: |
| 254 |
self.to_do = Actions.Update |
| 255 |
|
| 256 |
if self.to_do == Actions.Create: |
| 257 |
self.results['changed'] = True |
| 258 |
if self.check_mode: |
| 259 |
return self.results |
| 260 |
response = self.create_storage_share() |
| 261 |
elif self.to_do == Actions.Delete: |
| 262 |
self.results['changed'] = True |
| 263 |
if self.check_mode: |
| 264 |
return self.results |
| 265 |
response = self.delete_storage_share() |
| 266 |
elif self.to_do == Actions.Update: |
| 267 |
if self.update_needed(old_response): |
| 268 |
self.results['changed'] = True |
| 269 |
if self.check_mode: |
| 270 |
return self.results |
| 271 |
response = self.update_storage_share(old_response) |
| 272 |
else: |
| 273 |
self.results['changed'] = False |
| 274 |
response = old_response |
| 275 |
|
| 276 |
if response is not None: |
| 277 |
self.results['state'] = response |
| 278 |
else: |
| 279 |
self.results['state'] = dict() |
| 280 |
|
| 281 |
return self.results |
| 282 |
|
| 283 |
def update_needed(self, old_response): |
| 284 |
''' |
| 285 |
Define if storage file share update needed. |
| 286 |
:param old_response: dict with properties of the storage file share |
| 287 |
:return: True if update needed, else False |
| 288 |
''' |
| 289 |
return ((self.access_tier is not None) and (self.access_tier != old_response.get('access_tier')) or |
| 290 |
(self.quota is not None) and (self.quota != old_response.get('share_quota')) or |
| 291 |
(self.metadata is not None) and (self.metadata != old_response.get('metadata')) or |
| 292 |
(self.root_squash is not None) and (self.root_squash != old_response.get('root_squash')) or |
| 293 |
(self.enabled_protocols is not None) and (self.enabled_protocols != old_response.get('enabled_protocols'))) |
| 294 |
|
| 295 |
def get_share(self): |
| 296 |
''' |
| 297 |
Get the properties of the specified Azure Storage file share. |
| 298 |
:return: dict with properties of the storage file share |
| 299 |
''' |
| 300 |
found = False |
| 301 |
try: |
| 302 |
storage_share = self.storage_client.file_shares.get(resource_group_name=self.resource_group, |
| 303 |
account_name=self.account_name, |
| 304 |
share_name=self.name) |
| 305 |
found = True |
| 306 |
self.log("Response : {0}".format(storage_share)) |
| 307 |
except ResourceNotFoundError as e: |
| 308 |
self.log("Did not find the storage file share with name {0} : {1}".format(self.name, str(e))) |
| 309 |
return self.storage_share_to_dict(storage_share) if found else None |
| 310 |
|
| 311 |
def storage_share_to_dict(self, storage_share): |
| 312 |
''' |
| 313 |
Transform Azure RM Storage share object to dictionary |
| 314 |
:param storage_share: contains information about storage file share |
| 315 |
:type storage_share: FileShare |
| 316 |
:return: dict generated from storage_share |
| 317 |
''' |
| 318 |
return dict( |
| 319 |
id=storage_share.id, |
| 320 |
name=storage_share.name, |
| 321 |
type=storage_share.type, |
| 322 |
etag=storage_share.etag.replace('"', ''), |
| 323 |
last_modified_time=storage_share.last_modified_time, |
| 324 |
metadata=storage_share.metadata, |
| 325 |
share_quota=storage_share.share_quota, |
| 326 |
access_tier=storage_share.access_tier, |
| 327 |
access_tier_change_time=storage_share.access_tier_change_time, |
| 328 |
root_squash=storage_share.root_squash, |
| 329 |
enabled_protocols=storage_share.enabled_protocols |
| 330 |
) |
| 331 |
|
| 332 |
def create_storage_share(self): |
| 333 |
''' |
| 334 |
Method calling the Azure SDK to create storage file share. |
| 335 |
:return: dict with description of the new storage file share |
| 336 |
''' |
| 337 |
self.log("Creating fileshare {0}".format(self.name)) |
| 338 |
try: |
| 339 |
self.storage_client.file_shares.create(resource_group_name=self.resource_group, |
| 340 |
account_name=self.account_name, |
| 341 |
share_name=self.name, |
| 342 |
file_share=dict(access_tier=self.access_tier, |
| 343 |
share_quota=self.quota, |
| 344 |
metadata=self.metadata, |
| 345 |
root_squash=self.root_squash, |
| 346 |
enabled_protocols=self.enabled_protocols)) |
| 347 |
except Exception as e: |
| 348 |
self.fail("Error creating file share {0} : {1}".format(self.name, str(e))) |
| 349 |
return self.get_share() |
| 350 |
|
| 351 |
def update_storage_share(self, old_responce): |
| 352 |
''' |
| 353 |
Method calling the Azure SDK to update storage file share. |
| 354 |
:param old_response: dict with properties of the storage file share |
| 355 |
:return: dict with description of the new storage file share |
| 356 |
''' |
| 357 |
self.log("Creating file share {0}".format(self.name)) |
| 358 |
file_share_details = dict( |
| 359 |
access_tier=self.access_tier if self.access_tier else old_responce.get('access_tier'), |
| 360 |
share_quota=self.quota if self.quota else old_responce.get('share_quota'), |
| 361 |
metadata=self.metadata if self.metadata else old_responce.get('metadata'), |
| 362 |
enabled_protocols=self.enabled_protocols if self.enabled_protocols else old_responce.get('enabled_protocols'), |
| 363 |
root_squash=self.root_squash if self.root_squash else old_responce.get('self.root_squash') |
| 364 |
) |
| 365 |
try: |
| 366 |
self.storage_client.file_shares.update(resource_group_name=self.resource_group, |
| 367 |
account_name=self.account_name, |
| 368 |
share_name=self.name, |
| 369 |
file_share=file_share_details) |
| 370 |
except Exception as e: |
| 371 |
self.fail("Error updating file share {0} : {1}".format(self.name, str(e))) |
| 372 |
return self.get_share() |
| 373 |
|
| 374 |
def delete_storage_share(self): |
| 375 |
''' |
| 376 |
Method calling the Azure SDK to delete storage share. |
| 377 |
:return: object resulting from the original request |
| 378 |
''' |
| 379 |
try: |
| 380 |
self.storage_client.file_shares.delete(resource_group_name=self.resource_group, |
| 381 |
account_name=self.account_name, |
| 382 |
share_name=self.name) |
| 383 |
except Exception as e: |
| 384 |
self.fail("Error deleting file share {0} : {1}".format(self.name, str(e))) |
| 385 |
return self.get_share() |
| 386 |
|
| 387 |
|
| 388 |
def main(): |
| 389 |
AzureRMStorageShare() |
| 390 |
|
| 391 |
|
| 392 |
if __name__ == '__main__': |
| 393 |
main() |