| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Scaleway Serverless function namespace 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_namespace_info |
| 13 |
short_description: Retrieve information on Scaleway Function namespace |
| 14 |
version_added: 6.0.0 |
| 15 |
author: Guillaume MARTINEZ (@Lunik) |
| 16 |
description: |
| 17 |
- This module return information about a function namespace 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 |
project_id: |
| 30 |
type: str |
| 31 |
description: |
| 32 |
- Project 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 namespace. |
| 49 |
required: true |
| 50 |
""" |
| 51 |
|
| 52 |
EXAMPLES = r""" |
| 53 |
- name: Get a function namespace info |
| 54 |
community.general.scaleway_function_namespace_info: |
| 55 |
project_id: '{{ scw_project }}' |
| 56 |
region: fr-par |
| 57 |
name: my-awesome-function-namespace |
| 58 |
register: function_namespace_info_task |
| 59 |
""" |
| 60 |
|
| 61 |
RETURN = r""" |
| 62 |
function_namespace: |
| 63 |
description: The function namespace information. |
| 64 |
returned: always |
| 65 |
type: dict |
| 66 |
sample: |
| 67 |
description: "" |
| 68 |
environment_variables: |
| 69 |
MY_VAR: my_value |
| 70 |
error_message: |
| 71 |
id: 531a1fd7-98d2-4a74-ad77-d398324304b8 |
| 72 |
name: my-awesome-function-namespace |
| 73 |
organization_id: e04e3bdc-015c-4514-afde-9389e9be24b0 |
| 74 |
project_id: d44cea58-dcb7-4c95-bff1-1105acb60a98 |
| 75 |
region: fr-par |
| 76 |
registry_endpoint: "" |
| 77 |
registry_namespace_id: "" |
| 78 |
secret_environment_variables: |
| 79 |
- key: MY_SECRET_VAR |
| 80 |
value: $argon2id$v=19$m=65536,t=1,p=2$tb6UwSPWx/rH5Vyxt9Ujfw$5ZlvaIjWwNDPxD9Rdght3NarJz4IETKjpvAU3mMSmFg |
| 81 |
status: pending |
| 82 |
""" |
| 83 |
|
| 84 |
from ansible.module_utils.basic import AnsibleModule |
| 85 |
|
| 86 |
from ansible_collections.community.general.plugins.module_utils._scaleway import ( |
| 87 |
SCALEWAY_REGIONS, |
| 88 |
Scaleway, |
| 89 |
scaleway_argument_spec, |
| 90 |
) |
| 91 |
|
| 92 |
|
| 93 |
def info_strategy(api, wished_fn): |
| 94 |
fn_list = api.fetch_all_resources("namespaces") |
| 95 |
fn_lookup = {fn["name"]: fn for fn in fn_list} |
| 96 |
|
| 97 |
if wished_fn["name"] not in fn_lookup: |
| 98 |
msg = f"Error during function namespace lookup: Unable to find function namespace named '{wished_fn['name']}' in project '{wished_fn['project_id']}'" |
| 99 |
|
| 100 |
api.module.fail_json(msg=msg) |
| 101 |
|
| 102 |
target_fn = fn_lookup[wished_fn["name"]] |
| 103 |
|
| 104 |
response = api.get(path=f"{api.api_path}/{target_fn['id']}") |
| 105 |
if not response.ok: |
| 106 |
msg = f"Error during function namespace lookup: {response.info['msg']}: '{response.json['message']}' ({response.json})" |
| 107 |
api.module.fail_json(msg=msg) |
| 108 |
|
| 109 |
return response.json |
| 110 |
|
| 111 |
|
| 112 |
def core(module): |
| 113 |
region = module.params["region"] |
| 114 |
wished_function_namespace = {"project_id": module.params["project_id"], "name": module.params["name"]} |
| 115 |
|
| 116 |
api = Scaleway(module=module) |
| 117 |
api.api_path = f"functions/v1beta1/regions/{region}/namespaces" |
| 118 |
|
| 119 |
summary = info_strategy(api=api, wished_fn=wished_function_namespace) |
| 120 |
|
| 121 |
module.exit_json(changed=False, function_namespace=summary) |
| 122 |
|
| 123 |
|
| 124 |
def main(): |
| 125 |
argument_spec = scaleway_argument_spec() |
| 126 |
argument_spec.update( |
| 127 |
dict( |
| 128 |
project_id=dict(type="str", required=True), |
| 129 |
region=dict(type="str", required=True, choices=SCALEWAY_REGIONS), |
| 130 |
name=dict(type="str", required=True), |
| 131 |
) |
| 132 |
) |
| 133 |
module = AnsibleModule( |
| 134 |
argument_spec=argument_spec, |
| 135 |
supports_check_mode=True, |
| 136 |
) |
| 137 |
|
| 138 |
core(module) |
| 139 |
|
| 140 |
|
| 141 |
if __name__ == "__main__": |
| 142 |
main() |