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 NewAWSTarget(options *AWSTargetOptions) *Target {
return &Target{
Name: "org.osbuild.aws",
Options: options,
}
return newTarget("org.osbuild.aws", options)
}

View file

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

View file

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

View file

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