From df7e14d5eb40ac96c6b898ebd069d54d117a92a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Budai?= Date: Tue, 26 Nov 2019 20:07:44 +0100 Subject: [PATCH] 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. --- internal/target/aws_target.go | 5 +--- internal/target/azure_target.go | 5 +--- internal/target/local_target.go | 5 +--- internal/target/target.go | 53 +++++++++++++++++++++++++-------- 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/internal/target/aws_target.go b/internal/target/aws_target.go index 5048c3aba..d54172afa 100644 --- a/internal/target/aws_target.go +++ b/internal/target/aws_target.go @@ -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) } diff --git a/internal/target/azure_target.go b/internal/target/azure_target.go index 10ad8434c..9b9a2e2fd 100644 --- a/internal/target/azure_target.go +++ b/internal/target/azure_target.go @@ -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) } diff --git a/internal/target/local_target.go b/internal/target/local_target.go index 816354bba..35209d524 100644 --- a/internal/target/local_target.go +++ b/internal/target/local_target.go @@ -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) } diff --git a/internal/target/target.go b/internal/target/target.go index f66d1f6fc..19d8abd52 100644 --- a/internal/target/target.go +++ b/internal/target/target.go @@ -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 }