Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_ml_workspace.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import absolute_import, division, print_function
6 __metaclass__ = type
7
8 DOCUMENTATION = '''
9 ---
10 module: azure_rm_ml_workspace
11 version_added: "3.15.0"
12 short_description: Create, Update or Delete an Azure Machine Learning Workspace
13 description:
14 - Create, Update or Delete an Azure Machine Learning Workspace.
15 options:
16 name:
17 description:
18 - Name of the Azure ML workspace.
19 type: str
20 required: true
21 display_name:
22 description:
23 - Display name for the workspace.
24 type: str
25 required: false
26 description:
27 description:
28 - Description of the Azure ML workspace.
29 type: str
30 required: false
31 resource_definition:
32 description:
33 - Workspace definition in YAML
34 type: str
35 required: false
36 kind:
37 description:
38 - Specifies the workspace as a specific kind.
39 type: str
40 choices:
41 - hub
42 - project
43 required: false
44 update_dependent_resources:
45 description:
46 - Specifying update_dependent_resources, gives your
47 consent to update the workspace dependent
48 resources. Updating the workspace-attached Azure
49 Container Registry or Application Insights resource
50 may break lineage of previous jobs, deployed
51 inference endpoints, or your ability to rerun
52 earlier jobs in this workspace.
53 type: bool
54 default: false
55 public_network_access:
56 description:
57 - Allow public endpoint connectivity when a workspace
58 is private link enabled.
59 type: str
60 choices:
61 - Disabled
62 - Enabled
63 default: Disabled
64 image_build_compute:
65 description:
66 - The name of the compute target to use for building
67 environment Docker images when the container
68 registry is behind a VNet.
69 type: str
70 required: false
71 storage_account:
72 description:
73 - ARM id of the storage account associated with this workspace.
74 type: str
75 required: false
76 key_vault:
77 description:
78 - ARM id of the key vault associated with this workspace.
79 type: str
80 required: false
81 application_insights:
82 description:
83 - ARM id of the application insights associated with
84 this workspace.
85 type: str
86 required: false
87 container_registry:
88 description:
89 - ARM id of the container registry associated with
90 this workspace.
91 type: str
92 required: false
93 hub_id:
94 description:
95 - Only applies to O(kind=Project). An ARM ID which defines the
96 parent hub of this project.
97 type: str
98 required: false
99 enable_data_isolation:
100 description:
101 - A flag to determine if a workspace has data
102 isolation enabled. The flag can only be set at the
103 creation stage, it can't be updated.
104 type: bool
105 default: false
106 primary_user_assigned_identity:
107 description:
108 - ARM identifier of primary user assigned managed
109 identity, in case multiple ones are specified. Also
110 the default managed identity for clusterless compute.
111 type: str
112 required: false
113 managed_network:
114 description:
115 - Managed Network Isolation Mode for the workspace.
116 type: str
117 choices:
118 - disabled
119 - allow_internet_outbound
120 - allow_only_approved_outbound
121 required: false
122 network_acls:
123 description:
124 - List of inbound IP rules
125 required: false
126 type: list
127 elements: str
128 provision_network_now:
129 description:
130 - Set to trigger the provisioning of the
131 managed network when creating a worksapce with the
132 manged network enabled, or else it does nothing.
133 type: bool
134 default: false
135 system_datastores_auth_mode:
136 description:
137 - Specifies the auth mode for the system data stores.
138 type: str
139 choices:
140 - accesskey
141 - identity
142 allow_roleassignment_on_rg:
143 description:
144 - A flag to determine if a workspace could
145 have role assignments on resource group level.
146 type: bool
147 default: true
148 location:
149 description:
150 - The location to be used for the new workspace.
151 type: str
152 required: false
153 resource_group:
154 description:
155 - Name of resource group.
156 required: true
157 type: str
158 default_resource_group:
159 description:
160 - Only applies to O(kind=hub). If set, then child projects of this
161 hub will have their resource group set this by default.
162 required: false
163 type: str
164 all_resources:
165 description:
166 - Only applies to O(state=absent). Delete all the dependent resources
167 associated with the workspace (Azure Storage account,
168 Azure Container Registry, Azure Application Insights, Azure Key Vault).
169 type: bool
170 default: false
171 permanently_delete:
172 description:
173 - Only applies to O(state=absent). Workspaces are soft-deleted
174 state by default to allow recovery of workspace data. Set this flag
175 to override the soft-delete behavior and permanently delete your
176 workspace.
177 type: bool
178 default: false
179 state:
180 description:
181 - State of the Workspace. Use C(present) to create
182 or update and C(absent) to delete.
183 default: present
184 type: str
185 choices:
186 - absent
187 - present
188
189 extends_documentation_fragment:
190 - azure.azcollection.azure
191 - azure.azcollection.azure_tags
192
193 author:
194 - Bill Peck (@p3ck)
195 '''
196
197 EXAMPLES = '''
198 - name: Create ML Workspace
199 azure.azcollection.azure_rm_ml_workspace:
200 location: eastus
201 name: MyWorkspace
202 description: My Workspace created by Ansible
203 resource_group: myResourceGroup
204 resource_definition: |
205 managed_network:
206 isolation_mode: allow_only_approved_outbound
207 outbound_rules:
208 - name: microsoft
209 destination: 'microsoft.com'
210 type: fqdn
211 - name: servicetag-w-prefixes
212 destination:
213 port_ranges: 80, 8080-8089
214 protocol: TCP
215 service_tag: sometag
216 address_prefixes: ["168.63.129.16","10.0.0.0/24"]
217 type: service_tag
218 tags:
219 createdByToolkit: Ansible
220 '''
221
222 RETURN = '''
223 changed:
224 description:
225 - Whether the resource is changed.
226 returned: always
227 type: bool
228 sample: false
229 ml_workspace:
230 description:
231 - Workspace that was just created or updated.
232 returned: always
233 type: dict
234 sample: {
235 "allow_roleassignment_on_rg": false,
236 "application_insights": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/test-ml-workspace/providers/Microsoft.insights/components/workspacinsightsxxxxxxxx",
237 "description": "Workspace Integration Tests",
238 "discovery_url": "https://westus3.api.azureml.ms/discovery",
239 "display_name": "workspace-xxxxxxx-xxx",
240 "enable_data_isolation": false,
241 "hbi_workspace": false,
242 "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/test-ml-workspace/providers/Microsoft.MachineLearningServices/workspaces/workspace-xxxxxxx-xxx",
243 "identity": {
244 "principal_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
245 "tenant_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
246 "type": "system_assigned"
247 },
248 "key_vault": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/test-ml-workspace/providers/Microsoft.Keyvault/vaults/workspackeyvaultxxxxxxxx",
249 "location": "westus3",
250 "managed_network": {
251 "firewall_sku": "standard",
252 "isolation_mode": "allow_only_approved_outbound",
253 "network_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
254 "outbound_rules": [
255 {
256 "category": "user_defined",
257 "destination": "microsoft.com",
258 "name": "microsoft",
259 "status": "Inactive",
260 "type": "fqdn"
261 },
262 {
263 "category": "user_defined",
264 "destination": {
265 "address_prefixes": [
266 "168.63.129.16",
267 "10.0.0.0/24"
268 ],
269 "port_ranges": "80, 8080-8089",
270 "protocol": "TCP",
271 "service_tag": "sometag"
272 },
273 "name": "servicetag-w-prefixes",
274 "status": "Inactive",
275 "type": "service_tag"
276 }
277 ],
278 "status": {
279 "spark_ready": false,
280 "status": "Inactive"
281 }
282 },
283 "mlflow_tracking_uri": "azureml://westus3.api.azureml.ms/mlflow/v1.0/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/test-ml-workspace/providers/Microsoft.MachineLearningServices/workspaces/workspace-xxxxxxx-xxx",
284 "name": "workspace-xxxxxxx-xxx",
285 "network_acls": {
286 "default_action": "Allow",
287 "ip_rules": []
288 },
289 "provision_network_now": false,
290 "public_network_access": "Disabled",
291 "resource_group": "test-ml-workspace",
292 "serverless_compute": {"no_public_ip": false},
293 "storage_account": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/test-ml-workspace/providers/Microsoft.Storage/storageAccounts/workspacstoragexxxxxxxxx",
294 "system_datastores_auth_mode": "accesskey",
295 "tags": {
296 "createdByToolkit": "sdk-v2-1.31.0",
297 "foo": "bar"
298 }
299 }
300 ''' # NOQA
301
302
303 try:
304 import io
305 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ml import MLClientCommon
306 from azure.core.exceptions import ResourceNotFoundError
307 from azure.ai.ml.entities._load_functions import load_workspace
308 except ImportError:
309 # This is handled in azure_rm_common
310 pass
311
312
313 class AzureRMMLWorkspace(MLClientCommon):
314
315 PARAM_OVERRIDES = ['name',
316 'display_name',
317 'description',
318 'tags',
319 'location',
320 'image_build_compute',
321 'public_network_access',
322 'storage_account',
323 'key_vault',
324 'application_insights',
325 'container_registry',
326 'primary_user_assigned_identity',
327 'provision_network_now',
328 'enable_data_isolation',
329 'hub_id',
330 'default_resource_group',
331 'kind',
332 'system_datastores_auth_mode',
333 'allow_roleassignment_on_rg',
334 ]
335
336 def __init__(self):
337
338 self.module_arg_spec = dict(
339 name=dict(
340 type='str',
341 required=True
342 ),
343 display_name=dict(
344 type='str',
345 required=False,
346 ),
347 description=dict(
348 type='str',
349 required=False,
350 ),
351 resource_definition=dict(
352 type='str',
353 required=False,
354 ),
355 kind=dict(
356 type='str',
357 choices=['hub',
358 'project'],
359 required=False,
360 ),
361 update_dependent_resources=dict(
362 type='bool',
363 default=False,
364 ),
365 public_network_access=dict(
366 type='str',
367 choices=['Disabled', 'Enabled'],
368 default='Disabled',
369 ),
370 image_build_compute=dict(
371 type='str',
372 required=False,
373 ),
374 storage_account=dict(
375 type='str',
376 required=False,
377 ),
378 key_vault=dict(
379 type='str',
380 required=False,
381 no_log=False,
382 ),
383 application_insights=dict(
384 type='str',
385 required=False,
386 ),
387 container_registry=dict(
388 type='str',
389 required=False,
390 ),
391 hub_id=dict(
392 type='str',
393 required=False,
394 ),
395 enable_data_isolation=dict(
396 type='bool',
397 default=False,
398 ),
399 primary_user_assigned_identity=dict(
400 type='str',
401 required=False,
402 ),
403 managed_network=dict(
404 type='str',
405 choices=['disabled',
406 'allow_internet_outbound',
407 'allow_only_approved_outbound'],
408 required=False,
409 ),
410 network_acls=dict(
411 type='list',
412 required=False,
413 elements='str',
414 ),
415 provision_network_now=dict(
416 type='bool',
417 default=False,
418 ),
419 system_datastores_auth_mode=dict(
420 type='str',
421 choices=['accesskey',
422 'identity'],
423 required=False,
424 ),
425 allow_roleassignment_on_rg=dict(
426 type='bool',
427 default=True,
428 ),
429 location=dict(
430 type='str',
431 required=False
432 ),
433 resource_group=dict(
434 type='str',
435 required=True
436 ),
437 default_resource_group=dict(
438 type='str',
439 required=False
440 ),
441 all_resources=dict(
442 type='bool',
443 default=False
444 ),
445 permanently_delete=dict(
446 type='bool',
447 default=False
448 ),
449 state=dict(
450 type='str',
451 default='present',
452 choices=['present', 'absent']
453 ),
454 )
455
456 self._client = None
457 self.ml_workspace = None
458 self.ml_registry = None
459 self.state = None
460
461 self.results = dict(
462 changed=False,
463 compare=[],
464 ml_workspace={}
465 )
466
467 super(AzureRMMLWorkspace, self).__init__(self.module_arg_spec,
468 supports_check_mode=True,
469 )
470
471 def exec_module(self, **kwargs):
472
473 params_override = []
474 for key in list(self.module_arg_spec.keys()) + ['tags']:
475 setattr(self, key, kwargs[key])
476 if kwargs[key] is not None and key in self.PARAM_OVERRIDES:
477 params_override.append({key: kwargs[key]})
478
479 if self.resource_definition:
480 resource_definition = io.StringIO(self.resource_definition)
481 else:
482 resource_definition = None
483
484 # Process managed_network specially
485 if self.managed_network:
486 params_override.append({"managed_network": {"isolation_mode": self.managed_network}})
487
488 # Process network_acls specially
489 if self.network_acls:
490 network_acls = {
491 "default_action": "Deny",
492 "ip_rules": [{"value": ip} for ip in self.network_acls]
493 }
494 else:
495 network_acls = {
496 "default_action": "Allow",
497 "ip_rules": []
498 }
499 params_override.append({'network_acls': network_acls})
500
501 changed = False
502 # Generate ml_workspace based on ansible args passed in.
503 ml_workspace = load_workspace(resource_definition,
504 params_override=params_override)
505 try:
506 # Attempt to retrieve ml_workspace based on name
507 ml_workspace_info = self.client.workspaces.get(self.name)
508 except ResourceNotFoundError:
509 ml_workspace_info = None
510
511 if self.state == 'present':
512 if ml_workspace_info:
513 # Update
514 ml_workspace_info = self.entity_to_dict(ml_workspace_info)
515 ml_workspace_info = self.filter_required(ml_workspace_info)
516 changed = not self.default_compare({},
517 self.filter_required(
518 self.entity_to_dict(ml_workspace)),
519 ml_workspace_info,
520 '',
521 self.results)
522 if changed and not self.check_mode:
523 response = self.client.workspaces.begin_update(ml_workspace,
524 update_dependent_resources=self.update_dependent_resources
525 )
526 ml_workspace_object = self.get_poller_result(response)
527 ml_workspace_info = self.entity_to_dict(ml_workspace_object)
528
529 else:
530 # Create
531 changed = True
532 if not self.check_mode:
533 response = self.client.workspaces.begin_create(
534 workspace=ml_workspace,
535 update_dependent_resources=self.update_dependent_resources)
536 ml_workspace_object = self.get_poller_result(response)
537 ml_workspace_info = self.entity_to_dict(ml_workspace_object)
538 else:
539 # Delete
540 if ml_workspace_info:
541 changed = True
542 if not self.check_mode:
543 response = self.client.workspaces.begin_delete(
544 name=self.name,
545 delete_dependent_resources=self.all_resources,
546 permanently_delete=self.permanently_delete)
547 ml_workspace_info = self.get_poller_result(response)
548
549 self.results['ml_workspace'] = ml_workspace_info
550 self.results['changed'] = changed
551 return self.results
552
553
554 def main():
555 AzureRMMLWorkspace()
556
557
558 if __name__ == '__main__':
559 main()