Debian Codegraph @

ansible_collections/community/general/plugins/modules/scaleway_container_info.py

raw
1 #!/usr/bin/python
2 #
3 # Scaleway Serverless container 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_info
13 short_description: Retrieve information on Scaleway Container
14 version_added: 6.0.0
15 author: Guillaume MARTINEZ (@Lunik)
16 description:
17 - This module return information about a container 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 container.
49 required: true
50 """
51
52 EXAMPLES = r"""
53 - name: Get a container info
54 community.general.scaleway_container_info:
55 namespace_id: '{{ scw_container_namespace }}'
56 region: fr-par
57 name: my-awesome-container
58 register: container_info_task
59 """
60
61 RETURN = r"""
62 container:
63 description: The container information.
64 returned: always
65 type: dict
66 sample:
67 cpu_limit: 140
68 description: Container used for testing scaleway_container ansible module
69 domain_name: cnansibletestgfogtjod-cn-ansible-test.functions.fnc.fr-par.scw.cloud
70 environment_variables:
71 MY_VAR: my_value
72 error_message: null
73 http_option: ""
74 id: c9070eb0-d7a4-48dd-9af3-4fb139890721
75 max_concurrency: 50
76 max_scale: 5
77 memory_limit: 256
78 min_scale: 0
79 name: cn-ansible-test
80 namespace_id: 75e299f1-d1e5-4e6b-bc6e-4fb51cfe1e69
81 port: 80
82 privacy: public
83 protocol: http1
84 region: fr-par
85 registry_image: rg.fr-par.scw.cloud/namespace-ansible-ci/nginx:latest
86 secret_environment_variables:
87 - key: MY_SECRET_VAR
88 value: $argon2id$v=19$m=65536,t=1,p=2$tb6UwSPWx/rH5Vyxt9Ujfw$5ZlvaIjWwNDPxD9Rdght3NarJz4IETKjpvAU3mMSmFg
89 status: created
90 timeout: 300s
91 """
92
93 from ansible.module_utils.basic import AnsibleModule
94
95 from ansible_collections.community.general.plugins.module_utils._scaleway import (
96 SCALEWAY_REGIONS,
97 Scaleway,
98 scaleway_argument_spec,
99 )
100
101
102 def info_strategy(api, wished_cn):
103 cn_list = api.fetch_all_resources("containers")
104 cn_lookup = {cn["name"]: cn for cn in cn_list}
105
106 if wished_cn["name"] not in cn_lookup:
107 msg = f"Error during container lookup: Unable to find container named '{wished_cn['name']}' in namespace '{wished_cn['namespace_id']}'"
108
109 api.module.fail_json(msg=msg)
110
111 target_cn = cn_lookup[wished_cn["name"]]
112
113 response = api.get(path=f"{api.api_path}/{target_cn['id']}")
114 if not response.ok:
115 msg = f"Error during container lookup: {response.info['msg']}: '{response.json['message']}' ({response.json})"
116 api.module.fail_json(msg=msg)
117
118 return response.json
119
120
121 def core(module):
122 region = module.params["region"]
123 wished_container = {"namespace_id": module.params["namespace_id"], "name": module.params["name"]}
124
125 api = Scaleway(module=module)
126 api.api_path = f"containers/v1beta1/regions/{region}/containers"
127
128 summary = info_strategy(api=api, wished_cn=wished_container)
129
130 module.exit_json(changed=False, container=summary)
131
132
133 def main():
134 argument_spec = scaleway_argument_spec()
135 argument_spec.update(
136 dict(
137 namespace_id=dict(type="str", required=True),
138 region=dict(type="str", required=True, choices=SCALEWAY_REGIONS),
139 name=dict(type="str", required=True),
140 )
141 )
142 module = AnsibleModule(
143 argument_spec=argument_spec,
144 supports_check_mode=True,
145 )
146
147 core(module)
148
149
150 if __name__ == "__main__":
151 main()