From e722eb2a7dd10672b6b3fcd046d1eb253f2dee2b Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Sun, 10 Feb 2013 17:20:44 -0500 Subject: [PATCH] Updating how to run commands in check mode --- rabbitmq_plugin | 8 +++++--- rabbitmq_user | 42 +++++++++++++++++++----------------------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/rabbitmq_plugin b/rabbitmq_plugin index 73f3dc58ee2..4bd549dc618 100644 --- a/rabbitmq_plugin +++ b/rabbitmq_plugin @@ -57,9 +57,11 @@ class RabbitMqPlugins(object): self._rabbitmq_plugins = module.get_bin_path('rabbitmq-plugins', True) def _exec(self, args, run_in_check_mode=False): - cmd = [self._rabbitmq_plugins] - rc, out, err = self.module.run_command(cmd + args, check_rc=True) - return out.splitlines() + if not self.module.check_mode or (self.module.check_mode and run_in_check_mode): + cmd = [self._rabbitmq_plugins] + rc, out, err = self.module.run_command(cmd + args, check_rc=True) + return out.splitlines() + return list() def get_all(self): return self._exec(['list', '-E', '-m'], True) diff --git a/rabbitmq_user b/rabbitmq_user index ab8e9b00f35..497ce29e5d9 100644 --- a/rabbitmq_user +++ b/rabbitmq_user @@ -108,13 +108,15 @@ class RabbitMqUser(object): self._permissions = None self._rabbitmqctl = module.get_bin_path('rabbitmqctl', True) - def _exec(self, args): - cmd = [self._rabbitmqctl, '-q'] - rc, out, err = self.module.run_command(cmd + args, check_rc=True) - return out.splitlines() + def _exec(self, args, run_in_check_mode=False): + if not self.module.check_mode or (self.module.check_mode and run_in_check_mode): + cmd = [self._rabbitmqctl, '-q'] + rc, out, err = self.module.run_command(cmd + args, check_rc=True) + return out.splitlines() + return list() def get(self): - users = self._exec(['list_users']) + users = self._exec(['list_users'], True) for user_tag in users: user, tags = user_tag.split('\t') @@ -129,13 +131,11 @@ class RabbitMqUser(object): self._tags = list() self._permissions = self._get_permissions() - return True - return False def _get_permissions(self): - perms_out = self._exec(['list_user_permissions', self.username]) + perms_out = self._exec(['list_user_permissions', self.username], True) for perm in perms_out: vhost, configure_priv, write_priv, read_priv = perm.split('\t') @@ -145,27 +145,23 @@ class RabbitMqUser(object): return dict() def add(self): - if not self.module.check_mode: - self._exec(['add_user', self.username, self.password]) + self._exec(['add_user', self.username, self.password]) def delete(self): - if not self.module.check_mode: - self._exec(['delete_user', self.username]) + self._exec(['delete_user', self.username]) def set_tags(self): - if not self.module.check_mode: - self._exec(['set_user_tags', self.username] + self.tags) + self._exec(['set_user_tags', self.username] + self.tags) def set_permissions(self): - if not self.module.check_mode: - cmd = ['set_permissions'] - cmd.append('-p') - cmd.append(self.permissions['vhost']) - cmd.append(self.username) - cmd.append(self.permissions['configure_priv']) - cmd.append(self.permissions['write_priv']) - cmd.append(self.permissions['read_priv']) - self._exec(cmd) + cmd = ['set_permissions'] + cmd.append('-p') + cmd.append(self.permissions['vhost']) + cmd.append(self.username) + cmd.append(self.permissions['configure_priv']) + cmd.append(self.permissions['write_priv']) + cmd.append(self.permissions['read_priv']) + self._exec(cmd) def has_tags_modifications(self): return set(self.tags) != set(self._tags)