| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Scaleway Serverless function info module |
| 4 |
# |
| 5 |
# Copyright (c) 2022, Guillaume MARTINEZ <[email protected]> |
| 6 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
module: scaleway_function_info |
| 13 |
short_description: Retrieve information on Scaleway Function |
| 14 |
version_added: 6.0.0 |
| 15 |
author: Guillaume MARTINEZ (@Lunik) |
| 16 |
description: |
| 17 |
- This module return information about a function on Scaleway account. |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._scaleway |
| 20 |
- community.general._attributes |
| 21 |
- community.general._scaleway.actiongroup_scaleway |
| 22 |
- community.general._attributes.info_module |
| 23 |
|
| 24 |
attributes: |
| 25 |
action_group: |
| 26 |
version_added: 11.3.0 |
| 27 |
|
| 28 |
options: |
| 29 |
namespace_id: |
| 30 |
type: str |
| 31 |
description: |
| 32 |
- Container namespace identifier. |
| 33 |
required: true |
| 34 |
|
| 35 |
region: |
| 36 |
type: str |
| 37 |
description: |
| 38 |
- Scaleway region to use (for example C(fr-par)). |
| 39 |
required: true |
| 40 |
choices: |
| 41 |
- fr-par |
| 42 |
- nl-ams |
| 43 |
- pl-waw |
| 44 |
|
| 45 |
name: |
| 46 |
type: str |
| 47 |
description: |
| 48 |
- Name of the function. |
| 49 |
required: true |
| 50 |
""" |
| 51 |
|
| 52 |
EXAMPLES = r""" |
| 53 |
- name: Get a function info |
| 54 |
community.general.scaleway_function_info: |
| 55 |
namespace_id: '{{ scw_function_namespace }}' |
| 56 |
region: fr-par |
| 57 |
name: my-awesome-function |
| 58 |
register: function_info_task |
| 59 |
""" |
| 60 |
|
| 61 |
RETURN = r""" |
| 62 |
function: |
| 63 |
description: The function information. |
| 64 |
returned: always |
| 65 |
type: dict |
| 66 |
sample: |
| 67 |
cpu_limit: 140 |
| 68 |
description: Function used for testing scaleway_function ansible module |
| 69 |
domain_name: fnansibletestfxamabuc-fn-ansible-test.functions.fnc.fr-par.scw.cloud |
| 70 |
environment_variables: |
| 71 |
MY_VAR: my_value |
| 72 |
error_message: |
| 73 |
handler: handler.handle |
| 74 |
http_option: "" |
| 75 |
id: ceb64dc4-4464-4196-8e20-ecef705475d3 |
| 76 |
max_scale: 5 |
| 77 |
memory_limit: 256 |
| 78 |
min_scale: 0 |
| 79 |
name: fn-ansible-test |
| 80 |
namespace_id: 82737d8d-0ebb-4d89-b0ad-625876eca50d |
| 81 |
privacy: public |
| 82 |
region: fr-par |
| 83 |
runtime: python310 |
| 84 |
runtime_message: "" |
| 85 |
secret_environment_variables: |
| 86 |
- key: MY_SECRET_VAR |
| 87 |
value: $argon2id$v=19$m=65536,t=1,p=2$tb6UwSPWx/rH5Vyxt9Ujfw$5ZlvaIjWwNDPxD9Rdght3NarJz4IETKjpvAU3mMSmFg |
| 88 |
status: created |
| 89 |
timeout: 300s |
| 90 |
""" |
| 91 |
|
| 92 |
from ansible.module_utils.basic import AnsibleModule |
| 93 |
|
| 94 |
from ansible_collections.community.general.plugins.module_utils._scaleway import ( |
| 95 |
SCALEWAY_REGIONS, |
| 96 |
Scaleway, |
| 97 |
scaleway_argument_spec, |
| 98 |
) |
| 99 |
|
| 100 |
|
| 101 |
def info_strategy(api, wished_fn): |
| 102 |
fn_list = api.fetch_all_resources("functions") |
| 103 |
fn_lookup = {fn["name"]: fn for fn in fn_list} |
| 104 |
|
| 105 |
if wished_fn["name"] not in fn_lookup: |
| 106 |
msg = f"Error during function lookup: Unable to find function named '{wished_fn['name']}' in namespace '{wished_fn['namespace_id']}'" |
| 107 |
|
| 108 |
api.module.fail_json(msg=msg) |
| 109 |
|
| 110 |
target_fn = fn_lookup[wished_fn["name"]] |
| 111 |
|
| 112 |
response = api.get(path=f"{api.api_path}/{target_fn['id']}") |
| 113 |
if not response.ok: |
| 114 |
msg = f"Error during function lookup: {response.info['msg']}: '{response.json['message']}' ({response.json})" |
| 115 |
api.module.fail_json(msg=msg) |
| 116 |
|
| 117 |
return response.json |
| 118 |
|
| 119 |
|
| 120 |
def core(module): |
| 121 |
region = module.params["region"] |
| 122 |
wished_function = {"namespace_id": module.params["namespace_id"], "name": module.params["name"]} |
| 123 |
|
| 124 |
api = Scaleway(module=module) |
| 125 |
api.api_path = f"functions/v1beta1/regions/{region}/functions" |
| 126 |
|
| 127 |
summary = info_strategy(api=api, wished_fn=wished_function) |
| 128 |
|
| 129 |
module.exit_json(changed=False, function=summary) |
| 130 |
|
| 131 |
|
| 132 |
def main(): |
| 133 |
argument_spec = scaleway_argument_spec() |
| 134 |
argument_spec.update( |
| 135 |
dict( |
| 136 |
namespace_id=dict(type="str", required=True), |
| 137 |
region=dict(type="str", required=True, choices=SCALEWAY_REGIONS), |
| 138 |
name=dict(type="str", required=True), |
| 139 |
) |
| 140 |
) |
| 141 |
module = AnsibleModule( |
| 142 |
argument_spec=argument_spec, |
| 143 |
supports_check_mode=True, |
| 144 |
) |
| 145 |
|
| 146 |
core(module) |
| 147 |
|
| 148 |
|
| 149 |
if __name__ == "__main__": |
| 150 |
main() |