-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mock_uss] Configure mock_uss locality at runtime (#239)
Add configurable locality for mock_uss
- Loading branch information
1 parent
a831fea
commit a76b519
Showing
36 changed files
with
656 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
29 changes: 29 additions & 0 deletions
29
monitoring/mock_uss/dynamic_configuration/configuration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import json | ||
|
||
from implicitdict import ImplicitDict | ||
from monitoring.mock_uss import require_config_value, webapp | ||
from monitoring.mock_uss.config import KEY_BEHAVIOR_LOCALITY | ||
from monitoring.monitorlib.locality import Locality, LocalityCode | ||
from monitoring.monitorlib.multiprocessing import SynchronizedValue | ||
|
||
|
||
require_config_value(KEY_BEHAVIOR_LOCALITY) | ||
|
||
|
||
class DynamicConfiguration(ImplicitDict): | ||
locale: LocalityCode | ||
|
||
|
||
db = SynchronizedValue( | ||
DynamicConfiguration(locale=LocalityCode(webapp.config[KEY_BEHAVIOR_LOCALITY])), | ||
decoder=lambda b: ImplicitDict.parse( | ||
json.loads(b.decode("utf-8")), DynamicConfiguration | ||
), | ||
capacity_bytes=10000, | ||
) | ||
|
||
|
||
def get_locality() -> Locality: | ||
with db as tx: | ||
code = tx.locale | ||
return Locality.from_locale(code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from typing import Tuple | ||
|
||
import flask | ||
from implicitdict import ImplicitDict | ||
|
||
from monitoring.mock_uss import webapp | ||
from monitoring.mock_uss.auth import requires_scope, MOCK_USS_CONFIG_SCOPE | ||
from monitoring.mock_uss.dynamic_configuration.configuration import db, get_locality | ||
from monitoring.monitorlib.clients.mock_uss.locality import ( | ||
PutLocalityRequest, | ||
GetLocalityResponse, | ||
) | ||
from monitoring.monitorlib.locality import Locality | ||
|
||
|
||
@webapp.route("/configuration/locality", methods=["GET"]) | ||
def locality_get() -> Tuple[str, int]: | ||
return flask.jsonify( | ||
GetLocalityResponse(locality_code=get_locality().locality_code()) | ||
) | ||
|
||
|
||
@webapp.route("/configuration/locality", methods=["PUT"]) | ||
@requires_scope([MOCK_USS_CONFIG_SCOPE]) # TODO: use separate public key for this | ||
def locality_set() -> Tuple[str, int]: | ||
"""Set the locality of the mock_uss.""" | ||
try: | ||
json = flask.request.json | ||
if json is None: | ||
raise ValueError("Request did not contain a JSON payload") | ||
req: PutLocalityRequest = ImplicitDict.parse(json, PutLocalityRequest) | ||
except ValueError as e: | ||
msg = f"Change locality unable to parse JSON: {str(e)}" | ||
return msg, 400 | ||
|
||
# Make sure this is a valid locality | ||
try: | ||
Locality.from_locale(req.locality_code) | ||
except ValueError as e: | ||
msg = f"Invalid locality_code: {str(e)}" | ||
return msg, 400 | ||
|
||
with db as tx: | ||
tx.locale = req.locality_code | ||
|
||
return flask.jsonify( | ||
GetLocalityResponse(locality_code=get_locality().locality_code()) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from implicitdict import ImplicitDict | ||
|
||
from monitoring.monitorlib.locality import LocalityCode | ||
|
||
|
||
class PutLocalityRequest(ImplicitDict): | ||
"""API object to request a change in locality""" | ||
|
||
locality_code: LocalityCode | ||
|
||
|
||
class GetLocalityResponse(ImplicitDict): | ||
"""API object defining a response indicating locality""" | ||
|
||
locality_code: LocalityCode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
1 change: 1 addition & 0 deletions
1
monitoring/uss_qualifier/action_generators/interuss/mock_uss/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .with_locality import WithLocality |
Oops, something went wrong.