upload/pulp: list and create repositories

This commit is contained in:
Achilleas Koutsou 2023-08-16 16:01:46 +02:00
parent bc6c2fb6af
commit 1365347382

View file

@ -2,6 +2,8 @@ package pulp
import (
"context"
"fmt"
"io"
"net/http"
"github.com/osbuild/pulp-client/pulpclient"
@ -41,3 +43,53 @@ func NewClient(url string, creds *Credentials) *Client {
ctx: ctx,
}
}
// readBody returns the body of a response as a string and ignores
// errors. Useful for returning details from failed requests.
func readBody(r *http.Response) string {
if r == nil {
return ""
}
b, err := io.ReadAll(r.Body)
if err != nil {
return ""
}
return string(b)
}
// ListOSTreeRepositories returns a map (repository name -> pulp href) of
// existing ostree repositories.
func (cl *Client) ListOSTreeRepositories() (map[string]string, error) {
list, resp, err := cl.client.RepositoriesOstreeAPI.RepositoriesOstreeOstreeList(cl.ctx).Execute()
if err != nil {
return nil, fmt.Errorf("repository list request returned an error: %s (%s)", err.Error(), readBody(resp))
}
repos := make(map[string]string, list.GetCount())
for _, repo := range list.GetResults() {
name := repo.Name
href := repo.GetPulpHref()
repos[name] = href
}
return repos, nil
}
// CreateOSTreeRepository creates a new ostree repository with a name and description
// and returns the pulp href.
func (cl *Client) CreateOSTreeRepository(name, description string) (string, error) {
req := cl.client.RepositoriesOstreeAPI.RepositoriesOstreeOstreeCreate(cl.ctx)
repo := pulpclient.OstreeOstreeRepository{
Name: name,
}
if description != "" {
repo.Description = *pulpclient.NewNullableString(&description)
}
req = req.OstreeOstreeRepository(repo)
result, resp, err := req.Execute()
if err != nil {
return "", fmt.Errorf("repository creation failed: %s (%s)", err.Error(), readBody(resp))
}
return result.GetPulpHref(), nil
}