Skip to content

Commit eb5bc0c

Browse files
committed
chore: drop context manager protocol from StackletContext
### what drop the context manager protocol from StackletContext ### why it's not used for anything and just makes the code more indented ### testing unit tests and local testing ### docs n/a
1 parent a93fb0f commit eb5bc0c

6 files changed

Lines changed: 120 additions & 128 deletions

File tree

‎stacklet/client/platform/cli.py‎

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,14 @@ def show(ctx):
268268
"""
269269
Show your config
270270
"""
271-
with StackletContext(ctx.obj["config"], ctx.obj["raw_config"]) as context:
272-
fmt = Formatter.registry.get(ctx.obj["output"])()
273-
if os.path.exists(os.path.expanduser(StackletContext.DEFAULT_ID)):
274-
with open(os.path.expanduser(StackletContext.DEFAULT_ID), "r") as f:
275-
id_details = jwt.decode(f.read(), options={"verify_signature": False})
276-
click.echo(fmt(id_details))
277-
click.echo()
278-
click.echo(fmt(context.config.to_json()))
271+
context = StackletContext(ctx.obj["config"], ctx.obj["raw_config"])
272+
fmt = Formatter.registry.get(ctx.obj["output"])()
273+
if os.path.exists(os.path.expanduser(StackletContext.DEFAULT_ID)):
274+
with open(os.path.expanduser(StackletContext.DEFAULT_ID), "r") as f:
275+
id_details = jwt.decode(f.read(), options={"verify_signature": False})
276+
click.echo(fmt(id_details))
277+
click.echo()
278+
click.echo(fmt(context.config.to_json()))
279279

280280

