def run_action(cmd_options, required_options, resource, action, callback): cmd_options = cmd_options or [] all_options = GLOBAL_OPTIONS + cmd_options required_options = required_options or [] parser = get_parser() def done(result): if action == 'list': print_list(result, options.details) elif action == 'create': print_success('Resource created. ID: ' + result.id) elif action == 'update': print_success('Resource has been successfully updated') elif action == 'delete': if result: print_success('Resource deleted') else: print_success('Resource not deleted') else: pprint(result) if action in ACTION_OPTIONS: all_options.extend(ACTION_OPTIONS[action]) for args, kwargs in all_options: parser.add_option(*args, **kwargs) (options, args) = parser.parse_args() for option in required_options: if not getattr(options, option, None): raise Exception('Missing required option: ' + option) username, api_key, api_url = get_credentials() if options.username: username = options.username if options.api_key: api_key = options.api_key api_url = api_url or API_URL_ADDRESS if options.api_url: api_url = options.api_url if not username or not api_key: print('No username and API key provided!') print('You need to either put credentials in ~/.raxrc or ' + 'pass them to the command using --username and --api-key option') sys.exit(1) instance = get_instance(username, api_key, api_url) try: callback(instance, options, args, done) except Exception: traceback.print_exc(file=sys.stderr)