-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/SK-1229 | Project resource for CLI extension #784
base: master
Are you sure you want to change the base?
Changes from 23 commits
ff770f2
94bfc2e
3f9eca6
71623d9
16549a8
2133fcf
8fe7ad0
7a3f337
18cba87
4149c3e
c9bacbe
4273a00
5030a24
2cf9959
c49b770
a6f6446
c12ada9
8e55110
08594c0
de6fa38
d8d38ea
e174f77
d5bf9df
91ad574
a430156
f236c89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
from .client_cmd import client_cmd # noqa: F401 | ||
from .combiner_cmd import combiner_cmd # noqa: F401 | ||
from .config_cmd import config_cmd # noqa: F401 | ||
from .controller_cmd import controller_cmd # noqa: F401 | ||
from .hooks_cmd import hooks_cmd # noqa: F401 | ||
from .login_cmd import login_cmd # noqa: F401 | ||
from .main import main # noqa: F401 | ||
from .model_cmd import model_cmd # noqa: F401 | ||
from .package_cmd import package_cmd # noqa: F401 | ||
from .project_cmd import project_cmd # noqa: F401 | ||
from .round_cmd import round_cmd # noqa: F401 | ||
from .run_cmd import run_cmd # noqa: F401 | ||
from .session_cmd import session_cmd # noqa: F401 | ||
from .status_cmd import status_cmd # noqa: F401 | ||
from .validation_cmd import validation_cmd # noqa: F401 | ||
from .controller_cmd import controller_cmd # noqa: F401 | ||
from .login_cmd import login_cmd # noqa: F401 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
import yaml | ||
|
||
from .main import main | ||
from .shared import STUDIO_DEFAULTS, get_api_url, get_token | ||
|
||
# Replace this with the platform's actual login endpoint | ||
home_dir = os.path.expanduser("~") | ||
|
@@ -19,8 +20,8 @@ def login_cmd(ctx): | |
|
||
|
||
@login_cmd.command("login") | ||
@click.option("-p", "--protocol", required=False, default="https", help="Communication protocol") | ||
@click.option("-H", "--host", required=False, default="fedn.scaleoutsystems.com", help="Hostname of controller (api)") | ||
@click.option("-p", "--protocol", required=False, default=STUDIO_DEFAULTS["protocol"], help="Communication protocol of studio (api)") | ||
@click.option("-H", "--host", required=False, default=STUDIO_DEFAULTS["host"], help="Hostname of studio (api)") | ||
@click.pass_context | ||
def login_cmd(ctx, protocol: str, host: str): | ||
"""Logging into FEDn Studio""" | ||
|
@@ -44,18 +45,55 @@ def login_cmd(ctx, protocol: str, host: str): | |
|
||
# Handle the response | ||
if response.status_code == 200: | ||
data = response.json() | ||
if data.get("access"): | ||
click.secho("Login successful!", fg="green") | ||
context_path = os.path.join(home_dir, ".fedn") | ||
if not os.path.exists(context_path): | ||
os.makedirs(context_path) | ||
try: | ||
with open(f"{context_path}/context.yaml", "w") as yaml_file: | ||
yaml.dump(data, yaml_file, default_flow_style=False) # Add access and refresh tokens to context yaml file | ||
except Exception as e: | ||
print(f"Error: Failed to write to YAML file. Details: {e}") | ||
else: | ||
click.secho("Login failed. Please check your credentials.", fg="red") | ||
context_data = get_context(response, protocol, host) | ||
|
||
context_path = os.path.join(home_dir, ".fedn") | ||
if not os.path.exists(context_path): | ||
os.makedirs(context_path) | ||
try: | ||
with open(f"{context_path}/context.yaml", "w") as yaml_file: | ||
yaml.dump(context_data, yaml_file, default_flow_style=False) # Add access and refresh tokens to context yaml file | ||
except Exception as e: | ||
print(f"Error: Failed to write to YAML file. Details: {e}") | ||
else: | ||
click.secho(f"Unexpected error: {response.text}", fg="red") | ||
|
||
|
||
def get_context(response, protocol, host): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add typing |
||
user_token_data = response.json() | ||
if user_token_data.get("access"): | ||
studio_api = True | ||
url_projects = get_api_url(protocol=protocol, host=host, port=None, endpoint="projects", usr_api=studio_api) | ||
headers_projects = {} | ||
user_access_token = user_token_data.get("access") | ||
_token = get_token(user_access_token, True) | ||
if _token: | ||
headers_projects["Authorization"] = _token | ||
|
||
try: | ||
response_projects = requests.get(url_projects, headers=headers_projects) | ||
Wrede marked this conversation as resolved.
Show resolved
Hide resolved
|
||
projects_response_json = response_projects.json() | ||
except requests.exceptions.ConnectionError: | ||
click.echo(f"Error: Could not connect to {url_projects}") | ||
|
||
slug = projects_response_json[0].get("slug") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hårdkodat att alltid ta första projektet? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ja, i alla fall när man loggar in så aktiveras senast skapade projektet som default. Kan ju ändra så att det senast aktiverade projektet behålls istället, men uppstår fall som jag inte på rak arm vet hur jag ska hantera. T.ex. om man loggar in på annat konto på samma dator typ. Skulle typ kunna lägga in owner # i context-filen också? |
||
headers_projects["X-Project-Slug"] = slug | ||
url_project_token = get_api_url(protocol=protocol, host=host, port=None, endpoint="admin-token", usr_api=studio_api) | ||
|
||
try: | ||
response_project_tokens = requests.get(url_project_token, headers=headers_projects) | ||
project_tokens = response_project_tokens.json() | ||
except requests.exceptions.ConnectionError: | ||
click.echo(f"Error: Could not connect to {url_project_token}") | ||
|
||
controller_url = f"{protocol}://{host}/{slug}-fedn-reducer" | ||
Wrede marked this conversation as resolved.
Show resolved
Hide resolved
|
||
context_data = { | ||
"User tokens": user_token_data, | ||
"Active project tokens": project_tokens, | ||
"Active project slug": slug, | ||
"Active project url": controller_url, | ||
} | ||
click.secho("Login successful!", fg="green") | ||
return context_data | ||
else: | ||
click.secho("Login failed. Please check your credentials.", fg="red") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vad händer om det redan finns en context.yaml med innehåll?