281281
@cli.command(short_help="Login to Stacklet")
@@ -295,40 +295,38 @@ def login(ctx, username, password):
295295
296296
If password is not passed in, your password will be prompted
297297
"""
298-
with StackletContext(ctx.obj["config"], ctx.obj["raw_config"]) as context:
299-
# sso login
300-
if context.can_sso_login() and not any([username, password]):
301-
from stacklet.client.platform.vendored.auth import BrowserAuthenticator
302-
303-
BrowserAuthenticator(
304-
authority_url=context.config.auth_url,
305-
client_id=context.config.cognito_client_id,
306-
idp_id=context.config.idp_id,
307-
)()
308-
return
309-
elif not context.can_sso_login() and not any([username, password]):
310-
click.echo(
311-
"To login with SSO ensure that your configuration includes "
312-
+ "auth_url, and cognito_client_id values."
313-
)
314-
raise Exception()
315-
316-
# handle login with username/password
317-
if not username:
318-
username = click.prompt("Username")
319-
if not password:
320-
password = click.prompt("Password", hide_input=True)
321-
manager = CognitoUserManager.from_context(context)
322-
res = manager.login(
323-
user=username,
324-
password=password,
298+
context = StackletContext(ctx.obj["config"], ctx.obj["raw_config"])
299+
# sso login
300+
if context.can_sso_login() and not any([username, password]):
301+
from stacklet.client.platform.vendored.auth import BrowserAuthenticator
302+
303+
BrowserAuthenticator(
304+
authority_url=context.config.auth_url,
305+
client_id=context.config.cognito_client_id,
306+
idp_id=context.config.idp_id,
307+
)()
308+
return
309+
elif not context.can_sso_login() and not any([username, password]):
310+
click.echo(
311+
"To login with SSO ensure that your configuration includes "
312+
+ "auth_url, and cognito_client_id values."
325313
)
326-
if not os.path.exists(
327-
os.path.dirname(os.path.expanduser(StackletContext.DEFAULT_CREDENTIALS))
328-
):
329-
os.makedirs(os.path.dirname(os.path.expanduser(StackletContext.DEFAULT_CREDENTIALS)))
330-
with open(os.path.expanduser(StackletContext.DEFAULT_CREDENTIALS), "w+") as f: # noqa
331-
f.write(res)
314+
raise Exception()
315+
316+
# handle login with username/password
317+
if not username:
318+
username = click.prompt("Username")
319+
if not password:
320+
password = click.prompt("Password", hide_input=True)
321+
manager = CognitoUserManager.from_context(context)
322+
res = manager.login(
323+
user=username,
324+
password=password,
325+
)
326+
if not os.path.exists(os.path.dirname(os.path.expanduser(StackletContext.DEFAULT_CREDENTIALS))):
327+
os.makedirs(os.path.dirname(os.path.expanduser(StackletContext.DEFAULT_CREDENTIALS)))
328+
with open(os.path.expanduser(StackletContext.DEFAULT_CREDENTIALS), "w+") as f: # noqa
329+
f.write(res)
332330

333331

334332
for c in commands:

‎stacklet/client/platform/commands/cube.py‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,31 @@ def resource_counts(ctx):
6060

6161

6262
def _run_query(ctx, query):
63-
with StackletContext(ctx.obj["config"], ctx.obj["raw_config"]) as context:
64-
token = get_token()
63+
context = StackletContext(ctx.obj["config"], ctx.obj["raw_config"])
64+
token = get_token()
6565

66-
url = f"https://{context.config.cubejs}/cubejs-api/v1/load"
66+
url = f"https://{context.config.cubejs}/cubejs-api/v1/load"
6767

68-
data = {"query": query}
69-
response = requests.post(
70-
url=url,
71-
headers={"Authorization": token, "Content-Type": "application/json"},
72-
data=json.dumps(data),
73-
)
74-
return response.json()
68+
data = {"query": query}
69+
response = requests.post(
70+
url=url,
71+
headers={"Authorization": token, "Content-Type": "application/json"},
72+
data=json.dumps(data),
73+
)
74+
return response.json()
7575

7676

7777
def _get(ctx, path: str):
78-
with StackletContext(ctx.obj["config"], ctx.obj["raw_config"]) as context:
79-
token = get_token()
78+
context = StackletContext(ctx.obj["config"], ctx.obj["raw_config"])
79+
token = get_token()
8080

81-
url = f"https://{context.config.cubejs}/cubejs-api/{path}"
81+
url = f"https://{context.config.cubejs}/cubejs-api/{path}"
8282

83-
response = requests.get(
84-
url=url,
85-
headers={"Authorization": token, "Content-Type": "application/json"},
86-
)
87-
return response.json()
83+
response = requests.get(
84+
url=url,
85+
headers={"Authorization": token, "Content-Type": "application/json"},
86+
)
87+
return response.json()
8888

8989

9090
_resource_counts = {

‎stacklet/client/platform/commands/user.py‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ def add(ctx, username, password, email=None, phone_number=None, permanent=True):
2929
"""
3030
Add a cognito user in Stacklet
3131
"""
32-
with StackletContext(ctx.obj["config"], ctx.obj["raw_config"]) as context:
33-
manager = CognitoUserManager.from_context(context)
34-
manager.create_user(
35-
user=username,
36-
password=password,
37-
email=email,
38-
phone_number=phone_number,
39-
permanent=permanent,
40-
)
32+
context = StackletContext(ctx.obj["config"], ctx.obj["raw_config"])
33+
manager = CognitoUserManager.from_context(context)
34+
manager.create_user(
35+
user=username,
36+
password=password,
37+
email=email,
38+
phone_number=phone_number,
39+
permanent=permanent,
40+
)
4141

4242

4343
@user.command("ensure-group")
@@ -48,10 +48,10 @@ def ensure_group(ctx, username, group):
4848
"""
4949
Ensure that the specified user has the group if the group is available.
5050
"""
51-
with StackletContext(ctx.obj["config"], ctx.obj["raw_config"]) as context:
52-
manager = CognitoUserManager.from_context(context)
53-
success = manager.ensure_group(
54-
user=username,
55-
group=group,
56-
)
57-
ctx.exit(0 if success else 1)
51+
context = StackletContext(ctx.obj["config"], ctx.obj["raw_config"])
52+
manager = CognitoUserManager.from_context(context)
53+
success = manager.ensure_group(
54+
user=username,
55+
group=group,
56+
)
57+
ctx.exit(0 if success else 1)

‎stacklet/client/platform/context.py‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ def __init__(self, config=None, raw_config=None):
2424
else:
2525
self.config = StackletConfig.from_file(self.DEFAULT_CONFIG)
2626

27-
def __enter__(self):
28-
return self
29-
30-
def __exit__(self, type, value, traceback):
31-
return
32-
3327
def can_sso_login(self):
3428
return all(
3529
[

‎stacklet/client/platform/executor.py‎

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,28 +59,28 @@ def wrapper(func):
5959

6060

6161
def _run_graphql(ctx, name=None, variables=None, snippet=None, raw=False):
62-
with StackletContext(ctx.obj["config"], ctx.obj["raw_config"]) as context:
63-
token = get_token()
64-
executor = StackletGraphqlExecutor(context, token)
65-
66-
if variables is None:
67-
variables = {}
68-
69-
registry_snippet = StackletGraphqlExecutor.registry.get(name)
70-
71-
for k, v in variables.items():
72-
transformer = registry_snippet.variable_transformers.get(k)
73-
if not transformer:
74-
continue
75-
variables[k] = transformer(v)
76-
77-
if name and registry_snippet:
78-
snippet = registry_snippet
79-
elif name and registry_snippet is None:
80-
raise Exception("No snippet found, got name:%s snippet:%s" % (name, registry_snippet))
81-
82-
res = executor.run(snippet=snippet, variables=variables)
83-
if raw:
84-
return res
85-
fmt = Formatter.registry.get(ctx.obj["output"])()
86-
return fmt(res)
62+
context = StackletContext(ctx.obj["config"], ctx.obj["raw_config"])
63+
token = get_token()
64+
executor = StackletGraphqlExecutor(context, token)
65+
66+
if variables is None:
67+
variables = {}
68+
69+
registry_snippet = StackletGraphqlExecutor.registry.get(name)
70+
71+
for k, v in variables.items():
72+
transformer = registry_snippet.variable_transformers.get(k)
73+
if not transformer:
74+
continue
75+
variables[k] = transformer(v)
76+
77+
if name and registry_snippet:
78+
snippet = registry_snippet
79+
elif name and registry_snippet is None:
80+
raise Exception("No snippet found, got name:%s snippet:%s" % (name, registry_snippet))
81+
82+
res = executor.run(snippet=snippet, variables=variables)
83+
if raw:
84+
return res
85+
fmt = Formatter.registry.get(ctx.obj["output"])()
86+
return fmt(res)

‎tests/test_cognito.py‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,28 @@ def test_cognito_user_manager_create_user(self):
5656
users = self.client.list_users(UserPoolId=self.cognito_user_pool_id)
5757
assert len(users["Users"]) == 0
5858

59-
with StackletContext(raw_config=config) as context:
60-
manager = CognitoUserManager.from_context(context)
61-
manager.create_user(
62-
user="test-user",
63-
password="Foobar123!",
64-
email="foo@acme.org",
65-
phone_number="+15551234567",
66-
)
67-
users = self.client.list_users(UserPoolId=self.cognito_user_pool_id)
68-
assert users["Users"][0]["Username"] == "test-user"
69-
70-
# creating a user is an idempotent action, this should return true without
71-
# raising an error
72-
res = manager.create_user(
73-
user="test-user",
74-
password="Foobar123!",
75-
email="foo@acme.org",
76-
phone_number="+15551234567",
77-
)
78-
assert res is True
79-
users = self.client.list_users(UserPoolId=self.cognito_user_pool_id)
80-
assert users["Users"][0]["Username"] == "test-user"
59+
context = StackletContext(raw_config=config)
60+
manager = CognitoUserManager.from_context(context)
61+
manager.create_user(
62+
user="test-user",
63+
password="Foobar123!",
64+
email="foo@acme.org",
65+
phone_number="+15551234567",
66+
)
67+
users = self.client.list_users(UserPoolId=self.cognito_user_pool_id)
68+
assert users["Users"][0]["Username"] == "test-user"
69+
70+
# creating a user is an idempotent action, this should return true without
71+
# raising an error
72+
res = manager.create_user(
73+
user="test-user",
74+
password="Foobar123!",
75+
email="foo@acme.org",
76+
phone_number="+15551234567",
77+
)
78+
assert res is True
79+
users = self.client.list_users(UserPoolId=self.cognito_user_pool_id)
80+
assert users["Users"][0]["Username"] == "test-user"
8181

8282
def default_args(self) -> list[str]:
8383
return [

0 commit comments

Comments
 (0)