target: add new fields to target and cleanup

We need some more fields in target struct to support weldr API. This commit
introduces them. Also a bit of cleanup is done.
This commit is contained in:
Ondřej Budai 2019-11-26 20:07:44 +01:00 committed by Tom Gundersen
parent 364ea62f59
commit df7e14d5eb
4 changed files with 44 additions and 24 deletions

View file

@ -11,8 +11,5 @@ type AWSTargetOptions struct {
func (AWSTargetOptions) isTargetOptions() {} func (AWSTargetOptions) isTargetOptions() {}
func NewAWSTarget(options *AWSTargetOptions) *Target { func NewAWSTarget(options *AWSTargetOptions) *Target {
return &Target{ return newTarget("org.osbuild.aws", options)
Name: "org.osbuild.aws",
Options: options,
}
} }

View file

@ -9,8 +9,5 @@ type AzureTargetOptions struct {
func (AzureTargetOptions) isTargetOptions() {} func (AzureTargetOptions) isTargetOptions() {}
func NewAzureTarget(options *AzureTargetOptions) *Target { func NewAzureTarget(options *AzureTargetOptions) *Target {
return &Target{ return newTarget("org.osbuild.azure", options)
Name: "org.osbuild.azure",
Options: options,
}
} }

View file

@ -7,8 +7,5 @@ type LocalTargetOptions struct {
func (LocalTargetOptions) isTargetOptions() {} func (LocalTargetOptions) isTargetOptions() {}
func NewLocalTarget(options *LocalTargetOptions) *Target { func NewLocalTarget(options *LocalTargetOptions) *Target {
return &Target{ return newTarget("org.osbuild.local", options)
Name: "org.osbuild.local",
Options: options,
}
} }

View file

@ -3,20 +3,39 @@ package target
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/google/uuid"
"time"
) )
type Target struct { type Target struct {
Uuid uuid.UUID `json:"uuid"`
ImageName string `json:"image_name"` ImageName string `json:"image_name"`
Name string `json:"name"` Name string `json:"name"`
Created time.Time `json:"created"`
Status string `json:"status"`
Options TargetOptions `json:"options"` Options TargetOptions `json:"options"`
} }
func newTarget(name string, options TargetOptions) *Target {
return &Target{
Uuid: uuid.New(),
Name: name,
Created: time.Now(),
Status: "WAITING",
Options: options,
}
}
type TargetOptions interface { type TargetOptions interface {
isTargetOptions() isTargetOptions()
} }
type rawTarget struct { type rawTarget struct {
Uuid uuid.UUID `json:"uuid"`
ImageName string `json:"image_name"`
Name string `json:"name"` Name string `json:"name"`
Created time.Time `json:"created"`
Status string `json:"status"`
Options json.RawMessage `json:"options"` Options json.RawMessage `json:"options"`
} }
@ -26,8 +45,24 @@ func (target *Target) UnmarshalJSON(data []byte) error {
if err != nil { if err != nil {
return err return err
} }
options, err := UnmarshalTargetOptions(rawTarget.Name, rawTarget.Options)
if err != nil {
return err
}
target.Uuid = rawTarget.Uuid
target.ImageName = rawTarget.ImageName
target.Name = rawTarget.Name
target.Created = rawTarget.Created
target.Status = rawTarget.Status
target.Options = options
return nil
}
func UnmarshalTargetOptions(targetName string, rawOptions json.RawMessage) (TargetOptions, error) {
var options TargetOptions var options TargetOptions
switch rawTarget.Name { switch targetName {
case "org.osbuild.azure": case "org.osbuild.azure":
options = new(AzureTargetOptions) options = new(AzureTargetOptions)
case "org.osbuild.aws": case "org.osbuild.aws":
@ -35,15 +70,9 @@ func (target *Target) UnmarshalJSON(data []byte) error {
case "org.osbuild.local": case "org.osbuild.local":
options = new(LocalTargetOptions) options = new(LocalTargetOptions)
default: default:
return errors.New("unexpected target name") return nil, errors.New("unexpected target name")
}
err = json.Unmarshal(rawTarget.Options, options)
if err != nil {
return err
} }
err := json.Unmarshal(rawOptions, options)
target.Name = rawTarget.Name return options, err
target.Options = options
return nil
} }