-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsupport_tunnel.py
More file actions
156 lines (134 loc) · 4.54 KB
/
support_tunnel.py
File metadata and controls
156 lines (134 loc) · 4.54 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2023, XLAB Steampunk <steampunk@xlab.si>
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
__metaclass__ = type
# language=yaml
DOCUMENTATION = r"""
module: support_tunnel
author:
- Polona Mihalič (@PolonaM)
short_description: Opens or closes remote support tunnel.
description:
- Opens or closes remote support tunnel on the node.
version_added: 1.2.0
extends_documentation_fragment:
- scale_computing.hypercore.cluster_instance
seealso: []
options:
state:
description:
- The desired state of the remote support tunnel.
- If C(state) is I(absent) the remote support tunnel will be closed.
- If C(state) is I(present) the remote support tunnel will be opened.
type: str
choices: [ present, absent ]
required: true
code:
description:
- Code from support used to open remote support tunnel.
- Relevant only if C(state) is I(present).
type: int
notes:
- C(check_mode) is not supported.
"""
# language=yaml
EXAMPLES = r"""
- name: Open support tunnel
scale_computing.hypercore.support_tunnel:
state: present
code: 4422
- name: Close support tunnel
scale_computing.hypercore.support_tunnel:
state: absent
"""
# language=yaml
RETURN = r"""
record:
description:
- Support tunnel status.
returned: success
type: dict
sample:
open: true
code: 4422
"""
# validate-modules does not agree with "from __future__ import annotations"
from typing import Tuple
from ansible.module_utils.basic import AnsibleModule
from ..module_utils import arguments
from ..module_utils import errors
from ..module_utils.client import Client
from ..module_utils.support_tunnel import SupportTunnel
from ..module_utils.typed_classes import TypedDiff
from ..module_utils.typed_classes import TypedSupportTunnelToAnsible
def open_tunnel(module: AnsibleModule, client: Client) -> Tuple[bool, TypedSupportTunnelToAnsible, TypedDiff]:
tunnel_status = SupportTunnel.check_tunnel_status(client)
if tunnel_status.open: # if tunnel already opened
if tunnel_status.code == module.params["code"]:
return (
False,
tunnel_status.to_ansible(),
dict(before=tunnel_status.to_ansible(), after=tunnel_status.to_ansible()),
)
else:
SupportTunnel.close_tunnel(client)
SupportTunnel.open_tunnel(module, client)
new_tunnel_status = SupportTunnel.check_tunnel_status(client)
if new_tunnel_status.open is False:
raise errors.SupportTunnelError("Support tunnel can't be opened, probably the code is already in use.")
return (
True,
new_tunnel_status.to_ansible(),
dict(before=tunnel_status.to_ansible(), after=new_tunnel_status.to_ansible()),
)
def close_tunnel(
client: Client,
) -> Tuple[bool, TypedSupportTunnelToAnsible, TypedDiff]:
tunnel_status = SupportTunnel.check_tunnel_status(client)
if not tunnel_status.open: # if tunnel already closed
return (
False,
tunnel_status.to_ansible(),
dict(before=tunnel_status.to_ansible(), after=tunnel_status.to_ansible()),
)
SupportTunnel.close_tunnel(client)
new_tunnel_status = SupportTunnel.check_tunnel_status(client)
return (
True,
new_tunnel_status.to_ansible(),
dict(before=tunnel_status.to_ansible(), after=new_tunnel_status.to_ansible()),
)
def run(module: AnsibleModule, client: Client) -> Tuple[bool, TypedSupportTunnelToAnsible, TypedDiff]:
if module.params["state"] == "present":
return open_tunnel(module, client)
else:
return close_tunnel(client)
def main() -> None:
module = AnsibleModule(
supports_check_mode=False,
argument_spec=dict(
arguments.get_spec("cluster_instance"),
state=dict(
type="str",
choices=["present", "absent"],
required=True,
),
code=dict(type="int"),
),
required_if=[
("state", "present", ("code",)),
],
)
try:
with Client.get_client(module.params["cluster_instance"]) as client:
changed, record, diff = run(module, client)
module.exit_json(changed=changed, record=record, diff=diff)
except errors.ScaleComputingError as e:
module.fail_json(msg=str(e))
if __name__ == "__main__":
main()