-
Notifications
You must be signed in to change notification settings - Fork 7
Added shared account selection module based on the old python API #64
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,17 @@ | ||
import flow360 as fl | ||
from flow360.examples import OM6wing | ||
|
||
fl.Env.dev.active() | ||
|
||
# choose shared account interactively | ||
fl.Accounts.choose_shared_account() | ||
|
||
# retrieve mesh files | ||
OM6wing.get_files() | ||
|
||
# submit mesh | ||
volume_mesh = fl.VolumeMesh.from_file(OM6wing.mesh_filename, name="OM6wing-mesh") | ||
volume_mesh = volume_mesh.submit() | ||
|
||
# leave the account | ||
fl.Accounts.leave_shared_account() |
This file contains hidden or 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 hidden or 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,166 @@ | ||
""" | ||
This module provides utility functions for managing access between interconnected accounts. | ||
|
||
Functions: | ||
- choose_shared_account(None) -> None - select account from the list of client and organization accounts interactively | ||
- choose_shared_account(email: str, optional) -> None - select account matching the provided email (if exists) | ||
- shared_account_info(None) -> str - return current shared account email address (if exists, None otherwise) | ||
- leave_shared_account(None) -> None - leave current shared account | ||
""" | ||
|
||
from requests import HTTPError | ||
|
||
from flow360.cloud.http_util import http | ||
from flow360.environment import Env | ||
from flow360.log import log | ||
|
||
from .exceptions import WebError | ||
|
||
|
||
class AccountsUtils: | ||
""" | ||
Current account info and utility functions. | ||
""" | ||
|
||
def __init__(self): | ||
self._current_email = None | ||
self._current_user_identity = None | ||
self._confirmed_submit = False | ||
|
||
@staticmethod | ||
def _interactive_selection(users): | ||
print( | ||
"Choosing account in interactive mode, please select email from the organization list: " | ||
) | ||
|
||
user_count = len(users) | ||
|
||
for i in range(0, user_count): | ||
print(f"{i}: {users[i]['userEmail']}") | ||
|
||
while True: | ||
try: | ||
value = input( | ||
f"Enter address of the account to switch to [0 - {user_count - 1}] or 'q' to abort: " | ||
) | ||
if value == "q": | ||
return None | ||
if int(value) in range(0, user_count): | ||
return int(value) | ||
print(f"Value out of range [0 - {user_count - 1}]") | ||
continue | ||
except ValueError: | ||
print("Invalid input type, please input an integer value:") | ||
continue | ||
|
||
# Requires fixing from the backend side - support for portal webapi calls with apikey authentication | ||
@staticmethod | ||
def _get_supported_users(): | ||
try: | ||
response = http.portal_api_get("auth") | ||
access = response.json()["data"] | ||
keys = access["user"] | ||
supported_users = keys["guestUsers"] | ||
if supported_users: | ||
return supported_users | ||
return [] | ||
except HTTPError as error: | ||
raise WebError("Failed to retrieve supported user data from server") from error | ||
|
||
@staticmethod | ||
def _get_company_users(): | ||
try: | ||
response = http.get("flow360/account") | ||
company_users = response["tenantMembers"] | ||
if company_users: | ||
return company_users | ||
return [] | ||
except HTTPError as error: | ||
raise WebError("Failed to retrieve company user data from server") from error | ||
|
||
def _check_state_consistency(self): | ||
if Env.impersonate != self._current_user_identity: | ||
log.warning( | ||
( | ||
f"Environment impersonation ({Env.impersonate}) does " | ||
f"not match current account ({self._current_user_identity}), " | ||
"this may be caused by explicit modification of impersonation " | ||
"in the environment, use choose_shared_account() instead." | ||
) | ||
) | ||
self._current_email = None | ||
self._current_user_identity = None | ||
|
||
def shared_account_submit_is_confirmed(self): | ||
"""check if the user confirmed that he wants to submit resources to a shared account""" | ||
return self._confirmed_submit | ||
|
||
def shared_account_confirm_submit(self): | ||
"""confirm submit for the current session""" | ||
self._confirmed_submit = True | ||
|
||
def choose_shared_account(self, email=None): | ||
"""choose a shared account to impersonate | ||
|
||
Parameters | ||
---------- | ||
email : str, optional | ||
user email to impersonate (if email exists among shared accounts), | ||
if email is not provided user can select the account interactively | ||
""" | ||
shared_accounts = self._get_company_users() | ||
|
||
if len(shared_accounts) == 0: | ||
log.info("There are no accounts shared with the current user") | ||
return | ||
|
||
selected = None | ||
|
||
addresses = [user["userEmail"] for user in shared_accounts] | ||
|
||
if email is None: | ||
selected = self._interactive_selection(shared_accounts) | ||
elif email in addresses: | ||
selected = addresses.index(email) | ||
|
||
if selected is None: | ||
raise ValueError("Invalid or empty email address selected, cannot change account.") | ||
|
||
user_email = shared_accounts[selected]["userEmail"] | ||
user_id = shared_accounts[selected]["userIdentity"] | ||
|
||
Env.impersonate = user_id | ||
|
||
self._confirmed_submit = False | ||
self._current_email = user_email | ||
self._current_user_identity = user_id | ||
|
||
def shared_account_info(self): | ||
""" | ||
retrieve current shared account name, if possible | ||
""" | ||
self._check_state_consistency() | ||
|
||
if self._current_email is not None: | ||
log.info(f"Currently operating as {self._current_email}") | ||
else: | ||
log.info("Currently not logged into a shared account") | ||
|
||
return self._current_email | ||
|
||
def leave_shared_account(self): | ||
""" | ||
leave current shared account name, if possible | ||
""" | ||
self._check_state_consistency() | ||
|
||
if Env.impersonate is None: | ||
log.warning("You are not currently logged into any shared account") | ||
else: | ||
log.info(f"Leaving shared account {self._current_email}") | ||
self._current_email = None | ||
self._current_user_identity = None | ||
Env.impersonate = None | ||
|
||
|
||
Accounts = AccountsUtils() |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
{ | ||
"data": { | ||
"credit": 100000000000.0, | ||
"s3Usage": 0, | ||
"totalTaskCount": 0, | ||
"monthlyTaskCount": 0, | ||
"creditExpiration": "2029-05-31T07:28:31.829Z", | ||
"accountType": "tenant", | ||
"tenantId": "0000000-0000-0000-0000-000000000000", | ||
"tenantName": "Test", | ||
"clientAdmin": true, | ||
"allowanceCurrentCycleAmount": null, | ||
"allowanceCurrentCycleTotalAmount": null, | ||
"allowanceCurrentCycleStartDate": null, | ||
"allowanceCurrentCycleEndDate": null, | ||
"allowanceNextCycleStartDate": null, | ||
"allowanceAllCycleStartDate": null, | ||
"allowanceAllCycleEndDate": null, | ||
"tenantMembers": [ | ||
{"userIdentity": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", "userId": "AAAAAAAAAAAAAAAAAAAAA", "userEmail": "user1@test.com"}, | ||
{"userIdentity": "us-east-1:bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb", "userId": "BBBBBBBBBBBBBBBBBBBBB", "userEmail": "user2@test.com"}], | ||
"intraCompanySharingEnabled": true, | ||
"taskDeInfo": null, | ||
"userId": "ABCDEFGHIJKLMNOPRSTUV", | ||
"internal": true | ||
} | ||
} | ||
|
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
on leave, we should reset the submit confirm question