Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
ML Engine: Versions on Ansible (#315)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and rambleraptor committed Jul 17, 2019
1 parent 4e8fe73 commit ad4b74b
Show file tree
Hide file tree
Showing 7 changed files with 1,047 additions and 2 deletions.
42 changes: 40 additions & 2 deletions lib/ansible/modules/cloud/google/gcp_mlengine_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@

from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest, remove_nones_from_dict, replace_resource_dict
import json
import time

################################################################################
# Main
Expand Down Expand Up @@ -226,7 +227,7 @@ def update(module, link):

def delete(module, link):
auth = GcpSession(module, 'mlengine')
return return_if_object(module, auth.delete(link))
return wait_for_operation(module, auth.delete(link))


def resource_to_request(module):
Expand Down Expand Up @@ -316,10 +317,47 @@ def response_to_hash(module, response):
}


def async_op_url(module, extra_data=None):
if extra_data is None:
extra_data = {}
url = "https://ml.googleapis.com/v1/{op_id}"
combined = extra_data.copy()
combined.update(module.params)
return url.format(**combined)


def wait_for_operation(module, response):
op_result = return_if_object(module, response)
if op_result is None:
return {}
status = navigate_hash(op_result, ['done'])
wait_done = wait_for_completion(status, op_result, module)
raise_if_errors(wait_done, ['error'], module)
return navigate_hash(wait_done, ['response'])


def wait_for_completion(status, op_result, module):
op_id = navigate_hash(op_result, ['name'])
op_uri = async_op_url(module, {'op_id': op_id})
while not status:
raise_if_errors(op_result, ['error'], module)
time.sleep(1.0)
op_result = fetch_resource(module, op_uri, False)
status = navigate_hash(op_result, ['done'])
return op_result


def raise_if_errors(response, err_path, module):
errors = navigate_hash(response, err_path)
if errors is not None:
module.fail_json(msg=errors)


# Short names are given (and expected) by the API
# but are returned as full names.
def decode_response(response, module):
response['name'] = response['name'].split('/')[-1]
if 'name' in response and 'metadata' not in response:
response['name'] = response['name'].split('/')[-1]
return response


Expand Down
Loading

0 comments on commit ad4b74b

Please sign in to comment.