Debian Codegraph @

ansible_collections/azure/azcollection/plugins/module_utils/azure_rm_common_ml.py

raw symbols SCIP
1 # Copyright (c) 2026 Bill Peck, <[email protected]>
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 import traceback
9 try:
10 from typing import Dict
11 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
12 from azure.ai.ml import MLClient
13 from azure.ai.ml._restclient.v2022_02_01_preview.models import ListViewType
14 from azure.ai.ml.entities._builders.base_node import BaseNode
15 from azure.ai.ml._restclient.v2020_09_01_dataplanepreview.models import BatchJobResource
16 import json
17 except ImportError:
18 pass
19
20
21 class MLClientCommon(AzureRMModuleBaseExt):
22
23 @property
24 def client(self):
25 self.log('Getting client')
26 if not self._client:
27 self._client = MLClient(self.azure_auth.azure_credential_track2,
28 subscription_id=self.subscription_id,
29 resource_group_name=self.resource_group,
30 workspace_name=self.ml_workspace,
31 registry_name=self.ml_registry)
32 return self._client
33
34 def entity_to_dict(self, entity):
35 """
36 ENTITY._to_dict() returns and OrderedDict so we abuse json
37 dumps and loads to return a Dict
38 """
39 if isinstance(entity, BatchJobResource):
40 entity = entity.as_dict()
41 if isinstance(entity, BaseNode):
42 entity = entity._to_job() # pylint: disable=protected-access
43 if isinstance(entity, Dict):
44 return entity
45 try:
46 entity = json.loads(json.dumps(entity._to_dict()))
47 return entity
48 except Exception as err: # pylint: disable=broad-exception-caught
49 self.module.warn("Failed to deserialize response: %s" % str(err))
50 self.module.warn(str(entity))
51 self.module.debug(traceback.format_exc())
52
53 def filter_required(self, workspace):
54 """
55 Filter out required networks which are automatically
56 added so we can be idempotent.
57 """
58
59 if "managed_network" in workspace and \
60 "outbound_rules" in workspace["managed_network"]:
61 outbound_rules = workspace["managed_network"].pop("outbound_rules")
62 updated_rules = []
63 for outbound_rule in outbound_rules:
64 if outbound_rule.get("category") != "required":
65 updated_rules.append(outbound_rule)
66 workspace["managed_network"]["outbound_rules"] = updated_rules
67
68 return workspace
69
70 def get_list_view_type(self, list_type):
71 list_types = dict(active=ListViewType.ACTIVE_ONLY,
72 archived=ListViewType.ARCHIVED_ONLY,
73 all=ListViewType.ALL)
74 return list_types.get(list_type, ListViewType.ACTIVE_ONLY)