blueprint: add proper error handling for querying invalid output types

Otherwise an invalid output type would crash composer.

Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
Tom Gundersen 2019-10-07 23:23:47 +02:00 committed by Lars Karlitski
parent 976d59cfda
commit 4845826048
2 changed files with 32 additions and 8 deletions

View file

@ -7,6 +7,14 @@ import (
"sort"
)
type InvalidOutputFormatError struct {
message string
}
func (e *InvalidOutputFormatError) Error() string {
return e.message
}
// A Blueprint is a high-level description of an image.
type Blueprint struct {
Name string `json:"name"`
@ -53,13 +61,20 @@ func ListOutputFormats() []string {
}
// ToPipeline converts the blueprint to a pipeline for a given output format.
func (b *Blueprint) ToPipeline(outputFormat string) *pipeline.Pipeline {
return outputs[outputFormat].translate(b)
func (b *Blueprint) ToPipeline(outputFormat string) (*pipeline.Pipeline, error) {
if output, exists := outputs[outputFormat]; exists {
return output.translate(b), nil
}
return nil, &InvalidOutputFormatError{outputFormat}
}
// FilenameFromType gets the canonical filename and MIME type for a given
// output format
func FilenameFromType(outputFormat string) (string, string) {
translator := outputs[outputFormat]
return translator.getName(), translator.getMime()
func FilenameFromType(outputFormat string) (string, string, error) {
if output, exists := outputs[outputFormat]; exists {
return output.getName(), output.getMime(), nil
}
return "", "", &InvalidOutputFormatError{outputFormat}
}

View file

@ -340,10 +340,14 @@ func (s *Store) DeleteBlueprintFromWorkspace(name string) {
})
}
func (s *Store) PushCompose(composeID uuid.UUID, bp *blueprint.Blueprint, composeType string) {
func (s *Store) PushCompose(composeID uuid.UUID, bp *blueprint.Blueprint, composeType string) error {
targets := []*target.Target{
target.NewLocalTarget(target.NewLocalTargetOptions("/var/lib/osbuild-composer/outputs/" + composeID.String())),
}
pipeline, err := bp.ToPipeline(composeType)
if err != nil {
return &InvalidRequestError{"invalid output type: " + composeType}
}
s.change(func() error {
s.Composes[composeID] = Compose{
QueueStatus: "WAITING",
@ -356,9 +360,11 @@ func (s *Store) PushCompose(composeID uuid.UUID, bp *blueprint.Blueprint, compos
})
s.pendingJobs <- Job{
ComposeID: composeID,
Pipeline: bp.ToPipeline(composeType),
Pipeline: pipeline,
Targets: targets,
}
return nil
}
func (s *Store) PopCompose() Job {
@ -408,7 +414,10 @@ func (s *Store) GetImage(composeID uuid.UUID) (*Image, error) {
if compose.QueueStatus != "FINISHED" {
return nil, &InvalidRequestError{"compose was not finished"}
}
name, mime := blueprint.FilenameFromType(compose.OutputType)
name, mime, err := blueprint.FilenameFromType(compose.OutputType)
if err != nil {
panic("invalid output type")
}
for _, t := range compose.Targets {
switch options := t.Options.(type) {
case *target.LocalTargetOptions: