10
10
from azure .cli .core .profiles import get_sdk
11
11
from azure .cli .core .profiles import ResourceType
12
12
from msrestazure .tools import resource_id
13
+ from msrestazure .tools import parse_resource_id
13
14
14
15
15
16
CONTRIBUTOR = 'b24988ac-6180-42a0-ab88-20f7382dd24c'
@@ -20,7 +21,7 @@ def _gen_uuid():
20
21
21
22
22
23
def assign_contributor_to_vnet (cli_ctx , vnet , object_id ):
23
- client = get_mgmt_service_client (cli_ctx , ResourceType .MGMT_AUTHORIZATION )
24
+ auth_client = get_mgmt_service_client (cli_ctx , ResourceType .MGMT_AUTHORIZATION )
24
25
25
26
RoleAssignmentCreateParameters = get_sdk (cli_ctx , ResourceType .MGMT_AUTHORIZATION ,
26
27
'RoleAssignmentCreateParameters' , mod = 'models' ,
@@ -33,15 +34,63 @@ def assign_contributor_to_vnet(cli_ctx, vnet, object_id):
33
34
name = CONTRIBUTOR ,
34
35
)
35
36
36
- for assignment in list (client .role_assignments .list_for_scope (vnet )):
37
- if assignment .role_definition_id .lower () == role_definition_id .lower () and \
38
- assignment .principal_id .lower () == object_id .lower ():
39
- return
37
+ if has_assignment (auth_client .role_assignments .list_for_scope (vnet ), role_definition_id , object_id ):
38
+ return
40
39
40
+ # generate random uuid for role assignment
41
41
role_uuid = _gen_uuid ()
42
42
43
- client .role_assignments .create (vnet , role_uuid , RoleAssignmentCreateParameters (
43
+ auth_client .role_assignments .create (vnet , role_uuid , RoleAssignmentCreateParameters (
44
44
role_definition_id = role_definition_id ,
45
45
principal_id = object_id ,
46
46
principal_type = 'ServicePrincipal' ,
47
47
))
48
+
49
+
50
+ def assign_contributor_to_routetable (cli_ctx , master_subnet , worker_subnet , object_id ):
51
+ auth_client = get_mgmt_service_client (cli_ctx , ResourceType .MGMT_AUTHORIZATION )
52
+ network_client = get_mgmt_service_client (cli_ctx , ResourceType .MGMT_NETWORK )
53
+
54
+ RoleAssignmentCreateParameters = get_sdk (cli_ctx , ResourceType .MGMT_AUTHORIZATION ,
55
+ 'RoleAssignmentCreateParameters' , mod = 'models' ,
56
+ operation_group = 'role_assignments' )
57
+
58
+ role_definition_id = resource_id (
59
+ subscription = get_subscription_id (cli_ctx ),
60
+ namespace = 'Microsoft.Authorization' ,
61
+ type = 'roleDefinitions' ,
62
+ name = CONTRIBUTOR ,
63
+ )
64
+
65
+ route_tables = set ()
66
+ for sn in [master_subnet , worker_subnet ]:
67
+ sid = parse_resource_id (sn )
68
+
69
+ subnet = network_client .subnets .get (resource_group_name = sid ['resource_group' ],
70
+ virtual_network_name = sid ['name' ],
71
+ subnet_name = sid ['resource_name' ])
72
+
73
+ if subnet .route_table is not None :
74
+ route_tables .add (subnet .route_table .id )
75
+
76
+ for rt in route_tables :
77
+ if has_assignment (auth_client .role_assignments .list_for_scope (rt ),
78
+ role_definition_id , object_id ):
79
+ continue
80
+
81
+ role_uuid = _gen_uuid ()
82
+
83
+ auth_client .role_assignments .create (rt , role_uuid , RoleAssignmentCreateParameters (
84
+ role_definition_id = role_definition_id ,
85
+ principal_id = object_id ,
86
+ principal_type = 'ServicePrincipal' ,
87
+ ))
88
+
89
+
90
+ def has_assignment (assignments , role_definition_id , object_id ):
91
+ for assignment in assignments :
92
+ if assignment .role_definition_id .lower () == role_definition_id .lower () and \
93
+ assignment .principal_id .lower () == object_id .lower ():
94
+ return True
95
+
96
+ return False
0 commit comments