Debian Codegraph @

ansible_collections/community/rabbitmq/plugins/modules/rabbitmq_user_limits.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2013, Chatham Financial <[email protected]>
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 DOCUMENTATION = r'''
12 ---
13 module: rabbitmq_user_limits
14 short_description: Manage RabbitMQ user limits
15 description:
16 - Manage the state of user limits in RabbitMQ. Supported since RabbitMQ version 3.8.10.
17 author: Aitor Pazos (@aitorpazos)
18 version_added: '1.1.0'
19 options:
20 user:
21 description:
22 - Name of user to manage limits for.
23 type: str
24 required: true
25 aliases: [username, name]
26 max_connections:
27 description:
28 - Max number of concurrent client connections.
29 - Negative value means "no limit".
30 - Ignored when the I(state) is C(absent).
31 type: int
32 default: -1
33 max_channels:
34 description:
35 - Max number of channels.
36 - Negative value means "no limit".
37 - Ignored when the I(state) is C(absent).
38 type: int
39 default: -1
40 node:
41 description:
42 - Name of the RabbitMQ Erlang node to manage.
43 type: str
44 state:
45 description:
46 - Specify whether the limits are to be set or cleared.
47 - If set to C(absent), the limits of both I(max_connections) and I(max_channels) will be cleared.
48 type: str
49 default: present
50 choices: [present, absent]
51 notes:
52 - Supports C(check_mode).
53 '''
54
55 EXAMPLES = r'''
56 - name: Limit both of the max number of connections and channels on the user 'guest'.
57 community.rabbitmq.rabbitmq_user_limits:
58 user: guest
59 max_connections: 64
60 max_channels: 256
61 state: present
62
63 # This task implicitly clears the max number of channels limit using default value: -1.
64 - name: Limit the max number of connections on the user 'guest'.
65 community.rabbitmq.rabbitmq_user_limits:
66 user: guest
67 max_connections: 64
68 state: present
69
70 - name: Clear the limits on the user 'guest'.
71 community.rabbitmq.rabbitmq_user_limits:
72 user: guest
73 state: absent
74 '''
75
76 RETURN = r''' # '''
77
78 import json
79 import re
80 from ansible_collections.community.rabbitmq.plugins.module_utils.version import LooseVersion as Version
81 from ansible.module_utils.basic import AnsibleModule
82
83
84 class RabbitMqUserLimits(object):
85 def __init__(self, module):
86 self._module = module
87 self._max_connections = module.params['max_connections']
88 self._max_channels = module.params['max_channels']
89 self._node = module.params['node']
90 self._state = module.params['state']
91 self._user = module.params['user']
92 self._rabbitmqctl = module.get_bin_path('rabbitmqctl', True)
93
94 self._version = self._rabbit_version()
95
96 def _exec(self,
97 args,
98 force_exec_in_check_mode=False):
99
100 if not self._module.check_mode or (self._module.check_mode and force_exec_in_check_mode):
101 cmd = [self._rabbitmqctl, '-q']
102 if self._node is not None:
103 cmd.extend(['-n', self._node])
104 rc, out, err = self._module.run_command(cmd + args, check_rc=True)
105
106 return out
107 return ""
108
109 def _rabbit_version(self):
110 status = self._exec(['status'], True)
111
112 # 3.7.x erlang style output
113 version_match = re.search('{rabbit,".*","(?P<version>.*)"}', status)
114 if version_match:
115 return Version(version_match.group('version'))
116
117 # 3.8.x style output
118 version_match = re.search('RabbitMQ version: (?P<version>.*)', status)
119 if version_match:
120 return Version(version_match.group('version'))
121
122 return None
123
124 def _assert_version(self):
125 if self._version and self._version < Version('3.8.10'):
126 self._module.fail_json(changed=False,
127 msg="User limits are only available for RabbitMQ >= 3.8.10. Detected version: %s" % self._version)
128
129 def list(self):
130 self._assert_version()
131
132 exec_result = self._exec(['list_user_limits', '--user', self._user], False)
133 max_connections = None
134 max_channels = None
135 if exec_result:
136 user_limits = json.loads(exec_result)
137 if 'max-connections' in user_limits:
138 max_connections = user_limits['max-connections']
139 if 'max-channels' in user_limits:
140 max_channels = user_limits['max-channels']
141 return dict(
142 max_connections=max_connections,
143 max_channels=max_channels
144 )
145
146 def set(self):
147 self._assert_version()
148
149 if self._module.check_mode:
150 return
151
152 if self._max_connections != -1:
153 json_str = '{{"max-connections": {0}}}'.format(self._max_connections)
154 self._exec(['set_user_limits', self._user, json_str])
155 else:
156 self._exec(['clear_user_limits', self._user, "max-connections"])
157
158 if self._max_channels != -1:
159 json_str = '{{"max-channels": {0}}}'.format(self._max_channels)
160 self._exec(['set_user_limits', self._user, json_str])
161 else:
162 self._exec(['clear_user_limits', self._user, "max-channels"])
163
164 def clear(self):
165 self._assert_version()
166
167 if self._module.check_mode:
168 return
169
170 return self._exec(['clear_user_limits', self._user, 'all'])
171
172
173 def main():
174 arg_spec = dict(
175 user=dict(required=True, type='str', aliases=['username', 'name']),
176 max_connections=dict(default=-1, type='int'),
177 max_channels=dict(default=-1, type='int'),
178 state=dict(default='present', choices=['present', 'absent'], type='str'),
179 node=dict(default=None, type='str')
180 )
181
182 module = AnsibleModule(
183 argument_spec=arg_spec,
184 supports_check_mode=True
185 )
186
187 max_connections = module.params['max_connections']
188 max_channels = module.params['max_channels']
189 state = module.params['state']
190
191 module_result = dict(changed=False)
192 rabbitmq_user_limits = RabbitMqUserLimits(module)
193 current_status = rabbitmq_user_limits.list()
194
195 if state == 'present':
196 wanted_status = dict(
197 max_connections=max_connections,
198 max_channels=max_channels
199 )
200 else: # state == 'absent'
201 wanted_status = dict(
202 max_connections=None,
203 max_channels=None
204 )
205
206 if current_status != wanted_status:
207 module_result['changed'] = True
208 if state == 'present':
209 rabbitmq_user_limits.set()
210 else: # state == 'absent'
211 rabbitmq_user_limits.clear()
212
213 module.exit_json(**module_result)
214
215
216 if __name__ == '__main__':
217 main()