Skip to content

Instantly share code, notes, and snippets.

@mhrivnak
Last active December 13, 2015 19:28
Show Gist options
  • Save mhrivnak/4962453 to your computer and use it in GitHub Desktop.
Save mhrivnak/4962453 to your computer and use it in GitHub Desktop.
class ConsumerBindCommand(PulpCliCommand):
"""
Base class that binds a consumer to a repository and an arbitrary
distributor.
"""
def __init__(self, context, name=None, description=None, distributor_id=None):
name = name or 'bind'
description = description or _('binds a consumer to a repository')
super(self.__class__, self).__init__(name, description, self.run)
self.add_option(OPTION_CONSUMER_ID)
self.add_option(OPTION_REPO_ID)
if distributor_id is None:
self.add_option(OPTION_DISTRIBUTOR_ID)
self.context = context
self.distributor_id = distributor_id
def run(self, **kwargs):
consumer_id = kwargs[OPTION_CONSUMER_ID.keyword]
repo_id = kwargs[OPTION_REPO_ID.keyword]
distributor_id = self.distributor_id or kwargs[OPTION_DISTRIBUTOR_ID.keyword]
try:
response = self.context.server.bind.bind(consumer_id, repo_id, distributor_id)
except NotFoundException, e:
resources = e.extra_data['resources']
missing = []
msg = _('%(t)s [ %(i)s ] does not exist on the server')
if 'consumer' in resources:
missing.append((_('Consumer'), consumer_id))
if 'repository' in resources:
missing.append((_('Repository'), repo_id))
if 'distributor' in resources:
missing.append((_('Distributor'), distributor_id))
for option_title, option_id in missing:
self.context.prompt.render_failure_message(msg % {'t': option_title, 'i': option_id}, tag='not-found')
return os.EX_DATAERR
else:
msg = _('Bind tasks successfully created:')
self.context.prompt.render_success_message(msg)
task_dicts = [dict(('task_id', str(t.task_id))) for t in response.response_body]
self.context.prompt.render_document_list(task_dicts)
def initialize(context):
section = structure.ensure_puppet_root(context.cli)
# no need to subclass or override anything!
section.add_command(ConsumerBindCommand(distributor_id=constants.PUPPET_DISTRIBUTOR_ID))
@mhrivnak
Copy link
Author

This is just a thrown-together pattern demo that probably has typos and doesn't do things like import stuff. ConsumerBindCommand is modified only slightly from jconnor's original.

The point of this is to show that nothing about this command will be different between rpm, puppet, deb, or most (all?) other support types except the distributor ID. Allowing the generic command to accept a distributor ID lets the command be reused as-is without subclassing and overriding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment