1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
"""S3TablesBackend class with methods for supported APIs."""
import datetime
import re
from hashlib import md5
from typing import Literal, Optional, Union
from moto.core.base_backend import BackendDict, BaseBackend
from moto.moto_api._internal import mock_random as random
from moto.s3.models import FakeBucket
from moto.s3tables.exceptions import (
ConflictException,
DestinationNamespaceDoesNotExist,
InvalidContinuationToken,
InvalidMetadataLocation,
InvalidNamespaceName,
InvalidTableBucketName,
InvalidTableName,
NotFoundException,
NothingToRename,
TableAlreadyExists,
TableDoesNotExist,
VersionTokenMismatch,
)
from moto.utilities.paginator import paginate
from moto.utilities.utils import get_partition
# https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-tables-buckets-naming.html
TABLE_BUCKET_NAME_PATTERN = re.compile(r"[a-z0-9_-]{3,63}")
TABLE_BUCKET_NAME_RESERVED_PREFIXES = ("xn--", "sthree-", "amzn-s3-demo")
TABLE_BUCKET_NAME_RESERVED_SUFFIXES = ("-s3alias", "--ol-s3", "--x-s3")
NAMESPACE_NAME_PATTERN = re.compile(r"[0-9a-z_]*")
TABLE_NAME_PATTERN = re.compile(r"[0-9a-z_]*")
def _validate_table_bucket_name(name: str) -> None:
if (
not TABLE_BUCKET_NAME_PATTERN.match(name)
or any(
name.startswith(prefix) for prefix in TABLE_BUCKET_NAME_RESERVED_PREFIXES
)
or any(name.endswith(suffix) for suffix in TABLE_BUCKET_NAME_RESERVED_SUFFIXES)
):
raise InvalidTableBucketName()
def _validate_namespace_name(name: str) -> None:
if not NAMESPACE_NAME_PATTERN.match(name):
raise InvalidNamespaceName()
def _validate_table_name(name: str) -> None:
if not TABLE_NAME_PATTERN.match(name):
raise InvalidTableName(name)
PAGINATION_MODEL = {
"list_table_buckets": {
"input_token": "continuation_token",
"limit_key": "max_buckets",
"limit_default": 1000,
"unique_attribute": ["arn"],
"fail_on_invalid_token": InvalidContinuationToken,
},
"list_namespaces": {
"input_token": "continuation_token",
"limit_key": "max_namespaces",
"limit_default": 1000,
"unique_attribute": ["name"],
"fail_on_invalid_token": InvalidContinuationToken,
},
"list_tables": {
"input_token": "continuation_token",
"limit_key": "max_tables",
"limit_default": 1000,
"unique_attribute": ["arn"],
"fail_on_invalid_token": InvalidContinuationToken,
},
}
class Table:
def __init__(
self,
name: str,
account_id: str,
created_by: str,
format: Literal["ICEBERG"],
namespace: str,
table_bucket_arn: str,
type: Union[Literal["customer"], Literal["aws"]],
managed_by_service: bool,
partition: str,
):
_validate_table_name(name)
self.name = name
self.account_id = account_id
self.partition = partition
self.created_by = created_by
self.format = format
self.type = type
self.version_token = self._generate_version_token()
self.creation_date = datetime.datetime.now(tz=datetime.timezone.utc)
self.last_modified = self.creation_date
self.modified_by: Optional[str] = None
self.namespace = namespace
self.table_bucket_arn = table_bucket_arn
self.region_name = table_bucket_arn.split(":")[3]
self.managed_by_service = managed_by_service
self.metadata_location: Optional[str] = None
self._bucket = self._create_underlying_bucket()
self.warehouse_location: str = f"s3://{self._bucket.name}"
@property
def arn(self) -> str:
return f"{self.table_bucket_arn}/table/{self.name}"
def was_modified(self, by: str) -> None:
self.last_modified = datetime.datetime.now(tz=datetime.timezone.utc)
self.modified_by = by
def _generate_version_token(self) -> str:
return md5(random.uuid4().bytes).hexdigest()[:20]
def _create_underlying_bucket(self) -> FakeBucket:
from moto.s3.models import s3_backends
bucket = s3_backends[self.account_id][
self.partition
].create_table_storage_bucket(region_name=self.region_name)
return bucket
def update_metadata_location(
self, metadata_location: str, version_token: str
) -> None:
if not metadata_location.startswith(self.warehouse_location):
raise InvalidMetadataLocation()
if not self.version_token == version_token:
raise VersionTokenMismatch()
self.metadata_location = metadata_location
self.version_token = self._generate_version_token()
def rename(self, new_name: str, by: str) -> None:
_validate_table_name(new_name)
self.name = new_name
self.was_modified(by)
class Namespace:
def __init__(self, name: str, account_id: str, created_by: str):
_validate_namespace_name(name)
self.name = name
self.account_id = account_id
self.created_by = created_by
self.creation_date = datetime.datetime.now(tz=datetime.timezone.utc)
self.tables: dict[str, Table] = {}
class FakeTableBucket:
def __init__(self, name: str, account_id: str, region_name: str):
self.name = name
self.account_id = account_id
self.region_name = region_name
self.partition = get_partition(region_name)
self.creation_date = datetime.datetime.now(tz=datetime.timezone.utc)
self.namespaces: dict[str, Namespace] = {}
@property
def arn(self) -> str:
return f"arn:{self.partition}:s3tables:{self.region_name}:{self.account_id}:bucket/{self.name}"
class S3TablesBackend(BaseBackend):
"""Implementation of S3Tables APIs."""
def __init__(self, region_name: str, account_id: str) -> None:
super().__init__(region_name, account_id)
self.table_buckets: dict[str, FakeTableBucket] = {}
def create_table_bucket(self, name: str) -> FakeTableBucket:
_validate_table_bucket_name(name)
new_table_bucket = FakeTableBucket(
name=name, account_id=self.account_id, region_name=self.region_name
)
if new_table_bucket.arn in self.table_buckets:
raise ConflictException(
"The bucket that you tried to create already exists, and you own it."
)
self.table_buckets[new_table_bucket.arn] = new_table_bucket
return new_table_bucket
@paginate(pagination_model=PAGINATION_MODEL)
def list_table_buckets(
self,
prefix: Optional[str] = None,
) -> list[FakeTableBucket]:
all_buckets = [
bucket
for bucket in self.table_buckets.values()
if (prefix is None or bucket.name.startswith(prefix))
]
return all_buckets
def get_table_bucket(self, table_bucket_arn: str) -> FakeTableBucket:
bucket = self.table_buckets.get(table_bucket_arn)
if not bucket:
raise NotFoundException("The specified bucket does not exist.")
return bucket
def delete_table_bucket(self, table_bucket_arn: str) -> None:
# make sure table exists first
self.get_table_bucket(table_bucket_arn)
self.table_buckets.pop(table_bucket_arn)
def create_namespace(self, table_bucket_arn: str, namespace: str) -> Namespace:
bucket = self.table_buckets.get(table_bucket_arn)
if not bucket:
raise NotFoundException(
"The request was rejected because the specified resource could not be found."
)
if namespace in bucket.namespaces:
raise ConflictException(
"A namespace with an identical name already exists in the bucket."
)
ns = Namespace(
namespace, account_id=self.account_id, created_by=self.account_id
)
bucket.namespaces[ns.name] = ns
return ns
@paginate(pagination_model=PAGINATION_MODEL)
def list_namespaces(
self,
table_bucket_arn: str,
prefix: Optional[str] = None,
) -> list[Namespace]:
bucket = self.get_table_bucket(table_bucket_arn)
all_namespaces = [
ns
for ns in bucket.namespaces.values()
if (prefix is None or ns.name.startswith(prefix))
]
return all_namespaces
def get_namespace(self, table_bucket_arn: str, namespace: str) -> Namespace:
bucket = self.table_buckets.get(table_bucket_arn)
if bucket and namespace in bucket.namespaces:
return bucket.namespaces[namespace]
raise NotFoundException(
"The request was rejected because the specified resource could not be found."
)
def delete_namespace(self, table_bucket_arn: str, namespace: str) -> None:
bucket = self.table_buckets.get(table_bucket_arn)
if bucket and namespace in bucket.namespaces:
bucket.namespaces.pop(namespace)
return
raise NotFoundException(
"The request was rejected because the specified resource could not be found."
)
def create_table(
self,
table_bucket_arn: str,
namespace: str,
name: str,
format: Literal["ICEBERG"],
) -> Table:
bucket = self.table_buckets.get(table_bucket_arn)
if not bucket:
raise NotFoundException("The specified bucket does not exist.")
if namespace not in bucket.namespaces:
raise NotFoundException("The specified namespace does not exist.")
ns = bucket.namespaces[namespace]
if name in ns.tables:
TableAlreadyExists()
table = Table(
name=name,
account_id=self.account_id,
created_by=self.account_id,
format=format,
namespace=namespace,
table_bucket_arn=table_bucket_arn,
type="customer",
managed_by_service=False,
partition=self.partition,
)
ns.tables[table.name] = table
return table
def get_table(self, table_bucket_arn: str, namespace: str, name: str) -> Table:
bucket = self.table_buckets.get(table_bucket_arn)
if bucket and namespace in bucket.namespaces:
if name in bucket.namespaces[namespace].tables:
return bucket.namespaces[namespace].tables[name]
raise TableDoesNotExist()
@paginate(pagination_model=PAGINATION_MODEL)
def list_tables(
self,
table_bucket_arn: str,
namespace: Optional[str] = None,
prefix: Optional[str] = None,
) -> list[Table]:
bucket = self.table_buckets.get(table_bucket_arn)
if not bucket or (namespace and namespace not in bucket.namespaces):
raise NotFoundException(
"The request was rejected because the specified resource could not be found."
)
if namespace:
all_tables = [
table
for table in bucket.namespaces[namespace].tables.values()
if (prefix is None or table.name.startswith(prefix))
]
else:
all_tables = [
table
for ns in bucket.namespaces.values()
for table in ns.tables.values()
if (prefix is None or table.name.startswith(prefix))
]
return all_tables
def delete_table(
self,
table_bucket_arn: str,
namespace: str,
name: str,
version_token: Optional[str] = None,
) -> None:
bucket = self.table_buckets.get(table_bucket_arn)
if (
not bucket
or namespace not in bucket.namespaces
or (name not in bucket.namespaces[namespace].tables)
):
raise TableDoesNotExist()
ns = bucket.namespaces[namespace]
table = ns.tables[name]
if version_token and not version_token == table.version_token:
raise VersionTokenMismatch()
from moto.s3.models import s3_backends
s3_backends[self.account_id][self.partition].delete_table_storage_bucket(
table._bucket.name
)
ns.tables.pop(name)
def update_table_metadata_location(
self,
table_bucket_arn: str,
namespace: str,
name: str,
version_token: str,
metadata_location: str,
) -> Table:
bucket = self.table_buckets.get(table_bucket_arn)
if bucket and namespace in bucket.namespaces:
if name in bucket.namespaces[namespace].tables:
table = bucket.namespaces[namespace].tables[name]
table.update_metadata_location(
metadata_location=metadata_location, version_token=version_token
)
return table
raise TableDoesNotExist()
def rename_table(
self,
table_bucket_arn: str,
namespace: str,
name: str,
new_namespace_name: Optional[str] = None,
new_name: Optional[str] = None,
version_token: Optional[str] = None,
) -> None:
if not new_namespace_name and not new_name:
raise NothingToRename()
destination_namespace = new_namespace_name if new_namespace_name else namespace
destination_name = new_name if new_name else name
_validate_table_name(destination_name)
bucket = self.table_buckets.get(table_bucket_arn)
if not bucket or destination_namespace not in bucket.namespaces:
raise DestinationNamespaceDoesNotExist()
if namespace not in bucket.namespaces or (
name not in bucket.namespaces[namespace].tables
):
raise TableDoesNotExist()
table = bucket.namespaces[namespace].tables[name]
if version_token and not version_token == table.version_token:
raise VersionTokenMismatch()
if destination_name in bucket.namespaces[destination_namespace].tables:
raise TableAlreadyExists()
table = bucket.namespaces[namespace].tables.pop(name)
table.rename(new_name=destination_name, by=self.account_id)
bucket.namespaces[destination_namespace].tables[destination_name] = table
s3tables_backends = BackendDict(
S3TablesBackend,
"s3tables",
additional_regions=["us-east-1", "us-east-2", "us-west-2"],
)
|