This repository was archived by the owner on May 17, 2023. It is now read-only.
Repository navigation
Feat/national clouds #52
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
40c816b
Add cloud commands
jobala eb0a7a6
Move profile
jobala 309ccbc
Supports selecting a cloud
jobala 3823f8a
Remove list clouds
jobala ec9388b
Supports user defined clouds
jobala 816a37d
Add help information
jobala be657ca
Move constants into msgraph.cli.core
jobala e82f31d
Validate endpoint
jobala 6c25da7
Accept user provided client_id and read authority from user profile
jobala d9c08a5
Use cloud endpoint as base url
jobala 8f37e19
Fix auth-record conflict
jobala ca2520c
Add comments
jobala c378e1b
Specify version
jobala e629b75
Improve print message
jobala 276d34e
Add default base url constant
jobala eed4497
Refactor to graph_endpoint and azure_ad_endpoint
jobala 10afc67
Ask devs to update the help file
jobala f9d5b42
Update login help
jobala 24ba9f6
Refactor adding a cloud
jobala bc715be
Add get-current command
jobala 21b584b
Add support for deleting clouds
jobala afce461
Update help
jobala a8eba11
Show graph and azure_ad_endpoints when selecting a cloud
jobala 6dbd505
Move CloudManager to the profile module
jobala 741d1a9
Fixes import error
jobala 875770a
Add update command help information
jobala 4de033d
Raise CLI errors when a cloud is current or doesn't exist
jobala 3aa5c3e
Parameterizes error message in write_profile
jobala 5bf95a7
Improve error message
jobala 7192125
Document CloudManager
jobala 6400899
Add instructions on how to change and show the current cloud
jobala 3cb316f
Fix comment
jobala 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
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 |
|---|---|---|
| @@ -1,25 +1,25 @@ | ||
| installed_extensions = [ | ||
| 'azext_schemaextensions', | ||
| 'azext_cloudcommunications', | ||
| 'azext_calendar', | ||
| 'azext_usersfunctions', | ||
| 'azext_security', | ||
| 'azext_mail', | ||
| 'azext_planner', | ||
| 'azext_education', | ||
| 'azext_sites', | ||
| 'azext_changenotifications', | ||
| 'azext_notes', | ||
| 'azext_users', | ||
| 'azext_usersactions', | ||
| 'azext_people', | ||
| 'azext_teams', | ||
| 'azext_applications', | ||
| 'azext_files', | ||
| 'azext_identitydirectorymanagement', | ||
| 'azext_directoryobjects', | ||
| 'azext_crossdeviceexperiences', | ||
| 'azext_personalcontacts', | ||
| 'azext_identitysignins', | ||
| 'azext_reports', | ||
| installed_extensions = [ | ||
|
MIchaelMainer marked this conversation as resolved.
Resolved
|
||
| 'azext_applications', | ||
| 'azext_calendar', | ||
| 'azext_changenotifications', | ||
| 'azext_cloudcommunications', | ||
| 'azext_crossdeviceexperiences', | ||
| 'azext_directoryobjects', | ||
| 'azext_education', | ||
| 'azext_files', | ||
| 'azext_identitydirectorymanagement', | ||
| 'azext_identitysignins', | ||
| 'azext_mail', | ||
| 'azext_notes', | ||
| 'azext_people', | ||
| 'azext_personalcontacts', | ||
| 'azext_planner', | ||
| 'azext_reports', | ||
| 'azext_schemaextensions', | ||
| 'azext_security', | ||
| 'azext_sites', | ||
| 'azext_teams', | ||
| 'azext_users', | ||
| 'azext_usersactions', | ||
| 'azext_usersfunctions', | ||
| ] | ||
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,26 @@ | ||
| import json | ||
| from msgraph.cli.core.constants import PROFILE_LOCATION | ||
| from msgraph.cli.core.exceptions import CLIException | ||
|
|
||
|
|
||
| def read_profile() -> dict: | ||
| result = None | ||
|
|
||
| try: | ||
| with open(PROFILE_LOCATION, 'r') as file: | ||
| result = file.read() | ||
| return json.loads(result) | ||
| except IOError as ex: | ||
| # This error will be thrown if a user has not selected a cloud before. | ||
| # Returning an empty dictionary so that the result can be processed by json.loads() | ||
| return {} | ||
|
|
||
|
|
||
| def write_profile(profile: dict, error_msg: str): | ||
| #TODO: Parameterize the error message | ||
|
|
||
| try: | ||
| with open(PROFILE_LOCATION, 'w') as file: | ||
| file.write(json.dumps(profile)) | ||
| except IOError as ex: | ||
| raise CLIException(error_msg) from ex |
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,32 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
|
|
||
| from msgraph.cli.core import AzCommandsLoader | ||
| from msgraph.cli.core.commands import CliCommandType | ||
| from msgraph.command_modules.profile._exception_handler import profile_exception_handler | ||
| import msgraph.command_modules.profile._help | ||
|
|
||
|
|
||
| class ProfileCommandsLoader(AzCommandsLoader): | ||
| """Provides profile commands to msgraph-cli | ||
| """ | ||
| def __init__(self, cli_ctx, **kwargs): | ||
| super().__init__(cli_ctx, **kwargs) | ||
|
|
||
| def load_command_table(self, args): | ||
| # operations_tmpl is the file that contains the implementation of the command | ||
| command_type = CliCommandType(operations_tmpl='msgraph.command_modules.profile.custom#{}', | ||
| exception_handler=profile_exception_handler) | ||
| with self.command_group('cloud', command_type) as group: | ||
| group.command('select', 'select_cloud') | ||
| group.command('add', 'add_cloud') | ||
|
jobala marked this conversation as resolved.
Resolved
jobala marked this conversation as resolved.
Resolved
|
||
| group.command('show-current', 'current_cloud') | ||
| group.command('update', 'update_cloud') | ||
| group.command('delete', 'delete_cloud') | ||
|
|
||
| return self.command_table | ||
|
|
||
|
|
||
| COMMAND_LOADER_CLS = ProfileCommandsLoader | ||
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,96 @@ | ||
| # coding=utf-8 | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| from msgraph.cli import read_profile, write_profile | ||
| from msgraph.cli.core.constants import DEFAULT_CLOUDS | ||
| from msgraph.cli.core.exceptions import CLIError | ||
|
|
||
|
|
||
| class CloudManager: | ||
|
MIchaelMainer marked this conversation as resolved.
Resolved
|
||
| '''This class adds support for managing MicrosoftGraph and user defined clouds | ||
| List of supported Microsoft Graph clouds: https://docs.microsoft.com/en-us/graph/deployments | ||
| ''' | ||
| def __init__(self): | ||
| self.profile = read_profile() | ||
|
|
||
| def get_clouds(self) -> dict: | ||
| user_defined_clouds = self.profile.get('user_defined_clouds', {}) | ||
| supported_clouds = DEFAULT_CLOUDS | ||
|
|
||
| for cloud in user_defined_clouds: | ||
| supported_clouds.update(cloud) | ||
| return supported_clouds | ||
|
|
||
| def create_cloud(self, cloud_name, cloud_endpoints): | ||
| entry = {cloud_name: cloud_endpoints} | ||
|
|
||
| try: | ||
| user_defined_clouds = self.profile['user_defined_clouds'] | ||
| user_defined_clouds.append(entry) | ||
| except KeyError: | ||
| self.profile['user_defined_clouds'] = [entry] | ||
|
|
||
| write_profile(self.profile, 'An error occured while creating the cloud.') | ||
|
|
||
| def get_current_cloud(self) -> dict: | ||
| return self.profile.get('cloud', None) | ||
|
|
||
| def update_cloud(self, cloud_name: str, props: dict): | ||
| updated = False | ||
| user_defined_clouds = self.profile.get('user_defined_clouds') | ||
|
|
||
| # Search for the cloud to update in user defined clouds | ||
| for index, cloud in enumerate(user_defined_clouds): | ||
| found = cloud.get(cloud_name, None) | ||
|
|
||
| if found: | ||
| cloud_to_update = cloud[cloud_name] | ||
| cloud_to_update.update(props) | ||
|
|
||
| # Remove the old cloud and replace it with the updated cloud | ||
| user_defined_clouds.pop(index) | ||
| user_defined_clouds.insert(index, {cloud_to_update['name']: cloud_to_update}) | ||
|
|
||
| updated = True | ||
| self.profile['user_defined_clouds'] = user_defined_clouds | ||
| write_profile( | ||
| self.profile, | ||
| error_msg=f'An error occured while updating the "{cloud_name}" cloud') | ||
|
|
||
| # No need to keep looking for the cloud | ||
| break | ||
|
|
||
| return updated | ||
|
|
||
| def delete_cloud(self, name: str): | ||
| result = [] | ||
| current_cloud = self.profile.get('cloud', None) | ||
|
|
||
| # throw an error if a user attempts to delete a current cloud | ||
| if current_cloud and name == current_cloud['name']: | ||
| raise CLIError(f'''The cloud "{name}" could not be deleted because it is a current cloud | ||
|
|
||
| To see the current cloud run mg cloud show-current | ||
| To change to a different cloud run mg cloud select | ||
| ''') | ||
|
|
||
| # throw an error if the cloud is not a user defined cloud | ||
| if name not in self.profile['user_defined_clouds']: | ||
| raise CLIError(f'The cloud "{name}" is not a user defined cloud') | ||
|
|
||
| for cloud in self.profile['user_defined_clouds']: | ||
| if name not in cloud.keys(): | ||
|
MIchaelMainer marked this conversation as resolved.
Resolved
|
||
| result.append(cloud) | ||
|
|
||
| self.profile['user_defined_clouds'] = result | ||
| write_profile(self.profile, error_msg=f'An error occured while deleting the "{name}" cloud') | ||
|
|
||
| def set_current_cloud(self, name: str): | ||
| self.profile['cloud'] = self.get_clouds().get(name) | ||
| write_profile( | ||
| self.profile, | ||
| error_msg= | ||
| 'An error occured while setting the selected cloud, the CLI will use the PUBLIC cloud') | ||
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,11 @@ | ||
| # coding=utf-8 | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| from knack.cli import CLIError | ||
|
|
||
|
|
||
| def profile_exception_handler(exception): | ||
| raise CLIError(exception) |
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.
Uh oh!
There was an error while loading. Please reload this page.