Skip to content

Commit

Permalink
[Service Connector] az webapp connection create: Add `--store-conns…
Browse files Browse the repository at this point in the history
…tr` to support webapp connection string (#23288)

* add store_in_connection_string parameter

* fix linter

* add test

* record test

* change logging to logger
  • Loading branch information
ChenTanyi committed Jul 22, 2022
1 parent 4306570 commit 5bb5710
Show file tree
Hide file tree
Showing 5 changed files with 760 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
SOURCE_RESOURCES_PARAMS,
SOURCE_RESOURCES_CREATE_PARAMS,
TARGET_RESOURCES_PARAMS,
TARGET_RESOURCES_CONNECTION_STRING,
AUTH_TYPE_PARAMS,
SUPPORTED_AUTH_TYPE,
SUPPORTED_CLIENT_TYPE,
Expand Down Expand Up @@ -147,6 +148,15 @@ def add_vnet_block(context, target):
help='Connect target service by private endpoint. '
'The private endpoint in source virtual network must be created ahead.')

def add_connection_string_argument(context, source, target):
if source == RESOURCE.WebApp and target in TARGET_RESOURCES_CONNECTION_STRING:
context.argument('store_in_connection_string', options_list=['--store-connstr'],
arg_type=get_three_state_flag(), default=False, is_preview=True,
help='Store configuration into connection string, '
'only could be used together with dotnet client_type')
else:
context.ignore('store_in_connection_string')

def add_confluent_kafka_argument(context):
context.argument('bootstrap_server', options_list=['--bootstrap-server'], help='Kafka bootstrap server url')
context.argument('kafka_key', options_list=['--kafka-key'], help='Kafka API-Key (key)')
Expand Down Expand Up @@ -194,13 +204,15 @@ def add_confluent_kafka_argument(context):
add_new_addon_argument(c, source, target)
add_secret_store_argument(c)
add_vnet_block(c, target)
add_connection_string_argument(c, source, target)
with self.argument_context('{} connection update {}'.format(source.value, target.value)) as c:
add_client_type_argument(c, source, target)
add_connection_name_argument(c, source)
add_source_resource_block(c, source)
add_auth_block(c, source, target)
add_secret_store_argument(c)
add_vnet_block(c, target)
add_connection_string_argument(c, source, target)

# special target resource: independent implementation
target = RESOURCE.ConfluentKafka
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class AUTH_TYPE(Enum):
# The dict defines the client types
class CLIENT_TYPE(Enum):
Dotnet = 'dotnet'
DotnetConnectionString = 'dotnet-connectionString'
Java = 'java'
Nodejs = 'nodejs'
Python = 'python'
Expand All @@ -81,6 +82,9 @@ class CLIENT_TYPE(Enum):
# The target resources using user token
TARGET_RESOURCES_USERTOKEN = [RESOURCE.PostgresFlexible, RESOURCE.MysqlFlexible, RESOURCE.KeyVault]

# The target resources could be set to connection string
TARGET_RESOURCES_CONNECTION_STRING = [RESOURCE.Sql, RESOURCE.Mysql, RESOURCE.MysqlFlexible, RESOURCE.Postgres, RESOURCE.PostgresFlexible]

# The dict defines the resource id pattern of source resources.
SOURCE_RESOURCES = {
RESOURCE.WebApp: '/subscriptions/{subscription}/resourceGroups/{source_resource_group}/providers/Microsoft.Web/sites/{site}',
Expand Down
20 changes: 18 additions & 2 deletions src/azure-cli/azure/cli/command_modules/serviceconnector/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
AzureResponseError
)
from ._resource_config import (
CLIENT_TYPE,
SUPPORTED_AUTH_TYPE,
SUPPORTED_CLIENT_TYPE,
TARGET_RESOURCES
Expand Down Expand Up @@ -147,7 +148,7 @@ def connection_validate(cmd, client,
return auto_register(client.begin_validate, resource_uri=source_id, linker_name=connection_name)


def connection_create(cmd, client, # pylint: disable=too-many-locals
def connection_create(cmd, client, # pylint: disable=too-many-locals disable=too-many-statements
connection_name=None, client_type=None,
source_resource_group=None, source_id=None,
target_resource_group=None, target_id=None,
Expand All @@ -157,6 +158,7 @@ def connection_create(cmd, client, # pylint: disable=too-many-locals
key_vault_id=None,
service_endpoint=None,
private_endpoint=None,
store_in_connection_string=False,
new_addon=False, no_wait=False,
cluster=None, scope=None, enable_csi=False, # Resource.KubernetesCluster
site=None, # Resource.WebApp
Expand Down Expand Up @@ -190,6 +192,12 @@ def connection_create(cmd, client, # pylint: disable=too-many-locals
raise ValidationError('Only one auth info is needed')
auth_info = all_auth_info[0] if len(all_auth_info) == 1 else None

if store_in_connection_string:
if client_type == CLIENT_TYPE.Dotnet.value:
client_type = CLIENT_TYPE.DotnetConnectionString.value
else:
logger.warning('client_type is not dotnet, ignore "--store-in-connection-string"')

parameters = {
'target_service': {
"type": "AzureResource",
Expand Down Expand Up @@ -271,6 +279,7 @@ def connection_update(cmd, client, # pylint: disable=too-many-locals
key_vault_id=None,
service_endpoint=None,
private_endpoint=None,
store_in_connection_string=False,
no_wait=False,
scope=None,
cluster=None, enable_csi=False, # Resource.Kubernetes
Expand Down Expand Up @@ -311,13 +320,20 @@ def connection_update(cmd, client, # pylint: disable=too-many-locals
if linker.get('secretStore') and linker.get('secretStore').get('keyVaultId'):
key_vault_id = key_vault_id or linker.get('secretStore').get('keyVaultId')

client_type = client_type or linker.get('clientType')
if store_in_connection_string:
if client_type == CLIENT_TYPE.Dotnet.value:
client_type = CLIENT_TYPE.DotnetConnectionString.value
else:
logger.warning('client_type is not dotnet, ignore "--store-in-connection-string"')

parameters = {
'target_service': linker.get('targetService'),
'auth_info': auth_info,
'secret_store': {
'key_vault_id': key_vault_id,
},
'client_type': client_type or linker.get('clientType'),
'client_type': client_type,
# scope can be updated in container app while cannot be updated in aks due to some limitations
'scope': scope or linker.get('scope')
}
Expand Down
Loading

0 comments on commit 5bb5710

Please sign in to comment.