debian-forge-composer/internal/target/targetresult.go
Ygal Blum 3231aabbc0 cloudapi: add support for uploading to a container registry
Worker
------
Add configuration for the default container registry.
Use the default container registry if not provided as part
of the image name.
When using the default registry use the configured values
Return the image url as part of the result.

Composer Worker API
-------------------
Add `ContainerTargetResultOptions` to return the image url

Composer API
------------
Add UploadOptions to allow setting of the image name and tag
Add UploadStatus to return the url of the uploaded image

Co-Developed-By: Christian Kellner <christian@kellner.me>
2022-08-01 21:50:03 +01:00

78 lines
2 KiB
Go

package target
import (
"encoding/json"
"fmt"
"github.com/osbuild/osbuild-composer/internal/worker/clienterrors"
)
type TargetResult struct {
Name TargetName `json:"name"`
Options TargetResultOptions `json:"options,omitempty"`
TargetError *clienterrors.Error `json:"target_error,omitempty"`
}
func newTargetResult(name TargetName, options TargetResultOptions) *TargetResult {
return &TargetResult{
Name: name,
Options: options,
}
}
type TargetResultOptions interface {
isTargetResultOptions()
}
type rawTargetResult struct {
Name TargetName `json:"name"`
Options json.RawMessage `json:"options,omitempty"`
TargetError *clienterrors.Error `json:"target_error,omitempty"`
}
func (targetResult *TargetResult) UnmarshalJSON(data []byte) error {
var rawTR rawTargetResult
err := json.Unmarshal(data, &rawTR)
if err != nil {
return err
}
var options TargetResultOptions
// No options may be set if there was a target error.
// In addition, some targets don't set any options.
if len(rawTR.Options) > 0 {
options, err = UnmarshalTargetResultOptions(rawTR.Name, rawTR.Options)
if err != nil {
return err
}
}
targetResult.Name = rawTR.Name
targetResult.Options = options
targetResult.TargetError = rawTR.TargetError
return nil
}
func UnmarshalTargetResultOptions(trName TargetName, rawOptions json.RawMessage) (TargetResultOptions, error) {
var options TargetResultOptions
switch trName {
case TargetNameAWS:
options = new(AWSTargetResultOptions)
case TargetNameAWSS3:
options = new(AWSS3TargetResultOptions)
case TargetNameGCP:
options = new(GCPTargetResultOptions)
case TargetNameAzureImage:
options = new(AzureImageTargetResultOptions)
case TargetNameKoji:
options = new(KojiTargetResultOptions)
case TargetNameOCI:
options = new(OCITargetResultOptions)
case TargetNameContainer:
options = new(ContainerTargetResultOptions)
default:
return nil, fmt.Errorf("unexpected target result name: %s", trName)
}
err := json.Unmarshal(rawOptions, options)
return options, err
}