Debian Codegraph @

ansible_collections/community/docker/plugins/modules/docker_volume_info.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright 2017 Red Hat | Ansible, Alex Grönholm <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: docker_volume_info
11 short_description: Retrieve facts about Docker volumes
12 description:
13 - Performs largely the same function as the C(docker volume inspect) CLI subcommand.
14 extends_documentation_fragment:
15 - community.docker._docker.api_documentation
16 - community.docker._attributes
17 - community.docker._attributes.actiongroup_docker
18 - community.docker._attributes.info_module
19 - community.docker._attributes.idempotent_not_modify_state
20
21 options:
22 name:
23 description:
24 - Name of the volume to inspect.
25 type: str
26 required: true
27 aliases:
28 - volume_name
29
30 author:
31 - Felix Fontein (@felixfontein)
32
33 requirements:
34 - "Docker API >= 1.25"
35 """
36
37 EXAMPLES = r"""
38 ---
39 - name: Get infos on volume
40 community.docker.docker_volume_info:
41 name: mydata
42 register: result
43
44 - name: Does volume exist?
45 ansible.builtin.debug:
46 msg: "The volume {{ 'exists' if result.exists else 'does not exist' }}"
47
48 - name: Print information about volume
49 ansible.builtin.debug:
50 var: result.volume
51 when: result.exists
52 """
53
54 RETURN = r"""
55 exists:
56 description:
57 - Returns whether the volume exists.
58 type: bool
59 returned: always
60 sample: true
61 volume:
62 description:
63 - Volume inspection results for the affected volume.
64 - Will be V(none) if volume does not exist.
65 returned: success
66 type: dict
67 sample: '{ "CreatedAt": "2018-12-09T17:43:44+01:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/ansible-test-bd3f6172/_data",
68 "Name": "ansible-test-bd3f6172", "Options": {}, "Scope": "local" }'
69 """
70
71 import traceback
72 import typing as t
73
74 from ansible_collections.community.docker.plugins.module_utils._api.errors import (
75 DockerException,
76 NotFound,
77 )
78 from ansible_collections.community.docker.plugins.module_utils._common_api import (
79 AnsibleDockerClient,
80 RequestException,
81 )
82
83
84 def get_existing_volume(
85 client: AnsibleDockerClient, volume_name: str
86 ) -> dict[str, t.Any] | None:
87 try:
88 return client.get_json("/volumes/{0}", volume_name)
89 except NotFound:
90 return None
91 except Exception as exc: # pylint: disable=broad-exception-caught
92 client.fail(f"Error inspecting volume: {exc}")
93
94
95 def main() -> None:
96 argument_spec = {
97 "name": {"type": "str", "required": True, "aliases": ["volume_name"]},
98 }
99
100 client = AnsibleDockerClient(
101 argument_spec=argument_spec,
102 supports_check_mode=True,
103 )
104
105 try:
106 volume = get_existing_volume(client, client.module.params["name"])
107
108 client.module.exit_json(
109 changed=False,
110 exists=bool(volume),
111 volume=volume,
112 )
113 except DockerException as e:
114 client.fail(
115 f"An unexpected Docker error occurred: {e}",
116 exception=traceback.format_exc(),
117 )
118 except RequestException as e:
119 client.fail(
120 f"An unexpected requests error occurred when trying to talk to the Docker daemon: {e}",
121 exception=traceback.format_exc(),
122 )
123
124
125 if __name__ == "__main__":
126 main()