-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
custom.py
1135 lines (944 loc) · 49.1 KB
/
custom.py
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
# pylint: disable=too-many-lines
from __future__ import print_function
import json
import os
import re
import ssl
import sys
import uuid
from six.moves.urllib.request import urlopen # pylint: disable=import-error
from azure.mgmt.resource.resources.models import GenericResource
from azure.mgmt.resource.locks.models import ManagementLockObject
from azure.mgmt.resource.links.models import ResourceLinkProperties
from azure.mgmt.resource.managedapplications.models import Appliance
from azure.mgmt.resource.managedapplications.models import Plan
from azure.mgmt.resource.managedapplications.models import ApplianceDefinition
from azure.mgmt.resource.managedapplications.models import ApplianceProviderAuthorization
from azure.cli.core.parser import IncorrectUsageError
from azure.cli.core.prompting import prompt, prompt_pass, prompt_t_f, prompt_choice_list, prompt_int
from azure.cli.core.util import CLIError, get_file_json, shell_safe_json_parse
import azure.cli.core.azlogging as azlogging
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.cli.core.commands.arm import is_valid_resource_id, parse_resource_id
from azure.cli.core.profiles import get_sdk, ResourceType
from ._client_factory import (_resource_client_factory,
_resource_policy_client_factory,
_resource_lock_client_factory,
_resource_links_client_factory,
_authorization_management_client,
_resource_managedapps_client_factory)
logger = azlogging.get_az_logger(__name__)
def list_resource_groups(tag=None): # pylint: disable=no-self-use
""" List resource groups, optionally filtered by a tag.
:param str tag:tag to filter by in 'key[=value]' format
"""
rcf = _resource_client_factory()
filters = []
if tag:
key = list(tag.keys())[0]
filters.append("tagname eq '{}'".format(key))
filters.append("tagvalue eq '{}'".format(tag[key]))
filter_text = ' and '.join(filters) if filters else None
groups = rcf.resource_groups.list(filter=filter_text)
return list(groups)
def create_resource_group(rg_name, location, tags=None):
""" Create a new resource group.
:param str resource_group_name:the desired resource group name
:param str location:the resource group location
:param str tags:tags in 'a=b c' format
"""
rcf = _resource_client_factory()
ResourceGroup = get_sdk(ResourceType.MGMT_RESOURCE_RESOURCES, 'ResourceGroup', mod='models')
parameters = ResourceGroup(
location=location,
tags=tags
)
return rcf.resource_groups.create_or_update(rg_name, parameters)
def create_appliance(resource_group_name,
appliance_name, managedby_resource_group_id,
location, kind, managedapp_definition_id=None,
plan_name=None, plan_publisher=None, plan_product=None,
plan_version=None, tags=None, parameters=None):
""" Create a new managed application.
:param str resource_group_name:the desired resource group name
:param str appliance_name:the managed application name
:param str kind:the managed application kind. can be marketplace or servicecatalog
:param str plan_name:the managed application package plan name
:param str plan_publisher:the managed application package plan publisher
:param str plan_product:the managed application package plan product
:param str plan_version:the managed application package plan version
:param str tags:tags in 'a=b c' format
"""
racf = _resource_managedapps_client_factory()
appliance = Appliance(
location=location,
managed_resource_group_id=managedby_resource_group_id,
kind=kind,
tags=tags
)
if kind.lower() == 'servicecatalog':
if managedapp_definition_id:
appliance.appliance_definition_id = managedapp_definition_id
else:
raise CLIError('--managedapp-definition-id is required if kind is ServiceCatalog')
elif kind.lower() == 'marketplace':
if (plan_name is None and plan_product is None and
plan_publisher is None and plan_version is None):
raise CLIError('--plan-name, --plan-product, --plan-publisher and \
--plan-version are all required if kind is MarketPlace')
else:
appliance.plan = Plan(plan_name, plan_publisher, plan_product, plan_version)
applianceParameters = None
if parameters:
if os.path.exists(parameters):
applianceParameters = get_file_json(parameters)
else:
applianceParameters = shell_safe_json_parse(parameters)
appliance.parameters = applianceParameters
return racf.appliances.create_or_update(resource_group_name, appliance_name, appliance)
def show_appliance(resource_group_name=None, appliance_name=None, managedapp_id=None):
""" Gets a managed application.
:param str resource_group_name:the resource group name
:param str appliance_name:the managed application name
"""
racf = _resource_managedapps_client_factory()
if managedapp_id:
appliance = racf.appliances.get_by_id(managedapp_id)
else:
appliance = racf.appliances.get(resource_group_name, appliance_name)
return appliance
def show_appliancedefinition(resource_group_name=None, appliance_definition_name=None,
managedapp_definition_id=None):
""" Gets a managed application definition.
:param str resource_group_name:the resource group name
:param str appliance_definition_name:the managed application definition name
"""
racf = _resource_managedapps_client_factory()
if managedapp_definition_id:
appliancedef = racf.appliance_definitions.get_by_id(managedapp_definition_id)
else:
appliancedef = racf.appliance_definitions.get(resource_group_name,
appliance_definition_name)
return appliancedef
def create_appliancedefinition(resource_group_name,
appliance_definition_name, location,
lock_level, package_file_uri, authorizations,
description, display_name, tags=None):
""" Create a new managed application definition.
:param str resource_group_name:the desired resource group name
:param str appliance_definition_name:the managed application definition name
:param str description:the managed application definition description
:param str display_name:the managed application definition display name
:param str package_file_uri:the managed application definition package file uri
:param str tags:tags in 'a=b c' format
"""
racf = _resource_managedapps_client_factory()
authorizations = authorizations or []
applianceAuthList = []
for name_value in authorizations:
# split at the first ':', neither principalId nor roldeDefinitionId should have a ':'
principalId, roleDefinitionId = name_value.split(':', 1)
applianceAuth = ApplianceProviderAuthorization(principalId, roleDefinitionId)
applianceAuthList.append(applianceAuth)
applianceDef = ApplianceDefinition(lock_level, applianceAuthList, package_file_uri)
applianceDef.display_name = display_name
applianceDef.description = description
applianceDef.location = location
applianceDef.tags = tags
return racf.appliance_definitions.create_or_update(resource_group_name,
appliance_definition_name, applianceDef)
def list_appliances(resource_group_name=None):
racf = _resource_managedapps_client_factory()
if resource_group_name:
appliances = racf.appliances.list_by_resource_group(resource_group_name)
else:
appliances = racf.appliances.list_by_subscription()
return list(appliances)
def export_group_as_template(
resource_group_name, include_comments=False, include_parameter_default_value=False):
"""Captures a resource group as a template.
:param str resource_group_name:the name of the resoruce group.
:param bool include_comments:export template with comments.
:param bool include_parameter_default_value: export template parameter with default value.
"""
rcf = _resource_client_factory()
export_options = []
if include_comments:
export_options.append('IncludeComments')
if include_parameter_default_value:
export_options.append('IncludeParameterDefaultValue')
options = ','.join(export_options) if export_options else None
result = rcf.resource_groups.export_template(resource_group_name, '*', options=options)
# pylint: disable=no-member
# On error, server still returns 200, with details in the error attribute
if result.error:
error = result.error
if (hasattr(error, 'details') and error.details and
hasattr(error.details[0], 'message')):
error = error.details[0].message
raise CLIError(error)
print(json.dumps(result.template, indent=2))
def deploy_arm_template(resource_group_name,
template_file=None, template_uri=None, deployment_name=None,
parameters=None, mode='incremental', no_wait=False):
return _deploy_arm_template_core(resource_group_name, template_file, template_uri,
deployment_name, parameters, mode, no_wait=no_wait)
def validate_arm_template(resource_group_name, template_file=None, template_uri=None,
parameters=None, mode='incremental'):
return _deploy_arm_template_core(resource_group_name, template_file, template_uri,
'deployment_dry_run', parameters, mode, validate_only=True)
def _find_missing_parameters(parameters, template):
if template is None:
return {}
template_parameters = template.get('parameters', None)
if template_parameters is None:
return {}
missing = {}
for parameter_name in template_parameters:
parameter = template_parameters[parameter_name]
if parameter.get('defaultValue', None) is not None:
continue
if parameters is not None and parameters.get(parameter_name, None) is not None:
continue
missing[parameter_name] = parameter
return missing
def _prompt_for_parameters(missing_parameters):
result = {}
for param_name in missing_parameters:
prompt_str = 'Please provide a value for \'{}\' (? for help): '.format(param_name)
param = missing_parameters[param_name]
param_type = param.get('type', 'string')
description = 'Missing description'
metadata = param.get('metadata', None)
if metadata is not None:
description = metadata.get('description', description)
allowed_values = param.get('allowedValues', None)
while True:
if allowed_values is not None:
ix = prompt_choice_list(prompt_str, allowed_values, help_string=description)
result[param_name] = allowed_values[ix]
break
elif param_type == 'securestring':
value = prompt_pass(prompt_str, help_string=description)
result[param_name] = value
elif param_type == 'int':
int_value = prompt_int(prompt_str, help_string=description)
result[param_name] = int_value
break
elif param_type == 'bool':
value = prompt_t_f(prompt_str, help_string=description)
result[param_name] = value
break
else:
value = prompt(prompt_str, help_string=description)
result[param_name] = value
if value:
break
return result
def _get_missing_parameters(parameters, template, prompt_fn):
missing = _find_missing_parameters(parameters, template)
if missing:
prompt_parameters = prompt_fn(missing)
for param_name in prompt_parameters:
parameters[param_name] = {
"value": prompt_parameters[param_name]
}
return parameters
def _ssl_context():
if sys.version_info < (3, 4):
return ssl.SSLContext(ssl.PROTOCOL_TLSv1)
return ssl.create_default_context()
def _urlretrieve(url):
req = urlopen(url, context=_ssl_context())
return req.read()
def _deploy_arm_template_core(resource_group_name, # pylint: disable=too-many-arguments
template_file=None, template_uri=None, deployment_name=None,
parameters=None, mode='incremental', validate_only=False,
no_wait=False):
DeploymentProperties, TemplateLink = get_sdk(ResourceType.MGMT_RESOURCE_RESOURCES,
'DeploymentProperties',
'TemplateLink',
mod='models')
parameters = parameters or {}
template = None
template_link = None
template_obj = None
if template_uri:
template_link = TemplateLink(uri=template_uri)
template_obj = shell_safe_json_parse(_urlretrieve(template_uri).decode('utf-8'))
else:
template = get_file_json(template_file)
template_obj = template
parameters = _get_missing_parameters(parameters, template_obj, _prompt_for_parameters)
properties = DeploymentProperties(template=template, template_link=template_link,
parameters=parameters, mode=mode)
smc = get_mgmt_service_client(ResourceType.MGMT_RESOURCE_RESOURCES)
if validate_only:
return smc.deployments.validate(resource_group_name, deployment_name, properties, raw=no_wait)
return smc.deployments.create_or_update(resource_group_name, deployment_name, properties, raw=no_wait)
def export_deployment_as_template(resource_group_name, deployment_name):
smc = get_mgmt_service_client(ResourceType.MGMT_RESOURCE_RESOURCES)
result = smc.deployments.export_template(resource_group_name, deployment_name)
print(json.dumps(result.template, indent=2)) # pylint: disable=no-member
def create_resource(properties,
resource_group_name=None, resource_provider_namespace=None,
parent_resource_path=None, resource_type=None, resource_name=None,
resource_id=None, api_version=None, location=None, is_full_object=False):
res = _ResourceUtils(resource_group_name, resource_provider_namespace,
parent_resource_path, resource_type, resource_name,
resource_id, api_version)
return res.create_resource(properties, location, is_full_object)
def show_resource(resource_group_name=None,
resource_provider_namespace=None, parent_resource_path=None, resource_type=None,
resource_name=None, resource_id=None, api_version=None):
res = _ResourceUtils(resource_group_name, resource_provider_namespace,
parent_resource_path, resource_type, resource_name,
resource_id, api_version)
return res.get_resource()
def delete_resource(resource_group_name=None,
resource_provider_namespace=None, parent_resource_path=None, resource_type=None,
resource_name=None, resource_id=None, api_version=None):
res = _ResourceUtils(resource_group_name, resource_provider_namespace,
parent_resource_path, resource_type, resource_name,
resource_id, api_version)
return res.delete()
def update_resource(parameters,
resource_group_name=None, resource_provider_namespace=None,
parent_resource_path=None, resource_type=None, resource_name=None,
resource_id=None, api_version=None):
res = _ResourceUtils(resource_group_name, resource_provider_namespace,
parent_resource_path, resource_type, resource_name,
resource_id, api_version)
return res.update(parameters)
def tag_resource(tags,
resource_group_name=None, resource_provider_namespace=None,
parent_resource_path=None, resource_type=None, resource_name=None,
resource_id=None, api_version=None):
""" Updates the tags on an existing resource. To clear tags, specify the --tag option
without anything else. """
res = _ResourceUtils(resource_group_name, resource_provider_namespace,
parent_resource_path, resource_type, resource_name,
resource_id, api_version)
return res.tag(tags)
def get_deployment_operations(client, resource_group_name, deployment_name, operation_ids):
"""get a deployment's operation.
"""
result = []
for op_id in operation_ids:
dep = client.get(resource_group_name, deployment_name, op_id)
result.append(dep)
return result
def list_resources(resource_group_name=None,
resource_provider_namespace=None, resource_type=None, name=None, tag=None,
location=None):
rcf = _resource_client_factory()
if resource_group_name is not None:
rcf.resource_groups.get(resource_group_name)
odata_filter = _list_resources_odata_filter_builder(resource_group_name,
resource_provider_namespace,
resource_type, name, tag, location)
resources = rcf.resources.list(filter=odata_filter)
return list(resources)
def _list_resources_odata_filter_builder(resource_group_name=None,
resource_provider_namespace=None,
resource_type=None, name=None, tag=None, location=None):
"""Build up OData filter string from parameters
"""
filters = []
if resource_group_name:
filters.append("resourceGroup eq '{}'".format(resource_group_name))
if name:
filters.append("name eq '{}'".format(name))
if location:
filters.append("location eq '{}'".format(location))
if resource_type:
if resource_provider_namespace:
f = "'{}/{}'".format(resource_provider_namespace, resource_type)
else:
if not re.match('[^/]+/[^/]+', resource_type):
raise CLIError(
'Malformed resource-type: '
'--resource-type=<namespace>/<resource-type> expected.')
# assume resource_type is <namespace>/<type>. The worst is to get a server error
f = "'{}'".format(resource_type)
filters.append("resourceType eq " + f)
else:
if resource_provider_namespace:
raise CLIError('--namespace also requires --resource-type')
if tag:
if name or location:
raise IncorrectUsageError('you cannot use the tag filter with other filters')
tag_name = list(tag.keys())[0] if isinstance(tag, dict) else tag
tag_value = tag[tag_name] if isinstance(tag, dict) else ''
if tag_name:
if tag_name[-1] == '*':
filters.append("startswith(tagname, '%s')" % tag_name[0:-1])
else:
filters.append("tagname eq '%s'" % tag_name)
if tag_value != '':
filters.append("tagvalue eq '%s'" % tag_value)
return ' and '.join(filters)
def get_providers_completion_list(prefix, **kwargs): # pylint: disable=unused-argument
rcf = _resource_client_factory()
result = rcf.providers.list()
return [r.namespace for r in result]
def get_resource_types_completion_list(prefix, **kwargs): # pylint: disable=unused-argument
rcf = _resource_client_factory()
result = rcf.providers.list()
types = []
for p in list(result):
for r in p.resource_types:
types.append(p.namespace + '/' + r.resource_type)
return types
def register_provider(resource_provider_namespace, wait=False):
_update_provider(resource_provider_namespace, registering=True, wait=wait)
def unregister_provider(resource_provider_namespace, wait=False):
_update_provider(resource_provider_namespace, registering=False, wait=wait)
def _update_provider(namespace, registering, wait):
import time
rcf = _resource_client_factory()
if registering:
rcf.providers.register(namespace)
else:
rcf.providers.unregister(namespace)
if wait:
while True:
time.sleep(10)
rp_info = rcf.providers.get(namespace)
if rp_info.registration_state == ('Registered' if registering else 'Unregistered'):
break
else:
action = 'Registering' if registering else 'Unregistering'
msg_template = '%s is still on-going. You can monitor using \'az provider show -n %s\''
logger.warning(msg_template, action, namespace)
def list_provider_operations(api_version=None):
api_version = api_version or _get_auth_provider_latest_api_version()
auth_client = _authorization_management_client()
return auth_client.provider_operations_metadata.list(api_version)
def show_provider_operations(resource_provider_namespace, api_version=None):
api_version = api_version or _get_auth_provider_latest_api_version()
auth_client = _authorization_management_client()
return auth_client.provider_operations_metadata.get(resource_provider_namespace, api_version)
def _get_auth_provider_latest_api_version():
rcf = _resource_client_factory()
api_version = _ResourceUtils.resolve_api_version(rcf, 'Microsoft.Authorization',
None, 'providerOperations')
return api_version
def move_resource(ids, destination_group, destination_subscription_id=None):
"""Moves resources from one resource group to another(can be under different subscription)
:param ids: the space separated resource ids to be moved
:param destination_group: the destination resource group name
:param destination_subscription_id: the destination subscription identifier
"""
from azure.cli.core.commands.arm import resource_id
# verify all resource ids are valid and under the same group
resources = []
for i in ids:
if is_valid_resource_id(i):
resources.append(parse_resource_id(i))
else:
raise CLIError('Invalid id "{}", as it has no group or subscription field'.format(i))
if len(set([r['subscription'] for r in resources])) > 1:
raise CLIError('All resources should be under the same subscription')
if len(set([r['resource_group'] for r in resources])) > 1:
raise CLIError('All resources should be under the same group')
rcf = _resource_client_factory()
target = resource_id(subscription=(destination_subscription_id or rcf.config.subscription_id),
resource_group=destination_group)
return rcf.resources.move_resources(resources[0]['resource_group'], ids, target)
def list_features(client, resource_provider_namespace=None):
if resource_provider_namespace:
return client.list(resource_provider_namespace=resource_provider_namespace)
return client.list_all()
def create_policy_assignment(policy, name=None, display_name=None,
resource_group_name=None, scope=None):
policy_client = _resource_policy_client_factory()
scope = _build_policy_scope(policy_client.config.subscription_id,
resource_group_name, scope)
policy_id = _resolve_policy_id(policy, policy_client)
PolicyAssignment = get_sdk(ResourceType.MGMT_RESOURCE_POLICY, 'PolicyAssignment', mod='models')
assignment = PolicyAssignment(display_name, policy_id, scope)
return policy_client.policy_assignments.create(scope,
name or uuid.uuid4(),
assignment)
def delete_policy_assignment(name, resource_group_name=None, scope=None):
policy_client = _resource_policy_client_factory()
scope = _build_policy_scope(policy_client.config.subscription_id,
resource_group_name, scope)
policy_client.policy_assignments.delete(scope, name)
def show_policy_assignment(name, resource_group_name=None, scope=None):
policy_client = _resource_policy_client_factory()
scope = _build_policy_scope(policy_client.config.subscription_id,
resource_group_name, scope)
return policy_client.policy_assignments.get(scope, name)
def list_policy_assignment(disable_scope_strict_match=None, resource_group_name=None, scope=None):
policy_client = _resource_policy_client_factory()
if scope and not is_valid_resource_id(scope):
parts = scope.strip('/').split('/')
if len(parts) == 4:
resource_group_name = parts[3]
elif len(parts) == 2:
# rarely used, but still verify
if parts[1].lower() != policy_client.config.subscription_id.lower():
raise CLIError("Please use current active subscription's id")
else:
err = "Invalid scope '{}', it should point to a resource group or a resource"
raise CLIError(err.format(scope))
scope = None
_scope = _build_policy_scope(policy_client.config.subscription_id,
resource_group_name, scope)
if resource_group_name:
result = policy_client.policy_assignments.list_for_resource_group(resource_group_name)
elif scope:
# pylint: disable=redefined-builtin
id = parse_resource_id(scope)
parent_resource_path = '' if not id.get('child_name') else (id['type'] + '/' + id['name'])
resource_type = id.get('child_type') or id['type']
resource_name = id.get('child_name') or id['name']
result = policy_client.policy_assignments.list_for_resource(
id['resource_group'], id['namespace'],
parent_resource_path, resource_type, resource_name)
else:
result = policy_client.policy_assignments.list()
if not disable_scope_strict_match:
result = [i for i in result if _scope.lower() == i.scope.lower()]
return result
def _build_policy_scope(subscription_id, resource_group_name, scope):
subscription_scope = '/subscriptions/' + subscription_id
if scope:
if resource_group_name:
err = "Resource group '{}' is redundant because 'scope' is supplied"
raise CLIError(err.format(resource_group_name))
elif resource_group_name:
scope = subscription_scope + '/resourceGroups/' + resource_group_name
else:
scope = subscription_scope
return scope
def _resolve_policy_id(policy, client):
policy_id = policy
if not is_valid_resource_id(policy):
policy_def = client.policy_definitions.get(policy)
policy_id = policy_def.id
return policy_id
def create_policy_definition(name, rules, display_name=None, description=None):
if os.path.exists(rules):
rules = get_file_json(rules)
else:
rules = shell_safe_json_parse(rules)
policy_client = _resource_policy_client_factory()
PolicyDefinition = get_sdk(ResourceType.MGMT_RESOURCE_POLICY, 'PolicyDefinition', mod='models')
parameters = PolicyDefinition(policy_rule=rules, description=description,
display_name=display_name)
return policy_client.policy_definitions.create_or_update(name, parameters)
def update_policy_definition(policy_definition_name, rules=None,
display_name=None, description=None):
if rules is not None:
if os.path.exists(rules):
rules = get_file_json(rules)
else:
rules = shell_safe_json_parse(rules)
policy_client = _resource_policy_client_factory()
definition = policy_client.policy_definitions.get(policy_definition_name)
# pylint: disable=line-too-long,no-member
PolicyDefinition = get_sdk(ResourceType.MGMT_RESOURCE_POLICY, 'PolicyDefinition', mod='models')
parameters = PolicyDefinition(
policy_rule=rules if rules is not None else definition.policy_rule,
description=description if description is not None else definition.description,
display_name=display_name if display_name is not None else definition.display_name)
return policy_client.policy_definitions.create_or_update(policy_definition_name, parameters)
def get_policy_completion_list(prefix, **kwargs): # pylint: disable=unused-argument
policy_client = _resource_policy_client_factory()
result = policy_client.policy_definitions.list()
return [i.name for i in result]
def get_policy_assignment_completion_list(prefix, **kwargs): # pylint: disable=unused-argument
policy_client = _resource_policy_client_factory()
result = policy_client.policy_assignments.list()
return [i.name for i in result]
def list_locks(resource_group_name=None,
resource_provider_namespace=None, parent_resource_path=None, resource_type=None,
resource_name=None, filter_string=None):
"""
:param resource_provider_namespace: Name of a resource provider.
:type resource_provider_namespace: str
:param parent_resource_path: Path to a parent resource
:type parent_resource_path: str
:param resource_type: The type for the resource with the lock.
:type resource_type: str
:param resource_name: Name of a resource that has a lock.
:type resource_name: str
:param filter_string: A query filter to use to restrict the results.
:type filter_string: str
"""
lock_client = _resource_lock_client_factory()
lock_resource = _extract_lock_params(resource_group_name, resource_provider_namespace,
resource_type, resource_name)
resource_group_name = lock_resource[0]
resource_name = lock_resource[1]
resource_provider_namespace = lock_resource[2]
resource_type = lock_resource[3]
if resource_group_name is None:
return lock_client.management_locks.list_at_subscription_level(filter=filter_string)
if resource_name is None:
return lock_client.management_locks.list_at_resource_group_level(
resource_group_name, filter=filter_string)
return lock_client.management_locks.list_at_resource_level(
resource_group_name, resource_provider_namespace, parent_resource_path, resource_type,
resource_name, filter=filter_string)
def _validate_lock_params_match_lock(
lock_client, name, resource_group_name, resource_provider_namespace, parent_resource_path,
resource_type, resource_name):
"""
Locks are scoped to subscription, resource group or resource.
However, the az list command returns all locks for the current scopes
and all lower scopes (e.g. resource group level also includes resource locks).
This can lead to a confusing user experience where the user specifies a lock
name and assumes that it will work, even if they haven't given the right
scope. This function attempts to validate the parameters and help the
user find the right scope, by first finding the lock, and then infering
what it's parameters should be.
"""
locks = lock_client.management_locks.list_at_subscription_level()
found_count = 0 # locks at different levels can have the same name
lock_resource_id = None
for lock in locks:
if lock.name == name:
found_count = found_count + 1
lock_resource_id = lock.id
if found_count == 1:
# If we only found one lock, let's validate that the parameters are correct,
# if we found more than one, we'll assume the user knows what they're doing
# TODO: Add validation for that case too?
resource = parse_resource_id(lock_resource_id)
_resource_group = resource.get('resource_group', None)
_resource_namespace = resource.get('namespace', None)
if _resource_group is None:
return
if resource_group_name != _resource_group:
raise CLIError(
'Unexpected --resource-group for lock {}, expected {}'.format(
name, _resource_group))
if _resource_namespace is None:
return
if resource_provider_namespace != _resource_namespace:
raise CLIError(
'Unexpected --namespace for lock {}, expected {}'.format(name, _resource_namespace))
if resource.get('grandchild_type', None) is None:
_resource_type = resource.get('type', None)
_resource_name = resource.get('name', None)
else:
_resource_type = resource.get('child_type', None)
_resource_name = resource.get('child_name', None)
parent = (resource['type'] + '/' + resource['name'])
if parent != parent_resource_path:
raise CLIError(
'Unexpected --parent for lock {}, expected {}'.format(
name, parent))
if resource_type != _resource_type:
raise CLIError('Unexpected --resource-type for lock {}, expected {}'.format(
name, _resource_type))
if resource_name != _resource_name:
raise CLIError('Unexpected --resource-name for lock {}, expected {}'.format(
name, _resource_name))
def get_lock(name, resource_group_name=None):
"""
:param name: Name of the lock.
:type name: str
"""
lock_client = _resource_lock_client_factory()
if resource_group_name is None:
return lock_client.management_locks.get_at_subscription_level(name)
return lock_client.management_locks.get_at_resource_group_level(resource_group_name, name)
def delete_lock(name,
resource_group_name=None, resource_provider_namespace=None,
parent_resource_path=None, resource_type=None, resource_name=None):
"""
:param name: The name of the lock.
:type name: str
:param resource_provider_namespace: Name of a resource provider.
:type resource_provider_namespace: str
:param parent_resource_path: Path to a parent resource
:type parent_resource_path: str
:param resource_type: The type for the resource with the lock.
:type resource_type: str
:param resource_name: Name of a resource that has a lock.
:type resource_name: str
"""
lock_client = _resource_lock_client_factory()
lock_resource = _extract_lock_params(resource_group_name, resource_provider_namespace,
resource_type, resource_name)
resource_group_name = lock_resource[0]
resource_name = lock_resource[1]
resource_provider_namespace = lock_resource[2]
resource_type = lock_resource[3]
_validate_lock_params_match_lock(lock_client, name, resource_group_name,
resource_provider_namespace, parent_resource_path,
resource_type, resource_name)
if resource_group_name is None:
return lock_client.management_locks.delete_at_subscription_level(name)
if resource_name is None:
return lock_client.management_locks.delete_at_resource_group_level(
resource_group_name, name)
return lock_client.management_locks.delete_at_resource_level(
resource_group_name, resource_provider_namespace, parent_resource_path or '', resource_type,
resource_name, name)
def _extract_lock_params(resource_group_name, resource_provider_namespace,
resource_type, resource_name):
if resource_group_name is None:
return (None, None, None, None)
if resource_name is None:
return (resource_group_name, None, None, None)
parts = resource_type.split('/', 2)
if resource_provider_namespace is None and len(parts) == 2:
resource_provider_namespace = parts[0]
resource_type = parts[1]
return (resource_group_name, resource_name, resource_provider_namespace, resource_type)
def create_lock(name,
resource_group_name=None, resource_provider_namespace=None, notes=None,
parent_resource_path=None, resource_type=None, resource_name=None, level=None):
"""
:param name: The name of the lock.
:type name: str
:param resource_provider_namespace: Name of a resource provider.
:type resource_provider_namespace: str
:param parent_resource_path: Path to a parent resource
:type parent_resource_path: str
:param resource_type: The type for the resource with the lock.
:type resource_type: str
:param resource_name: Name of a resource that has a lock.
:type resource_name: str
:param notes: Notes about this lock.
:type notes: str
"""
if level != 'ReadOnly' and level != 'CanNotDelete':
raise CLIError('--lock-type must be one of "ReadOnly" or "CanNotDelete"')
parameters = ManagementLockObject(level=level, notes=notes, name=name)
lock_client = _resource_lock_client_factory()
lock_resource = _extract_lock_params(resource_group_name, resource_provider_namespace,
resource_type, resource_name)
resource_group_name = lock_resource[0]
resource_name = lock_resource[1]
resource_provider_namespace = lock_resource[2]
resource_type = lock_resource[3]
if resource_group_name is None:
return lock_client.management_locks.create_or_update_at_subscription_level(name, parameters)
if resource_name is None:
return lock_client.management_locks.create_or_update_at_resource_group_level(
resource_group_name, name, parameters)
return lock_client.management_locks.create_or_update_at_resource_level(
resource_group_name, resource_provider_namespace, parent_resource_path or '', resource_type,
resource_name, name, parameters)
def _update_lock_parameters(parameters, level, notes, lock_id, lock_type):
if level is not None:
parameters.level = level
if notes is not None:
parameters.nodes = notes
if lock_id is not None:
parameters.id = lock_id
if lock_type is not None:
parameters.type = lock_type
def update_lock(name, resource_group_name=None, level=None, notes=None):
lock_client = _resource_lock_client_factory()
if resource_group_name is None:
params = lock_client.management_locks.get(name)
_update_lock_parameters(params, level, notes, None, None)
return lock_client.management_locks.create_or_update_at_subscription_level(name, params)
params = lock_client.management_locks.get_at_resource_group_level(resource_group_name, name)
_update_lock_parameters(params, level, notes, None, None)
return lock_client.management_locks.create_or_update_at_resource_group_level(
resource_group_name, name, params)
def create_resource_link(link_id, target_id, notes=None):
"""
:param target_id: The id of the resource link target.
:type target_id: str
:param notes: Notes for this link.
:type notes: str
"""
links_client = _resource_links_client_factory().resource_links
properties = ResourceLinkProperties(target_id, notes)
links_client.create_or_update(link_id, properties)
def update_resource_link(link_id, target_id=None, notes=None):
"""
:param target_id: The id of the resource link target.
:type target_id: str
:param notes: Notes for this link.
:type notes: str
"""
links_client = _resource_links_client_factory().resource_links
params = links_client.get(link_id)
properties = ResourceLinkProperties(
target_id if target_id is not None else params.properties.target_id,
# pylint: disable=no-member
notes=notes if notes is not None else params.properties.notes) # pylint: disable=no-member
links_client.create_or_update(link_id, properties)
def list_resource_links(scope=None, filter_string=None):
"""
:param scope: The scope for the links
:type scope: str
:param filter_string: A filter for restricting the results
:type filter_string: str
"""
links_client = _resource_links_client_factory().resource_links
if scope is not None:
return links_client.list_at_source_scope(scope, filter=filter_string)
return links_client.list_at_subscription(filter=filter_string)
def _validate_resource_inputs(resource_group_name, resource_provider_namespace,
resource_type, resource_name):
if resource_group_name is None:
raise CLIError('--resource-group/-g is required.')
if resource_type is None:
raise CLIError('--resource-type is required')
if resource_name is None:
raise CLIError('--name/-n is required')
if resource_provider_namespace is None:
raise CLIError('--namespace is required')
class _ResourceUtils(object): # pylint: disable=too-many-instance-attributes
def __init__(self,
resource_group_name=None, resource_provider_namespace=None,
parent_resource_path=None, resource_type=None, resource_name=None,
resource_id=None, api_version=None, rcf=None):
# if the resouce_type is in format 'namespace/type' split it.
# (we don't have to do this, but commands like 'vm show' returns such values)
if resource_type and not resource_provider_namespace and not parent_resource_path:
parts = resource_type.split('/')
if len(parts) > 1:
resource_provider_namespace = parts[0]
resource_type = parts[1]
self.rcf = rcf or _resource_client_factory()
if api_version is None:
if resource_id:
api_version = _ResourceUtils._resolve_api_version_by_id(self.rcf, resource_id)
else:
_validate_resource_inputs(resource_group_name, resource_provider_namespace,
resource_type, resource_name)
api_version = _ResourceUtils.resolve_api_version(self.rcf,
resource_provider_namespace,
parent_resource_path,
resource_type)
self.resource_group_name = resource_group_name
self.resource_provider_namespace = resource_provider_namespace