From 4271e5ccdefbdd87d97daf3e42981490df147eff Mon Sep 17 00:00:00 2001 From: Pankil Daru Date: Fri, 19 Aug 2022 05:05:38 +0000 Subject: [PATCH] update style --- src/fleet/azext_fleet/_helpers.py | 14 ++++--- src/fleet/azext_fleet/_params.py | 5 +-- src/fleet/azext_fleet/custom.py | 64 ++++++++++++++++++------------- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/fleet/azext_fleet/_helpers.py b/src/fleet/azext_fleet/_helpers.py index b19c47e1a8f..d9f8ad05528 100644 --- a/src/fleet/azext_fleet/_helpers.py +++ b/src/fleet/azext_fleet/_helpers.py @@ -8,10 +8,10 @@ from knack.log import get_logger from knack.prompting import NoTTYException, prompt_y_n from knack.util import CLIError -from knack.log import get_logger logger = get_logger(__name__) + def print_or_merge_credentials(path, kubeconfig, overwrite_existing, context_name): """Merge an unencrypted kubeconfig into the file at the specified path, or print it to stdout if the path is "-". @@ -48,6 +48,7 @@ def print_or_merge_credentials(path, kubeconfig, overwrite_existing, context_nam additional_file.close() os.remove(temp_path) + def _merge_kubernetes_configurations(existing_file, addition_file, replace, context_name=None): existing = _load_kubernetes_configuration(existing_file) addition = _load_kubernetes_configuration(addition_file) @@ -70,7 +71,7 @@ def _merge_kubernetes_configurations(existing_file, addition_file, replace, cont if addition is None: raise CLIError( - 'failed to load additional configuration from {}'.format(addition_file)) + f'failed to load additional configuration from {addition_file}') if existing is None: existing = addition @@ -94,10 +95,10 @@ def _merge_kubernetes_configurations(existing_file, addition_file, replace, cont yaml.safe_dump(existing, stream, default_flow_style=False) current_context = addition.get('current-context', 'UNKNOWN') - msg = 'Merged "{}" as current context in {}'.format( - current_context, existing_file) + msg = f'Merged "{current_context}" as current context in {existing_file}' print(msg) + def _handle_merge(existing, addition, key, replace): if not addition[key]: return @@ -124,12 +125,13 @@ def _handle_merge(existing, addition, key, replace): raise CLIError(msg.format(i['name'], key)) existing[key].append(i) + def _load_kubernetes_configuration(filename): try: with open(filename) as stream: return yaml.safe_load(stream) except (IOError, OSError) as ex: if getattr(ex, 'errno', 0) == errno.ENOENT: - raise CLIError('{} does not exist'.format(filename)) + raise CLIError(f'{filename} does not exist') except (yaml.parser.ParserError, UnicodeDecodeError) as ex: - raise CLIError('Error parsing {} ({})'.format(filename, str(ex))) \ No newline at end of file + raise CLIError(f'Error parsing {filename} ({str(ex)})') diff --git a/src/fleet/azext_fleet/_params.py b/src/fleet/azext_fleet/_params.py index 7c34607cdad..99b2cd15f6f 100644 --- a/src/fleet/azext_fleet/_params.py +++ b/src/fleet/azext_fleet/_params.py @@ -7,7 +7,6 @@ import os from argcomplete.completers import FilesCompleter from azure.cli.core.commands.parameters import ( - get_resource_name_completion_list, tags_type, file_type ) @@ -22,10 +21,10 @@ def load_arguments(self, _): with self.argument_context('fleet create') as c: c.argument('tags', tags_type) c.argument('dns_name_prefix', options_list=['--dns-name-prefix', '-p']) - + with self.argument_context('fleet patch') as c: c.argument('tags', tags_type) - + with self.argument_context('fleet get-credentials') as c: c.argument('context_name', options_list=['--context'], help='If specified, overwrite the default context name.') c.argument('path', options_list=['--file', '-f'], type=file_type, completer=FilesCompleter(), default=os.path.join(os.path.expanduser('~'), '.kube', 'config')) diff --git a/src/fleet/azext_fleet/custom.py b/src/fleet/azext_fleet/custom.py index 12ada9d6bf1..0ac7a67a4b4 100644 --- a/src/fleet/azext_fleet/custom.py +++ b/src/fleet/azext_fleet/custom.py @@ -45,13 +45,13 @@ def create_fleet(cmd, if not name_part[0].isalpha(): name_part = (str('a') + name_part)[0:10] resource_group_part = re.sub('[^A-Za-z0-9-]', '', resource_group_name)[0:16] - dns_name_prefix = '{}-{}-{}'.format(name_part, resource_group_part, subscription_id[0:6]) + dns_name_prefix = f'{name_part}-{resource_group_part}-{subscription_id[0:6]}' fleetHubProfile = FleetHubProfile(dns_prefix=dns_name_prefix) rg_location = get_rg_location(cmd.cli_ctx, resource_group_name) if location is None: location = rg_location - + fleet = Fleet( location=location, tags=tags, @@ -60,10 +60,11 @@ def create_fleet(cmd, return sdk_no_wait(no_wait, client.begin_create_or_update, resource_group_name, name, fleet) -def patch_fleet(cmd, - client, - resource_group_name, - name, + +def patch_fleet(cmd, + client, + resource_group_name, + name, tags=None): FleetPatch = cmd.get_models( "FleetPatch", @@ -73,25 +74,29 @@ def patch_fleet(cmd, fleetPatch = FleetPatch(tags=tags) return client.update(resource_group_name, name, None, fleetPatch) -def get_fleet(cmd, # pylint: disable=unused-argument - client, - resource_group_name, + +def get_fleet(cmd, # pylint: disable=unused-argument + client, + resource_group_name, name): - logger.info('Getting fleet: {0} for resource group: {1}'.format(name, resource_group_name)) + logger.info('Getting fleet: %s for resource group: %s', name, resource_group_name) return client.get(resource_group_name, name) -def list_fleet_by_resource_group(cmd, # pylint: disable=unused-argument - client, + +def list_fleet_by_resource_group(cmd, # pylint: disable=unused-argument + client, resource_group_name): - logger.info('Listing fleet for resource group: {0}'.format(resource_group_name)) + logger.info('Listing fleet for resource group: %s', resource_group_name) return client.list_by_resource_group(resource_group_name) -def list_fleet_by_subscription(cmd, # pylint: disable=unused-argument + +def list_fleet_by_subscription(cmd, # pylint: disable=unused-argument client): subscription_id = get_subscription_id(cmd.cli_ctx) - logger.info('Listing fleet for subscription id: {0}'.format(subscription_id)) + logger.info('Listing fleet for subscription id: %s', subscription_id) return client.list() + def delete_fleet(cmd, # pylint: disable=unused-argument client, resource_group_name, @@ -99,14 +104,15 @@ def delete_fleet(cmd, # pylint: disable=unused-argument no_wait=False): return sdk_no_wait(no_wait, client.begin_delete, resource_group_name, name) -def get_credentials(cmd, # pylint: disable=unused-argument - client, - resource_group_name, - name, - path=os.path.join(os.path.expanduser( - '~'), '.kube', 'config'), - overwrite_existing=False, - context_name=None): + +def get_credentials(cmd, # pylint: disable=unused-argument + client, + resource_group_name, + name, + path=os.path.join(os.path.expanduser( + '~'), '.kube', 'config'), + overwrite_existing=False, + context_name=None): credentialResults = client.list_credentials(resource_group_name, name) if not credentialResults: raise CLIError("No Kubernetes credentials found.") @@ -119,6 +125,7 @@ def get_credentials(cmd, # pylint: disable=unused-argument except (IndexError, ValueError): raise CLIError("Fail to find kubeconfig file.") + def join_fleet_member(cmd, client, resource_group_name, @@ -134,20 +141,23 @@ def join_fleet_member(cmd, fleetMember = FleetMember(cluster_resource_id=member_cluster_id) return sdk_no_wait(no_wait, client.begin_create_or_update, resource_group_name, fleet_name, name, fleetMember) + def list_fleet_member(cmd, # pylint: disable=unused-argument client, resource_group_name, fleet_name): - logger.info('Listing fleet member(s) for fleet: {0} for resource group: {1}'.format(fleet_name, resource_group_name)) + logger.info('Listing fleet member(s) for fleet: %s for resource group: %s', fleet_name, resource_group_name) return client.list_by_fleet(resource_group_name, fleet_name) + def get_fleet_member(cmd, # pylint: disable=unused-argument - client, - resource_group_name, - fleet_name, + client, + resource_group_name, + fleet_name, name): return client.get(resource_group_name, fleet_name, name) + def remove_fleet_member(cmd, # pylint: disable=unused-argument client, resource_group_name,