| 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_storageblob_info |
| 14 |
short_description: Get or list the containers blob facts |
| 15 |
version_added: "3.0.0" |
| 16 |
description: |
| 17 |
- Get or list the blobs under the specified container. |
| 18 |
options: |
| 19 |
auth_mode: |
| 20 |
description: |
| 21 |
- The mode in which to run the command. C(login) mode will directly use your login credentials for the authentication. |
| 22 |
- The legacy C(key) mode will attempt to query for an account key if no authentication parameters for the account are provided. |
| 23 |
- Can also be set via the environment variable C(AZURE_STORAGE_AUTH_MODE). |
| 24 |
default: key |
| 25 |
type: str |
| 26 |
choices: |
| 27 |
- key |
| 28 |
- login |
| 29 |
storage_account_name: |
| 30 |
description: |
| 31 |
- Name of the storage account to use. |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
aliases: |
| 35 |
- account_name |
| 36 |
- storage_account |
| 37 |
blob_name: |
| 38 |
description: |
| 39 |
- Name of a blob object within the container. |
| 40 |
aliases: |
| 41 |
- blob |
| 42 |
type: str |
| 43 |
container_name: |
| 44 |
description: |
| 45 |
- Name of a blob container within the storage account. |
| 46 |
required: true |
| 47 |
type: str |
| 48 |
aliases: |
| 49 |
- container |
| 50 |
resource_group: |
| 51 |
description: |
| 52 |
- Name of the resource group to use. |
| 53 |
required: true |
| 54 |
type: str |
| 55 |
aliases: |
| 56 |
- resource_group_name |
| 57 |
name_starts_with: |
| 58 |
description: |
| 59 |
- Filters the results to return only blobs whose names begin with the specified prefix. |
| 60 |
type: str |
| 61 |
include: |
| 62 |
description: |
| 63 |
- Specifies one or more additional datasets to include in the response. |
| 64 |
type: list |
| 65 |
elements: str |
| 66 |
choices: |
| 67 |
- snapshots |
| 68 |
- metadata |
| 69 |
- uncommittedblobs |
| 70 |
- copy |
| 71 |
- deleted |
| 72 |
- deletedwithversions |
| 73 |
- tags |
| 74 |
- versions |
| 75 |
- immutabilitypolicy |
| 76 |
- legalhold |
| 77 |
extends_documentation_fragment: |
| 78 |
- azure.azcollection.azure |
| 79 |
|
| 80 |
author: |
| 81 |
- xuzhang3 (@xuzhang3) |
| 82 |
- Fred-sun (@Fred-sun) |
| 83 |
|
| 84 |
''' |
| 85 |
|
| 86 |
EXAMPLES = ''' |
| 87 |
- name: Get the blob facts by name |
| 88 |
azure_rm_storageblob_info: |
| 89 |
resource_group: "{{ resource_group }}" |
| 90 |
account_name: "{{ storage_account }}" |
| 91 |
container_name: my-blobs |
| 92 |
blob_name: blobname01 |
| 93 |
|
| 94 |
- name: List the blob facts in specify container |
| 95 |
azure_rm_storageblob_info: |
| 96 |
resource_group: "{{ resource_group }}" |
| 97 |
account_name: "{{ storage_account }}" |
| 98 |
container_name: my-blobs |
| 99 |
''' |
| 100 |
|
| 101 |
RETURN = ''' |
| 102 |
blob: |
| 103 |
description: |
| 104 |
- Facts about the current state of the blob. |
| 105 |
returned: when a blob is operated on |
| 106 |
type: dict |
| 107 |
sample: { |
| 108 |
"content_length": 136532, |
| 109 |
"content_settings": { |
| 110 |
"cache_control": null, |
| 111 |
"content_disposition": null, |
| 112 |
"content_encoding": null, |
| 113 |
"content_language": null, |
| 114 |
"content_md5": null, |
| 115 |
"content_type": "application/image" |
| 116 |
}, |
| 117 |
"last_modified": "20-11-2024 22:08:25 +0000", |
| 118 |
"name": "graylog.png", |
| 119 |
'metadata': {'key1': 'value1'}, |
| 120 |
"standard_blob_tier": "Hot", |
| 121 |
"tags": {}, |
| 122 |
"type": "BlockBlob" |
| 123 |
} |
| 124 |
''' |
| 125 |
|
| 126 |
|
| 127 |
try: |
| 128 |
from azure.core.exceptions import ResourceNotFoundError |
| 129 |
except ImportError: |
| 130 |
|
| 131 |
pass |
| 132 |
|
| 133 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 134 |
from ansible.module_utils.basic import env_fallback |
| 135 |
|
| 136 |
|
| 137 |
class AzureRMStorageBlobInfo(AzureRMModuleBase): |
| 138 |
|
| 139 |
def __init__(self): |
| 140 |
|
| 141 |
self.module_arg_spec = dict( |
| 142 |
auth_mode=dict( |
| 143 |
type='str', |
| 144 |
choices=['key', 'login'], |
| 145 |
fallback=(env_fallback, ['AZURE_STORAGE_AUTH_MODE']), |
| 146 |
default="key" |
| 147 |
), |
| 148 |
storage_account_name=dict(required=True, type='str', aliases=['account_name', 'storage_account']), |
| 149 |
blob_name=dict(type='str', aliases=['blob']), |
| 150 |
container_name=dict(required=True, type='str', aliases=['container']), |
| 151 |
resource_group=dict(required=True, type='str', aliases=['resource_group_name']), |
| 152 |
name_starts_with=dict(type='str'), |
| 153 |
include=dict( |
| 154 |
type='list', |
| 155 |
elements='str', |
| 156 |
choices=['snapshots', 'metadata', 'uncommittedblobs', 'copy', 'deleted', |
| 157 |
'deletedwithversions', 'tags', 'versions', 'immutabilitypolicy', 'legalhold'] |
| 158 |
), |
| 159 |
) |
| 160 |
|
| 161 |
self.blob_service_client = None |
| 162 |
self.storage_account_name = None |
| 163 |
self.blob_name = None |
| 164 |
self.container_name = None |
| 165 |
self.resource_group = None |
| 166 |
self.name_starts_with = None |
| 167 |
self.include = None |
| 168 |
self.results = dict( |
| 169 |
changed=False, |
| 170 |
blob=dict() |
| 171 |
) |
| 172 |
|
| 173 |
super(AzureRMStorageBlobInfo, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 174 |
supports_check_mode=True, |
| 175 |
supports_tags=False) |
| 176 |
|
| 177 |
def exec_module(self, **kwargs): |
| 178 |
|
| 179 |
for key in list(self.module_arg_spec.keys()): |
| 180 |
setattr(self, key, kwargs[key]) |
| 181 |
|
| 182 |
self.blob_service_client = self.get_blob_service_client(self.resource_group, self.storage_account_name, self.auth_mode) |
| 183 |
if self.blob_name: |
| 184 |
response = self.get_blob() |
| 185 |
else: |
| 186 |
response = self.list_blob() |
| 187 |
self.results['blob'] = response |
| 188 |
return self.results |
| 189 |
|
| 190 |
def get_blob(self): |
| 191 |
response = None |
| 192 |
if self.blob_name: |
| 193 |
try: |
| 194 |
response = self.blob_service_client.get_blob_client(container=self.container_name, blob=self.blob_name).get_blob_properties() |
| 195 |
except ResourceNotFoundError: |
| 196 |
pass |
| 197 |
return self.format_blob(response) if response else None |
| 198 |
|
| 199 |
def list_blob(self): |
| 200 |
response = [] |
| 201 |
try: |
| 202 |
client = self.blob_service_client.get_container_client(container=self.container_name) |
| 203 |
blobs = client.list_blobs(name_starts_with=self.name_starts_with, include=self.include) |
| 204 |
for blob in blobs: |
| 205 |
response.append(self.format_blob(blob)) |
| 206 |
except Exception as exc: |
| 207 |
self.fail("Error list container blob {0} - {1}".format(self.container_name, str(exc))) |
| 208 |
return response |
| 209 |
|
| 210 |
def format_blob(self, blob): |
| 211 |
result = dict( |
| 212 |
name=blob["name"], |
| 213 |
tags=blob["metadata"], |
| 214 |
last_modified=blob["last_modified"].strftime('%d-%b-%Y %H:%M:%S %z'), |
| 215 |
type=blob["blob_type"], |
| 216 |
standard_blob_tier=blob.get('blob_tier'), |
| 217 |
metadata=blob.get('metadata'), |
| 218 |
content_length=blob["size"], |
| 219 |
content_settings=dict( |
| 220 |
content_type=blob["content_settings"]["content_type"], |
| 221 |
content_encoding=blob["content_settings"]["content_encoding"], |
| 222 |
content_language=blob["content_settings"]["content_language"], |
| 223 |
content_disposition=blob["content_settings"]["content_disposition"], |
| 224 |
cache_control=blob["content_settings"]["cache_control"], |
| 225 |
content_md5=blob["content_settings"]["content_md5"].hex() if blob["content_settings"]["content_md5"] else None, |
| 226 |
) |
| 227 |
) |
| 228 |
return result |
| 229 |
|
| 230 |
|
| 231 |
def main(): |
| 232 |
AzureRMStorageBlobInfo() |
| 233 |
|
| 234 |
|
| 235 |
if __name__ == '__main__': |
| 236 |
main() |