forked from Azure/azure-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_params.py
More file actions
422 lines (358 loc) · 19.7 KB
/
_params.py
File metadata and controls
422 lines (358 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from azure.cli.core.commands.parameters import (
get_enum_type,
get_location_type,
get_three_state_flag
)
from azure.cli.core.commands.validators import get_default_location_from_resource_group
from ._validators import (
validate_connstr_props,
validate_params,
validate_kafka_params,
validate_local_params,
get_default_object_id_of_current_user
)
from ._resource_config import (
AUTH_TYPE,
RESOURCE,
SOURCE_RESOURCES_PARAMS,
SOURCE_RESOURCES_CREATE_PARAMS,
TARGET_RESOURCES_PARAMS,
SOURCE_RESOURCES_OPTIONAL_PARAMS,
TARGET_RESOURCES_CONNECTION_STRING,
AUTH_TYPE_PARAMS,
SUPPORTED_AUTH_TYPE,
SUPPORTED_CLIENT_TYPE,
TARGET_SUPPORT_SERVICE_ENDPOINT,
TARGET_SUPPORT_PRIVATE_ENDPOINT,
LOCAL_CONNECTION_PARAMS,
OPT_OUT_OPTION
)
from ._addon_factory import AddonFactory
from knack.arguments import CLIArgumentType
from .action import AddAdditionalConnectionStringProperties, AddCustomizedKeys
def add_source_resource_block(context, source, enable_id=True, target=None):
source_args = SOURCE_RESOURCES_PARAMS.get(source)
for resource, args in SOURCE_RESOURCES_PARAMS.items():
if resource != source:
for arg in args:
if arg not in source_args:
context.ignore(arg)
required_args = []
for arg, content in SOURCE_RESOURCES_PARAMS.get(source).items():
id_arg = '\'--id\'' if enable_id else '\'--source-id\''
deprecate_info = content.get('deprecate_info')
context.argument(arg, configured_default=content.get('configured_default'),
options_list=content.get('options'), type=str,
deprecate_info=context.deprecate() if deprecate_info else None,
help='{}. Required if {} is not specified.{}'.format(
content.get('help'), id_arg, deprecate_info))
required_args.append(content.get('options')[0])
validator_kwargs = {
'validator': validate_params}
if not enable_id:
context.argument('source_id', options_list=['--source-id'], type=str,
help="The resource id of a {source}. Required if {required_args} "
"are not specified.".format(
source=source.value, required_args=str(required_args)),
**validator_kwargs)
else:
required_args.append('--connection')
context.argument('indentifier', options_list=['--id'], type=str,
help="The resource id of the connection. {required_args} are required "
"if '--id' is not specified.".format(required_args=str(required_args)))
context.ignore('source_id')
# scope parameter
if source == RESOURCE.KubernetesCluster:
context.argument('scope', options_list=['--kube-namespace'], type=str, default='default',
help="The kubernetes namespace where the connection information "
"will be saved into (as kubernetes secret).")
context.argument('enable_csi', options_list=['--enable-csi'], arg_type=get_three_state_flag(),
help="Use keyvault as a secrets store via a CSI volume. "
"If specified, AuthType Arguments are not needed.")
if target == RESOURCE.AppConfig:
context.argument('enable_appconfig_extension', options_list=['--use-appconfig-extension', '-e'],
arg_type=get_three_state_flag(),
help="Install Azure App Configuration extension in the Kubernetes cluster.")
else:
context.ignore('enable_appconfig_extension')
elif source == RESOURCE.ContainerApp:
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')
context.ignore('enable_appconfig_extension')
else:
context.ignore('scope')
context.ignore('enable_csi')
context.ignore('enable_appconfig_extension')
# slot parameter
for resource, args in SOURCE_RESOURCES_OPTIONAL_PARAMS.items():
for arg in args:
if resource == source:
deprecate_info = args.get(arg).get('deprecate_info')
context.argument(arg, options_list=args.get(arg).get(
'options'), type=str,
deprecate_info=context.deprecate() if deprecate_info else None,
help=args.get(arg).get('help'))
else:
context.ignore(arg)
def add_auth_block(context, source, target):
support_auth_types = SUPPORTED_AUTH_TYPE.get(
source, {}).get(target, [])
for auth_type in AUTH_TYPE_PARAMS:
if auth_type in support_auth_types:
validator = None
if auth_type == AUTH_TYPE.UserAccount:
validator = get_default_object_id_of_current_user
for arg, params in AUTH_TYPE_PARAMS.get(auth_type).items():
context.argument(arg, options_list=params.get('options'), action=params.get('action'), nargs='*',
help=params.get('help'), arg_group='AuthType', validator=validator)
else:
for arg in AUTH_TYPE_PARAMS.get(auth_type):
context.ignore(arg)
def add_local_connection_block(context, show_id=True):
context.argument('location', arg_type=CLIArgumentType(
arg_type=get_location_type(context),
required=False,
validator=get_default_location_from_resource_group))
if show_id:
context.argument('id', options_list=[
'--id'], type=str, help='The id of connection.', validator=validate_local_params)
params = LOCAL_CONNECTION_PARAMS.get('connection_name')
context.argument('connection_name', options_list=params.get('options'), type=params.get('type'),
help=params.get('help'), validator=validate_local_params)
def add_target_resource_block(context, target):
target_args = TARGET_RESOURCES_PARAMS.get(target)
for resource, args in TARGET_RESOURCES_PARAMS.items():
if resource != target:
for arg in args:
if arg not in target_args:
context.ignore(arg)
required_args = []
if target in TARGET_RESOURCES_PARAMS:
for arg, content in TARGET_RESOURCES_PARAMS.get(target).items():
context.argument(arg, options_list=content.get('options'), type=str,
help='{}. Required if \'--target-id\' is not specified.'.format(content.get('help')))
required_args.append(content.get('options')[0])
if target == RESOURCE.NeonPostgres or target == RESOURCE.MongoDbAtlas:
context.ignore('target_id')
else:
context.argument('target_id', type=str,
help='The resource id of target service. Required if {required_args} '
'are not specified.'.format(required_args=str(required_args)))
if target != RESOURCE.KeyVault:
context.ignore('enable_csi')
def add_connection_name_argument(context, source):
context.argument('connection_name', options_list=['--connection'], type=str,
help='Name of the {} connection.'.format(source.value), validator=validate_params)
def add_client_type_argument(context, source, target):
client_types = SUPPORTED_CLIENT_TYPE.get(source).get(target, [])
client_types = [item.value for item in client_types]
context.argument('client_type', options_list=['--client-type'], arg_type=get_enum_type(client_types),
help='The client type used on the {}'.format(source.value))
def add_customized_keys_argument(context):
context.argument('customized_keys', options_list=['--customized-keys'], action=AddCustomizedKeys, nargs='*',
help='The customized keys used to change default configuration names. Key is the original '
'name, value is the customized name.')
def add_connstr_props_argument(context):
# linter: length '--additional-connection-string-properties' longer than 22, so use abbreviation
context.argument('connstr_props', options_list=['--connstr-props'],
action=AddAdditionalConnectionStringProperties, nargs='*',
help='The additional connection string properties used to build connection string.',
validator=validate_connstr_props)
def add_target_type_argument(context, source):
TARGET_TYPES = [
elem.value for elem in SUPPORTED_AUTH_TYPE.get(source).keys()]
context.argument('target_resource_type', options_list=['--target-type', '-t'],
arg_type=get_enum_type(TARGET_TYPES), help='The target resource type')
def add_new_addon_argument(context, source, target):
if AddonFactory.get(target, None):
context.argument('new_addon', options_list=['--new'], arg_type=get_three_state_flag(), default=False,
help='Indicates whether to create a new {} when '
'creating the {} connection'.format(target.value, source.value))
else:
context.ignore('new_addon')
def add_secret_store_argument(context, source=""):
if source == RESOURCE.KubernetesCluster:
context.ignore('key_vault_id')
else:
context.argument('key_vault_id', options_list=[
'--vault-id'], help='The id of key vault to store secret value')
def add_configuration_store_argument(context):
context.argument('app_config_id', options_list=[
'--appconfig-id'], help='The app configuration id to store configuration')
def add_vnet_block(context, target):
if target not in TARGET_SUPPORT_SERVICE_ENDPOINT:
context.ignore('service_endpoint')
else:
context.argument('service_endpoint', options_list=['--service-endpoint'], arg_type=get_three_state_flag(),
default=None, arg_group='NetworkSolution',
help='Connect target service by service endpoint. Source resource must be in the VNet'
' and target SKU must support service endpoint feature.')
if target not in TARGET_SUPPORT_PRIVATE_ENDPOINT:
context.ignore('private_endpoint')
else:
context.argument('private_endpoint', options_list=['--private-endpoint'], arg_type=get_three_state_flag(),
default=None, arg_group='NetworkSolution',
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=['--config-connstr'],
arg_type=get_three_state_flag(), default=False, is_preview=True,
help='Store configuration into connection strings, '
'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)')
context.argument('kafka_secret', options_list=[
'--kafka-secret'], help='Kafka API-Key (secret)')
context.argument('schema_registry', options_list=[
'--schema-registry'], help='Schema registry url')
context.argument('schema_key', options_list=[
'--schema-key'], help='Schema registry API-Key (key)')
context.argument('schema_secret', options_list=[
'--schema-secret'], help='Schema registry API-Key (secret)')
context.argument('connection_name', options_list=['--connection'],
help='Name of the connection', validator=validate_kafka_params)
def add_opt_out_argument(context):
context.argument('opt_out_list', options_list=['--opt-out'],
default=None, nargs='+',
arg_type=get_enum_type(OPT_OUT_OPTION),
help='Whether to disable some configuration steps. '
'Use configinfo to disbale configuration information changes on source. '
'Use publicnetwork to disable public network access configuration.'
'Use auth to skip auth configuration such as enabling managed identity and granting RBAC roles.'
)
def load_arguments(self, _): # pylint: disable=too-many-statements
for source in SOURCE_RESOURCES_PARAMS:
with self.argument_context('{} connection list'.format(source.value)) as c:
add_source_resource_block(
c, source, enable_id=False)
with self.argument_context('{} connection show'.format(source.value)) as c:
add_source_resource_block(c, source)
add_connection_name_argument(c, source)
with self.argument_context('{} connection delete'.format(source.value)) as c:
add_connection_name_argument(c, source)
add_source_resource_block(c, source)
with self.argument_context('{} connection list-configuration'.format(source.value)) as c:
add_connection_name_argument(c, source)
add_source_resource_block(c, source)
with self.argument_context('{} connection validate'.format(source.value)) as c:
add_connection_name_argument(c, source)
add_source_resource_block(c, source)
with self.argument_context('{} connection list-support-types'.format(source.value)) as c:
add_target_type_argument(c, source)
with self.argument_context('{} connection wait'.format(source.value)) as c:
add_connection_name_argument(c, source)
add_source_resource_block(c, source)
for target in TARGET_RESOURCES_PARAMS:
with self.argument_context('{} connection create {}'.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, enable_id=False, target=target)
add_target_resource_block(c, target)
add_auth_block(c, source, target)
add_new_addon_argument(c, source, target)
add_configuration_store_argument(c)
add_secret_store_argument(c, source)
add_vnet_block(c, target)
add_connection_string_argument(c, source, target)
add_customized_keys_argument(c)
add_opt_out_argument(c)
add_connstr_props_argument(c)
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, enable_id=True, target=target)
add_auth_block(c, source, target)
add_configuration_store_argument(c)
add_secret_store_argument(c, source)
add_vnet_block(c, target)
add_connection_string_argument(c, source, target)
add_customized_keys_argument(c)
add_opt_out_argument(c)
add_connstr_props_argument(c)
# special target resource: independent implementation
target = RESOURCE.ConfluentKafka
with self.argument_context('{} connection create {}'.format(source.value, target.value)) as c:
add_client_type_argument(c, source, target)
add_source_resource_block(c, source, enable_id=False)
add_confluent_kafka_argument(c)
add_configuration_store_argument(c)
add_secret_store_argument(c, source)
add_customized_keys_argument(c)
add_opt_out_argument(c)
with self.argument_context('{} connection update {}'.format(source.value, target.value)) as c:
add_client_type_argument(c, source, target)
add_source_resource_block(c, source, enable_id=False)
add_confluent_kafka_argument(c)
add_configuration_store_argument(c)
add_secret_store_argument(c, source)
add_customized_keys_argument(c)
add_opt_out_argument(c)
# local connection
with self.argument_context('connection list') as c:
add_local_connection_block(c, show_id=False)
with self.argument_context('connection show') as c:
add_local_connection_block(c)
with self.argument_context('connection delete') as c:
add_local_connection_block(c)
with self.argument_context('connection generate-configuration') as c:
add_local_connection_block(c)
with self.argument_context('connection validate') as c:
add_local_connection_block(c)
with self.argument_context('connection list-support-types') as c:
add_target_type_argument(c, source)
with self.argument_context('connection wait') as c:
add_local_connection_block(c)
source = RESOURCE.Local
for target in TARGET_RESOURCES_PARAMS:
with self.argument_context('connection preview-configuration {}'.format(target.value)) as c:
add_auth_block(c, source, target)
add_client_type_argument(c, source, target)
with self.argument_context('connection create {}'.format(target.value)) as c:
add_client_type_argument(c, source, target)
add_target_resource_block(c, target)
add_auth_block(c, source, target)
add_new_addon_argument(c, source, target)
add_configuration_store_argument(c)
add_secret_store_argument(c, source)
add_vnet_block(c, target)
add_local_connection_block(c)
add_customized_keys_argument(c)
with self.argument_context('connection update {}'.format(target.value)) as c:
add_client_type_argument(c, source, target)
add_auth_block(c, source, target)
add_configuration_store_argument(c)
add_secret_store_argument(c, source)
add_vnet_block(c, target)
add_local_connection_block(c)
add_customized_keys_argument(c)
# special target resource: independent implementation
target = RESOURCE.ConfluentKafka
with self.argument_context('connection create {}'.format(target.value)) as c:
add_client_type_argument(c, source, target)
add_confluent_kafka_argument(c)
add_configuration_store_argument(c)
add_secret_store_argument(c, source)
add_local_connection_block(c, show_id=False)
add_customized_keys_argument(c)
with self.argument_context('connection update {}'.format(target.value)) as c:
add_client_type_argument(c, source, target)
add_confluent_kafka_argument(c)
add_configuration_store_argument(c)
add_secret_store_argument(c, source)
add_local_connection_block(c, show_id=False)
add_customized_keys_argument(c)
with self.argument_context('connection preview-configuration {}'.format(target.value)) as c:
add_auth_block(c, source, target)
add_client_type_argument(c, source, target)