| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Scaleway Serverless container registry 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_container_registry_info |
| 13 |
short_description: Scaleway Container registry info module |
| 14 |
version_added: 5.8.0 |
| 15 |
author: Guillaume MARTINEZ (@Lunik) |
| 16 |
description: |
| 17 |
- This module return information about a container registry 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 container registry. |
| 49 |
required: true |
| 50 |
""" |
| 51 |
|
| 52 |
EXAMPLES = r""" |
| 53 |
- name: Get a container registry info |
| 54 |
community.general.scaleway_container_registry_info: |
| 55 |
project_id: '{{ scw_project }}' |
| 56 |
region: fr-par |
| 57 |
name: my-awesome-container-registry |
| 58 |
register: container_registry_info_task |
| 59 |
""" |
| 60 |
|
| 61 |
RETURN = r""" |
| 62 |
container_registry: |
| 63 |
description: The container registry information. |
| 64 |
returned: always |
| 65 |
type: dict |
| 66 |
sample: |
| 67 |
created_at: "2022-10-14T09:51:07.949716Z" |
| 68 |
description: Managed by Ansible |
| 69 |
endpoint: rg.fr-par.scw.cloud/my-awesome-registry |
| 70 |
id: 0d7d5270-7864-49c2-920b-9fd6731f3589 |
| 71 |
image_count: 0 |
| 72 |
is_public: false |
| 73 |
name: my-awesome-registry |
| 74 |
organization_id: 10697b59-5c34-4d24-8d15-9ff2d3b89f58 |
| 75 |
project_id: 3da4f0b2-06be-4773-8ec4-5dfa435381be |
| 76 |
region: fr-par |
| 77 |
size: 0 |
| 78 |
status: ready |
| 79 |
status_message: "" |
| 80 |
updated_at: "2022-10-14T09:51:07.949716Z" |
| 81 |
""" |
| 82 |
|
| 83 |
from ansible.module_utils.basic import AnsibleModule |
| 84 |
|
| 85 |
from ansible_collections.community.general.plugins.module_utils._scaleway import ( |
| 86 |
SCALEWAY_REGIONS, |
| 87 |
Scaleway, |
| 88 |
scaleway_argument_spec, |
| 89 |
) |
| 90 |
|
| 91 |
|
| 92 |
def info_strategy(api, wished_cn): |
| 93 |
cn_list = api.fetch_all_resources("namespaces") |
| 94 |
cn_lookup = {cn["name"]: cn for cn in cn_list} |
| 95 |
|
| 96 |
if wished_cn["name"] not in cn_lookup: |
| 97 |
msg = f"Error during container registries lookup: Unable to find container registry named '{wished_cn['name']}' in project '{wished_cn['project_id']}'" |
| 98 |
|
| 99 |
api.module.fail_json(msg=msg) |
| 100 |
|
| 101 |
target_cn = cn_lookup[wished_cn["name"]] |
| 102 |
|
| 103 |
response = api.get(path=f"{api.api_path}/{target_cn['id']}") |
| 104 |
if not response.ok: |
| 105 |
msg = f"Error during container registry lookup: {response.info['msg']}: '{response.json['message']}' ({response.json})" |
| 106 |
api.module.fail_json(msg=msg) |
| 107 |
|
| 108 |
return response.json |
| 109 |
|
| 110 |
|
| 111 |
def core(module): |
| 112 |
region = module.params["region"] |
| 113 |
wished_container_namespace = {"project_id": module.params["project_id"], "name": module.params["name"]} |
| 114 |
|
| 115 |
api = Scaleway(module=module) |
| 116 |
api.api_path = f"registry/v1/regions/{region}/namespaces" |
| 117 |
|
| 118 |
summary = info_strategy(api=api, wished_cn=wished_container_namespace) |
| 119 |
|
| 120 |
module.exit_json(changed=False, container_registry=summary) |
| 121 |
|
| 122 |
|
| 123 |
def main(): |
| 124 |
argument_spec = scaleway_argument_spec() |
| 125 |
argument_spec.update( |
| 126 |
dict( |
| 127 |
project_id=dict(type="str", required=True), |
| 128 |
region=dict(type="str", required=True, choices=SCALEWAY_REGIONS), |
| 129 |
name=dict(type="str", required=True), |
| 130 |
) |
| 131 |
) |
| 132 |
module = AnsibleModule( |
| 133 |
argument_spec=argument_spec, |
| 134 |
supports_check_mode=True, |
| 135 |
) |
| 136 |
|
| 137 |
core(module) |
| 138 |
|
| 139 |
|
| 140 |
if __name__ == "__main__": |
| 141 |
main() |