aws-python-helper 1.8.1__tar.gz → 1.9.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/PKG-INFO +1 -1
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/__init__.py +2 -1
- aws_python_helper-1.9.1/aws_python_helper/monitoring/__init__.py +3 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/monitoring/run.py +67 -19
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/repository/audit.py +22 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/sns/publisher.py +40 -18
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper.egg-info/PKG-INFO +1 -1
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper.egg-info/SOURCES.txt +2 -1
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/pyproject.toml +1 -1
- aws_python_helper-1.9.1/tests/test_audit_stamp.py +88 -0
- aws_python_helper-1.8.1/aws_python_helper/monitoring/__init__.py +0 -3
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/README.md +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/api/__init__.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/api/auth_middleware.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/api/auth_validators.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/api/base.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/api/dispatcher.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/api/exceptions.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/api/fetcher.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/api/handler.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/context/__init__.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/context/session.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/context/state_validator.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/database/__init__.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/database/database_proxy.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/database/external_database_proxy.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/database/external_mongo_manager.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/database/mongo_manager.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/fargate/__init__.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/fargate/executor.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/fargate/fetcher.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/fargate/handler.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/fargate/task_base.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/invoker/__init__.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/invoker/api_client.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/invoker/exceptions.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/invoker/lambda_invoker.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/lambda_standalone/__init__.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/lambda_standalone/base.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/lambda_standalone/fetcher.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/lambda_standalone/handler.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/model_index_sync/__init__.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/model_index_sync/base.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/model_query/__init__.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/model_query/base.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/permissions/__init__.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/permissions/catalog.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/repository/__init__.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/repository/base.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/sns/__init__.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/sqs/__init__.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/sqs/consumer_base.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/sqs/fetcher.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/sqs/handler.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/utils/__init__.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/utils/json_encoder.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/utils/response.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/utils/serializer.py +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper.egg-info/dependency_links.txt +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper.egg-info/requires.txt +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper.egg-info/top_level.txt +0 -0
- {aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/setup.cfg +0 -0
|
@@ -57,11 +57,12 @@ from .invoker import (
|
|
|
57
57
|
)
|
|
58
58
|
|
|
59
59
|
|
|
60
|
-
from .monitoring import process_run, RunReporter, ProcessRunPublisher
|
|
60
|
+
from .monitoring import process_run, resolve_code, RunReporter, ProcessRunPublisher
|
|
61
61
|
|
|
62
62
|
__all__ = [
|
|
63
63
|
'API',
|
|
64
64
|
'process_run',
|
|
65
|
+
'resolve_code',
|
|
65
66
|
'RunReporter',
|
|
66
67
|
'ProcessRunPublisher',
|
|
67
68
|
'SQSConsumer',
|
|
@@ -4,10 +4,21 @@ Two hand-written publishes would work, and the second one would get forgotten on
|
|
|
4
4
|
the path that matters: the failure. So the boundary is a context manager — enter
|
|
5
5
|
and it says it started, leave and it says how it went, exception included.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
The process code is the name of the AWS resource doing the work, taken from the
|
|
8
|
+
environment. Nobody types it twice: a code invented here and typed again in the
|
|
9
|
+
panel is a code that will not match one day, and a mismatch is silent — the runs
|
|
10
|
+
arrive, core finds no process, and nothing is recorded. Taking it from the
|
|
11
|
+
environment means the panel can offer the real resource list and the two sides
|
|
12
|
+
cannot drift.
|
|
13
|
+
|
|
14
|
+
Nothing here decides whether a run is kept. It publishes; core has the database
|
|
15
|
+
and filters. Wrapping something in `process_run` is not turning monitoring on —
|
|
16
|
+
registering the process in the panel is.
|
|
17
|
+
|
|
18
|
+
Nothing here can raise into the caller either. Monitoring must never be able to
|
|
19
|
+
break the thing it monitors: a topic that does not exist, credentials that are
|
|
20
|
+
wrong, SNS being slow are all reasons to lose a record, never reasons to fail a
|
|
21
|
+
run that otherwise worked.
|
|
11
22
|
"""
|
|
12
23
|
|
|
13
24
|
import logging
|
|
@@ -17,6 +28,7 @@ from contextlib import asynccontextmanager
|
|
|
17
28
|
from datetime import datetime, timezone
|
|
18
29
|
from typing import Any, Dict, Optional
|
|
19
30
|
|
|
31
|
+
from ..context.session import get_session
|
|
20
32
|
from ..sns.publisher import SNSPublisher
|
|
21
33
|
|
|
22
34
|
logger = logging.getLogger(__name__)
|
|
@@ -28,6 +40,11 @@ FAILED = "failed"
|
|
|
28
40
|
|
|
29
41
|
TOPIC_ENV = "PROCESS_RUNS_TOPIC_ARN"
|
|
30
42
|
|
|
43
|
+
# De dónde sale el código del proceso, en orden. `PROCESS_CODE` es para lo que no
|
|
44
|
+
# es una lambda —una tarea de Fargate lo recibe en su definición— y el nombre de
|
|
45
|
+
# la función es lo que AWS deja puesto solo.
|
|
46
|
+
CODE_ENVS = ("PROCESS_CODE", "AWS_LAMBDA_FUNCTION_NAME")
|
|
47
|
+
|
|
31
48
|
|
|
32
49
|
class ProcessRunPublisher(SNSPublisher):
|
|
33
50
|
pass
|
|
@@ -81,12 +98,25 @@ def _now() -> str:
|
|
|
81
98
|
return datetime.now(timezone.utc).isoformat()
|
|
82
99
|
|
|
83
100
|
|
|
101
|
+
def resolve_code(explicit: Optional[str] = None) -> Optional[str]:
|
|
102
|
+
"""El código del proceso: el nombre del recurso que está corriendo."""
|
|
103
|
+
if explicit:
|
|
104
|
+
return explicit
|
|
105
|
+
for name in CODE_ENVS:
|
|
106
|
+
value = (os.getenv(name) or "").strip()
|
|
107
|
+
if value:
|
|
108
|
+
return value
|
|
109
|
+
return None
|
|
110
|
+
|
|
111
|
+
|
|
84
112
|
@asynccontextmanager
|
|
85
113
|
async def process_run(
|
|
86
|
-
process_code: str,
|
|
114
|
+
process_code: Optional[str] = None,
|
|
87
115
|
*,
|
|
88
116
|
run_id: Optional[str] = None,
|
|
117
|
+
parent_run_id: Optional[str] = None,
|
|
89
118
|
scope: Optional[str] = None,
|
|
119
|
+
state: Optional[str] = None,
|
|
90
120
|
resource_arn: Optional[str] = None,
|
|
91
121
|
request_id: Optional[str] = None,
|
|
92
122
|
):
|
|
@@ -101,47 +131,65 @@ async def process_run(
|
|
|
101
131
|
se está vigilando, y registrarlo solo llenaría la pantalla de ejecuciones que
|
|
102
132
|
nadie puede configurar.
|
|
103
133
|
"""
|
|
104
|
-
|
|
134
|
+
code = resolve_code(process_code)
|
|
135
|
+
if not code:
|
|
136
|
+
# Sin código no hay a qué proceso pertenecer. Se sigue adelante sin
|
|
137
|
+
# reportar: el trabajo importa más que su registro.
|
|
138
|
+
logger.warning(
|
|
139
|
+
"No process code: pass one, or set %s. The run was not reported",
|
|
140
|
+
" or ".join(CODE_ENVS),
|
|
141
|
+
)
|
|
142
|
+
yield RunReporter("", "")
|
|
143
|
+
return
|
|
144
|
+
|
|
145
|
+
# El estado sale de la sesión: core identifica un proceso por código *y*
|
|
146
|
+
# estado, porque una misma lambda corre para varios con frecuencias
|
|
147
|
+
# distintas.
|
|
148
|
+
if state is None:
|
|
149
|
+
session = get_session()
|
|
150
|
+
state = session.state if session else None
|
|
151
|
+
|
|
152
|
+
reporter = RunReporter(run_id or str(uuid.uuid4()), code)
|
|
105
153
|
started_at = _now()
|
|
154
|
+
common = {
|
|
155
|
+
"run_id": reporter.run_id,
|
|
156
|
+
"process_code": code,
|
|
157
|
+
"state": state,
|
|
158
|
+
"parent_run_id": parent_run_id,
|
|
159
|
+
"request_id": request_id,
|
|
160
|
+
}
|
|
106
161
|
|
|
107
162
|
await _publish({
|
|
108
|
-
|
|
109
|
-
"process_code": process_code,
|
|
163
|
+
**common,
|
|
110
164
|
"status": LAUNCHED,
|
|
111
165
|
"started_at": started_at,
|
|
112
166
|
"scope": scope,
|
|
113
|
-
#
|
|
114
|
-
#
|
|
115
|
-
|
|
116
|
-
"resource_arn": resource_arn or os.getenv("AWS_LAMBDA_FUNCTION_NAME") or None,
|
|
117
|
-
"request_id": request_id,
|
|
167
|
+
# El ARN completo y el id de la invocación viven en el `context` del
|
|
168
|
+
# handler, que acá no se tiene: quien lo tenga los pasa por parámetro.
|
|
169
|
+
"resource_arn": resource_arn,
|
|
118
170
|
})
|
|
119
171
|
|
|
120
172
|
try:
|
|
121
173
|
yield reporter
|
|
122
174
|
except Exception as error:
|
|
123
175
|
await _publish({
|
|
124
|
-
|
|
125
|
-
"process_code": process_code,
|
|
176
|
+
**common,
|
|
126
177
|
"status": FAILED,
|
|
127
178
|
"started_at": started_at,
|
|
128
179
|
"finished_at": _now(),
|
|
129
180
|
"error": f"{type(error).__name__}: {error}"[:2000],
|
|
130
181
|
"counters": reporter.counters,
|
|
131
182
|
"scope": reporter.scope or scope,
|
|
132
|
-
"request_id": request_id,
|
|
133
183
|
})
|
|
134
184
|
# La excepción sigue su camino: el monitoreo anota lo que pasó, no lo
|
|
135
185
|
# decide. Tragarla acá convertiría una falla en un éxito silencioso.
|
|
136
186
|
raise
|
|
137
187
|
else:
|
|
138
188
|
await _publish({
|
|
139
|
-
|
|
140
|
-
"process_code": process_code,
|
|
189
|
+
**common,
|
|
141
190
|
"status": SUCCESS,
|
|
142
191
|
"started_at": started_at,
|
|
143
192
|
"finished_at": _now(),
|
|
144
193
|
"counters": reporter.counters,
|
|
145
194
|
"scope": reporter.scope or scope,
|
|
146
|
-
"request_id": request_id,
|
|
147
195
|
})
|
|
@@ -124,6 +124,23 @@ def _merge_into_set(update: Dict[str, Any], values: Dict[str, Any], operator: st
|
|
|
124
124
|
section.setdefault(key, value)
|
|
125
125
|
|
|
126
126
|
|
|
127
|
+
def _written_elsewhere(update: Dict[str, Any], key: str, operator: str) -> bool:
|
|
128
|
+
"""Si otro operador de esta misma actualización ya escribe ese campo.
|
|
129
|
+
|
|
130
|
+
Mongo rechaza el update entero si dos operadores tocan la misma ruta —
|
|
131
|
+
`$set: {status}` junto a `$setOnInsert: {status}` da "would create a
|
|
132
|
+
conflict at 'status'" — y el sello no tiene por qué ser el que lo provoque.
|
|
133
|
+
Si el llamador ya escribe el campo, el insert va a quedar con su valor, así
|
|
134
|
+
que el sello ahí sobra.
|
|
135
|
+
"""
|
|
136
|
+
for other, section in update.items():
|
|
137
|
+
if other == operator or not other.startswith('$') or not isinstance(section, dict):
|
|
138
|
+
continue
|
|
139
|
+
if key in section:
|
|
140
|
+
return True
|
|
141
|
+
return False
|
|
142
|
+
|
|
143
|
+
|
|
127
144
|
def stamp_update(
|
|
128
145
|
update: Union[Dict[str, Any], List[Dict[str, Any]]],
|
|
129
146
|
upsert: bool = False,
|
|
@@ -164,6 +181,11 @@ def stamp_update(
|
|
|
164
181
|
born = {CREATED_AT: now, CREATED_BY: who}
|
|
165
182
|
if statuses:
|
|
166
183
|
born[STATUS] = default_status or statuses[0]
|
|
184
|
+
born = {
|
|
185
|
+
key: value
|
|
186
|
+
for key, value in born.items()
|
|
187
|
+
if not _written_elsewhere(update, key, '$setOnInsert')
|
|
188
|
+
}
|
|
167
189
|
_merge_into_set(update, born, '$setOnInsert')
|
|
168
190
|
return update
|
|
169
191
|
|
|
@@ -15,6 +15,11 @@ from abc import ABC
|
|
|
15
15
|
from ..context.session import get_session
|
|
16
16
|
|
|
17
17
|
|
|
18
|
+
# Lo que SNS acepta en una sola llamada a PublishBatch. No es configurable: es
|
|
19
|
+
# un límite del servicio, y pasarse falla el lote entero sin publicar nada.
|
|
20
|
+
MAX_BATCH_ENTRIES = 10
|
|
21
|
+
|
|
22
|
+
|
|
18
23
|
class SNSPublisher(ABC):
|
|
19
24
|
"""
|
|
20
25
|
Base class to publish messages to SNS
|
|
@@ -172,35 +177,52 @@ class SNSPublisher(ABC):
|
|
|
172
177
|
message_ids_success = []
|
|
173
178
|
message_ids_failed = []
|
|
174
179
|
|
|
175
|
-
params = {
|
|
176
|
-
'TopicArn': self.topic_arn
|
|
177
|
-
}
|
|
178
|
-
|
|
179
180
|
for i, message in enumerate(messages):
|
|
180
181
|
try:
|
|
181
182
|
message_to_publish.append(self._format_message(message, i))
|
|
182
183
|
except Exception as e:
|
|
183
184
|
self.logger.error(f"Error publishing message {i} in batch: {e}")
|
|
185
|
+
message_ids_failed.append(None)
|
|
184
186
|
# Continue with the other messages
|
|
185
187
|
continue
|
|
186
188
|
|
|
187
|
-
|
|
189
|
+
# De a diez, que es lo que SNS acepta por llamada. Mandar más falla el
|
|
190
|
+
# lote entero sin publicar nada, así que una lista larga no se pierde a
|
|
191
|
+
# medias: se pierde completa.
|
|
192
|
+
for start in range(0, len(message_to_publish), MAX_BATCH_ENTRIES):
|
|
193
|
+
chunk = message_to_publish[start:start + MAX_BATCH_ENTRIES]
|
|
188
194
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
195
|
+
# El Id sólo tiene que ser único dentro de su propia llamada.
|
|
196
|
+
entries = [
|
|
197
|
+
{**entry, 'Id': str(position)}
|
|
198
|
+
for position, entry in enumerate(chunk)
|
|
199
|
+
]
|
|
200
|
+
|
|
201
|
+
try:
|
|
202
|
+
result = self.sns_client.publish_batch(
|
|
203
|
+
TopicArn=self.topic_arn,
|
|
204
|
+
PublishBatchRequestEntries=entries,
|
|
205
|
+
)
|
|
206
|
+
except Exception as e:
|
|
207
|
+
# Se sigue con los demás lotes: cortar acá dejaría sin publicar
|
|
208
|
+
# todo lo que venía detrás por un problema que puede ser de este
|
|
209
|
+
# lote solo.
|
|
210
|
+
self.logger.error(
|
|
211
|
+
f"Error calling publish_batch to {self.topic_arn} "
|
|
212
|
+
f"({start}-{start + len(chunk) - 1} of {len(message_to_publish)}): {e}"
|
|
213
|
+
)
|
|
214
|
+
message_ids_failed.extend([None] * len(chunk))
|
|
215
|
+
continue
|
|
194
216
|
|
|
195
|
-
|
|
196
|
-
|
|
217
|
+
for message in result.get('Successful', []):
|
|
218
|
+
message_ids_success.append(message['MessageId'])
|
|
197
219
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
220
|
+
for message in result.get('Failed', []):
|
|
221
|
+
self.logger.error(
|
|
222
|
+
f"Failed to publish message {message.get('Id')} to {self.topic_arn}: "
|
|
223
|
+
f"Code={message.get('Code')}, Message={message.get('Message')}"
|
|
224
|
+
)
|
|
225
|
+
message_ids_failed.append(None)
|
|
204
226
|
|
|
205
227
|
self.logger.info(
|
|
206
228
|
f"Batch publish to {self.topic_arn}: "
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""What the audit stamp must never do to a write.
|
|
2
|
+
|
|
3
|
+
Runs on its own, no dependencies:
|
|
4
|
+
|
|
5
|
+
python tests/test_audit_stamp.py
|
|
6
|
+
|
|
7
|
+
These are here because the stamp broke a write in production once: an upsert
|
|
8
|
+
that set `status` itself collided with the `$setOnInsert` the stamp added, and
|
|
9
|
+
Mongo refused the whole update with "would create a conflict at 'status'". It
|
|
10
|
+
failed at runtime, on one endpoint, in a log nobody was reading.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import os
|
|
14
|
+
import sys
|
|
15
|
+
|
|
16
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
17
|
+
|
|
18
|
+
from aws_python_helper.repository.audit import stamp_update # noqa: E402
|
|
19
|
+
|
|
20
|
+
STATUSES = ["active", "inactive"]
|
|
21
|
+
|
|
22
|
+
failures = []
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def check(label, condition, detail=""):
|
|
26
|
+
mark = "ok " if condition else "FAIL"
|
|
27
|
+
print(f" {mark} {label}{' ' + detail if detail else ''}")
|
|
28
|
+
if not condition:
|
|
29
|
+
failures.append(label)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def conflicts(update):
|
|
33
|
+
"""Los campos que dos operadores de un mismo update escriben a la vez.
|
|
34
|
+
|
|
35
|
+
Es lo que Mongo rechaza, y rechaza el update entero: no escribe nada.
|
|
36
|
+
"""
|
|
37
|
+
sections = [
|
|
38
|
+
(name, section) for name, section in update.items()
|
|
39
|
+
if name.startswith("$") and isinstance(section, dict)
|
|
40
|
+
]
|
|
41
|
+
clash = set()
|
|
42
|
+
for index, (_, first) in enumerate(sections):
|
|
43
|
+
for _, second in sections[index + 1:]:
|
|
44
|
+
clash |= set(first) & set(second)
|
|
45
|
+
return clash
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
print("no two operators may write the same path")
|
|
49
|
+
for label, update in [
|
|
50
|
+
("upsert that sets status itself", {"$set": {"name": "ct", "status": "active"}}),
|
|
51
|
+
("upsert that sets created_at itself", {"$set": {"created_at": "fixed"}}),
|
|
52
|
+
("upsert that sets created_by itself", {"$set": {"created_by": {"name": "x"}}}),
|
|
53
|
+
("plain upsert", {"$set": {"name": "ct"}}),
|
|
54
|
+
("upsert with $inc alongside", {"$set": {"name": "ct"}, "$inc": {"hits": 1}}),
|
|
55
|
+
]:
|
|
56
|
+
result = stamp_update(dict(update), upsert=True, statuses=STATUSES, default_status="active")
|
|
57
|
+
found = conflicts(result)
|
|
58
|
+
check(label, not found, f"conflicts={sorted(found)}" if found else "")
|
|
59
|
+
|
|
60
|
+
print("\nthe stamp is still complete when nothing conflicts")
|
|
61
|
+
result = stamp_update({"$set": {"name": "ct"}}, upsert=True, statuses=STATUSES, default_status="active")
|
|
62
|
+
check("status on insert", result["$setOnInsert"].get("status") == "active")
|
|
63
|
+
check("created_at on insert", "created_at" in result["$setOnInsert"])
|
|
64
|
+
check("created_by on insert", "created_by" in result["$setOnInsert"])
|
|
65
|
+
check("updated_at always", "updated_at" in result["$set"])
|
|
66
|
+
|
|
67
|
+
print("\nthe caller's own value wins")
|
|
68
|
+
result = stamp_update({"$set": {"status": "inactive"}}, upsert=True, statuses=STATUSES, default_status="active")
|
|
69
|
+
check("status stays what the caller set", result["$set"]["status"] == "inactive")
|
|
70
|
+
check("no default forced on top", "status" not in result.get("$setOnInsert", {}))
|
|
71
|
+
|
|
72
|
+
print("\nwithout upsert there is no insert stamp")
|
|
73
|
+
result = stamp_update({"$set": {"status": "inactive"}}, upsert=False, statuses=STATUSES)
|
|
74
|
+
check("no $setOnInsert", "$setOnInsert" not in result)
|
|
75
|
+
|
|
76
|
+
print("\nwithout a vocabulary no status is invented")
|
|
77
|
+
result = stamp_update({"$set": {"name": "ct"}}, upsert=True, statuses=None)
|
|
78
|
+
check("no status", "status" not in result.get("$setOnInsert", {}))
|
|
79
|
+
|
|
80
|
+
print("\na pipeline update takes stages, not operators")
|
|
81
|
+
result = stamp_update([{"$set": {"name": "ct"}}], upsert=True, statuses=STATUSES)
|
|
82
|
+
check("still a list", isinstance(result, list))
|
|
83
|
+
check("stamp appended last", result[-1]["$set"].get("updated_at") is not None)
|
|
84
|
+
|
|
85
|
+
if failures:
|
|
86
|
+
print(f"\n{len(failures)} failed: {failures}")
|
|
87
|
+
raise SystemExit(1)
|
|
88
|
+
print("\nall good")
|
|
File without changes
|
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/api/auth_middleware.py
RENAMED
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/api/auth_validators.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/context/state_validator.py
RENAMED
|
File without changes
|
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/database/database_proxy.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/database/mongo_manager.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/invoker/lambda_invoker.py
RENAMED
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/lambda_standalone/__init__.py
RENAMED
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/lambda_standalone/base.py
RENAMED
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/lambda_standalone/fetcher.py
RENAMED
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/lambda_standalone/handler.py
RENAMED
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/model_index_sync/__init__.py
RENAMED
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/model_index_sync/base.py
RENAMED
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/model_query/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/permissions/__init__.py
RENAMED
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/permissions/catalog.py
RENAMED
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper/repository/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
{aws_python_helper-1.8.1 → aws_python_helper-1.9.1}/aws_python_helper.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|