-
Notifications
You must be signed in to change notification settings - Fork 3k
/
custom.py
184 lines (142 loc) · 7.01 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
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from knack.prompting import prompt_y_n
from knack.util import CLIError
from knack.log import get_logger
from azure.cli.command_modules.cognitiveservices._client_factory import cf_accounts, cf_resource_skus
from azure.mgmt.cognitiveservices.models import CognitiveServicesAccountCreateParameters, Sku,\
VirtualNetworkRule, IpRule, NetworkRuleSet
logger = get_logger(__name__)
def list_resources(client, resource_group_name=None):
if resource_group_name:
return client.list_by_resource_group(resource_group_name)
return client.list()
def list_usages(client, resource_group_name, account_name):
"""
List usages for Azure Cognitive Services account.
"""
return client.get_usages(resource_group_name, account_name).value
def list_kinds(client):
"""
List all valid kinds for Azure Cognitive Services account.
:param client: the ResourceSkusOperations
:return: a list
"""
# The client should be ResourceSkusOperations, and list() should return a list of SKUs for all regions.
# The sku will have "kind" and we use that to extract full list of kinds.
kinds = {x.kind for x in client.list()}
return sorted(list(kinds))
def list_skus(cmd, kind=None, location=None, resource_group_name=None, account_name=None):
if resource_group_name is not None or account_name is not None:
logger.warning(
'list-skus with an existing account has been deprecated and will be removed in a future release.')
if resource_group_name is None:
# account_name must not be None
raise CLIError('--resource-group is required when --name is specified.')
# keep the original behavior to avoid breaking changes
return cf_accounts(cmd.cli_ctx).list_skus(resource_group_name, account_name)
# in other cases, use kind and location to filter SKUs
def _filter_sku(_sku):
if kind is not None:
if _sku.kind != kind:
return False
if location is not None:
if location.lower() not in [x.lower() for x in _sku.locations]:
return False
return True
return [x for x in cf_resource_skus(cmd.cli_ctx).list() if _filter_sku(x)]
def create(
client, resource_group_name, account_name, sku_name, kind, location, custom_domain=None,
tags=None, api_properties=None, yes=None):
terms = 'Notice\nMicrosoft will use data you send to Bing Search Services'\
' to improve Microsoft products and services.'\
'Where you send personal data to these Cognitive Services, you are responsible '\
'for obtaining sufficient consent from the data subjects.'\
'The General Privacy and Security Terms in the Online Services Terms '\
'do not apply to these Cognitive Services.'\
'Please refer to the Microsoft Cognitive Services section in the Online '\
'Services Terms'\
' (https://www.microsoft.com/Licensing/product-licensing/products.aspx)'\
' for details.'\
'Microsoft offers policy controls that may be used to disable new Cognitive'\
' Services deployments (https://docs.microsoft.com/azure/cognitive-servic'\
'es/cognitive-services-apis-create-account).'
hint = '\nPlease select'
import re
pattern = re.compile("^[Bb]ing\\..*$")
if pattern.match(kind):
if yes:
logger.warning(terms)
else:
logger.warning(terms)
option = prompt_y_n(hint)
if not option:
raise CLIError('Operation cancelled.')
sku = Sku(name=sku_name)
properties = {}
if api_properties is not None:
properties["apiProperties"] = api_properties
if custom_domain:
properties["customSubDomainName"] = custom_domain
params = CognitiveServicesAccountCreateParameters(sku=sku, kind=kind, location=location,
properties=properties, tags=tags)
return client.create(resource_group_name, account_name, params)
def update(client, resource_group_name, account_name, sku_name=None, custom_domain=None,
tags=None, api_properties=None):
if sku_name is None:
sa = client.get_properties(resource_group_name, account_name)
sku_name = sa.sku.name
sku = Sku(name=sku_name)
properties = {}
if api_properties is not None:
properties["apiProperties"] = api_properties
if custom_domain:
properties["customSubDomainName"] = custom_domain
return client.update(resource_group_name, account_name, sku, tags, properties)
def default_network_acls():
rules = NetworkRuleSet()
rules.default_action = 'Deny'
rules.ip_rules = []
rules.virtual_network_rules = []
return rules
def list_network_rules(client, resource_group_name, account_name):
sa = client.get_properties(resource_group_name, account_name)
rules = sa.network_acls
if rules is None:
rules = default_network_acls()
delattr(rules, 'bypass')
delattr(rules, 'default_action')
return rules
def add_network_rule(client, resource_group_name, account_name, subnet=None,
vnet_name=None, ip_address=None): # pylint: disable=unused-argument
sa = client.get_properties(resource_group_name, account_name)
rules = sa.network_acls
if rules is None:
rules = default_network_acls()
if subnet:
from msrestazure.tools import is_valid_resource_id
if not is_valid_resource_id(subnet):
raise CLIError("Expected fully qualified resource ID: got '{}'".format(subnet))
if not rules.virtual_network_rules:
rules.virtual_network_rules = []
rules.virtual_network_rules.append(VirtualNetworkRule(id=subnet, ignore_missing_vnet_service_endpoint=True))
if ip_address:
if not rules.ip_rules:
rules.ip_rules = []
rules.ip_rules.append(IpRule(value=ip_address))
return client.update(resource_group_name, account_name, properties={"networkAcls": rules})
def remove_network_rule(client, resource_group_name, account_name, ip_address=None, subnet=None,
vnet_name=None): # pylint: disable=unused-argument
sa = client.get_properties(resource_group_name, account_name)
rules = sa.network_acls
if rules is None:
# nothing to update, but return the object
return client.update(resource_group_name, account_name)
if subnet:
rules.virtual_network_rules = [x for x in rules.virtual_network_rules
if not x.id.endswith(subnet)]
if ip_address:
rules.ip_rules = [x for x in rules.ip_rules if x.value != ip_address]
return client.update(resource_group_name, account_name, properties={"networkAcls": rules})