rcm: introduce rpmmd member of the api structure

This is needed for unit tests, because it wasn't possible to mock the
rpmmd module before. This also requires that the checksum is moved to
the compose request and evaluated in the endpoint handler instead of
push compose. I think it makes sense to have the checksum in the compose
request directly.

Also a "module platform ID" is required now, but we don't have the
"global" distribution any more, so this patch introduces mapping from a
distribution to the module platform ID.
This commit is contained in:
Martin Sehnoutka 2020-02-19 11:50:38 +01:00 committed by Tom Gundersen
parent d1c766abe7
commit 923a0b0b97
5 changed files with 72 additions and 20 deletions

View file

@ -18,6 +18,16 @@ func (err *CustomJsonConversionError) Error() string {
return err.reason
}
// CustomTypeError is thrown when parsing strings into enumerations
type CustomTypeError struct {
reason string
}
// Error returns the error as a string
func (err *CustomTypeError) Error() string {
return err.reason
}
// Since Go has no generics, this is the only way to write common code for all the types present in this package.
// It uses weakly typed maps to convert between strings and integers which are then wrapped into type aliases.
// Specific implementations are bellow each type.
@ -243,6 +253,7 @@ const (
// getArchMapping is a helper function that defines the conversion from JSON string value
// to Distribution.
func getDistributionMapping() map[string]int {
// Don't forget to add module-platform-id mapping below
mapping := map[string]int{
"fedora-30": int(Fedora30),
"fedora-31": int(Fedora31),
@ -252,6 +263,22 @@ func getDistributionMapping() map[string]int {
return mapping
}
func (dist *Distribution) ModulePlatformID() (string, error) {
mapping := map[Distribution]string{
// TODO: this could be refactored so we don't have these strings hard coded in two places
Fedora30: "platform:f30",
Fedora31: "platform:f31",
Fedora32: "platform:f32",
RHEL82: "platform:el8",
}
id, exists := mapping[*dist]
if !exists {
return "", &CustomTypeError{reason: "Distribution does not have a module platform ID assigned"}
} else {
return id, nil
}
}
func ListDistributions() []string {
return listHelper(getDistributionMapping())
}
@ -325,5 +352,6 @@ type ComposeRequest struct {
Distro Distribution `json:"distro"`
Arch Architecture `json:"arch"`
Repositories []rpmmd.RepoConfig `json:"repositories"`
Checksums map[string]string `json:"checksums"`
RequestedImages []ImageRequest `json:"requested_images"`
}