def args_to_importer_config(kwargs): """ Takes the arguments read from the CLI and converts the client-side input to the server-side expectations. The supplied dict will not be modified. @return: config to pass into the add/update importer calls @raise InvalidConfig: if one or more arguments is not valid for the importer """ importer_config = _prep_config(kwargs, IMPORTER_CONFIG_KEYS) # Parsing of true/false boolean_arguments = ('ssl_verify', 'verify_size', 'verify_checksum', 'newest', 'remove_old') convert_boolean_arguments(boolean_arguments, importer_config) # Read in the contents of any files that were specified file_arguments = ('ssl_ca_cert', 'ssl_client_cert', 'ssl_client_key') convert_file_contents(file_arguments, importer_config) # Handle skip types if importer_config.get('skip', None) is not None: skip_as_list = _convert_skip_types(importer_config['skip']) importer_config['skip'] = skip_as_list if 'num_old_packages' in importer_config: if importer_config['num_old_packages'] is None: importer_config['num_old_packages'] = 0 else: importer_config['num_old_packages'] = int(importer_config['num_old_packages']) LOG.debug('Importer configuration options') LOG.debug(importer_config) return importer_config
def run(self, **kwargs): convert_boolean_arguments([AUTO_PUBLISH_OPTION.keyword], kwargs) repo_id = kwargs[OPTION_REPO_ID.keyword] auto_publish = kwargs[AUTO_PUBLISH_OPTION.keyword] binding = self.context.server.repo_distributor if repository_enabled(self.context, repo_id): msg = ALREADY_ENABLED self.context.prompt.render_success_message(msg) return try: binding.create(repo_id, constants.HTTP_DISTRIBUTOR, {}, auto_publish, constants.HTTP_DISTRIBUTOR) self.context.prompt.render_success_message(REPO_ENABLED) self.context.prompt.render_warning_message(ENABLE_WARNING % dict(r=repo_id)) if auto_publish: self.context.prompt.render_warning_message( AUTO_PUBLISH_WARNING) except NotFoundException, e: for _id, _type in missing_resources(e): if _type == 'repository': msg = RESOURCE_MISSING_ERROR % dict(t=REPOSITORY, id=_id) self.context.prompt.render_failure_message(msg) else: raise return os.EX_DATAERR
def run(self, **kwargs): # -- importer metadata -- queries = kwargs.pop(OPTION_QUERIES.keyword, None) if queries is None: queries = kwargs.pop(OPTION_QUERY.keyword, None) importer_config = self.parse_user_input(kwargs) importer_config.update({constants.CONFIG_QUERIES: queries}) if importer_config: arg_utils.convert_removed_options(importer_config) kwargs['importer_config'] = importer_config # Remove the importer keys from kwargs so they don't get added to the repo config for key in importer_config: kwargs.pop(key, None) # -- distributor metadata -- distributor_config = { constants.CONFIG_SERVE_HTTP: kwargs.pop(OPTION_HTTP.keyword, None), constants.CONFIG_SERVE_HTTPS: kwargs.pop(OPTION_HTTPS.keyword, None) } arg_utils.convert_removed_options(distributor_config) arg_utils.convert_boolean_arguments((constants.CONFIG_SERVE_HTTP, constants.CONFIG_SERVE_HTTPS), distributor_config) # Remove the distributor keys from kwargs so they don't get added to the repo config for key in distributor_config: kwargs.pop(key, None) kwargs['distributor_configs'] = {} if distributor_config: kwargs['distributor_configs'][constants.DISTRIBUTOR_ID] = distributor_config super(UpdatePuppetRepositoryCommand, self).run(**kwargs)
def run(self, **kwargs): # -- importer metadata -- queries = kwargs.pop(OPTION_QUERIES.keyword, None) if queries is None: queries = kwargs.pop(OPTION_QUERY.keyword, None) importer_config = self.parse_user_input(kwargs) importer_config.update({constants.CONFIG_QUERIES: queries}) if importer_config: arg_utils.convert_removed_options(importer_config) kwargs['importer_config'] = importer_config # Remove the importer keys from kwargs so they don't get added to the repo config for key in importer_config: kwargs.pop(key.replace('_', '-'), None) # -- distributor metadata -- distributor_config = { constants.CONFIG_SERVE_HTTP: kwargs.pop(OPTION_HTTP.keyword, None), constants.CONFIG_SERVE_HTTPS: kwargs.pop(OPTION_HTTPS.keyword, None) } arg_utils.convert_removed_options(distributor_config) arg_utils.convert_boolean_arguments((constants.CONFIG_SERVE_HTTP, constants.CONFIG_SERVE_HTTPS), distributor_config) # Remove the distributor keys from kwargs so they don't get added to the repo config for key in distributor_config: kwargs.pop(key, None) kwargs['distributor_configs'] = {} if distributor_config: kwargs['distributor_configs'][constants.DISTRIBUTOR_ID] = distributor_config super(UpdatePuppetRepositoryCommand, self).run(**kwargs)
def run(self, **kwargs): convert_boolean_arguments([AUTO_PUBLISH_OPTION.keyword], kwargs) repo_id = kwargs[OPTION_REPO_ID.keyword] auto_publish = kwargs[AUTO_PUBLISH_OPTION.keyword] binding = self.context.server.repo_distributor if repository_enabled(self.context, repo_id): msg = ALREADY_ENABLED self.context.prompt.render_success_message(msg) return try: binding.create( repo_id, constants.HTTP_DISTRIBUTOR, {}, auto_publish, constants.HTTP_DISTRIBUTOR) self.context.prompt.render_success_message(REPO_ENABLED) self.context.prompt.render_warning_message(ENABLE_WARNING % dict(r=repo_id)) if auto_publish: self.context.prompt.render_warning_message(AUTO_PUBLISH_WARNING) except NotFoundException, e: for _id, _type in missing_resources(e): if _type == 'repository': msg = RESOURCE_MISSING_ERROR % dict(t=REPOSITORY, id=_id) self.context.prompt.render_failure_message(msg) else: raise return os.EX_DATAERR
def test_args_value_none(self, mock_arg_to_bool): # Setup key_list = ['key1', 'key2'] args = {'key1': None, 'key2': None} # Assert arg_to_bool is never called arg_utils.convert_boolean_arguments(key_list, args) self.assertEqual(0, mock_arg_to_bool.call_count)
def test_key_not_in_args(self): # Setup key_list = ['key1', 'key2'] args = {'other_key1': 'false', 'other_key2': 'true'} original_args = args.copy() # Assert no keys got changed arg_utils.convert_boolean_arguments(key_list, args) self.assertEqual(args, original_args)
def test_valid_input(self): # Setup key_list = ['key1', 'key2', 'key3'] args = {'key1': 'false', 'key2': 'FALSE', 'key3': 'true'} # Test that the boolean equivalents were written to args arg_utils.convert_boolean_arguments(key_list, args) self.assertFalse(args['key1']) self.assertFalse(args['key2']) self.assertTrue(args['key3'])
def run(self, **kwargs): schedule_id = kwargs.pop(OPT_SCHEDULE_ID.keyword) ft = kwargs.pop(OPT_FAILURE_THRESHOLD.keyword, None) if ft: kwargs['failure_threshold'] = ft convert_removed_options(kwargs) convert_boolean_arguments([OPT_ENABLED.keyword], kwargs) self.strategy.update_schedule(schedule_id, **kwargs) self.context.prompt.render_success_message(_('Successfully updated schedule'))
def update(self, **kwargs): schedule_id = kwargs.pop('schedule-id') ft = kwargs.pop('failure-threshold', None) if ft: kwargs['failure_threshold'] = ft convert_removed_options(kwargs) convert_boolean_arguments(['enabled'], kwargs) response = self.strategy.update_schedule(schedule_id, **kwargs) self.context.prompt.render_success_message(_('Successfully updated schedule'))
def run(self, **kwargs): schedule_id = kwargs.pop(OPT_SCHEDULE_ID.keyword) ft = kwargs.pop(OPT_FAILURE_THRESHOLD.keyword, None) if ft: kwargs['failure_threshold'] = ft convert_removed_options(kwargs) convert_boolean_arguments([OPT_ENABLED.keyword], kwargs) self.strategy.update_schedule(schedule_id, **kwargs) self.context.prompt.render_success_message( _('Successfully updated schedule'))
def run(self, **kwargs): # -- repository metadata -- repo_id = kwargs[options.OPTION_REPO_ID.keyword] description = kwargs[options.OPTION_DESCRIPTION.keyword] notes = kwargs.pop(options.OPTION_NOTES.keyword) or {} # Add a note to indicate this is a Puppet repository notes[constants.REPO_NOTE_KEY] = constants.REPO_NOTE_PUPPET name = repo_id if options.OPTION_NAME.keyword in kwargs: name = kwargs[options.OPTION_NAME.keyword] # -- importer metadata -- importer_config = self.parse_user_input(kwargs) importer_config.update( {constants.CONFIG_QUERIES: kwargs[OPTION_QUERIES.keyword] or kwargs[OPTION_QUERY.keyword]} ) arg_utils.convert_removed_options(importer_config) # -- distributor metadata -- distributor_config = { constants.CONFIG_SERVE_HTTP: kwargs[OPTION_HTTP.keyword], constants.CONFIG_SERVE_HTTPS: kwargs[OPTION_HTTPS.keyword], } arg_utils.convert_removed_options(distributor_config) arg_utils.convert_boolean_arguments( (constants.CONFIG_SERVE_HTTP, constants.CONFIG_SERVE_HTTPS), distributor_config ) distributors = [ dict( distributor_type=constants.DISTRIBUTOR_TYPE_ID, distributor_config=distributor_config, auto_publish=True, distributor_id=constants.DISTRIBUTOR_ID, ) ] # Create the repository self.context.server.repo.create_and_configure( repo_id, name, description, notes, constants.IMPORTER_TYPE_ID, importer_config, distributors ) msg = _("Successfully created repository [%(r)s]") self.context.prompt.render_success_message(msg % {"r": repo_id})
def run(self, **kwargs): # -- repository metadata -- repo_id = kwargs[options.OPTION_REPO_ID.keyword] description = kwargs[options.OPTION_DESCRIPTION.keyword] notes = kwargs.pop(options.OPTION_NOTES.keyword) or {} # Add a note to indicate this is a Puppet repository notes[constants.REPO_NOTE_KEY] = constants.REPO_NOTE_PUPPET name = repo_id if options.OPTION_NAME.keyword in kwargs: name = kwargs[options.OPTION_NAME.keyword] # -- importer metadata -- importer_config = { constants.CONFIG_FEED: kwargs[OPTION_FEED.keyword], constants.CONFIG_QUERIES: kwargs[OPTION_QUERIES.keyword] or kwargs[OPTION_QUERY.keyword], } arg_utils.convert_removed_options(importer_config) # -- distributor metadata -- distributor_config = { constants.CONFIG_SERVE_HTTP: kwargs[OPTION_HTTP.keyword], constants.CONFIG_SERVE_HTTPS: kwargs[OPTION_HTTPS.keyword], } arg_utils.convert_removed_options(distributor_config) arg_utils.convert_boolean_arguments( (constants.CONFIG_SERVE_HTTP, constants.CONFIG_SERVE_HTTPS), distributor_config) distributors = [ dict(distributor_type=constants.DISTRIBUTOR_TYPE_ID, distributor_config=distributor_config, auto_publish=True, distributor_id=constants.DISTRIBUTOR_ID) ] # Create the repository self.context.server.repo.create_and_configure( repo_id, name, description, notes, constants.IMPORTER_TYPE_ID, importer_config, distributors) msg = _('Successfully created repository [%(r)s]') self.context.prompt.render_success_message(msg % {'r': repo_id})
def run(self, **kwargs): # -- repository metadata -- repo_id = kwargs.pop(options.OPTION_REPO_ID.keyword) description = kwargs.pop(options.OPTION_DESCRIPTION.keyword, None) name = kwargs.pop(options.OPTION_NAME.keyword, None) notes = kwargs.pop(options.OPTION_NOTES.keyword, None) # -- importer metadata -- queries = kwargs.pop(OPTION_QUERIES.keyword, None) if queries is None: queries = kwargs.pop(OPTION_QUERY.keyword, None) importer_config = { constants.CONFIG_FEED: kwargs.pop(OPTION_FEED.keyword, None), constants.CONFIG_QUERIES: queries } arg_utils.convert_removed_options(importer_config) # -- distributor metadata -- distributor_config = { constants.CONFIG_SERVE_HTTP: kwargs.pop(OPTION_HTTP.keyword, None), constants.CONFIG_SERVE_HTTPS: kwargs.pop(OPTION_HTTPS.keyword, None), } arg_utils.convert_removed_options(distributor_config) arg_utils.convert_boolean_arguments( (constants.CONFIG_SERVE_HTTP, constants.CONFIG_SERVE_HTTPS), distributor_config) distributor_configs = {constants.DISTRIBUTOR_ID: distributor_config} # -- server update -- response = self.context.server.repo.update_repo_and_plugins( repo_id, name, description, notes, importer_config, distributor_configs) if not response.is_async(): msg = _('Repository [%(r)s] successfully updated') self.context.prompt.render_success_message(msg % {'r': repo_id}) else: d = _( 'Repository update postponed due to another operation. Progress ' 'on this task can be viewed using the commands under "repo tasks".' ) self.context.prompt.render_paragraph(d, tag='postponed') self.context.prompt.render_reasons(response.response_body.reasons)
def run(self, **kwargs): # -- repository metadata -- repo_id = kwargs.pop(options.OPTION_REPO_ID.keyword) description = kwargs.pop(options.OPTION_DESCRIPTION.keyword, None) name = kwargs.pop(options.OPTION_NAME.keyword, None) notes = None if options.OPTION_NOTES.keyword in kwargs and kwargs[options.OPTION_NOTES.keyword] is not None: notes = arg_utils.args_to_notes_dict(kwargs[options.OPTION_NOTES.keyword], include_none=True) # Make sure the note indicating it's a puppet repository is still present notes[constants.REPO_NOTE_KEY] = constants.REPO_NOTE_PUPPET # -- importer metadata -- importer_config = { constants.CONFIG_FEED : kwargs.pop(OPTION_FEED.keyword, None), constants.CONFIG_QUERIES : kwargs.pop(OPTION_QUERY.keyword, None), } arg_utils.convert_removed_options(importer_config) # -- distributor metadata -- distributor_config = { constants.CONFIG_SERVE_HTTP : kwargs.pop(OPTION_HTTP.keyword, None), constants.CONFIG_SERVE_HTTPS : kwargs.pop(OPTION_HTTPS.keyword, None), } arg_utils.convert_removed_options(distributor_config) arg_utils.convert_boolean_arguments((constants.CONFIG_SERVE_HTTP, constants.CONFIG_SERVE_HTTPS), distributor_config) distributor_configs = {constants.DISTRIBUTOR_ID : distributor_config} # -- server update -- response = self.context.server.repo.update_repo_and_plugins(repo_id, name, description, notes, importer_config, distributor_configs) if not response.is_async(): msg = _('Repository [%(r)s] successfully updated') self.context.prompt.render_success_message(msg % {'r' : repo_id}) else: d = _('Repository update postponed due to another operation. Progress ' 'on this task can be viewed using the commands under "repo tasks".') self.context.prompt.render_paragraph(d, tag='postponed') self.context.prompt.render_reasons(response.response_body.reasons)
def run(self, **kwargs): # -- repository metadata -- repo_id = kwargs.pop(options.OPTION_REPO_ID.keyword) description = kwargs.pop(options.OPTION_DESCRIPTION.keyword, None) name = kwargs.pop(options.OPTION_NAME.keyword, None) notes = kwargs.pop(options.OPTION_NOTES.keyword, None) # -- importer metadata -- queries = kwargs.pop(OPTION_QUERIES.keyword, None) if queries is None: queries = kwargs.pop(OPTION_QUERY.keyword, None) importer_config = self.parse_user_input(kwargs) importer_config.update({constants.CONFIG_QUERIES: queries}) arg_utils.convert_removed_options(importer_config) # -- distributor metadata -- distributor_config = { constants.CONFIG_SERVE_HTTP: kwargs.pop(OPTION_HTTP.keyword, None), constants.CONFIG_SERVE_HTTPS: kwargs.pop(OPTION_HTTPS.keyword, None), } arg_utils.convert_removed_options(distributor_config) arg_utils.convert_boolean_arguments( (constants.CONFIG_SERVE_HTTP, constants.CONFIG_SERVE_HTTPS), distributor_config ) distributor_configs = {constants.DISTRIBUTOR_ID: distributor_config} # -- server update -- response = self.context.server.repo.update_repo_and_plugins( repo_id, name, description, notes, importer_config, distributor_configs ) if not response.is_async(): msg = _("Repository [%(r)s] successfully updated") self.context.prompt.render_success_message(msg % {"r": repo_id}) else: d = _( "Repository update postponed due to another operation. Progress " 'on this task can be viewed using the commands under "repo tasks".' ) self.context.prompt.render_paragraph(d, tag="postponed") self.context.prompt.render_reasons(response.response_body.reasons)
def run(self, **kwargs): repo_id = kwargs[options.OPTION_REPO_ID.keyword] description = kwargs[options.OPTION_DESCRIPTION.keyword] notes = kwargs.pop(options.OPTION_NOTES.keyword) or {} # Add a note to indicate this is a Deb repository notes[constants.REPO_NOTE_KEY] = constants.REPO_NOTE name = repo_id if options.OPTION_NAME.keyword in kwargs: name = kwargs[options.OPTION_NAME.keyword] # -- importer metadata -- importer_config = { constants.CONFIG_URL: kwargs[OPTION_URL.keyword], constants.CONFIG_DIST: kwargs[OPTION_DIST.keyword], constants.CONFIG_COMPONENT: kwargs[OPTION_COMPONENT.keyword], constants.CONFIG_ARCH: kwargs[OPTION_ARCH.keyword], constants.CONFIG_QUERIES: kwargs[OPTION_QUERY.keyword], } arg_utils.convert_removed_options(importer_config) # -- distributor metadata -- distributor_config = { constants.CONFIG_SERVE_INSECURE: kwargs[OPTION_INSECURE.keyword], } arg_utils.convert_removed_options(distributor_config) arg_utils.convert_boolean_arguments((constants.CONFIG_SERVE_INSECURE), distributor_config) distributors = [ dict(distributor_type=constants.DISTRIBUTOR_TYPE_ID, distributor_config=distributor_config, auto_publish=True, distributor_id=constants.DISTRIBUTOR_ID) ] # Create the repository self.context.server.repo.create_and_configure(repo_id, name, description, notes, constants.IMPORTER_TYPE_ID, importer_config) #notes, constants.IMPORTER_TYPE_ID, importer_config, distributors) msg = _('Successfully created repository [%(r)s]') self.context.prompt.render_success_message(msg % {'r': repo_id})
def args_to_distributor_config(kwargs): """ Takes the arguments read from the CLI and converts the client-side input to the server-side expectations. The supplied dict will not be modified. @return: config to pass into the add/update distributor calls @raise InvalidConfig: if one or more arguments is not valid for the distributor """ distributor_config = _prep_config(kwargs, DISTRIBUTOR_CONFIG_KEYS) # Parsing of true/false boolean_arguments = ('http', 'https', 'generate_metadata') convert_boolean_arguments(boolean_arguments, distributor_config) # Read in the contents of any files that were specified file_arguments = ('auth_cert', 'auth_ca', 'https_ca', 'gpgkey') convert_file_contents(file_arguments, distributor_config) # Handle skip types if distributor_config.get('skip', None) is not None: skip_as_list = _convert_skip_types(distributor_config['skip']) distributor_config['skip'] = skip_as_list # There is an explicit flag for enabling/disabling repository protection. # This may be useful to expose to the user to quickly turn on/off repo # auth for debugging purposes. For now, if the user has specified an auth # CA, assume they also want to flip that protection flag to true. if 'auth_ca' in distributor_config: if distributor_config['auth_ca'] is None: # This would occur if the user requested to remove the CA (as # compared to not mentioning it at all, which is the outer if statement. distributor_config['protected'] = False else: # If there is something in the CA, assume it's turning on auth and # flip the flag to true. distributor_config['protected'] = True LOG.debug('Distributor configuration options') LOG.debug(distributor_config) return distributor_config
def run(self, **kwargs): # -- repository metadata -- repo_id = kwargs.pop(options.OPTION_REPO_ID.keyword) description = kwargs.pop(options.OPTION_DESCRIPTION.keyword, None) name = kwargs.pop(options.OPTION_NAME.keyword, None) notes = kwargs.pop(options.OPTION_NOTES.keyword, None) # -- importer metadata -- importer_config = { constants.CONFIG_URL: kwargs[OPTION_URL.keyword], constants.CONFIG_DIST: kwargs[OPTION_DIST.keyword], constants.CONFIG_COMPONENT: kwargs[OPTION_COMPONENT.keyword], constants.CONFIG_ARCH: kwargs[OPTION_ARCH.keyword], constants.CONFIG_QUERIES: kwargs[OPTION_QUERY.keyword], } arg_utils.convert_removed_options(importer_config) # -- distributor metadata -- distributor_config = { constants.CONFIG_SERVE_INSECURE: kwargs[OPTION_INSECURE.keyword], } arg_utils.convert_removed_options(distributor_config) arg_utils.convert_boolean_arguments((constants.CONFIG_SERVE_HTTP, constants.CONFIG_SERVE_HTTPS), distributor_config) distributor_configs = {constants.DISTRIBUTOR_ID: distributor_config} # -- server update -- response = self.context.server.repo.update_repo_and_plugins(repo_id, name, description, notes, importer_config, distributor_configs) if not response.is_async(): msg = _('Repository [%(r)s] successfully updated') self.context.prompt.render_success_message(msg % {'r': repo_id}) else: d = _('Repository update postponed due to another operation. Progress ' 'on this task can be viewed using the commands under "repo tasks".') self.context.prompt.render_paragraph(d, tag='postponed') self.context.prompt.render_reasons(response.response_body.reasons)
def run(self, **kwargs): # -- repository metadata -- repo_id = kwargs[options.OPTION_REPO_ID.keyword] description = kwargs[options.OPTION_DESCRIPTION.keyword] notes = {} if kwargs[options.OPTION_NOTES.keyword]: notes = arg_utils.args_to_notes_dict(kwargs[options.OPTION_NOTES.keyword], include_none=True) name = repo_id if options.OPTION_NAME.keyword in kwargs: name = kwargs[options.OPTION_NAME.keyword] # Add a note to indicate this is a Puppet repository notes[constants.REPO_NOTE_KEY] = constants.REPO_NOTE_PUPPET # -- importer metadata -- importer_config = { constants.CONFIG_FEED : kwargs[OPTION_FEED.keyword], constants.CONFIG_QUERIES : kwargs[OPTION_QUERY.keyword], } arg_utils.convert_removed_options(importer_config) # -- distributor metadata -- distributor_config = { constants.CONFIG_SERVE_HTTP : kwargs[OPTION_HTTP.keyword], constants.CONFIG_SERVE_HTTPS : kwargs[OPTION_HTTPS.keyword], } arg_utils.convert_removed_options(distributor_config) arg_utils.convert_boolean_arguments((constants.CONFIG_SERVE_HTTP, constants.CONFIG_SERVE_HTTPS), distributor_config) distributors = [(constants.DISTRIBUTOR_TYPE_ID, distributor_config, True, constants.DISTRIBUTOR_ID)] # Create the repository self.context.server.repo.create_and_configure(repo_id, name, description, notes, constants.IMPORTER_TYPE_ID, importer_config, distributors) msg = _('Successfully created repository [%(r)s]') self.context.prompt.render_success_message(msg % {'r' : repo_id})