-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
backend: add support for pulp domains #3311
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
import os | ||
import tomllib | ||
import requests | ||
from six.moves.urllib.parse import urlencode | ||
|
||
|
||
class PulpClient: | ||
|
@@ -53,7 +52,20 @@ def url(self, endpoint): | |
""" | ||
A fully qualified URL for a given API endpoint | ||
""" | ||
return self.config["base_url"] + self.config["api_root"] + endpoint | ||
domain = self.config["domain"] | ||
if domain == "default": | ||
domain = "" | ||
|
||
relative = os.path.normpath("/".join([ | ||
self.config["api_root"], | ||
domain, | ||
endpoint, | ||
])) | ||
|
||
# Normpath removes the trailing slash. If it was there, put it back | ||
if endpoint[-1] == "/": | ||
relative += "/" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. os.path.join() preserves trailing slashes |
||
return self.config["base_url"] + relative | ||
|
||
@property | ||
def request_params(self): | ||
|
@@ -79,7 +91,7 @@ def get_repository(self, name): | |
# There is no endpoint for querying a single repository by its name, | ||
# even Pulp CLI does this workaround | ||
url = self.url("api/v3/repositories/rpm/rpm/?") | ||
url += urlencode({"name": name, "offset": 0, "limit": 1}) | ||
url += self._urlencode({"name": name, "offset": 0, "limit": 1}) | ||
return requests.get(url, **self.request_params) | ||
|
||
def get_distribution(self, name): | ||
|
@@ -90,9 +102,19 @@ def get_distribution(self, name): | |
# There is no endpoint for querying a single repository by its name, | ||
# even Pulp CLI does this workaround | ||
url = self.url("api/v3/distributions/rpm/rpm/?") | ||
url += urlencode({"name": name, "offset": 0, "limit": 1}) | ||
url += self._urlencode({"name": name, "offset": 0, "limit": 1}) | ||
return requests.get(url, **self.request_params) | ||
|
||
def _urlencode(self, query): | ||
""" | ||
Join a dict into URL query string but don't encode special characters | ||
https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode | ||
Our repository names are e.g. frostyx/test-pulp/fedora-39-x86_64. | ||
The standard urlencode would change the slashes to %2F making Pulp to | ||
not find the project when filtering by name. | ||
""" | ||
return "&".join([f"{k}={v}" for k, v in query.items()]) | ||
|
||
def create_distribution(self, name, repository, basepath=None): | ||
""" | ||
Create an RPM distribution | ||
|
@@ -111,7 +133,7 @@ def create_publication(self, repository): | |
Create an RPM publication | ||
https://docs.pulpproject.org/pulp_rpm/restapi.html#tag/Publications:-Rpm/operation/publications_rpm_rpm_create | ||
""" | ||
url = self.url("api/v3/publications/rpm/rpm") | ||
url = self.url("api/v3/publications/rpm/rpm/") | ||
data = {"repository": repository} | ||
return requests.post(url, json=data, **self.request_params) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Test Pulp client | ||
""" | ||
|
||
# pylint: disable=attribute-defined-outside-init | ||
|
||
from copr_backend.pulp import PulpClient | ||
|
||
|
||
class TestPulp: | ||
|
||
def setup_method(self, _method): | ||
self.config = { | ||
|
||
"api_root": "/pulp/", | ||
"base_url": "http://pulp.fpo:24817", | ||
"cert": "", | ||
"domain": "default", | ||
"dry_run": False, | ||
"format": "json", | ||
"key": "", | ||
"password": "1234", | ||
"timeout": 0, | ||
"username": "admin", | ||
"verbose": 0, | ||
"verify_ssl": True, | ||
} | ||
|
||
def test_url(self): | ||
client = PulpClient(self.config) | ||
assert self.config["domain"] == "default" | ||
assert client.url("api/v3/artifacts/")\ | ||
== "http://pulp.fpo:24817/pulp/api/v3/artifacts/" | ||
|
||
assert client.url("api/v3/repositories/rpm/rpm/?")\ | ||
== "http://pulp.fpo:24817/pulp/api/v3/repositories/rpm/rpm/?" | ||
|
||
self.config["domain"] = "copr" | ||
assert client.url("api/v3/artifacts/")\ | ||
== "http://pulp.fpo:24817/pulp/copr/api/v3/artifacts/" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we use
os.path
for urls already (sub-optimal), don't you want to useos.path.join()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with
os.path.join
is that it does something like this:while the
"/".join
variant produces something more expectedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, OK