def export_config(recipe_types=None, job_types=None, errors=None): recipe_types = recipe_types or [] job_types = job_types or [] errors = errors or [] export_config = { 'version': '1.0', 'recipe_types': [ serializers.ConfigurationRecipeTypeSerializer(r).data for r in recipe_types ], 'job_types': [ serializers.ConfigurationJobTypeSerializer(j).data for j in job_types ], 'errors': [serializers.ConfigurationErrorSerializer(e).data for e in errors], } return Configuration(export_config)
def _import_recipe_type(recipe_type_dict, recipe_type=None): """Attempts to apply the given recipe types configuration to the system. Note that proper model locking must be performed before calling this method. :param recipe_type_dict: A dictionary of recipe type configuration changes to import. :type recipe_type_dict: dict :param recipe_type: The existing recipe type model to update if applicable. :type recipe_type: :class:`recipe.models.RecipeType` :returns: A list of warnings discovered during import. :rtype: list[:class:`port.schema.ValidationWarning`] :raises :class:`port.schema.InvalidConfiguration`: If any part of the configuration violates the specification. """ warnings = [] # Parse the JSON content into validated model fields recipe_type_serializer = serializers.ConfigurationRecipeTypeSerializer(recipe_type, data=recipe_type_dict) if not recipe_type_serializer.is_valid(): raise InvalidConfiguration('Invalid recipe type schema: %s -> %s' % (recipe_type_dict['name'], recipe_type_serializer.errors)) result = recipe_type_serializer.validated_data # Validate the recipe definition try: definition_dict = None if 'definition' in result: definition_dict = result.get('definition') elif recipe_type: definition_dict = recipe_type.definition definition = RecipeDefinitionSunset.create(definition_dict) warnings.extend(definition.validate_job_interfaces()) except (InvalidDefinition, InvalidRecipeConnection) as ex: raise InvalidConfiguration('Recipe type definition invalid: %s -> %s' % (result.get('name'), unicode(ex))) # Validate the trigger rule trigger_rule = None if 'trigger_rule' in result and result.get('trigger_rule'): trigger_rule = TriggerRule(**result.get('trigger_rule')) if trigger_rule: trigger_config = trigger_rule.get_configuration() if not isinstance(trigger_config, RecipeTriggerRuleConfiguration): logger.exception('Recipe type trigger rule type invalid') raise InvalidConfiguration('Recipe type trigger type invalid: %s -> %s' % (result.get('name'), trigger_rule.type)) try: warnings.extend(trigger_config.validate_trigger_for_recipe(definition)) # Create a new rule when the trigger content was provided if recipe_type_dict.get('trigger_rule'): rule_handler = trigger_handler.get_trigger_rule_handler(trigger_rule.type) trigger_rule = rule_handler.create_trigger_rule(trigger_rule.configuration, trigger_rule.name, trigger_rule.is_active) except (InvalidDefinition, InvalidTriggerType, InvalidTriggerRule, InvalidRecipeConnection) as ex: logger.exception('Recipe type trigger rule invalid') raise InvalidConfiguration('Recipe type trigger rule invalid: %s -> %s' % (result.get('name'), unicode(ex))) remove_trigger_rule = 'trigger_rule' in recipe_type_dict and not recipe_type_dict['trigger_rule'] # Edit or create the associated recipe type model if recipe_type: try: RecipeType.objects.edit_recipe_type(recipe_type.id, result.get('title'), result.get('description'), definition, trigger_rule, remove_trigger_rule) except (InvalidDefinition, InvalidTriggerType, InvalidTriggerRule, InvalidRecipeConnection) as ex: logger.exception('Recipe type edit failed') raise InvalidConfiguration('Unable to edit recipe type: %s -> %s' % (result.get('name'), unicode(ex))) else: try: RecipeType.objects.create_recipe_type(result.get('name'), result.get('version'), result.get('title'), result.get('description'), definition, trigger_rule) except (InvalidDefinition, InvalidTriggerType, InvalidTriggerRule, InvalidRecipeConnection) as ex: logger.exception('Recipe type create failed') raise InvalidConfiguration('Unable to create new recipe type: %s -> %s' % (result.get('name'), unicode(ex))) return warnings