Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_ml_datastore.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_datastore
11 version_added: "3.16.0"
12 short_description: Create, Update, Archive, Restore an Azure Machine Learning Datastore
13 description:
14 - Create, Update, Delete an Azure Machine Learning Datastore.
15 options:
16 name:
17 description:
18 - Name of the Azure ML datastore asset.
19 type: str
20 required: true
21 resource_group:
22 description:
23 - Name of resource group.
24 required: true
25 type: str
26 ml_workspace:
27 description:
28 - Name of the Azure ML workspace.
29 required: true
30 type: str
31 description:
32 description:
33 - Description of the datastore target.
34 type: str
35 required: false
36 resource_definition:
37 description:
38 - "Local path to the YAML file containing the Azure ML
39 datastore specification. The YAML reference docs for
40 datastore can be found at:
41 https://aka.ms/ml-cli-v2-datastore-blob-yaml-reference,
42 https://aka.ms/ml-cli-v2-datastore-file-yaml-reference,
43 https://aka.ms/ml-cli-v2-datastore-data-lake-gen1-yaml-reference,
44 https://aka.ms/ml-cli-v2-datastore-data-lake-gen2-yaml-reference."
45 type: str
46 required: false
47 state:
48 description:
49 - State of the Datastore. Use C(present) to create
50 or update. C(absent) to delete.
51 default: present
52 type: str
53 choices:
54 - present
55 - absent
56
57 extends_documentation_fragment:
58 - azure.azcollection.azure
59 - azure.azcollection.azure_tags
60
61 author:
62 - Bill Peck (@p3ck)
63 '''
64
65 EXAMPLES = '''
66 - name: Create ML Datastore
67 azure.azcollection.azure_rm_ml_datastore:
68 name: MyDataStore
69 description: My Datastore created by Ansible
70 resource_group: myResourceGroup
71 ml_workspace: myMLWorkspace
72 resource_definition: |
73 FIXME.....
74 tags:
75 createdByToolkit: Ansible
76 '''
77
78 RETURN = '''
79 changed:
80 description:
81 - Whether the resource is changed.
82 returned: always
83 type: bool
84 sample: false
85 ml_datastore:
86 description:
87 - Datastore that was just created or updated.
88 returned: always
89 type: dict
90 sample: {
91 "account_name": "workspacstoragexxxxxxxxx",
92 "container_name": "datastorexxxxxxxxx-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
93 "credentials": {},
94 "description": "here is a description",
95 "endpoint": "core.windows.net",
96 "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-datastore/providers/Microsoft.MachineLearningServices/workspaces/workspaceexxxxxxxx/datastores/datastorexxxxxxxxxx",
97 "name": "datastorexxxxxxxxx",
98 "protocol": "https",
99 "tags": {},
100 "type": "azure_blob",
101 }
102 ''' # NOQA
103
104
105 try:
106 import io
107 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ml import MLClientCommon
108 from azure.core.exceptions import ResourceNotFoundError
109 from azure.ai.ml.entities._load_functions import load_datastore
110 except ImportError:
111 # This is handled in azure_rm_common
112 pass
113
114
115 class AzureRMMLDatastore(MLClientCommon):
116
117 UPDATE_OVERRIDES = ['description',
118 'tags'
119 ]
120
121 PARAM_OVERRIDES = UPDATE_OVERRIDES + ['name']
122
123 def __init__(self):
124
125 self.module_arg_spec = dict(
126 name=dict(
127 type='str',
128 required=True,
129 ),
130 description=dict(
131 type='str',
132 required=False,
133 ),
134 resource_definition=dict(
135 type='str',
136 required=False,
137 ),
138 resource_group=dict(
139 type='str',
140 required=True,
141 ),
142 ml_workspace=dict(
143 type='str',
144 required=True,
145 ),
146 state=dict(
147 type='str',
148 default='present',
149 choices=['present', 'absent'],
150 ),
151 )
152
153 self._client = None
154 self.ml_registry = None
155 self.state = None
156
157 self.results = dict(
158 changed=False,
159 compare=[],
160 ml_datastore={}
161 )
162
163 required_if = [
164 ('state', 'present', ['resource_definition'])
165 ]
166 super(AzureRMMLDatastore, self).__init__(self.module_arg_spec,
167 supports_check_mode=True,
168 required_if=required_if)
169
170 def exec_module(self, **kwargs):
171
172 for key in list(self.module_arg_spec.keys()) + ['tags']:
173 setattr(self, key, kwargs[key])
174
175 if self.resource_definition:
176 resource_definition = io.StringIO(self.resource_definition)
177 else:
178 resource_definition = None
179
180 changed = False
181 ml_datastore_info = self.get(self.name)
182
183 if self.state == 'present':
184 if ml_datastore_info:
185 # Update
186 params_override = self.update_params(kwargs,
187 self.UPDATE_OVERRIDES,
188 ml_datastore_info)
189
190 # Generate ml_datastore based on ansible args passed in.
191 ml_datastore = load_datastore(None, params_override=params_override)
192 ml_datastore_info = self.entity_to_dict(ml_datastore_info)
193 changed = not self.default_compare({},
194 self.entity_to_dict(ml_datastore),
195 ml_datastore_info,
196 '',
197 self.results)
198 if changed and not self.check_mode:
199 response = self.client.datastores.create_or_update(ml_datastore)
200 ml_datastore_info = self.entity_to_dict(response)
201
202 else:
203 # Create
204 params_override = self.update_params(kwargs, self.PARAM_OVERRIDES)
205
206 # Generate ml_datastore based on ansible args passed in.
207 ml_datastore = load_datastore(resource_definition,
208 params_override=params_override)
209 changed = True
210 if not self.check_mode:
211 response = self.client.datastores.create_or_update(ml_datastore)
212 ml_datastore_info = self.entity_to_dict(response)
213 elif self.state == 'absent':
214 # Delete
215 if ml_datastore_info:
216 changed = True
217 if not self.check_mode:
218 self.delete(self.name)
219 ml_datastore_info = self.get(self.name,
220 as_dict=True)
221
222 self.results['ml_datastore'] = ml_datastore_info
223 self.results['changed'] = changed
224 return self.results
225
226 def update_params(self, kwargs, param_overrides, ml_datastore=None):
227 params_override = []
228
229 # If ml_datastore is defined use those values as a base.
230 # Update can't change some of these values but we need
231 # to keep the same values as before the update.
232 if ml_datastore:
233 items = self.entity_to_dict(ml_datastore).items()
234 params_override = [{k: v} for k, v in items]
235
236 # Update values based on what param_overrides says we can update.
237 for key in list(self.module_arg_spec.keys()) + ['tags']:
238 if kwargs[key] is not None and key in param_overrides:
239 params_override.append({key: kwargs[key]})
240 return params_override
241
242 def get(self, name, as_dict=False):
243 try:
244 # Attempt to retrieve ml_datastore based on name
245 ml_datastore_info = self.client.datastores.get(name=name,
246 include_secrets=False)
247 if as_dict:
248 ml_datastore_info = self.entity_to_dict(ml_datastore_info)
249 except ResourceNotFoundError:
250 ml_datastore_info = None
251 except Exception as e: # pylint: disable=broad-exception-caught
252 ml_datastore_info = None
253 return ml_datastore_info
254
255 def delete(self, name=None):
256 try:
257 response = self.client.datastores.delete(name=name)
258 return response
259 except Exception as e: # pylint: disable=broad-exception-caught
260 self.fail("Error delete the data instance: {0}".format(str(e)))
261
262
263 def main():
264 AzureRMMLDatastore()
265
266
267 if __name__ == '__main__':
268 main()