Skip to content

Commit 77ce1bb

Browse files
authored
[Service Connector] az webapp connection create mongodb-atlas (#31308)
1 parent ba0ba29 commit 77ce1bb

File tree

7 files changed

+1939
-21
lines changed

7 files changed

+1939
-21
lines changed

src/azure-cli/azure/cli/command_modules/serviceconnector/_help.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,23 @@ def get_source_display_name(sourcename):
270270
else:
271271
auth_params = get_auth_info_params(auth_types[0])
272272

273+
if target in {RESOURCE.MongoDbAtlas}:
274+
auth_params = '--secret secret=xx'
275+
273276
# auth info params in help message
274277
secret_param = ''
275278
if AUTH_TYPE.Secret in auth_types:
276-
if source.value != RESOURCE.KubernetesCluster.value:
279+
if target in {RESOURCE.MongoDbAtlas}:
280+
secret_param = '''
281+
- name: --secret
282+
short-summary: The connection string for secret auth
283+
long-summary: |
284+
Usage: --secret secret=XX
285+
286+
secret : Connection string for secret auth.
287+
Example: mongodb+srv://myUser:myPassword@cluster0.a12345.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
288+
'''
289+
elif source.value != RESOURCE.KubernetesCluster.value:
277290
secret_param = '''
278291
- name: --secret
279292
short-summary: The secret auth info
@@ -402,7 +415,7 @@ def get_source_display_name(sourcename):
402415
target_id=target_id,
403416
auth_params=auth_params,
404417
source_display_name=source_display_name
405-
) if target is not RESOURCE.NeonPostgres else ''
418+
) if target not in [RESOURCE.NeonPostgres, RESOURCE.MongoDbAtlas] else ''
406419

407420
helps['{source} connection create {target}'.format(source=source.value, target=target.value)] = """
408421
type: command

src/azure-cli/azure/cli/command_modules/serviceconnector/_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def add_target_resource_block(context, target):
154154
context.argument(arg, options_list=content.get('options'), type=str,
155155
help='{}. Required if \'--target-id\' is not specified.'.format(content.get('help')))
156156
required_args.append(content.get('options')[0])
157-
if target == RESOURCE.NeonPostgres:
157+
if target == RESOURCE.NeonPostgres or target == RESOURCE.MongoDbAtlas:
158158
context.ignore('target_id')
159159
else:
160160
context.argument('target_id', type=str,

src/azure-cli/azure/cli/command_modules/serviceconnector/_resource_config.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class RESOURCE(Enum):
5454
AppInsights = 'app-insights'
5555
CognitiveServices = 'cognitiveservices'
5656
NeonPostgres = 'neon-postgres'
57+
MongoDbAtlas = 'mongodb-atlas'
5758

5859
@classmethod
5960
def value_of(cls, value):
@@ -157,7 +158,8 @@ class CLIENT_TYPE(Enum):
157158
RESOURCE.ContainerApp: '/subscriptions/{subscription}/resourceGroups/{target_resource_group}/providers/Microsoft.App/containerApps/{target_app_name}',
158159

159160
RESOURCE.FabricSql: 'https://api.fabric.microsoft.com/v1/workspaces/{fabric_workspace_uuid}/SqlDatabases/{fabric_sql_db_uuid}',
160-
RESOURCE.NeonPostgres: '/subscriptions/aaaabbbb-0000-cccc-1111-dddd2222eeee/resourceGroups/testrg/providers/Neon.Postgres/organizations/neontest'
161+
RESOURCE.NeonPostgres: '/subscriptions/aaaabbbb-0000-cccc-1111-dddd2222eeee/resourceGroups/testrg/providers/Neon.Postgres/organizations/neontest',
162+
RESOURCE.MongoDbAtlas: '/subscriptions/aaaabbbb-0000-cccc-1111-dddd2222eeee/resourceGroups/{target_resource_group}/providers/MongoDB.Atlas/organizations/{server}'
161163

162164
}
163165

@@ -696,6 +698,18 @@ class CLIENT_TYPE(Enum):
696698
'help': 'Name of the sql database',
697699
'placeholder': 'MyDB'
698700
}
701+
},
702+
RESOURCE.MongoDbAtlas: {
703+
'target_resource_group': {
704+
'options': ['--target-resource-group', '--tg'],
705+
'help': 'The resource group which contains the MongoDB Atlas',
706+
'placeholder': 'MongoDBAtlasRG'
707+
},
708+
'server': {
709+
'options': ['--server'],
710+
'help': 'Name of the MongoDB Atlas server',
711+
'placeholder': 'MongoDBAtlasServer'
712+
}
699713
}
700714
}
701715

@@ -862,7 +876,8 @@ class CLIENT_TYPE(Enum):
862876
RESOURCE.AppInsights: [AUTH_TYPE.SecretAuto],
863877

864878
RESOURCE.CognitiveServices: [AUTH_TYPE.SystemIdentity, AUTH_TYPE.SecretAuto, AUTH_TYPE.UserIdentity, AUTH_TYPE.ServicePrincipalSecret],
865-
RESOURCE.NeonPostgres: [AUTH_TYPE.Secret]
879+
RESOURCE.NeonPostgres: [AUTH_TYPE.Secret],
880+
RESOURCE.MongoDbAtlas: [AUTH_TYPE.Secret]
866881
},
867882
RESOURCE.SpringCloud: {
868883
RESOURCE.Postgres: [AUTH_TYPE.Secret, AUTH_TYPE.SystemIdentity, AUTH_TYPE.UserIdentity, AUTH_TYPE.ServicePrincipalSecret],
@@ -895,7 +910,8 @@ class CLIENT_TYPE(Enum):
895910
RESOURCE.AppInsights: [AUTH_TYPE.SecretAuto],
896911

897912
RESOURCE.CognitiveServices: [AUTH_TYPE.SystemIdentity, AUTH_TYPE.SecretAuto, AUTH_TYPE.UserIdentity, AUTH_TYPE.ServicePrincipalSecret],
898-
RESOURCE.NeonPostgres: [AUTH_TYPE.Secret]
913+
RESOURCE.NeonPostgres: [AUTH_TYPE.Secret],
914+
RESOURCE.MongoDbAtlas: [AUTH_TYPE.Secret]
899915
},
900916
RESOURCE.KubernetesCluster: {
901917
RESOURCE.Postgres: [AUTH_TYPE.Secret],
@@ -927,7 +943,8 @@ class CLIENT_TYPE(Enum):
927943
RESOURCE.AppInsights: [AUTH_TYPE.SecretAuto],
928944

929945
RESOURCE.CognitiveServices: [AUTH_TYPE.WorkloadIdentity, AUTH_TYPE.SecretAuto, AUTH_TYPE.ServicePrincipalSecret],
930-
RESOURCE.NeonPostgres: [AUTH_TYPE.Secret]
946+
RESOURCE.NeonPostgres: [AUTH_TYPE.Secret],
947+
RESOURCE.MongoDbAtlas: [AUTH_TYPE.Secret]
931948
},
932949
RESOURCE.ContainerApp: {
933950
RESOURCE.Postgres: [AUTH_TYPE.Secret, AUTH_TYPE.SystemIdentity, AUTH_TYPE.UserIdentity, AUTH_TYPE.ServicePrincipalSecret],
@@ -961,7 +978,7 @@ class CLIENT_TYPE(Enum):
961978

962979
RESOURCE.CognitiveServices: [AUTH_TYPE.SystemIdentity, AUTH_TYPE.SecretAuto, AUTH_TYPE.UserIdentity, AUTH_TYPE.ServicePrincipalSecret],
963980
RESOURCE.NeonPostgres: [AUTH_TYPE.Secret],
964-
981+
RESOURCE.MongoDbAtlas: [AUTH_TYPE.Secret],
965982
RESOURCE.ContainerApp: [AUTH_TYPE.Null]
966983
},
967984
}
@@ -1012,6 +1029,19 @@ class CLIENT_TYPE(Enum):
10121029
CLIENT_TYPE.SpringBoot,
10131030
CLIENT_TYPE.Blank
10141031
],
1032+
RESOURCE.MongoDbAtlas: [
1033+
CLIENT_TYPE.Dotnet,
1034+
CLIENT_TYPE.DotnetInternal,
1035+
CLIENT_TYPE.Java,
1036+
CLIENT_TYPE.Python,
1037+
CLIENT_TYPE.Nodejs,
1038+
CLIENT_TYPE.Go,
1039+
CLIENT_TYPE.Php,
1040+
CLIENT_TYPE.Ruby,
1041+
CLIENT_TYPE.Django,
1042+
CLIENT_TYPE.SpringBoot,
1043+
CLIENT_TYPE.Blank
1044+
],
10151045
RESOURCE.Mysql: [
10161046
CLIENT_TYPE.Dotnet,
10171047
CLIENT_TYPE.DotnetInternal,

src/azure-cli/azure/cli/command_modules/serviceconnector/_validators.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -232,23 +232,35 @@ def _infer_springcloud(source_id):
232232
return client_type.value
233233

234234

235-
def interactive_input(arg, hint):
235+
def interactive_input(arg, hint, cmd):
236236
'''Get interactive inputs from users
237237
'''
238238
value = None
239239
cmd_value = None
240240
if arg == 'secret_auth_info':
241-
name = prompt('User name of database (--secret name=): ')
242-
secret = prompt_pass('Password of database (--secret secret=): ')
243-
value = {
244-
'name': name,
245-
'secret_info': {
246-
'secret_type': 'rawValue',
247-
'value': secret
248-
},
249-
'auth_type': 'secret'
250-
}
251-
cmd_value = 'name={} secret={}'.format(name, '*' * len(secret))
241+
if 'mongodb-atlas' in cmd.name:
242+
secret = prompt_pass('Connection string of cluster (--secret secret=): ')
243+
value = {
244+
'name': 'NA',
245+
'secret_info': {
246+
'secret_type': 'rawValue',
247+
'value': secret
248+
},
249+
'auth_type': 'secret'
250+
}
251+
cmd_value = 'secret={}'.format('*' * len(secret))
252+
else:
253+
name = prompt('User name of database (--secret name=): ')
254+
secret = prompt_pass('Password of database (--secret secret=): ')
255+
value = {
256+
'name': name,
257+
'secret_info': {
258+
'secret_type': 'rawValue',
259+
'value': secret
260+
},
261+
'auth_type': 'secret'
262+
}
263+
cmd_value = 'name={} secret={}'.format(name, '*' * len(secret))
252264
elif arg == 'service_principal_auth_info_secret':
253265
client_id = prompt('ServicePrincipal client-id (--service-principal client_id=): ')
254266
object_id = prompt('Enterprise Application object-id (--service-principal object-id=): ')
@@ -373,7 +385,7 @@ def intelligent_experience(cmd, namespace, missing_args):
373385
for arg in missing_args:
374386
if arg not in cmd_arg_values:
375387
hint = '{} ({})'.format(missing_args[arg].get('help'), '/'.join(missing_args[arg].get('options')))
376-
value, cmd_value = interactive_input(arg, hint)
388+
value, cmd_value = interactive_input(arg, hint, cmd)
377389
cmd_arg_values[arg] = value
378390

379391
# show applied params

src/azure-cli/azure/cli/command_modules/serviceconnector/action.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ def get_action(self, values, option_string, command_name): # pylint: disable=no
8888
else:
8989
raise ValidationError('Unsupported Key {} is provided for parameter secret_auth_info. All possible '
9090
'keys are: name, secret/secret-uri/secret-name'.format(k))
91+
if is_mongodb_atlas_target(command_name):
92+
d['name'] = 'NA'
9193
if len(d) != 2:
9294
raise ValidationError('Required keys missing for parameter --secret.'
9395
' All possible keys are: name, secret/secret-uri/secret-name')
@@ -120,6 +122,11 @@ def is_mysql_target(command_name):
120122
return target_name.lower() == "mysql-flexible"
121123

122124

125+
def is_mongodb_atlas_target(command_name):
126+
target_name = command_name.split(' ')[-1]
127+
return target_name.lower() == "mongodb-atlas"
128+
129+
123130
class AddUserAssignedIdentityAuthInfo(argparse.Action):
124131
def __call__(self, parser, namespace, values, option_string=None):
125132
action = self.get_action(values, option_string, namespace.command)

0 commit comments

Comments
 (0)