Skip to content

Instantly share code, notes, and snippets.

@pirogoeth
Created July 15, 2020 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirogoeth/e4b58dc35d88df939e6315f874573b31 to your computer and use it in GitHub Desktop.
Save pirogoeth/e4b58dc35d88df939e6315f874573b31 to your computer and use it in GitHub Desktop.
@respond_to(regex=r"^scale\s+(?P<params>.*)", flags=re.I)
@abac.allow_access("nomad.scale", defer=True)
async def scale(self, msg: Message, authz: abac.Authorizer, params: str):
params: dict = kvinline.parse(params)
service_name: Optional[str] = None
regions: Optional[str] = None
scale_groups: Mapping[str, int] = {}
for key, value in params.items():
if key.lower() in ["region", "regions"]:
regions = value
elif key.lower() in ["service", "service_name", "svc", "name"]:
service_name = value
authz.match(service_name=service_name)
elif key.lower().startswith("group_"):
group = key.rsplit("_", 1)[1]
try:
scale_groups[group] = int(value)
except ValueError:
await msg.react("x")
await msg.reply(
f"`group_*` parameter expected integer argument, not '{value}'"
)
return
else:
await msg.react("x")
await msg.reply(f"I don't understand parameter `{key}`.", in_thread=True)
return
if regions:
regions: List[str] = regions.split(",")
authz.match(regions=regions)
else:
await msg.react("x")
await msg.reply(
f"I can't scale without knowing which region to scale in!", in_thread=True
)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment