Skip to content
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

Code Cleanup and Style Fixes #73

Merged
merged 1 commit into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/k8s-extension/azext_k8s_extension/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def __init__(self, cli_ctx=None):
k8s_extension_custom = CliCommandType(
operations_tmpl=consts.EXTENSION_PACKAGE_NAME + '.custom#{}',
client_factory=cf_k8s_extension)
super(K8sExtensionCommandsLoader, self).__init__(cli_ctx=cli_ctx,
custom_command_type=k8s_extension_custom)
super().__init__(cli_ctx=cli_ctx,
custom_command_type=k8s_extension_custom)

def load_command_table(self, args):
from .commands import load_command_table
Expand Down
12 changes: 6 additions & 6 deletions src/k8s-extension/azext_k8s_extension/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def __call__(self, parser, namespace, values, option_string=None):
try:
key, value = item.split('=', 1)
settings[key] = value
except ValueError:
except ValueError as ex:
raise ArgumentUsageError('Usage error: {} configuration_setting_key=configuration_setting_value'.
format(option_string))
super(AddConfigurationSettings, self).__call__(parser, namespace, settings, option_string)
format(option_string)) from ex
super().__call__(parser, namespace, settings, option_string)


# pylint: disable=protected-access, too-few-public-methods
Expand All @@ -31,7 +31,7 @@ def __call__(self, parser, namespace, values, option_string=None):
try:
key, value = item.split('=', 1)
prot_settings[key] = value
except ValueError:
except ValueError as ex:
raise ArgumentUsageError('Usage error: {} configuration_protected_setting_key='
'configuration_protected_setting_value'.format(option_string))
super(AddConfigurationProtectedSettings, self).__call__(parser, namespace, prot_settings, option_string)
'configuration_protected_setting_value'.format(option_string)) from ex
super().__call__(parser, namespace, prot_settings, option_string)
26 changes: 12 additions & 14 deletions src/k8s-extension/azext_k8s_extension/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def show_k8s_extension(client, resource_group_name, cluster_name, name, cluster_
cluster_rp, cluster_type, cluster_name, name)
else:
message = ex.message
raise ResourceNotFoundError(message)
raise ResourceNotFoundError(message) from ex
raise ex


def create_k8s_extension(cmd, client, resource_group_name, cluster_name, name, cluster_type,
Expand Down Expand Up @@ -98,7 +99,7 @@ def create_k8s_extension(cmd, client, resource_group_name, cluster_name, name, c
config_protected_settings = {}
# Get Configuration Settings from file
if configuration_settings_file is not None:
config_settings = __get_config_settings_from_file(configuration_settings_file)
config_settings = __read_config_settings_file(configuration_settings_file)

if configuration_settings is not None:
for dicts in configuration_settings:
Expand All @@ -107,7 +108,7 @@ def create_k8s_extension(cmd, client, resource_group_name, cluster_name, name, c

# Get Configuration Protected Settings from file
if configuration_protected_settings_file is not None:
config_protected_settings = __get_config_settings_from_file(configuration_protected_settings_file)
config_protected_settings = __read_config_settings_file(configuration_protected_settings_file)

if configuration_protected_settings is not None:
for dicts in configuration_protected_settings:
Expand Down Expand Up @@ -281,18 +282,15 @@ def __validate_version_and_auto_upgrade(version, auto_upgrade_minor_version):
auto_upgrade_minor_version = False


def __get_config_settings_from_file(file_path):
def __read_config_settings_file(file_path):
try:
config_file = open(file_path,)
settings = json.load(config_file)
except ValueError:
raise Exception("File {} is not a valid JSON file".format(file_path))

files = len(settings)
if files == 0:
raise Exception("File {} is empty".format(file_path))

return settings
with open(file_path, 'r') as f:
settings = json.load(f)
if len(settings) == 0:
raise Exception("File {} is empty".format(file_path))
return settings
except ValueError as ex:
raise Exception("File {} is not a valid JSON file".format(file_path)) from ex


def __is_dogfood_cluster(cmd):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@

# pylint: disable=unused-argument

from ..vendored_sdks.models import ExtensionInstance
from ..vendored_sdks.models import ExtensionInstanceUpdate
from ..vendored_sdks.models import ScopeCluster
from ..vendored_sdks.models import ScopeNamespace
from ..vendored_sdks.models import Scope

from .DefaultExtension import DefaultExtension


Expand All @@ -24,34 +18,10 @@ def Create(self, cmd, client, resource_group_name, cluster_name, name, cluster_t
Must create and return a valid 'ExtensionInstance' object.

"""
ext_scope = None
if scope is not None:
if scope.lower() == 'cluster':
scope_cluster = ScopeCluster(release_namespace=release_namespace)
ext_scope = Scope(cluster=scope_cluster, namespace=None)
elif scope.lower() == 'namespace':
scope_namespace = ScopeNamespace(target_namespace=target_namespace)
ext_scope = Scope(namespace=scope_namespace, cluster=None)

create_identity = True
extension_instance = ExtensionInstance(
extension_type=extension_type,
auto_upgrade_minor_version=auto_upgrade_minor_version,
release_train=release_train,
version=version,
scope=ext_scope,
configuration_settings=configuration_settings,
configuration_protected_settings=configuration_protected_settings,
)
return extension_instance, name, create_identity

def Update(self, extension, auto_upgrade_minor_version, release_train, version):
"""Default validations & defaults for Update
Must create and return a valid 'ExtensionInstanceUpdate' object.

"""
return ExtensionInstanceUpdate(
auto_upgrade_minor_version=auto_upgrade_minor_version,
release_train=release_train,
version=version
)
extension_instance, extension_name, _ = super(). \
Create(cmd, client, resource_group_name, cluster_name, name, cluster_type, extension_type,
scope, auto_upgrade_minor_version, release_train, version, target_namespace,
release_namespace, configuration_settings, configuration_protected_settings,
configuration_settings_file, configuration_protected_settings_file)
return extension_instance, extension_name, True