def wrapper(data): try: return loader(data) except (ValueError, TypeError, yaml.error.YAMLError) as e: raise error.BadDataException('{0}: {1}' ''.format(e.__class__.__name__, six.text_type(e)))
def update(self, node_id, **updated_attributes): node = self._entity_wrapper(obj_id=node_id) for attr in updated_attributes: if attr not in self._updatable_attributes: msg = 'Only {0} are updatable'.format( self._updatable_attributes) raise error.BadDataException(msg) return node.set(updated_attributes)
def update(self, network_id, **kwargs): for attr in kwargs: if attr not in self.updatable_attributes: raise error.BadDataException( 'Update of attribute "{0}" is not allowed'.format(attr)) net_group = self._entity_wrapper(network_id) net_group.set(kwargs) return net_group.data
def take_action(self, parsed_args): try: confs = utils.parse_to_list_of_dicts(parsed_args.conf) except TypeError: raise error.BadDataException( 'VM configuration should be a dictionary ' 'or a list of dictionaries') data = self.client.node_vms_create(parsed_args.id, confs) msg = "{0}".format(data) self.app.stdout.write(msg)
def create(self, name, release_id, net_segment_type): supported_nst = ('gre', 'vlan', 'tun') if net_segment_type not in supported_nst: msg = ('Network segmentation type should be one ' 'of {0}'.format(' '.join(supported_nst))) raise error.BadDataException(msg) env = self._entity_wrapper.create(name, release_id, net_segment_type) return env.data
def send(cls, message, topic=default_topic): if not topic: topic = cls.default_topic if not message: raise error.BadDataException('Message not specified.') resp = cls.connection.post_request(cls.class_api_path, { 'message': message, 'topic': topic, }) return resp
def mark_as_read(cls, ids=None): if not ids: raise error.BadDataException('Message id not specified.') if '*' in ids: data = Notifications.get_all_data() else: try: ids = map(int, ids) except ValueError: raise error.BadDataException( "Numerical ids expected or the '*' symbol.") notifications = Notifications.get_by_ids(ids) data = [ notification.get_fresh_data() for notification in notifications ] for notification in data: notification['status'] = 'read' resp = cls.connection.put_request(cls.class_api_path, data) return resp
def get_plugin(cls, name, version): """Returns plugin fetched by name and version. :param str name: plugin name :param str version: plugin version :returns: dictionary with plugin data :raises: error.BadDataException if no plugin was found """ plugins = [p for p in cls.get_all_data() if (p['name'], p['version']) == (name, version)] if not plugins: raise error.BadDataException( 'Plugin "{name}" with version {version}, does ' 'not exist'.format(name=name, version=version)) return plugins[0]
def parse_to_list_of_dicts(str_list): """Parse list of json strings to dictionaries :param list: list of dicts and json string :returns" list of dictionaries """ dict_list = [] for json_str in str_list: if not isinstance(json_str, dict): try: json_str = json.loads(json_str) except Exception: raise error.BadDataException( 'Not valid JSON data: {0}'.format(json_str)) dict_list.append(json_str) return dict_list
def get_display_data_single(fields, data): """Performs slicing of data by set of given fields :param fields: Iterable containing names of fields to be retrieved from data :param data: Collection of JSON objects representing some external entities :return: list containing the collection of values of the supplied attributes. """ try: return [data[field] for field in fields] except KeyError as e: raise error.BadDataException('{} is not found in the supplied ' 'data.'.format(e.args[0]))
def update(self, environment_id, **kwargs): allowed_changes = {} extra_args = {} for i in kwargs: if i in self._updatable_attributes: allowed_changes[i] = kwargs[i] else: extra_args[i] = kwargs[i] if extra_args != {}: msg = 'Only {0} are updatable'.format(self._updatable_attributes) raise error.BadDataException(msg) env = self._entity_wrapper(obj_id=environment_id) env.set(allowed_changes) return env.data
def make_obj_by_file(cls, file_path): """Finds appropriate plugin class version, by plugin file. :param str file_path: plugin path :returns: plugin class :raises: error.BadDataException unsupported package version """ _, ext = os.path.splitext(file_path) if ext == '.fp': return PluginV1 elif ext == '.rpm': return PluginV2 raise error.BadDataException( 'Plugin {0} has unsupported format {1}'.format( file_path, ext))
def make_obj_by_name(cls, name, version): """Finds appropriate plugin class version, by plugin version and name. :param str name: :param str version: :returns: plugin class :raises: error.BadDataException unsupported package version """ plugin = cls.get_plugin(name, version) package_version = plugin['package_version'] if StrictVersion('1.0.0') <= \ StrictVersion(package_version) < \ StrictVersion('2.0.0'): return PluginV1 elif StrictVersion('2.0.0') <= StrictVersion(package_version): return PluginV2 raise error.BadDataException( 'Plugin {0}=={1} has unsupported package version {2}'.format( name, version, package_version))
def register(cls, name, version, force=False): """Tries to find plugin on file system, creates it in API service if it exists. :param str name: plugin name :param str version: plugin version :param bool force: if True updates meta information about the plugin even it does not support updates """ metadata = None for m in utils.glob_and_parse_yaml(METADATA_MASK): if m.get('version') == version and \ m.get('name') == name: metadata = m break if not metadata: raise error.BadDataException( 'Plugin {0} with version {1} does ' 'not exist, install it and try again'.format( name, version)) return cls.update_or_create(metadata, force=force)
def downgrade(cls, _): raise error.BadDataException( 'Downgrade action is not supported for old plugins with ' 'package version "1.0.0", you can install your plugin ' 'or use newer plugin format.')
def get_serializer(self, path): extension = os.path.splitext(path)[1][1:] if extension not in self.serializers: raise error.BadDataException( 'No serializer for provided file {0}'.format(path)) return self.serializers[extension]
def get_metadata(cls, plugin_tar): for member_name in plugin_tar.getnames(): if cls.metadata_config in member_name: return yaml.load(plugin_tar.extractfile(member_name).read()) raise error.BadDataException("Tarfile {0} doesn't have {1}".format( plugin_tar.name, cls.metadata_config))