cloudapi: support ostree options

Move OSTree option handling outside of the weldr API to make it usable
by other packages. New subpackage at internal/ostree.

Add support for ostree options ("Ref" and "URL") in the Cloud API.
Validate OSTree options and resolve the parent reference the same way as
in the Weldr API.

Unlike the Weldr API, the Cloud API doesn't support specifying the
Parent reference directly.

The exports list is included in the job information on the queue.
This commit is contained in:
Achilleas Koutsou 2021-05-25 23:03:03 +02:00 committed by Tom Gundersen
parent d701d237d0
commit b2f5e1cd72
5 changed files with 157 additions and 100 deletions

53
internal/ostree/ostree.go Normal file
View file

@ -0,0 +1,53 @@
package ostree
import (
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"regexp"
"strings"
)
var ostreeRefRE = regexp.MustCompile(`^(?:[\w\d][-._\w\d]*\/)*[\w\d][-._\w\d]*$`)
type OSTreeRequest struct {
URL string `json:"url"`
Ref string `json:"ref"`
Parent string `json:"parent"`
}
func VerifyRef(ref string) bool {
if len(ref) > 0 && ostreeRefRE.MatchString(ref) {
return true
}
return false
}
func ResolveRef(location, ref string) (string, error) {
u, err := url.Parse(location)
if err != nil {
return "", err
}
u.Path = path.Join(u.Path, "refs/heads/", ref)
resp, err := http.Get(u.String())
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("ostree repository %q returned status: %s", u.String(), resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
parent := strings.TrimSpace(string(body))
// Check that this is at least a hex string.
_, err = hex.DecodeString(parent)
if err != nil {
return "", fmt.Errorf("ostree repository %q returned invalid reference", u.String())
}
return parent, nil
}