Repository: pulp/pulpcore

Issue:
PuilpImport fails post-Domains (whether domains are enabled or not)
**Version**
core/3.23

**Describe the bug**
Domains adds a field to content "pulp_domain", which contains the pulp_id of the associated domain (or of the default-domain). Export exports that field, so import tries to hook it back up on the downstream side. The downstream "default" domain has a different pulp_domain, so the import fails with a traceback like this:
```
pulp [b2a311abd716444aada1d7ff310a5eb1]: pulpcore.tasking.pulpcore_worker:INFO:   File "/src/pulpcore/pulpcore/tasking/pulpcore_worker.py", line 458, in _perform_task
    result = func(*args, **kwargs)
  File "/src/pulpcore/pulpcore/app/tasks/importer.py", line 482, in pulp_import
    for ar_result in _import_file(os.path.join(temp_dir, ARTIFACT_FILE), ArtifactResource):
  File "/src/pulpcore/pulpcore/app/tasks/importer.py", line 143, in _import_file
    a_result = resource.import_data(data, raise_errors=True)
  File "/usr/local/lib/python3.8/site-packages/import_export/resources.py", line 813, in import_data
    result = self.import_data_inner(
  File "/usr/local/lib/python3.8/site-packages/import_export/resources.py", line 882, in import_data_inner
    raise row_result.errors[-1].error
  File "/usr/local/lib/python3.8/site-packages/import_export/resources.py", line 737, in import_row
    self.import_obj(instance, row, dry_run, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/import_export/resources.py", line 557, in import_obj
    self.import_field(field, obj, data, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/import_export/resources.py", line 540, in import_field
    field.save(obj, data, is_m2m, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/import_export/fields.py", line 119, in save
    cleaned = self.clean(data, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/import_export/fields.py", line 75, in clean
    value = self.widget.clean(value, row=data, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/import_export/widgets.py", line 423, in clean
    return self.get_queryset(value, row, **kwargs).get(**{self.field: val})
  File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 435, in get
    raise self.model.DoesNotExist(
```

**To Reproduce**
* Sync a repo.
* Export it.
* Reset the pulp-database (or move the export to a different instance)
* Attempt to import it.

**Expected behavior**
Import succeeds.






Discussion:

Resolving patch:
diff --git a/pulpcore/app/modelresource.py b/pulpcore/app/modelresource.py
--- a/pulpcore/app/modelresource.py
+++ b/pulpcore/app/modelresource.py
@@ -31,6 +31,8 @@ def before_import_row(self, row, **kwargs):
             kwargs: args passed along from the import() call.
 
         """
+        super().before_import_row(row, **kwargs)
+
         # the export converts None to blank strings but sha384 and sha512 have unique constraints
         # that get triggered if they are blank. convert checksums back into None if they are blank.
         for checksum in ALL_KNOWN_CONTENT_CHECKSUMS:
@@ -44,13 +46,19 @@ class Meta:
             "pulp_created",
             "pulp_last_updated",
         )
-        import_id_fields = ("sha256",)
+        import_id_fields = (
+            "pulp_domain",
+            "sha256",
+        )
 
 
 class RepositoryResource(QueryModelResource):
     class Meta:
         model = Repository
-        import_id_fields = ("name",)
+        import_id_fields = (
+            "pulp_domain",
+            "name",
+        )
         exclude = (
             "pulp_id",
             "pulp_created",
@@ -98,6 +106,7 @@ def before_import_row(self, row, **kwargs):
         Returns:
             (tablib.Dataset row): row that now points to the new downstream uuid for its content.
         """
+        super().before_import_row(row, **kwargs)
 
         linked_content = Content.objects.get(upstream_id=row["content"])
         row["content"] = str(linked_content.pulp_id)
diff --git a/pulpcore/plugin/importexport.py b/pulpcore/plugin/importexport.py
--- a/pulpcore/plugin/importexport.py
+++ b/pulpcore/plugin/importexport.py
@@ -1,4 +1,5 @@
 from import_export import resources
+from pulpcore.app.util import get_domain_pk
 
 
 class QueryModelResource(resources.ModelResource):
@@ -19,6 +20,21 @@ class QueryModelResource(resources.ModelResource):
             (driven by repo_version)
     """
 
+    def before_import_row(self, row, **kwargs):
+        """
+        Sets pulp_domain/_pulp_domain to the current-domain on import.
+        Args:
+            row (tablib.Dataset row): incoming import-row representing a single Variant.
+            kwargs: args passed along from the import() call.
+        """
+        # There is probably a more pythonic/elegant way to do the following - but I am deliberately
+        # opting for "verbose but COMPLETELY CLEAR" here.
+        if "_pulp_domain" in row:
+            row["_pulp_domain"] = get_domain_pk()
+
+        if "pulp_domain" in row:
+            row["pulp_domain"] = get_domain_pk()
+
     def set_up_queryset(self):
         return None
 


Test patch: