forked from Azure/azure-cli-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_database_utils.py
279 lines (242 loc) · 9.27 KB
/
_database_utils.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
# coding=utf-8
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from uuid import uuid4
from knack.log import get_logger
_DATABASES = {
'mysql': {
'host': '{}.mysql.database.azure.com',
'port': '3306'
},
'sql': {
'host': '{}.database.windows.net',
'port': '1433'
},
'cosmosdb': {
'host': '{}.documents.azure.com',
'port': '10255'
}
}
logger = get_logger(__name__)
# pylint: disable=too-few-public-methods
class DatabaseCreationStep():
def __init__(self, name: str, delegate, params):
self.delegate = delegate
self.params = params
self.name = name
class DatabaseCreator():
def __init__(self, steps):
if not steps:
raise 'Steps cannot be empty'
self.steps = steps
self.__current_poller = None
self.__is_active = True
def create(self):
self.__run_step()
return self
# pylint: disable=unused-argument
def __run_step(self, *args):
if self.steps:
step = self.steps.pop(0)
logger.warning('Configuring %s...', step.name)
return_value = None
if step.params:
return_value = step.delegate(**step.params)
else:
return_value = step.delegate()
if hasattr(return_value, 'add_done_callback'):
self.__current_poller = return_value
self.__current_poller.add_done_callback(self.__run_step)
else:
self.__run_step(None)
else:
self.__is_active = False
def result(self, timeout):
if self.__current_poller and hasattr(self.__current_poller, 'result'):
self.__current_poller.result(timeout)
def done(self):
if self.__is_active:
return False
return True
class Database():
def __init__(self, cmd, database_type: str, name: str, location: str):
self.__steps = []
self.__cmd = cmd
self.database_type = database_type.lower()
self.name = name
self.admin = 'database_admin'
self.password = str(uuid4())
self.location = location
def create(self):
if self.database_type == 'mysql':
steps = self.__get_mysql_steps(
self.__cmd, self.name, self.location, self.admin, self.password)
elif self.database_type == 'sql':
steps = self.__get_sql_steps(
self.__cmd, self.name, self.location, self.admin, self.password)
elif self.database_type == 'cosmosdb' or self.database_type == 'mongodb':
steps = self.__get_cosmosdb_steps(
self.__cmd, self.name, self.location)
else:
raise 'Unknown data store type'
creator = DatabaseCreator(steps)
return creator.create()
@property
def host(self) -> str:
return _DATABASES[self.database_type]['host'].format(self.name)
@property
def port(self) -> str:
return _DATABASES[self.database_type]['port']
def __get_cosmosdb_steps(self, cmd, name, location):
from azure.mgmt.cosmosdb import CosmosDB
from azure.cli.core.commands.client_factory import get_mgmt_service_client
steps = []
cosmosdb_client = get_mgmt_service_client(cmd.cli_ctx, CosmosDB)
from azure.mgmt.cosmosdb.models import (
DatabaseAccountCreateUpdateParameters,
DatabaseAccountKind,
Location,
ConsistencyPolicy
)
server_params = {
'resource_group_name': name,
'account_name': name,
'params': DatabaseAccountCreateUpdateParameters(
location=location,
locations=[Location(
location_name=location,
failover_priority=0,
is_zone_redundant=False)],
kind=DatabaseAccountKind.mongo_db.value,
consistency_policy=ConsistencyPolicy(
default_consistency_level=1,
max_staleness_prefix=100,
max_interval_in_seconds=5
)
)
}
steps.append(DatabaseCreationStep(
'server', cosmosdb_client.database_accounts.create_or_update, server_params))
database_params = {
'resource_group_name': name,
'account_name': name,
'database_name': name,
'resource': {'id': name},
'options': {}
}
steps.append(DatabaseCreationStep(
'database', cosmosdb_client.database_accounts.create_update_mongo_db_database, database_params))
def retrieve_password():
result = cosmosdb_client.database_accounts.list_keys(
resource_group_name=name,
account_name=name
)
self.password = result.primary_master_key
steps.append(DatabaseCreationStep(
'retrieve password', retrieve_password, None))
return steps
# pylint: disable=no-self-use
def __get_mysql_steps(self, cmd, name: str, location: str, admin: str, password: str):
from azure.cli.command_modules.rdbms._client_factory import (
cf_mysql_servers,
cf_mysql_db,
cf_mysql_firewall_rules
)
from azure.mgmt.rdbms.mysql.models import (
ServerPropertiesForDefaultCreate,
ServerForCreate,
StorageProfile,
Sku
)
mysql_server_client = cf_mysql_servers(cmd.cli_ctx, None)
mysql_database_client = cf_mysql_db(cmd.cli_ctx, None)
mysql_firewall_client = cf_mysql_firewall_rules(cmd.cli_ctx, None)
admin = admin[:16]
steps = []
server_params = {
'resource_group_name': name,
'server_name': name,
'parameters': ServerForCreate(
sku=Sku(name='B_Gen5_1'),
location=location,
properties=ServerPropertiesForDefaultCreate(
administrator_login=admin,
administrator_login_password=password,
version='5.7',
ssl_enforcement='Enabled',
storage_profile=StorageProfile(
backup_retention_days=0,
geo_redundant_backup='Disabled',
storage_mb=5120
)
)
)
}
steps.append(DatabaseCreationStep('database server',
mysql_server_client.create, server_params))
database_params = {
'resource_group_name': name,
'server_name': name,
'database_name': name
}
steps.append(DatabaseCreationStep(
'database', mysql_database_client.create_or_update, database_params))
firewall_params = {
'resource_group_name': name,
'server_name': name,
'firewall_rule_name': 'Azure',
'start_ip_address': '0.0.0.0',
'end_ip_address': '0.0.0.0'
}
steps.append(DatabaseCreationStep('database firewall',
mysql_firewall_client.create_or_update, firewall_params))
return steps
# pylint: disable=no-self-use
def __get_sql_steps(self, cmd, name, location, admin, password):
from azure.cli.command_modules.sql._util import (
get_sql_management_client
)
from azure.cli.command_modules.sql.custom import (
server_create as sql_server_create,
db_create as sql_database_create,
firewall_rule_allow_all_azure_ips as sql_firewall_allow_azure
)
from azure.mgmt.sql.models import (
Sku
)
sql_client = get_sql_management_client(cmd.cli_ctx)
sku = Sku(capacity=10, name='Standard', tier='Standard')
steps = []
server_parameters = {
'client': sql_client.servers,
'resource_group_name': name,
'server_name': name,
'location': location,
'version': '12.0',
'administrator_login': admin,
'administrator_login_password': password
}
steps.append(DatabaseCreationStep(
'server', sql_server_create, server_parameters))
database_parameters = {
'cmd': cmd,
'client': sql_client.databases,
'database_name': name,
'server_name': name,
'resource_group_name': name,
'location': location,
'sku': sku,
'elastic_pool_id': None
}
steps.append(DatabaseCreationStep(
'database', sql_database_create, database_parameters))
firewall_parameters = {
'client': sql_client.firewall_rules,
'server_name': name,
'resource_group_name': name
}
steps.append(DatabaseCreationStep(
'firewall', sql_firewall_allow_azure, firewall_parameters))
return steps