Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion plugins/module_utils/gcp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fnmatch
import os
import json
import pprint
import time
import typing as T

Expand Down Expand Up @@ -701,11 +702,13 @@ def get(self, link: str, allow_not_found: bool = True) -> T.Optional[dict]:
Make GET request.
"""

debug(self.module, method="get", link=link)
return self.if_object(self.session().get(link), allow_not_found)

def wait_for_op(self, op_url: str, retries: int) -> T.Optional[NestedDict]:
"Retry the given number of times for an async operation to succeed"

debug(self.module, msg="Waiting for async op", op_url=op_url, retries=retries)
for retry in range(1, retries):
op = self.session().get(op_url)
op_obj = self.if_object(op, allow_not_found=False)
Expand All @@ -714,6 +717,7 @@ def wait_for_op(self, op_url: str, retries: int) -> T.Optional[NestedDict]:
if done:
return op_obj["response"]

debug(self.module, op_url=op_url, retry=retry)
time.sleep(1.0) # TODO: should we relax the check?

self.module.fail_json(msg="Failed to poll for async op completion")
Expand All @@ -724,7 +728,7 @@ def async_op(self, op_func: T.Callable, link: str, async_link: str, retries: int
op_result: T.Optional[NestedDict] = op_func(link)

op_id: str = navigate_hash(op_result, ["name"], "")
params: NestedDict = self.module.params
params: NestedDict = self.module.params.copy()
params.update({"op_id": op_id})
op_url: str = async_link.format(**params)

Expand All @@ -733,6 +737,7 @@ def async_op(self, op_func: T.Callable, link: str, async_link: str, retries: int
def post_async(self, link: str, async_link: str, retries: int) -> T.Optional[NestedDict]:
"Perform an asynchronous post"

debug(self.module, method="post_async", link=link, async_link=async_link)
return self.async_op(
op_func=self.post,
link=link,
Expand All @@ -743,6 +748,7 @@ def post_async(self, link: str, async_link: str, retries: int) -> T.Optional[Nes
def put_async(self, link: str, async_link: str, retries: int) -> T.Optional[NestedDict]:
"Perform an asynchronous put"

debug(self.module, method="put_async", link=link, async_link=async_link)
return self.async_op(
op_func=self.put,
link=link,
Expand All @@ -753,6 +759,7 @@ def put_async(self, link: str, async_link: str, retries: int) -> T.Optional[Nest
def patch_async(self, link: str, async_link: str, retries: int) -> T.Optional[NestedDict]:
"Perform an asynchronous patch"

debug(self.module, method="patch_async", link=link, async_link=async_link)
return self.async_op(
op_func=self.patch,
link=link,
Expand All @@ -763,6 +770,7 @@ def patch_async(self, link: str, async_link: str, retries: int) -> T.Optional[Ne
def delete_async(self, link: str, async_link: str, retries: int) -> T.Optional[NestedDict]:
"Perform an asynchronous delete"

debug(self.module, method="delete_async", link=link, async_link=async_link)
return self.async_op(
op_func=self.delete,
link=link,
Expand All @@ -782,6 +790,7 @@ def post(self, link) -> T.Optional[NestedDict]:
Make POST request.
"""

debug(self.module, method="post", link=link)
return self.with_kind(
self.if_object(
self.session().post(
Expand All @@ -796,6 +805,7 @@ def put(self, link) -> T.Optional[NestedDict]:
Make PUT request.
"""

debug(self.module, method="put", link=link)
return self.with_kind(
self.if_object(
self.session().put(
Expand All @@ -810,6 +820,7 @@ def patch(self, link) -> T.Optional[NestedDict]:
Make PATCH request
"""

debug(self.module, method="patch", link=link)
return self.with_kind(
self.if_object(
self.session().patch(
Expand All @@ -824,6 +835,7 @@ def delete(self, link) -> T.Optional[NestedDict]:
Make DELETE request.
"""

debug(self.module, method="delete", link=link)
return self.if_object(self.session().delete(link))

def diff(self, response: NestedDict) -> bool:
Expand All @@ -844,3 +856,11 @@ def dot_fields(self) -> T.List[str]:
"tags.*",
]
return flatten_nested_dict(self.to_request() or {}, separator=".", glob_excludes=exclusions)


def debug(module: AnsibleModule | None, **kwargs) -> None:
"Prints debugging output using module logging"

if module is not None:
if module._verbosity >= 3:
module.log(pprint.saferepr(kwargs))
Loading
Loading