def test_json_schema_must_have_properties_present(self): """ Given a config context schema object With a JSON schema with properties not present Assert calling clean on the config context schema object raises a ValidationError """ invalid_schema = ConfigContextSchema(name="invalid", slug="invalid", data_schema={"type": "object"}) with self.assertRaises(ValidationError): invalid_schema.full_clean()
def test_json_schema_must_be_an_object(self): """ Given a config context schema object With a JSON schema of type object Assert calling clean on the config context schema object raises a ValidationError """ invalid_schema = ConfigContextSchema(name="invalid", slug="invalid", data_schema=["not an object"]) with self.assertRaises(ValidationError): invalid_schema.full_clean()
def test_json_schema_must_have_type_set_to_object(self): """ Given a config context schema object With a JSON schema with type set to integer Assert calling clean on the config context schema object raises a ValidationError """ invalid_schema = ConfigContextSchema( name="invalid", slug="invalid", data_schema={"type": "integer", "properties": {"a": {"type": "string"}}} ) with self.assertRaises(ValidationError): invalid_schema.full_clean()
def test_invalid_json_schema_is_not_allowed(self): """ Given a config context schema object With an invalid JSON schema Assert calling clean on the config context schema object raises a ValidationError """ invalid_schema = ConfigContextSchema( name="invalid", slug="invalid", data_schema={"properties": {"this": "is not a valid json schema"}} ) with self.assertRaises(ValidationError): invalid_schema.full_clean()
def import_config_context_schema(context_schema_data, repository_record, job_result, logger): """Using data from schema file, create schema record in Nautobot.""" git_repository_content_type = ContentType.objects.get_for_model( GitRepository) created = False modified = False schema_metadata = context_schema_data.setdefault("_metadata", {}) if not schema_metadata.get("name"): raise RuntimeError("File has no name set.") try: schema_record = ConfigContextSchema.objects.get( name=schema_metadata["name"], owner_content_type=git_repository_content_type, owner_object_id=repository_record.pk, ) except ConfigContextSchema.DoesNotExist: schema_record = ConfigContextSchema( name=schema_metadata["name"], slug=slugify(schema_metadata["name"]), owner_content_type=git_repository_content_type, owner_object_id=repository_record.pk, data_schema=context_schema_data["data_schema"], ) created = True if schema_record.description != schema_metadata.get("description", ""): schema_record.description = schema_metadata.get("description", "") modified = True if schema_record.data_schema != context_schema_data["data_schema"]: schema_record.data_schema = context_schema_data["data_schema"] modified = True if created: schema_record.validated_save() job_result.log( "Successfully created config context schema", obj=schema_record, level_choice=LogLevelChoices.LOG_SUCCESS, grouping="config context schemas", logger=logger, ) elif modified: schema_record.validated_save() job_result.log( "Successfully refreshed config context schema", obj=schema_record, level_choice=LogLevelChoices.LOG_SUCCESS, grouping="config context schemas", logger=logger, ) else: job_result.log( "No change to config context schema", obj=schema_record, level_choice=LogLevelChoices.LOG_INFO, grouping="config context schemas", logger=logger, ) return schema_record.name if schema_record else None