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
|
"""Handles incoming s3tables requests, invokes methods, returns responses."""
import json
from typing import Any
from urllib.parse import unquote
from moto.core.common_types import TYPE_RESPONSE
from moto.core.responses import BaseResponse
from .models import S3TablesBackend, s3tables_backends
class S3TablesResponse(BaseResponse):
"""Handler for S3Tables requests and responses."""
def __init__(self) -> None:
super().__init__(service_name="s3tables")
self.default_response_headers = {"Content-Type": "application/json"}
@property
def s3tables_backend(self) -> S3TablesBackend:
"""Return backend instance specific for this region."""
return s3tables_backends[self.current_account][self.region]
def create_table_bucket(self) -> TYPE_RESPONSE:
name = json.loads(self.body)["name"]
bucket = self.s3tables_backend.create_table_bucket(
name=name,
)
return 200, self.default_response_headers, json.dumps({"arn": bucket.arn})
def list_table_buckets(self) -> TYPE_RESPONSE:
params = self._get_params()
prefix = params.get("prefix")
continuation_token = params.get("continuationToken")
max_buckets = params.get("maxBuckets")
table_buckets, continuation_token = self.s3tables_backend.list_table_buckets(
prefix=prefix,
continuation_token=continuation_token,
max_buckets=int(max_buckets) if max_buckets else None,
)
body: dict[str, Any] = {
"tableBuckets": [
{
"arn": b.arn,
"name": b.name,
"ownerAccountId": b.account_id,
"createdAt": b.creation_date.isoformat(),
}
for b in table_buckets
]
}
if continuation_token:
body.update(continuationToken=continuation_token)
return 200, self.default_response_headers, json.dumps(body)
def get_table_bucket(self) -> TYPE_RESPONSE:
_, table_bucket_arn = self.raw_path.lstrip("/").split("/")
table_bucket_arn = unquote(table_bucket_arn)
bucket = self.s3tables_backend.get_table_bucket(
table_bucket_arn=table_bucket_arn,
)
return (
200,
self.default_response_headers,
json.dumps(
{
"arn": bucket.arn,
"name": bucket.name,
"ownerAccountId": bucket.account_id,
"createdAt": bucket.creation_date.isoformat(),
}
),
)
def delete_table_bucket(self) -> TYPE_RESPONSE:
_, table_bucket_arn = self.raw_path.lstrip("/").split("/")
table_bucket_arn = unquote(table_bucket_arn)
self.s3tables_backend.delete_table_bucket(
table_bucket_arn=table_bucket_arn,
)
return 204, {}, ""
def create_namespace(self) -> TYPE_RESPONSE:
_, table_bucket_arn = self.raw_path.lstrip("/").split("/")
table_bucket_arn = unquote(table_bucket_arn)
name = json.loads(self.body)["namespace"][0]
namespace = self.s3tables_backend.create_namespace(
table_bucket_arn=table_bucket_arn,
namespace=name,
)
return (
200,
self.default_response_headers,
json.dumps(
{"tableBucketArn": table_bucket_arn, "namespace": [namespace.name]}
),
)
def list_namespaces(self) -> TYPE_RESPONSE:
_, table_bucket_arn = self.raw_path.lstrip("/").split("/")
table_bucket_arn = unquote(table_bucket_arn)
params = self._get_params()
continuation_token = params.get("continuationToken")
max_namespaces = params.get("maxNamespaces")
prefix = params.get("prefix")
namespaces, continuation_token = self.s3tables_backend.list_namespaces(
table_bucket_arn=table_bucket_arn,
prefix=prefix,
continuation_token=continuation_token,
max_namespaces=int(max_namespaces) if max_namespaces else None,
)
body: dict[str, Any] = {
"namespaces": [
{
"namespace": [ns.name],
"createdAt": ns.creation_date.isoformat(),
"createdBy": ns.created_by,
"ownerAccountId": ns.account_id,
}
for ns in namespaces
]
}
if continuation_token:
body.update(continuationToken=continuation_token)
return 200, self.default_response_headers, json.dumps(body)
def get_namespace(self) -> TYPE_RESPONSE:
_, table_bucket_arn, name = self.raw_path.lstrip("/").split("/")
table_bucket_arn = unquote(table_bucket_arn)
namespace = self.s3tables_backend.get_namespace(
table_bucket_arn=table_bucket_arn,
namespace=name,
)
return (
200,
self.default_response_headers,
json.dumps(
{
"namespace": [namespace.name],
"createdAt": namespace.creation_date.isoformat(),
"createdBy": namespace.created_by,
"ownerAccountId": namespace.account_id,
}
),
)
def delete_namespace(self) -> TYPE_RESPONSE:
_, table_bucket_arn, namespace = self.raw_path.lstrip("/").split("/")
table_bucket_arn = unquote(table_bucket_arn)
self.s3tables_backend.delete_namespace(
table_bucket_arn=table_bucket_arn,
namespace=namespace,
)
return 204, self.default_response_headers, ""
def create_table(self) -> TYPE_RESPONSE:
_, table_bucket_arn, namespace = self.raw_path.lstrip("/").split("/")
table_bucket_arn = unquote(table_bucket_arn)
body = json.loads(self.body)
name = body["name"]
format = body["format"]
table = self.s3tables_backend.create_table(
table_bucket_arn=table_bucket_arn,
namespace=namespace,
name=name,
format=format,
)
return (
200,
self.default_response_headers,
json.dumps({"tableARN": table.arn, "versionToken": table.version_token}),
)
def get_table(self) -> TYPE_RESPONSE:
table_bucket_arn = unquote(self._get_param("tableBucketARN"))
namespace = self._get_param("namespace")
name = self._get_param("name")
table = self.s3tables_backend.get_table(
table_bucket_arn=table_bucket_arn,
namespace=namespace,
name=name,
)
return (
200,
self.default_response_headers,
json.dumps(
{
"name": table.name,
"type": table.type,
"tableARN": table.arn,
"namespace": [namespace],
"versionToken": table.version_token,
"metadataLocation": table.metadata_location,
"warehouseLocation": table.warehouse_location,
"createdAt": table.creation_date.isoformat(),
"createdBy": table.account_id,
"managedByService": table.managed_by_service,
"modifiedAt": table.last_modified.isoformat(),
"modifiedBy": table.modified_by,
"ownerAccountId": table.account_id,
"format": table.format,
}
),
)
def list_tables(self) -> TYPE_RESPONSE:
_, table_bucket_arn = self.raw_path.lstrip("/").split("/")
table_bucket_arn = unquote(table_bucket_arn)
params = self._get_params()
namespace = params.get("namespace")
prefix = params.get("prefix")
continuation_token = params.get("continuationToken")
max_tables = params.get("maxTables")
tables, continuation_token = self.s3tables_backend.list_tables(
table_bucket_arn=table_bucket_arn,
namespace=namespace,
prefix=prefix,
continuation_token=continuation_token,
max_tables=int(max_tables) if max_tables else None,
)
body: dict[str, Any] = {
"tables": [
{
"namespace": [table.namespace],
"name": table.name,
"createdAt": table.creation_date.isoformat(),
"modifiedAt": table.last_modified.isoformat(),
}
for table in tables
]
}
if continuation_token:
body.update(continuationToken=continuation_token)
return 200, self.default_response_headers, json.dumps(body)
def delete_table(self) -> TYPE_RESPONSE:
_, table_bucket_arn, namespace, name = self.raw_path.lstrip("/").split("/")
table_bucket_arn = unquote(table_bucket_arn)
params = self._get_params()
version_token = params.get("versionToken")
self.s3tables_backend.delete_table(
table_bucket_arn=table_bucket_arn,
namespace=namespace,
name=name,
version_token=version_token,
)
return 204, {}, ""
def get_table_metadata_location(self) -> TYPE_RESPONSE:
_, table_bucket_arn, namespace, name, _ = self.raw_path.lstrip("/").split("/")
table_bucket_arn = unquote(table_bucket_arn)
table = self.s3tables_backend.get_table(
table_bucket_arn=table_bucket_arn,
namespace=namespace,
name=name,
)
return (
200,
self.default_response_headers,
json.dumps(
{
"versionToken": table.version_token,
"metadataLocation": table.metadata_location,
"warehouseLocation": table.warehouse_location,
}
),
)
def update_table_metadata_location(self) -> TYPE_RESPONSE:
_, table_bucket_arn, namespace, name, _ = self.raw_path.lstrip("/").split("/")
table_bucket_arn = unquote(table_bucket_arn)
body = json.loads(self.body)
metadata_location = body["metadataLocation"]
version_token = body["versionToken"]
table = self.s3tables_backend.update_table_metadata_location(
table_bucket_arn=table_bucket_arn,
namespace=namespace,
name=name,
version_token=version_token,
metadata_location=metadata_location,
)
return (
200,
self.default_response_headers,
json.dumps(
{
"name": table.name,
"tableArn": table.arn,
"namespace": namespace,
"versionToken": table.version_token,
"metadataLocation": table.metadata_location,
}
),
)
def rename_table(self) -> TYPE_RESPONSE:
_, table_bucket_arn, namespace, name, _ = self.raw_path.lstrip("/").split("/")
table_bucket_arn = unquote(table_bucket_arn)
body = json.loads(self.body)
version_token = body.get("versionToken")
new_namespace_name = body.get("newNamespaceName")
new_name = body.get("newName")
self.s3tables_backend.rename_table(
table_bucket_arn=table_bucket_arn,
namespace=namespace,
name=name,
new_namespace_name=new_namespace_name,
new_name=new_name,
version_token=version_token,
)
return 200, {}, ""
|