summaryrefslogtreecommitdiffstats
path: root/test/lib/ansible_test/_internal/cli/parsers/host_config_parsers.py
blob: ee6f146c501ccd1d73abdffb8dce0fa041d56c29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""Composite parsers for the various types of hosts."""
from __future__ import annotations

import typing as t

from ...completion import (
    docker_completion,
    network_completion,
    remote_completion,
    windows_completion,
    filter_completion,
)

from ...host_configs import (
    ControllerConfig,
    DockerConfig,
    NetworkInventoryConfig,
    NetworkRemoteConfig,
    OriginConfig,
    PosixRemoteConfig,
    PosixSshConfig,
    WindowsInventoryConfig,
    WindowsRemoteConfig,
)

from ..compat import (
    get_fallback_remote_controller,
)

from ..argparsing.parsers import (
    ChoicesParser,
    DocumentationState,
    FileParser,
    MatchConditions,
    NamespaceWrappedParser,
    PairParser,
    Parser,
    ParserError,
    ParserState,
)

from .value_parsers import (
    PlatformParser,
    SshConnectionParser,
)

from .key_value_parsers import (
    ControllerKeyValueParser,
    DockerKeyValueParser,
    EmptyKeyValueParser,
    NetworkRemoteKeyValueParser,
    OriginKeyValueParser,
    PosixRemoteKeyValueParser,
    PosixSshKeyValueParser,
    WindowsRemoteKeyValueParser,
)

from .helpers import (
    get_docker_pythons,
    get_remote_pythons,
)


class OriginParser(Parser):
    """Composite argument parser for the origin."""
    def parse(self, state: ParserState) -> t.Any:
        """Parse the input from the given state and return the result."""
        namespace = OriginConfig()

        state.set_namespace(namespace)

        parser = OriginKeyValueParser()
        parser.parse(state)

        return namespace

    def document(self, state: DocumentationState) -> t.Optional[str]:
        """Generate and return documentation for this parser."""
        return OriginKeyValueParser().document(state)


class ControllerParser(Parser):
    """Composite argument parser for the controller."""
    def parse(self, state: ParserState) -> t.Any:
        """Parse the input from the given state and return the result."""
        namespace = ControllerConfig()

        state.set_namespace(namespace)

        parser = ControllerKeyValueParser()
        parser.parse(state)

        return namespace

    def document(self, state: DocumentationState) -> t.Optional[str]:
        """Generate and return documentation for this parser."""
        return ControllerKeyValueParser().document(state)


class DockerParser(PairParser):
    """Composite argument parser for a docker host."""
    def __init__(self, controller: bool) -> None:
        self.controller = controller

    def create_namespace(self) -> t.Any:
        """Create and return a namespace."""
        return DockerConfig()

    def get_left_parser(self, state: ParserState) -> Parser:
        """Return the parser for the left side."""
        return NamespaceWrappedParser('name', ChoicesParser(list(filter_completion(docker_completion(), controller_only=self.controller)),
                                                            conditions=MatchConditions.CHOICE | MatchConditions.ANY))

    def get_right_parser(self, choice: t.Any) -> Parser:
        """Return the parser for the right side."""
        return DockerKeyValueParser(choice, self.controller)

    def parse(self, state: ParserState) -> t.Any:
        """Parse the input from the given state and return the result."""
        value: DockerConfig = super().parse(state)

        if not value.python and not get_docker_pythons(value.name, self.controller, True):
            raise ParserError(f'Python version required for docker image: {value.name}')

        return value

    def document(self, state: DocumentationState) -> t.Optional[str]:
        """Generate and return documentation for this parser."""
        default = 'default'
        content = '\n'.join([f'  {image} ({", ".join(get_docker_pythons(image, self.controller, False))})'
                             for image, item in filter_completion(docker_completion(), controller_only=self.controller).items()])

        content += '\n'.join([
            '',
            '  {image}  # python must be specified for custom images',
        ])

        state.sections[f'{"controller" if self.controller else "target"} docker images and supported python version (choose one):'] = content

        return f'{{image}}[,{DockerKeyValueParser(default, self.controller).document(state)}]'


class PosixRemoteParser(PairParser):
    """Composite argument parser for a POSIX remote host."""
    def __init__(self, controller: bool) -> None:
        self.controller = controller

    def create_namespace(self) -> t.Any:
        """Create and return a namespace."""
        return PosixRemoteConfig()

    def get_left_parser(self, state: ParserState) -> Parser:
        """Return the parser for the left side."""
        return NamespaceWrappedParser('name', PlatformParser(list(filter_completion(remote_completion(), controller_only=self.controller))))

    def get_right_parser(self, choice: t.Any) -> Parser:
        """Return the parser for the right side."""
        return PosixRemoteKeyValueParser(choice, self.controller)

    def parse(self, state: ParserState) -> t.Any:
        """Parse the input from the given state and return the result."""
        value: PosixRemoteConfig = super().parse(state)

        if not value.python and not get_remote_pythons(value.name, self.controller, True):
            raise ParserError(f'Python version required for remote: {value.name}')

        return value

    def document(self, state: DocumentationState) -> t.Optional[str]:
        """Generate and return documentation for this parser."""
        default = get_fallback_remote_controller()
        content = '\n'.join([f'  {name} ({", ".join(get_remote_pythons(name, self.controller, False))})'
                             for name, item in filter_completion(remote_completion(), controller_only=self.controller).items()])

        content += '\n'.join([
            '',
            '  {platform}/{version}  # python must be specified for unknown systems',
        ])

        state.sections[f'{"controller" if self.controller else "target"} remote systems and supported python versions (choose one):'] = content

        return f'{{system}}[,{PosixRemoteKeyValueParser(default, self.controller).document(state)}]'


class WindowsRemoteParser(PairParser):
    """Composite argument parser for a Windows remote host."""
    def create_namespace(self) -> t.Any:
        """Create and return a namespace."""
        return WindowsRemoteConfig()

    def get_left_parser(self, state: ParserState) -> Parser:
        """Return the parser for the left side."""
        names = list(filter_completion(windows_completion()))

        for target in state.root_namespace.targets or []:  # type: WindowsRemoteConfig
            names.remove(target.name)

        return NamespaceWrappedParser('name', PlatformParser(names))

    def get_right_parser(self, choice: t.Any) -> Parser:
        """Return the parser for the right side."""
        return WindowsRemoteKeyValueParser()

    def document(self, state: DocumentationState) -> t.Optional[str]:
        """Generate and return documentation for this parser."""
        content = '\n'.join([f'  {name}' for name, item in filter_completion(windows_completion()).items()])

        content += '\n'.join([
            '',
            '  windows/{version}  # use an unknown windows version',
        ])

        state.sections['target remote systems (choose one):'] = content

        return f'{{system}}[,{WindowsRemoteKeyValueParser().document(state)}]'


class NetworkRemoteParser(PairParser):
    """Composite argument parser for a network remote host."""
    def create_namespace(self) -> t.Any:
        """Create and return a namespace."""
        return NetworkRemoteConfig()

    def get_left_parser(self, state: ParserState) -> Parser:
        """Return the parser for the left side."""
        names = list(filter_completion(network_completion()))

        for target in state.root_namespace.targets or []:  # type: NetworkRemoteConfig
            names.remove(target.name)

        return NamespaceWrappedParser('name', PlatformParser(names))

    def get_right_parser(self, choice: t.Any) -> Parser:
        """Return the parser for the right side."""
        return NetworkRemoteKeyValueParser()

    def document(self, state: DocumentationState) -> t.Optional[str]:
        """Generate and return documentation for this parser."""
        content = '\n'.join([f'  {name}' for name, item in filter_completion(network_completion()).items()])

        content += '\n'.join([
            '',
            '  {platform}/{version}  # use an unknown platform and version',
        ])

        state.sections['target remote systems (choose one):'] = content

        return f'{{system}}[,{NetworkRemoteKeyValueParser().document(state)}]'


class WindowsInventoryParser(PairParser):
    """Composite argument parser for a Windows inventory."""
    def create_namespace(self) -> t.Any:
        """Create and return a namespace."""
        return WindowsInventoryConfig()

    def get_left_parser(self, state: ParserState) -> Parser:
        """Return the parser for the left side."""
        return NamespaceWrappedParser('path', FileParser())

    def get_right_parser(self, choice: t.Any) -> Parser:
        """Return the parser for the right side."""
        return EmptyKeyValueParser()

    def document(self, state: DocumentationState) -> t.Optional[str]:
        """Generate and return documentation for this parser."""
        return '{path}  # INI format inventory file'


class NetworkInventoryParser(PairParser):
    """Composite argument parser for a network inventory."""
    def create_namespace(self) -> t.Any:
        """Create and return a namespace."""
        return NetworkInventoryConfig()

    def get_left_parser(self, state: ParserState) -> Parser:
        """Return the parser for the left side."""
        return NamespaceWrappedParser('path', FileParser())

    def get_right_parser(self, choice: t.Any) -> Parser:
        """Return the parser for the right side."""
        return EmptyKeyValueParser()

    def document(self, state: DocumentationState) -> t.Optional[str]:
        """Generate and return documentation for this parser."""
        return '{path}  # INI format inventory file'


class PosixSshParser(PairParser):
    """Composite argument parser for a POSIX SSH host."""
    def create_namespace(self) -> t.Any:
        """Create and return a namespace."""
        return PosixSshConfig()

    def get_left_parser(self, state: ParserState) -> Parser:
        """Return the parser for the left side."""
        return SshConnectionParser()

    def get_right_parser(self, choice: t.Any) -> Parser:
        """Return the parser for the right side."""
        return PosixSshKeyValueParser()

    @property
    def required(self) -> bool:
        """True if the delimiter (and thus right parser) is required, otherwise False."""
        return True

    def document(self, state: DocumentationState) -> t.Optional[str]:
        """Generate and return documentation for this parser."""
        return f'{SshConnectionParser().document(state)}[,{PosixSshKeyValueParser().document(state)}]'