def _import_object(self, obj_data, env_id, update_mode, dry_run=False): args, kwargs = obj_data['meta']['scalrctl']['ARGUMENTS'] route = obj_data['meta']['scalrctl']['ROUTE'] http_method = 'patch' if update_mode else 'post' obj_name = obj_data['data'].get('name') action = None for action_name, section in self.scheme['export'].items(): if 'http-method' in section and 'route' in section and \ section['http-method'] == 'get' and \ section['route'] == route: scheme = section['{}-params'.format(http_method)] cls = pydoc.locate( scheme['class']) if 'class' in scheme else commands.Action action = cls(name=action_name, route=scheme['route'], http_method=http_method, api_level=self.api_level) break if not action: msg = "Cannot import Scalr object: API method '{}: {}' not found" \ .format('GET', route) raise click.ClickException(msg) obj_type = action._get_body_type_params()[0]['name'] if action.name not in ('role-image', ): kwargs['import-data'] = {obj_type: obj_data['data']} kwargs['dryrun'] = dry_run if env_id: kwargs['envId'] = env_id click.secho("{} {} {} {}...".format( "Updating" if update_mode else "Creating", self._get_object_alias(obj_type), '\"%s\"' % obj_name if obj_name else '', "ID" if update_mode else ""), bold=True) result = action.run(*args, **kwargs) result_json = json.loads(result) alias = self._get_object_alias(obj_type) click.secho("{} created.\n".format(alias), bold=True) return result_json
def warning(*messages): """ Prints the warning message(s) to stderr. :param tuple messages: The list of the warning messages. :rtype: None """ color = 'yellow' if settings.colored_output else None for index, message in enumerate(messages or [], start=1): index = index if len(messages) > 1 else None code = message.get('code') or '' text = message.get('message') or '' click.secho("Warning{index}{code} {text}".format( index=' {}:'.format(index) if index else ':', code=' {}:'.format(code) if code else '', text=text ), err=True, fg=color)
def run(self, *args, **kwargs): if 'debug' in kwargs: settings.debug_mode = kwargs.pop('debug') env_id = kwargs.pop('env_id', None) or settings.envId dry_run = kwargs.pop('dryrun', False) update_mode = kwargs.pop('update', False) raw_objects = kwargs.pop('raw', None) or click.get_text_stream('stdin') import_objects = self._validate_object(raw_objects) for obj in import_objects: action_name = obj['meta']['scalrctl']['ACTION'] obj_name = obj['data'].get('name') result = None try: if action_name == 'role-categories': # finds objects with the same names in new environment result = self._find_existing_object( action_name, obj_name, env_id) if result: msg = "Warning: \"{}\" already exists\n".format(obj_name) click.secho(msg, bold=True, fg='yellow') else: obj = self._modify_object( obj) # updates object body with new ID's result = self._import_object(obj, env_id, update_mode, dry_run) # save ID's of objects in new environment self._save_imported(obj, result['data']) except Exception as e: error_code = getattr(e, 'code', None) ignored = ('role-categories', 'role-global-variables') if error_code == 'UnicityViolation' and action_name in ignored: click.secho("Warning: {}\n".format(str(e)), bold=True, fg='yellow') else: # TODO: delete imported objects raise click.ClickException(str(e))
def update(): """ Update spec for all available APIs. """ amount = len(defaults.API_LEVELS) for index, api_level in enumerate(defaults.API_LEVELS, 1): click.echo('[{}/{}] Updating specifications for {} API ... ' .format(index, amount, api_level), nl=False) with utils._spinner(): success, fail_reason = _update_spec(api_level) if success: click.secho('Done', fg='green') else: click.secho('Failed: {}'.format(fail_reason), fg='red')
def update(): """ Update spec for all available APIs. """ amount = len(defaults.API_LEVELS) for index, api_level in enumerate(defaults.API_LEVELS, 1): click.echo('[{}/{}] Updating specifications for {} API ... '.format( index, amount, api_level), nl=False) with utils._spinner(): success, fail_reason = _update_spec(api_level) if success: click.secho('Done', fg='green') else: click.secho('Failed: {}'.format(fail_reason), fg='red')
def debug(msg): if settings.debug_mode: click.secho("DEBUG: {}".format(msg), fg='green' if settings.colored_output else None)