| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the Ansible project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
--- |
| 11 |
module: s3_object_info |
| 12 |
version_added: 5.0.0 |
| 13 |
short_description: Gather information about objects in S3 |
| 14 |
description: |
| 15 |
- Describes objects in S3. |
| 16 |
- Compatible with AWS, DigitalOcean, Ceph, Walrus, FakeS3 and StorageGRID (only supports list_keys currently). |
| 17 |
- When using non-AWS services, O(endpoint_url) should be specified. |
| 18 |
author: |
| 19 |
- Mandar Vijay Kulkarni (@mandar242) |
| 20 |
options: |
| 21 |
bucket_name: |
| 22 |
description: |
| 23 |
- The name of the bucket that contains the object. |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
object_name: |
| 27 |
description: |
| 28 |
- The name of the object. |
| 29 |
- If not specified, a list of all objects in the specified bucket will be returned. |
| 30 |
- Mutually exclusive with O(prefix). |
| 31 |
required: false |
| 32 |
type: str |
| 33 |
endpoint_url: |
| 34 |
description: |
| 35 |
- S3 URL endpoint for usage with Ceph, Eucalyptus and fakes3 etc. Otherwise assumes AWS. |
| 36 |
type: str |
| 37 |
dualstack: |
| 38 |
description: |
| 39 |
- Enables Amazon S3 Dual-Stack Endpoints, allowing S3 communications using both IPv4 and IPv6. |
| 40 |
- Mutually exclusive with O(endpoint_url). |
| 41 |
type: bool |
| 42 |
default: false |
| 43 |
ceph: |
| 44 |
description: |
| 45 |
- Enable API compatibility with Ceph RGW. |
| 46 |
- It takes into account the S3 API subset working with Ceph in order to provide the same module |
| 47 |
behaviour where possible. |
| 48 |
- Requires O(endpoint_url) if O(ceph=true). |
| 49 |
aliases: ['rgw'] |
| 50 |
default: false |
| 51 |
type: bool |
| 52 |
object_details: |
| 53 |
description: |
| 54 |
- Retrieve requested S3 object detailed information. |
| 55 |
required: false |
| 56 |
type: dict |
| 57 |
suboptions: |
| 58 |
object_acl: |
| 59 |
description: |
| 60 |
- Retreive S3 object ACL. |
| 61 |
required: false |
| 62 |
type: bool |
| 63 |
default: false |
| 64 |
object_legal_hold: |
| 65 |
description: |
| 66 |
- Retreive S3 object legal_hold. |
| 67 |
required: false |
| 68 |
type: bool |
| 69 |
default: false |
| 70 |
object_lock_configuration: |
| 71 |
description: |
| 72 |
- Retreive S3 object lock_configuration. |
| 73 |
required: false |
| 74 |
type: bool |
| 75 |
default: false |
| 76 |
object_retention: |
| 77 |
description: |
| 78 |
- Retreive S3 object retention. |
| 79 |
required: false |
| 80 |
type: bool |
| 81 |
default: false |
| 82 |
object_tagging: |
| 83 |
description: |
| 84 |
- Retreive S3 object Tags. |
| 85 |
required: false |
| 86 |
type: bool |
| 87 |
default: false |
| 88 |
object_attributes: |
| 89 |
description: |
| 90 |
- Retreive S3 object attributes. |
| 91 |
required: false |
| 92 |
type: bool |
| 93 |
default: false |
| 94 |
attributes_list: |
| 95 |
description: |
| 96 |
- The fields/details that should be returned. |
| 97 |
- Required when O(object_details.object_attributes=true). |
| 98 |
type: list |
| 99 |
elements: str |
| 100 |
choices: ['ETag', 'Checksum', 'ObjectParts', 'StorageClass', 'ObjectSize'] |
| 101 |
marker: |
| 102 |
description: |
| 103 |
- Specifies the Object key to start with. Object keys are returned in alphabetical order, starting with key |
| 104 |
after the marker in order. |
| 105 |
type: str |
| 106 |
version_added: 9.0.0 |
| 107 |
max_keys: |
| 108 |
description: |
| 109 |
- Max number of results to return. Set this if you want to retrieve only partial results. |
| 110 |
type: int |
| 111 |
version_added: 9.0.0 |
| 112 |
prefix: |
| 113 |
description: |
| 114 |
- Limits the response to keys that begin with the specified prefix. |
| 115 |
- Mutually exclusive with O(object_name). |
| 116 |
type: str |
| 117 |
version_added: 9.2.0 |
| 118 |
extends_documentation_fragment: |
| 119 |
- amazon.aws.common.modules |
| 120 |
- amazon.aws.region.modules |
| 121 |
- amazon.aws.boto3 |
| 122 |
""" |
| 123 |
|
| 124 |
EXAMPLES = r""" |
| 125 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 126 |
|
| 127 |
- name: Retrieve a list of objects in S3 bucket |
| 128 |
amazon.aws.s3_object_info: |
| 129 |
bucket_name: MyTestBucket |
| 130 |
|
| 131 |
- name: Retrieve a list of objects in Ceph RGW S3 |
| 132 |
amazon.aws.s3_object_info: |
| 133 |
bucket_name: MyTestBucket |
| 134 |
ceph: true |
| 135 |
endpoint_url: "http://localhost:8000" |
| 136 |
|
| 137 |
- name: Retrieve object metadata without object itself |
| 138 |
amazon.aws.s3_object_info: |
| 139 |
bucket_name: MyTestBucket |
| 140 |
object_name: MyTestObjectKey |
| 141 |
|
| 142 |
- name: Retrieve detailed S3 information for all objects in the bucket |
| 143 |
amazon.aws.s3_object_info: |
| 144 |
bucket_name: MyTestBucket |
| 145 |
object_details: |
| 146 |
object_acl: true |
| 147 |
object_attributes: true |
| 148 |
attributes_list: |
| 149 |
- ETag |
| 150 |
- ObjectSize |
| 151 |
- StorageClass |
| 152 |
|
| 153 |
- name: Retrieve detailed S3 object information |
| 154 |
amazon.aws.s3_object_info: |
| 155 |
bucket_name: MyTestBucket |
| 156 |
object_name: MyTestObjectKey |
| 157 |
object_details: |
| 158 |
object_acl: true |
| 159 |
object_tagging: true |
| 160 |
object_legal_hold: true |
| 161 |
object_attributes: true |
| 162 |
attributes_list: |
| 163 |
- ETag |
| 164 |
- ObjectSize |
| 165 |
|
| 166 |
- name: Retrieve keys that begin with the prefix /my/desired/ |
| 167 |
amazon.aws.s3_object_info: |
| 168 |
bucket: mybucket |
| 169 |
prefix: /my/desired/ |
| 170 |
""" |
| 171 |
|
| 172 |
RETURN = r""" |
| 173 |
s3_keys: |
| 174 |
description: List of object keys. |
| 175 |
returned: when only O(bucket_name) is specified and O(object_name), O(object_details) are not specified. |
| 176 |
type: list |
| 177 |
elements: str |
| 178 |
sample: |
| 179 |
- prefix1/ |
| 180 |
- prefix1/key1 |
| 181 |
- prefix1/key2 |
| 182 |
object_info: |
| 183 |
description: S3 object details. |
| 184 |
returned: when O(bucket_name) and O(object_name) are specified. |
| 185 |
type: list |
| 186 |
elements: dict |
| 187 |
contains: |
| 188 |
object_data: |
| 189 |
description: A dict containing the metadata of S3 object. |
| 190 |
returned: when O(bucket_name) and O(object_name) are specified but O(object_details) is not specified. |
| 191 |
type: dict |
| 192 |
elements: str |
| 193 |
contains: |
| 194 |
accept_ranges: |
| 195 |
description: Indicates that a range of bytes was specified. |
| 196 |
returned: always |
| 197 |
type: str |
| 198 |
content_length: |
| 199 |
description: Size of the body (object data) in bytes. |
| 200 |
returned: always |
| 201 |
type: int |
| 202 |
content_type: |
| 203 |
description: A standard MIME type describing the format of the object data. |
| 204 |
returned: always |
| 205 |
type: str |
| 206 |
e_tag: |
| 207 |
description: A opaque identifier assigned by a web server to a specific version of a resource found at a URL. |
| 208 |
returned: always |
| 209 |
type: str |
| 210 |
last_modified: |
| 211 |
description: Creation date of the object. |
| 212 |
returned: always |
| 213 |
type: str |
| 214 |
metadata: |
| 215 |
description: A map of metadata to store with the object in S3. |
| 216 |
returned: always |
| 217 |
type: dict |
| 218 |
server_side_encryption: |
| 219 |
description: The server-side encryption algorithm used when storing this object in Amazon S3. |
| 220 |
returned: always |
| 221 |
type: str |
| 222 |
tag_count: |
| 223 |
description: The number of tags, if any, on the object. |
| 224 |
returned: always |
| 225 |
type: int |
| 226 |
object_acl: |
| 227 |
description: Access control list (ACL) of an object. |
| 228 |
returned: when O(object_details.object_acl=true). |
| 229 |
type: complex |
| 230 |
contains: |
| 231 |
owner: |
| 232 |
description: Bucket owner's display ID and name. |
| 233 |
returned: always |
| 234 |
type: complex |
| 235 |
contains: |
| 236 |
id: |
| 237 |
description: Bucket owner's ID. |
| 238 |
returned: always |
| 239 |
type: str |
| 240 |
sample: "xxxxxxxxxxxxxxxxxxxxx" |
| 241 |
display_name: |
| 242 |
description: Bucket owner's display name. |
| 243 |
returned: always |
| 244 |
type: str |
| 245 |
sample: 'abcd' |
| 246 |
grants: |
| 247 |
description: A list of grants. |
| 248 |
returned: always |
| 249 |
type: complex |
| 250 |
contains: |
| 251 |
grantee: |
| 252 |
description: The entity being granted permissions. |
| 253 |
returned: always |
| 254 |
type: complex |
| 255 |
contains: |
| 256 |
id: |
| 257 |
description: The canonical user ID of the grantee. |
| 258 |
returned: always |
| 259 |
type: str |
| 260 |
sample: "xxxxxxxxxxxxxxxxxxx" |
| 261 |
type: |
| 262 |
description: type of grantee. |
| 263 |
returned: always |
| 264 |
type: str |
| 265 |
sample: "CanonicalUser" |
| 266 |
permission: |
| 267 |
description: Specifies the permission given to the grantee. |
| 268 |
returned: always |
| 269 |
type: str |
| 270 |
sample: "FULL CONTROL" |
| 271 |
object_legal_hold: |
| 272 |
description: Object's current legal hold status |
| 273 |
returned: when O(object_details.object_legal_hold=true) and object legal hold is set on the bucket. |
| 274 |
type: complex |
| 275 |
contains: |
| 276 |
legal_hold: |
| 277 |
description: The current legal hold status for the specified object. |
| 278 |
returned: always |
| 279 |
type: complex |
| 280 |
contains: |
| 281 |
status: |
| 282 |
description: Indicates whether the specified object has a legal hold in place. |
| 283 |
returned: always |
| 284 |
type: str |
| 285 |
sample: "ON" |
| 286 |
object_lock_configuration: |
| 287 |
description: Object Lock configuration for a bucket. |
| 288 |
returned: when O(object_details.object_lock_configuration=true) and object lock configuration is set on the bucket. |
| 289 |
type: complex |
| 290 |
contains: |
| 291 |
object_lock_enabled: |
| 292 |
description: Indicates whether this bucket has an Object Lock configuration enabled. |
| 293 |
returned: always |
| 294 |
type: str |
| 295 |
rule: |
| 296 |
description: Specifies the Object Lock rule for the specified object. |
| 297 |
returned: always |
| 298 |
type: complex |
| 299 |
contains: |
| 300 |
default_retention: |
| 301 |
description: The default Object Lock retention mode and period that you want to apply to new objects placed in the specified bucket. |
| 302 |
returned: always |
| 303 |
type: complex |
| 304 |
contains: |
| 305 |
mode: |
| 306 |
description: |
| 307 |
- The default Object Lock retention mode you want to apply to new objects placed in the specified bucket. |
| 308 |
- Must be used with either Days or Years. |
| 309 |
returned: always |
| 310 |
type: str |
| 311 |
days: |
| 312 |
description: The number of days that you want to specify for the default retention period. |
| 313 |
returned: always |
| 314 |
type: int |
| 315 |
years: |
| 316 |
description: The number of years that you want to specify for the default retention period. |
| 317 |
returned: always |
| 318 |
type: int |
| 319 |
object_retention: |
| 320 |
description: Object's retention settings. |
| 321 |
returned: when O(object_details.object_retention=true) and object retention is set on the bucket. |
| 322 |
type: complex |
| 323 |
contains: |
| 324 |
retention: |
| 325 |
description: The container element for an object's retention settings. |
| 326 |
returned: always |
| 327 |
type: complex |
| 328 |
contains: |
| 329 |
mode: |
| 330 |
description: Indicates the Retention mode for the specified object. |
| 331 |
returned: always |
| 332 |
type: str |
| 333 |
retain_until_date: |
| 334 |
description: The date on which this Object Lock Retention will expire. |
| 335 |
returned: always |
| 336 |
type: str |
| 337 |
object_tagging: |
| 338 |
description: The tag-set of an object |
| 339 |
returned: when O(object_details.object_tagging=true). |
| 340 |
type: dict |
| 341 |
object_attributes: |
| 342 |
description: Object attributes. |
| 343 |
returned: when O(object_details.object_attributes=true). |
| 344 |
type: complex |
| 345 |
contains: |
| 346 |
etag: |
| 347 |
description: An ETag is an opaque identifier assigned by a web server to a specific version of a resource found at a URL. |
| 348 |
returned: always |
| 349 |
type: str |
| 350 |
sample: "8fa34xxxxxxxxxxxxxxxxxxxxx35c6f3b" |
| 351 |
last_modified: |
| 352 |
description: The creation date of the object. |
| 353 |
returned: always |
| 354 |
type: str |
| 355 |
sample: "2022-08-10T01:11:03+00:00" |
| 356 |
object_size: |
| 357 |
description: The size of the object in bytes. |
| 358 |
returned: alwayS |
| 359 |
type: int |
| 360 |
sample: 819 |
| 361 |
checksum: |
| 362 |
description: The checksum or digest of the object. |
| 363 |
returned: always |
| 364 |
type: complex |
| 365 |
contains: |
| 366 |
checksum_crc32: |
| 367 |
description: The base64-encoded, 32-bit CRC32 checksum of the object. |
| 368 |
returned: if it was upload with the object. |
| 369 |
type: str |
| 370 |
sample: "xxxxxxxxxxxx" |
| 371 |
checksum_crc32c: |
| 372 |
description: The base64-encoded, 32-bit CRC32C checksum of the object. |
| 373 |
returned: if it was upload with the object. |
| 374 |
type: str |
| 375 |
sample: "xxxxxxxxxxxx" |
| 376 |
checksum_sha1: |
| 377 |
description: The base64-encoded, 160-bit SHA-1 digest of the object. |
| 378 |
returned: if it was upload with the object. |
| 379 |
type: str |
| 380 |
sample: "xxxxxxxxxxxx" |
| 381 |
checksum_sha256: |
| 382 |
description: The base64-encoded, 256-bit SHA-256 digest of the object. |
| 383 |
returned: if it was upload with the object. |
| 384 |
type: str |
| 385 |
sample: "xxxxxxxxxxxx" |
| 386 |
object_parts: |
| 387 |
description: A collection of parts associated with a multipart upload. |
| 388 |
returned: always |
| 389 |
type: complex |
| 390 |
contains: |
| 391 |
total_parts_count: |
| 392 |
description: The total number of parts. |
| 393 |
returned: always |
| 394 |
type: int |
| 395 |
part_number_marker: |
| 396 |
description: The marker for the current part. |
| 397 |
returned: always |
| 398 |
type: int |
| 399 |
next_part_number_marker: |
| 400 |
description: |
| 401 |
- When a list is truncated, this element specifies the last part in the list |
| 402 |
- As well as the value to use for the PartNumberMarker request parameter in a subsequent request. |
| 403 |
returned: always |
| 404 |
type: int |
| 405 |
max_parts: |
| 406 |
description: The maximum number of parts allowed in the response. |
| 407 |
returned: always |
| 408 |
type: int |
| 409 |
is_truncated: |
| 410 |
description: Indicates whether the returned list of parts is truncated. |
| 411 |
returned: always |
| 412 |
type: bool |
| 413 |
storage_class: |
| 414 |
description: The storage class information of the object. |
| 415 |
returned: always |
| 416 |
type: str |
| 417 |
sample: "STANDARD" |
| 418 |
parts: |
| 419 |
description: A container for elements related to an individual part. |
| 420 |
returned: always |
| 421 |
type: complex |
| 422 |
contains: |
| 423 |
part_number: |
| 424 |
description: The part number identifying the part. This value is a positive integer between 1 and 10,000. |
| 425 |
returned: always |
| 426 |
type: int |
| 427 |
size: |
| 428 |
description: The size of the uploaded part in bytes. |
| 429 |
returned: always |
| 430 |
type: int |
| 431 |
checksum_crc32: |
| 432 |
description: The base64-encoded, 32-bit CRC32 checksum of the object. |
| 433 |
returned: if it was upload with the object. |
| 434 |
type: str |
| 435 |
sample: "xxxxxxxxxxxx" |
| 436 |
checksum_crc32c: |
| 437 |
description: The base64-encoded, 32-bit CRC32C checksum of the object. |
| 438 |
returned: if it was upload with the object. |
| 439 |
type: str |
| 440 |
sample: "xxxxxxxxxxxx" |
| 441 |
checksum_sha1: |
| 442 |
description: The base64-encoded, 160-bit SHA-1 digest of the object. |
| 443 |
returned: if it was upload with the object. |
| 444 |
type: str |
| 445 |
sample: "xxxxxxxxxxxx" |
| 446 |
checksum_sha256: |
| 447 |
description: The base64-encoded, 256-bit SHA-256 digest of the object. |
| 448 |
returned: if it was upload with the object. |
| 449 |
type: str |
| 450 |
sample: "xxxxxxxxxxxx" |
| 451 |
""" |
| 452 |
|
| 453 |
import typing |
| 454 |
|
| 455 |
if typing.TYPE_CHECKING: |
| 456 |
from typing import Dict |
| 457 |
from typing import List |
| 458 |
|
| 459 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import ClientType |
| 460 |
|
| 461 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 462 |
|
| 463 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 464 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 465 |
from ansible_collections.amazon.aws.plugins.module_utils.s3 import AnsibleS3Error |
| 466 |
from ansible_collections.amazon.aws.plugins.module_utils.s3 import AnsibleS3SupportError |
| 467 |
from ansible_collections.amazon.aws.plugins.module_utils.s3 import get_s3_object_acl |
| 468 |
from ansible_collections.amazon.aws.plugins.module_utils.s3 import get_s3_object_attributes |
| 469 |
from ansible_collections.amazon.aws.plugins.module_utils.s3 import get_s3_object_legal_hold |
| 470 |
from ansible_collections.amazon.aws.plugins.module_utils.s3 import get_s3_object_lock_configuration |
| 471 |
from ansible_collections.amazon.aws.plugins.module_utils.s3 import get_s3_object_retention |
| 472 |
from ansible_collections.amazon.aws.plugins.module_utils.s3 import get_s3_object_tagging |
| 473 |
from ansible_collections.amazon.aws.plugins.module_utils.s3 import head_s3_object |
| 474 |
from ansible_collections.amazon.aws.plugins.module_utils.s3 import list_bucket_object_keys |
| 475 |
from ansible_collections.amazon.aws.plugins.module_utils.s3 import s3_bucket_exists |
| 476 |
from ansible_collections.amazon.aws.plugins.module_utils.s3 import s3_extra_params |
| 477 |
from ansible_collections.amazon.aws.plugins.module_utils.s3 import s3_object_exists |
| 478 |
|
| 479 |
|
| 480 |
def describe_s3_object_acl(connection: ClientType, bucket_name: str, object_name: str) -> Dict: |
| 481 |
"""Get object ACL.""" |
| 482 |
try: |
| 483 |
acl_info = get_s3_object_acl(connection, bucket_name, object_name) |
| 484 |
if acl_info: |
| 485 |
return camel_dict_to_snake_dict(acl_info) |
| 486 |
return {} |
| 487 |
except AnsibleS3SupportError: |
| 488 |
# Silent failure for unsupported operations on S3 drop-ins |
| 489 |
return {} |
| 490 |
except AnsibleS3Error: |
| 491 |
# Silent failure to maintain backward compatibility |
| 492 |
return {} |
| 493 |
|
| 494 |
|
| 495 |
def describe_s3_object_attributes( |
| 496 |
connection: ClientType, module: AnsibleAWSModule, bucket_name: str, object_name: str |
| 497 |
) -> Dict: |
| 498 |
"""Get object attributes.""" |
| 499 |
try: |
| 500 |
attributes_list = module.params.get("object_details", {}).get("attributes_list", []) |
| 501 |
if not attributes_list: |
| 502 |
return {} |
| 503 |
|
| 504 |
attrs = get_s3_object_attributes(connection, bucket_name, object_name, object_attributes=attributes_list) |
| 505 |
if attrs: |
| 506 |
return camel_dict_to_snake_dict(attrs) |
| 507 |
return {} |
| 508 |
except AnsibleS3SupportError: |
| 509 |
# Silent failure for unsupported operations on S3 drop-ins |
| 510 |
return {"msg": "Object attributes not found"} |
| 511 |
except AnsibleS3Error: |
| 512 |
return {"msg": "Object attributes not found"} |
| 513 |
|
| 514 |
|
| 515 |
def describe_s3_object_legal_hold(connection: ClientType, bucket_name: str, object_name: str) -> Dict: |
| 516 |
"""Get object legal hold status.""" |
| 517 |
try: |
| 518 |
legal_hold = get_s3_object_legal_hold(connection, bucket_name, object_name) |
| 519 |
if legal_hold: |
| 520 |
return {"legal_hold": camel_dict_to_snake_dict(legal_hold)} |
| 521 |
return {} |
| 522 |
except AnsibleS3SupportError: |
| 523 |
# Silent failure for unsupported operations on S3 drop-ins |
| 524 |
return {} |
| 525 |
except AnsibleS3Error: |
| 526 |
# Silent failure to maintain backward compatibility |
| 527 |
return {} |
| 528 |
|
| 529 |
|
| 530 |
def describe_s3_object_lock_configuration(connection: ClientType, bucket_name: str) -> Dict: |
| 531 |
"""Get bucket-level object lock configuration.""" |
| 532 |
try: |
| 533 |
lock_config = get_s3_object_lock_configuration(connection, bucket_name) |
| 534 |
if lock_config: |
| 535 |
return camel_dict_to_snake_dict(lock_config) |
| 536 |
return {} |
| 537 |
except AnsibleS3SupportError: |
| 538 |
# Silent failure for unsupported operations on S3 drop-ins |
| 539 |
return {} |
| 540 |
except AnsibleS3Error: |
| 541 |
# Silent failure to maintain backward compatibility |
| 542 |
return {} |
| 543 |
|
| 544 |
|
| 545 |
def describe_s3_object_retention(connection: ClientType, bucket_name: str, object_name: str) -> Dict: |
| 546 |
"""Get object retention settings.""" |
| 547 |
try: |
| 548 |
retention = get_s3_object_retention(connection, bucket_name, object_name) |
| 549 |
if retention: |
| 550 |
return {"retention": camel_dict_to_snake_dict(retention)} |
| 551 |
return {} |
| 552 |
except AnsibleS3SupportError: |
| 553 |
# Silent failure for unsupported operations on S3 drop-ins |
| 554 |
return {} |
| 555 |
except AnsibleS3Error: |
| 556 |
# Silent failure to maintain backward compatibility |
| 557 |
return {} |
| 558 |
|
| 559 |
|
| 560 |
def describe_s3_object_tagging(connection: ClientType, bucket_name: str, object_name: str) -> Dict: |
| 561 |
"""Get object tags.""" |
| 562 |
try: |
| 563 |
return get_s3_object_tagging(connection, bucket_name, object_name) |
| 564 |
except AnsibleS3SupportError: |
| 565 |
# Silent failure for unsupported operations on S3 drop-ins |
| 566 |
return {} |
| 567 |
except AnsibleS3Error: |
| 568 |
# Silent failure to maintain backward compatibility |
| 569 |
return {} |
| 570 |
|
| 571 |
|
| 572 |
def get_object_details( |
| 573 |
connection: ClientType, module: AnsibleAWSModule, bucket_name: str, object_name: str, requested_facts: Dict |
| 574 |
) -> Dict: |
| 575 |
all_facts = {} |
| 576 |
|
| 577 |
# Remove non-requested facts |
| 578 |
requested_facts = {fact: value for fact, value in requested_facts.items() if value is True} |
| 579 |
|
| 580 |
all_facts["object_data"] = get_object(connection, bucket_name, object_name)["object_data"] |
| 581 |
|
| 582 |
# Below APIs do not return object_name, need to add it manually |
| 583 |
all_facts["object_name"] = object_name |
| 584 |
|
| 585 |
for key in requested_facts: |
| 586 |
if key == "object_acl": |
| 587 |
all_facts[key] = describe_s3_object_acl(connection, bucket_name, object_name) |
| 588 |
elif key == "object_attributes": |
| 589 |
all_facts[key] = describe_s3_object_attributes(connection, module, bucket_name, object_name) |
| 590 |
elif key == "object_legal_hold": |
| 591 |
all_facts[key] = describe_s3_object_legal_hold(connection, bucket_name, object_name) |
| 592 |
elif key == "object_lock_configuration": |
| 593 |
all_facts[key] = describe_s3_object_lock_configuration(connection, bucket_name) |
| 594 |
elif key == "object_retention": |
| 595 |
all_facts[key] = describe_s3_object_retention(connection, bucket_name, object_name) |
| 596 |
elif key == "object_tagging": |
| 597 |
all_facts[key] = describe_s3_object_tagging(connection, bucket_name, object_name) |
| 598 |
|
| 599 |
return all_facts |
| 600 |
|
| 601 |
|
| 602 |
def get_object(connection: ClientType, bucket_name: str, object_name: str) -> Dict: |
| 603 |
"""Get basic object metadata.""" |
| 604 |
try: |
| 605 |
object_info = head_s3_object(connection, bucket_name, object_name) |
| 606 |
if object_info: |
| 607 |
object_info = camel_dict_to_snake_dict(object_info) |
| 608 |
return {"object_data": object_info} |
| 609 |
except AnsibleS3SupportError: |
| 610 |
# Silent failure for unsupported operations on S3 drop-ins |
| 611 |
return {"object_data": {}} |
| 612 |
except AnsibleS3Error: |
| 613 |
# Silent failure to maintain backward compatibility |
| 614 |
return {"object_data": {}} |
| 615 |
|
| 616 |
|
| 617 |
def list_bucket_objects(connection: ClientType, module: AnsibleAWSModule, bucket_name: str) -> List[str]: |
| 618 |
keys = list_bucket_object_keys( |
| 619 |
connection, |
| 620 |
bucket=bucket_name, |
| 621 |
max_keys=module.params["max_keys"], |
| 622 |
start_after=module.params["marker"], |
| 623 |
prefix=module.params["prefix"], |
| 624 |
) |
| 625 |
return keys |
| 626 |
|
| 627 |
|
| 628 |
def bucket_check(connection: ClientType, module: AnsibleAWSModule, bucket_name: str) -> None: |
| 629 |
"""Check if bucket exists and is accessible.""" |
| 630 |
if not s3_bucket_exists(connection, bucket_name): |
| 631 |
module.fail_json(msg=f"The bucket {bucket_name} does not exist.") |
| 632 |
|
| 633 |
|
| 634 |
def object_check(connection: ClientType, module: AnsibleAWSModule, bucket_name: str, object_name: str) -> None: |
| 635 |
"""Check if object exists and is accessible.""" |
| 636 |
if not s3_object_exists(connection, bucket_name, object_name): |
| 637 |
module.fail_json(msg=f"The object {object_name} does not exist.") |
| 638 |
|
| 639 |
|
| 640 |
def main() -> None: |
| 641 |
argument_spec = dict( |
| 642 |
object_details=dict( |
| 643 |
type="dict", |
| 644 |
options=dict( |
| 645 |
object_acl=dict(type="bool", default=False), |
| 646 |
object_legal_hold=dict(type="bool", default=False), |
| 647 |
object_lock_configuration=dict(type="bool", default=False), |
| 648 |
object_retention=dict(type="bool", default=False), |
| 649 |
object_tagging=dict(type="bool", default=False), |
| 650 |
object_attributes=dict(type="bool", default=False), |
| 651 |
attributes_list=dict( |
| 652 |
type="list", |
| 653 |
elements="str", |
| 654 |
choices=["ETag", "Checksum", "ObjectParts", "StorageClass", "ObjectSize"], |
| 655 |
), |
| 656 |
), |
| 657 |
required_if=[ |
| 658 |
("object_attributes", True, ["attributes_list"]), |
| 659 |
], |
| 660 |
), |
| 661 |
bucket_name=dict(required=True, type="str"), |
| 662 |
object_name=dict(type="str"), |
| 663 |
dualstack=dict(default=False, type="bool"), |
| 664 |
ceph=dict(default=False, type="bool", aliases=["rgw"]), |
| 665 |
marker=dict(), |
| 666 |
max_keys=dict(type="int", no_log=False), |
| 667 |
prefix=dict(type="str", required=False), |
| 668 |
) |
| 669 |
|
| 670 |
required_if = [ |
| 671 |
["ceph", True, ["endpoint_url"]], |
| 672 |
] |
| 673 |
|
| 674 |
mutually_exclusive = [ |
| 675 |
["object_name", "prefix"], |
| 676 |
["dualstack", "endpoint_url"], |
| 677 |
] |
| 678 |
mutually_exclusive = [["object_name", "prefix"], ["dualstack", "endpoint_url"]] |
| 679 |
|
| 680 |
module = AnsibleAWSModule( |
| 681 |
argument_spec=argument_spec, |
| 682 |
supports_check_mode=True, |
| 683 |
required_if=required_if, |
| 684 |
mutually_exclusive=mutually_exclusive, |
| 685 |
) |
| 686 |
|
| 687 |
bucket_name = module.params.get("bucket_name") |
| 688 |
object_name = module.params.get("object_name") |
| 689 |
requested_object_details = module.params.get("object_details") |
| 690 |
|
| 691 |
result = [] |
| 692 |
extra_params = s3_extra_params(module.params) |
| 693 |
retry_decorator = AWSRetry.jittered_backoff() |
| 694 |
connection = module.client("s3", retry_decorator=retry_decorator, **extra_params) |
| 695 |
|
| 696 |
# check if specified bucket exists |
| 697 |
bucket_check(connection, module, bucket_name) |
| 698 |
# check if specified object exists |
| 699 |
if object_name: |
| 700 |
object_check(connection, module, bucket_name, object_name) |
| 701 |
|
| 702 |
if requested_object_details: |
| 703 |
if object_name: |
| 704 |
object_details = get_object_details(connection, module, bucket_name, object_name, requested_object_details) |
| 705 |
result.append(object_details) |
| 706 |
elif object_name is None: |
| 707 |
object_list = list_bucket_objects(connection, module, bucket_name) |
| 708 |
for bucket_object in object_list: |
| 709 |
result.append( |
| 710 |
get_object_details(connection, module, bucket_name, bucket_object, requested_object_details) |
| 711 |
) |
| 712 |
|
| 713 |
elif not requested_object_details and object_name: |
| 714 |
# if specific details are not requested, return object metadata |
| 715 |
object_details = get_object(connection, bucket_name, object_name) |
| 716 |
result.append(object_details) |
| 717 |
else: |
| 718 |
# return list of all objects in a bucket if object name and object details not specified |
| 719 |
object_list = list_bucket_objects(connection, module, bucket_name) |
| 720 |
module.exit_json(s3_keys=object_list) |
| 721 |
|
| 722 |
module.exit_json(object_info=result) |
| 723 |
|
| 724 |
|
| 725 |
if __name__ == "__main__": |
| 726 |
main() |