| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
DOCUMENTATION = ''' |
| 9 |
--- |
| 10 |
module: azure_rm_ml_batch_endpoint |
| 11 |
version_added: "3.18.0" |
| 12 |
short_description: Create, Delete or regenerate keys for an Azure Machine Learning Batch Endpoint |
| 13 |
description: |
| 14 |
- Create, Delete or regenerate keys for an Azure Machine Learning Batch Endpoint. |
| 15 |
options: |
| 16 |
name: |
| 17 |
description: |
| 18 |
- Name of the Azure ML batch_endpoint. |
| 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 |
resource_definition: |
| 32 |
description: |
| 33 |
- Batch Endpoint definition in YAML |
| 34 |
type: str |
| 35 |
required: false |
| 36 |
description: |
| 37 |
description: |
| 38 |
- Description of the batch_endpoint. |
| 39 |
type: str |
| 40 |
state: |
| 41 |
description: |
| 42 |
- State of the Batch Endpoint. Use C(present) to create |
| 43 |
or update. |
| 44 |
default: present |
| 45 |
type: str |
| 46 |
choices: |
| 47 |
- present |
| 48 |
- absent |
| 49 |
|
| 50 |
extends_documentation_fragment: |
| 51 |
- azure.azcollection.azure |
| 52 |
- azure.azcollection.azure_tags |
| 53 |
|
| 54 |
author: |
| 55 |
- Bill Peck (@p3ck) |
| 56 |
''' |
| 57 |
|
| 58 |
EXAMPLES = ''' |
| 59 |
- name: Create ML Batch Endpoint |
| 60 |
azure.azcollection.azure_rm_ml_batch_endpoint: |
| 61 |
name: endpoint-xxxxxxxxxx-ansible |
| 62 |
resource_group: xxxxx-ml-batch_endpoint |
| 63 |
ml_workspace: workspace-xxxxxxxxxx |
| 64 |
resource_definition: | |
| 65 |
$schema: https://azuremlschemas.azureedge.net/latest/batchEndpoint.schema.json |
| 66 |
name: hello-batch |
| 67 |
description: A hello world endpoint for component deployments. |
| 68 |
auth_mode: aad_token |
| 69 |
''' |
| 70 |
|
| 71 |
RETURN = ''' |
| 72 |
changed: |
| 73 |
description: |
| 74 |
- Whether the resource is changed. |
| 75 |
returned: always |
| 76 |
type: bool |
| 77 |
sample: false |
| 78 |
ml_batch_endpoint: |
| 79 |
description: |
| 80 |
- Batch Endpoint that was just created or updated. |
| 81 |
returned: always |
| 82 |
type: dict |
| 83 |
sample: { |
| 84 |
"auth_mode": "aad_token", |
| 85 |
"description": "A hello world endpoint for component deployments.", |
| 86 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-batch_endpoint_invoke/providers/Microsoft.MachineLearningServices/workspaces/workspace-xxxxxxxxxx/batchEndpoints/endpoint-xxxxxxxxxx-ansible", |
| 87 |
"location": "eastus", |
| 88 |
"name": "endpoint-xxxxxxxxxx-ansible", |
| 89 |
"properties": { |
| 90 |
"BatchEndpointCreationApiVersion": "2023-10-01", |
| 91 |
"azureml.onlineendpointid": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxx-ml-batch_endpoint_invoke/providers/Microsoft.MachineLearningServices/workspaces/workspace-xxxxxxxxxx/batchEndpoints/endpoint-xxxxxxxxxx-ansible" |
| 92 |
}, |
| 93 |
"provisioning_state": "Succeeded", |
| 94 |
"scoring_uri": "https://endpoint-xxxxxxxxxx-ansible.eastus.inference.ml.azure.com/jobs", |
| 95 |
"tags": {} |
| 96 |
} |
| 97 |
''' |
| 98 |
|
| 99 |
|
| 100 |
try: |
| 101 |
import io |
| 102 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ml import MLClientCommon |
| 103 |
from azure.core.exceptions import ResourceNotFoundError |
| 104 |
from azure.ai.ml.entities._load_functions import load_batch_endpoint |
| 105 |
except ImportError: |
| 106 |
|
| 107 |
pass |
| 108 |
|
| 109 |
|
| 110 |
class AzureRMMLBatchEndpoint(MLClientCommon): |
| 111 |
|
| 112 |
UPDATE_OVERRIDES = ['description', |
| 113 |
'tags'] |
| 114 |
|
| 115 |
PARAM_OVERRIDES = UPDATE_OVERRIDES + ['name'] |
| 116 |
|
| 117 |
def __init__(self): |
| 118 |
|
| 119 |
self.module_arg_spec = dict( |
| 120 |
name=dict( |
| 121 |
type='str', |
| 122 |
required=True, |
| 123 |
), |
| 124 |
resource_definition=dict( |
| 125 |
type='str', |
| 126 |
required=False, |
| 127 |
), |
| 128 |
resource_group=dict( |
| 129 |
type='str', |
| 130 |
required=True, |
| 131 |
), |
| 132 |
ml_workspace=dict( |
| 133 |
type='str', |
| 134 |
required=True, |
| 135 |
), |
| 136 |
description=dict( |
| 137 |
type='str', |
| 138 |
required=False, |
| 139 |
), |
| 140 |
state=dict( |
| 141 |
type='str', |
| 142 |
default='present', |
| 143 |
choices=['present', 'absent'], |
| 144 |
), |
| 145 |
) |
| 146 |
|
| 147 |
self._client = None |
| 148 |
self.ml_registry = None |
| 149 |
self.state = None |
| 150 |
|
| 151 |
self.results = dict( |
| 152 |
changed=False, |
| 153 |
compare=[], |
| 154 |
ml_batch_endpoint={} |
| 155 |
) |
| 156 |
|
| 157 |
required_if = [ |
| 158 |
('state', 'present', ['resource_definition']) |
| 159 |
] |
| 160 |
|
| 161 |
super(AzureRMMLBatchEndpoint, self).__init__(self.module_arg_spec, |
| 162 |
supports_check_mode=True, |
| 163 |
required_if=required_if, |
| 164 |
) |
| 165 |
|
| 166 |
def exec_module(self, **kwargs): |
| 167 |
|
| 168 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 169 |
setattr(self, key, kwargs[key]) |
| 170 |
|
| 171 |
changed = False |
| 172 |
|
| 173 |
ml_batch_endpoint_info = self.get(name=self.name) |
| 174 |
|
| 175 |
if self.state == 'present': |
| 176 |
if self.resource_definition: |
| 177 |
resource_definition = io.StringIO(self.resource_definition) |
| 178 |
|
| 179 |
if ml_batch_endpoint_info: |
| 180 |
|
| 181 |
params_override = self.update_params(kwargs, |
| 182 |
self.UPDATE_OVERRIDES, |
| 183 |
ml_batch_endpoint_info) |
| 184 |
|
| 185 |
|
| 186 |
ml_batch_endpoint = load_batch_endpoint(resource_definition, |
| 187 |
params_override=params_override) |
| 188 |
ml_batch_endpoint_info = self.entity_to_dict(ml_batch_endpoint_info) |
| 189 |
changed = not self.default_compare({}, |
| 190 |
self.entity_to_dict(ml_batch_endpoint), |
| 191 |
ml_batch_endpoint_info, |
| 192 |
'', |
| 193 |
self.results) |
| 194 |
if changed and not self.check_mode: |
| 195 |
response = self.client.begin_create_or_update(ml_batch_endpoint) |
| 196 |
ml_batch_endpoint_object = self.get_poller_result(response) |
| 197 |
ml_batch_endpoint_info = self.entity_to_dict(ml_batch_endpoint_object) |
| 198 |
else: |
| 199 |
|
| 200 |
params_override = self.update_params(kwargs, self.PARAM_OVERRIDES) |
| 201 |
|
| 202 |
|
| 203 |
ml_batch_endpoint = load_batch_endpoint(source=resource_definition, |
| 204 |
params_override=params_override) |
| 205 |
|
| 206 |
changed = True |
| 207 |
if not self.check_mode: |
| 208 |
response = self.client.begin_create_or_update(ml_batch_endpoint) |
| 209 |
ml_batch_endpoint_object = self.get_poller_result(response) |
| 210 |
ml_batch_endpoint_info = self.entity_to_dict(ml_batch_endpoint_object) |
| 211 |
elif self.state == 'absent': |
| 212 |
if ml_batch_endpoint_info: |
| 213 |
|
| 214 |
changed = True |
| 215 |
if not self.check_mode: |
| 216 |
response = self.client.batch_endpoints.begin_delete(name=self.name) |
| 217 |
ml_batch_endpoint_info = self.get_poller_result(response) |
| 218 |
|
| 219 |
self.results['ml_batch_endpoint'] = ml_batch_endpoint_info |
| 220 |
self.results['changed'] = changed |
| 221 |
return self.results |
| 222 |
|
| 223 |
def update_params(self, kwargs, param_overrides, ml_batch_endpoint=None): |
| 224 |
params_override = [] |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
if ml_batch_endpoint: |
| 230 |
items = self.entity_to_dict(ml_batch_endpoint).items() |
| 231 |
params_override = [{k: v} for k, v in items] |
| 232 |
|
| 233 |
|
| 234 |
for key in list(self.module_arg_spec.keys()): |
| 235 |
if kwargs[key] is not None and key in param_overrides: |
| 236 |
params_override.append({key: kwargs[key]}) |
| 237 |
return params_override |
| 238 |
|
| 239 |
def get(self, name, as_dict=False): |
| 240 |
try: |
| 241 |
|
| 242 |
ml_batch_endpoint_info = self.client.batch_endpoints.get(name=name) |
| 243 |
if as_dict: |
| 244 |
ml_batch_endpoint_info = self.entity_to_dict(ml_batch_endpoint_info) |
| 245 |
except ResourceNotFoundError: |
| 246 |
ml_batch_endpoint_info = None |
| 247 |
return ml_batch_endpoint_info |
| 248 |
|
| 249 |
|
| 250 |
def main(): |
| 251 |
AzureRMMLBatchEndpoint() |
| 252 |
|
| 253 |
|
| 254 |
if __name__ == '__main__': |
| 255 |
main() |