Skip to content

Commit

Permalink
[Service Connector] az containerapp connection create: Add required…
Browse files Browse the repository at this point in the history
… parameter for interactive mode (Azure#22311)

* default client type change to Blank

* add scope as required parameter in containerapp create

* update scope parameter in connection update

* update comment for scope in update command

* fix style
  • Loading branch information
ChenTanyi authored May 10, 2022
1 parent 1f4b298 commit 6f04960
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ._resource_config import (
RESOURCE,
SOURCE_RESOURCES_PARAMS,
SOURCE_RESOURCES_CREATE_PARAMS,
TARGET_RESOURCES_PARAMS,
AUTH_TYPE_PARAMS,
SUPPORTED_AUTH_TYPE,
Expand Down Expand Up @@ -63,9 +64,8 @@ def add_source_resource_block(context, source, enable_id=True, validate_source_i
help="Use keyvault as a secrets store via a CSI volume. "
"If specified, AuthType Arguments are not needed.")
elif source == RESOURCE.ContainerApp:
context.argument('scope', options_list=['-c', '--container'], type=str,
help="The container where the connection information "
"will be saved (as environment variables).")
for arg, content in SOURCE_RESOURCES_CREATE_PARAMS.get(source).items():
context.argument(arg, options_list=content.get('options'), type=str, help=content.get('help'))
context.ignore('enable_csi')
else:
context.ignore('scope')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ class CLIENT_TYPE(Enum):
}


# The dict defines the required parameters used in the source resources for creation.
SOURCE_RESOURCES_CREATE_PARAMS = {
RESOURCE.ContainerApp: {
'scope': {
'options': ['--container', '-c'],
'help': 'The container where the connection information will be saved (as environment variables).',
'placeholder': 'MyContainer'
},
},
}


# The dict defines the parameters used to position the target resources.
# The parmaters should include all variables defined in target resource id expect
# for 'subscription', which will be dealt by CLI core as a default parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
SOURCE_RESOURCES,
TARGET_RESOURCES,
SOURCE_RESOURCES_PARAMS,
SOURCE_RESOURCES_CREATE_PARAMS,
TARGET_RESOURCES_PARAMS,
AUTH_TYPE_PARAMS,
SUPPORTED_AUTH_TYPE
Expand Down Expand Up @@ -175,7 +176,7 @@ def _infer_springcloud(source_id):

return client_type

# fallback to use Dotnet as client type
# fallback to use None as client type
client_type = None
if 'webapp' in cmd.name:
client_type = _infer_webapp(namespace.source_id)
Expand All @@ -184,7 +185,7 @@ def _infer_springcloud(source_id):

method = 'detected'
if client_type is None:
client_type = CLIENT_TYPE.Dotnet
client_type = CLIENT_TYPE.Blank
method = 'default'

logger.warning('Client type is not specified, use %s one: --client-type %s', method, client_type.value)
Expand Down Expand Up @@ -385,6 +386,21 @@ def get_missing_source_args(cmd):
return missing_args


def get_missing_source_create_args(cmd, namespace):
'''Get source resource related args in create
'''
source = get_source_resource_name(cmd)
missing_args = dict()

args = SOURCE_RESOURCES_CREATE_PARAMS.get(source)
if args:
for arg, content in args.items():
if not getattr(namespace, arg, None):
missing_args[arg] = content

return missing_args


def get_missing_target_args(cmd):
'''Get target resource related args
'''
Expand Down Expand Up @@ -471,6 +487,7 @@ def validate_create_params(cmd, namespace):
missing_args = dict()
if not validate_source_resource_id(namespace):
missing_args.update(get_missing_source_args(cmd))
missing_args.update(get_missing_source_create_args(cmd, namespace))
if not validate_target_resource_id(namespace):
missing_args.update(get_missing_target_args(cmd))
missing_args.update(get_missing_auth_args(cmd, namespace))
Expand Down Expand Up @@ -519,6 +536,15 @@ def apply_source_args(cmd, namespace, arg_values):
)


def apply_source_create_args(cmd, namespace, arg_values):
'''Set source resource related args in create by arg_values
'''
source = get_source_resource_name(cmd)
for arg in SOURCE_RESOURCES_CREATE_PARAMS.get(source, {}):
if arg in arg_values:
setattr(namespace, arg, arg_values.get(arg, None))


def apply_target_args(cmd, namespace, arg_values):
'''Set target resource id by arg_values
'''
Expand Down Expand Up @@ -568,6 +594,7 @@ def apply_create_params(cmd, namespace, arg_values):
'''Set create command missing args
'''
apply_source_args(cmd, namespace, arg_values)
apply_source_create_args(cmd, namespace, arg_values)
apply_target_args(cmd, namespace, arg_values)
apply_auth_args(cmd, namespace, arg_values)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ def connection_update(cmd, client, # pylint: disable=too-many-locals
key_vault_id=None,
service_endpoint=None,
no_wait=False,
scope=None,
cluster=None, enable_csi=False, # Resource.Kubernetes
site=None, # Resource.WebApp
spring=None, app=None, deployment='default'): # Resource.SpringCloud
Expand Down Expand Up @@ -306,8 +307,8 @@ def connection_update(cmd, client, # pylint: disable=too-many-locals
'key_vault_id': key_vault_id,
},
'client_type': client_type or linker.get('clientType'),
# scope does not support update due to aks solution's limitation
'scope': linker.get('scope')
# scope can be updated in container app while cannot be updated in aks due to some limitations
'scope': scope or linker.get('scope')
}

# HACK: set user token to work around OBO
Expand Down

0 comments on commit 6f04960

Please sign in to comment.