Repository: pyinfra-dev/pyinfra

Issue:
Return repoid in `DnfRepositories`
## Is your feature request related to a problem? Please describe

Currently, `dnf.DnfRepositories` fact returns a list of repositories with key-value pairs from the respective `.repo` files. This does not include the repoid, which is the part in square brackets. This would be useful, as DNF uses repoid for a lot of operations.

## Describe the solution you'd like

Currently, the fact parses the repoid as `name`:

https://github.com/pyinfra-dev/pyinfra/blob/6fa182dfde25ba64e65bc895b31c2ef40bc43201/pyinfra/facts/util/packaging.py#L35

But since DNF repos often have a key called `name`, it gets overwritten. E.g., in AlmaLinux 9 (which I am testing under), _every_ repo has a `name`, so the return value only has human-readable names

I suggest that we parse it as `repoid`. I guess we will have to keep the `name` in there as well, because otherwise it would be a breaking change...

I'll prepare a PR.



Discussion:

Patch:
diff --git a/pyinfra/facts/dnf.py b/pyinfra/facts/dnf.py
index 5f80727b0..f20e5dbda 100644
--- a/pyinfra/facts/dnf.py
+++ b/pyinfra/facts/dnf.py
@@ -16,11 +16,15 @@ class DnfRepositories(FactBase):
 
         [
             {
-                "name": "CentOS-$releasever - Apps",
-                "baseurl": "http://mirror.centos.org/$contentdir/$releasever/Apps/$basearch/os/",
-                "gpgcheck": "1",
+                "repoid": "baseos",
+                "name": "AlmaLinux $releasever - BaseOS",
+                "mirrorlist": "https://mirrors.almalinux.org/mirrorlist/$releasever/baseos",
                 "enabled": "1",
-                "gpgkey": "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial",
+                "gpgcheck": "1",
+                "countme": "1",
+                "gpgkey": "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux-9",
+                "metadata_expire": "86400",
+                "enabled_metadata": "1"
             },
         ]
     """
diff --git a/pyinfra/facts/util/packaging.py b/pyinfra/facts/util/packaging.py
index 22442823e..017d23cf2 100644
--- a/pyinfra/facts/util/packaging.py
+++ b/pyinfra/facts/util/packaging.py
@@ -32,6 +32,7 @@ def _parse_yum_or_zypper_repositories(output):
                 repos.append(current_repo)
                 current_repo = {}
 
+            current_repo["repoid"] = line[1:-1]
             current_repo["name"] = line[1:-1]
 
         if current_repo and "=" in line:
diff --git a/pyinfra/facts/yum.py b/pyinfra/facts/yum.py
index 437d41d65..caf792365 100644
--- a/pyinfra/facts/yum.py
+++ b/pyinfra/facts/yum.py
@@ -16,11 +16,15 @@ class YumRepositories(FactBase):
 
         [
             {
-                "name": "CentOS-$releasever - Apps",
-                "baseurl": "http://mirror.centos.org/$contentdir/$releasever/Apps/$basearch/os/",
-                "gpgcheck": "1",
+                "repoid": "baseos",
+                "name": "AlmaLinux $releasever - BaseOS",
+                "mirrorlist": "https://mirrors.almalinux.org/mirrorlist/$releasever/baseos",
                 "enabled": "1",
-                "gpgkey": "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial",
+                "gpgcheck": "1",
+                "countme": "1",
+                "gpgkey": "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux-9",
+                "metadata_expire": "86400",
+                "enabled_metadata": "1"
             },
         ]
     """
diff --git a/pyinfra/facts/zypper.py b/pyinfra/facts/zypper.py
index aef4da801..55963f97e 100644
--- a/pyinfra/facts/zypper.py
+++ b/pyinfra/facts/zypper.py
@@ -16,11 +16,11 @@ class ZypperRepositories(FactBase):
 
         [
             {
+                "repoid": "repo-oss",
                 "name": "Main Repository",
                 "enabled": "1",
-                "autorefresh": "0",
-                "baseurl": "http://download.opensuse.org/distribution/leap/$releasever/repo/oss/",
-                "type": "rpm-md",
+                "autorefresh": "1",
+                "baseurl": "http://download.opensuse.org/distribution/leap/$releasever/repo/oss/"
             },
         ]
     """


Test patch:
diff --git a/tests/facts/dnf.DnfRepositories/repos-with-spaces.json b/tests/facts/dnf.DnfRepositories/repos-with-spaces.json
index c0d0ae27f..cc556dadc 100644
--- a/tests/facts/dnf.DnfRepositories/repos-with-spaces.json
+++ b/tests/facts/dnf.DnfRepositories/repos-with-spaces.json
@@ -7,6 +7,7 @@
     ],
     "fact": [
         {
+            "repoid": "rhel-atomic-7-cdk-3.6-source-rpms",
             "name": "Red Hat Container Development Kit 3.6 /(Source RPMs)"
         }
     ]
diff --git a/tests/facts/dnf.DnfRepositories/repos.json b/tests/facts/dnf.DnfRepositories/repos.json
index 6741299f2..5bac4cb44 100644
--- a/tests/facts/dnf.DnfRepositories/repos.json
+++ b/tests/facts/dnf.DnfRepositories/repos.json
@@ -10,10 +10,12 @@
     ],
     "fact": [
         {
+            "repoid": "somerepo",
             "name": "somerepo",
             "baseurl": "abc"
         },
         {
+            "repoid": "anotherrepo",
             "name": "anotherrepo"
         }
     ]
diff --git a/tests/facts/yum.YumRepositories/repos.json b/tests/facts/yum.YumRepositories/repos.json
index d310a861d..091ac312c 100644
--- a/tests/facts/yum.YumRepositories/repos.json
+++ b/tests/facts/yum.YumRepositories/repos.json
@@ -10,6 +10,7 @@
     ],
     "fact": [
         {
+            "repoid": "somerepo",
             "name": "somerepo",
             "baseurl": "abc"
         }
diff --git a/tests/facts/zypper.ZypperRepositories/repos.json b/tests/facts/zypper.ZypperRepositories/repos.json
index 0183262c4..7db623ee5 100644
--- a/tests/facts/zypper.ZypperRepositories/repos.json
+++ b/tests/facts/zypper.ZypperRepositories/repos.json
@@ -10,6 +10,7 @@
     ],
     "fact": [
         {
+            "repoid": "somerepo",
             "name": "somerepo",
             "baseurl": "abc"
         }