jobqueue: start moving API types to json.go
Similar to `weldr/json.go`, this file will contain structs that are shared between client and server and only meant to be used to serialize from and to JSON. This immediate change allows us to make fields in `Job` private in a future commit.
This commit is contained in:
parent
9a96908c1e
commit
269988a737
4 changed files with 31 additions and 11 deletions
|
|
@ -102,7 +102,7 @@ func (api *API) addJobHandler(writer http.ResponseWriter, request *http.Request,
|
|||
|
||||
writer.WriteHeader(http.StatusCreated)
|
||||
// FIXME: handle or comment this possible error
|
||||
_ = json.NewEncoder(writer).Encode(Job{
|
||||
_ = json.NewEncoder(writer).Encode(addJobResponse{
|
||||
ID: nextJob.ComposeID,
|
||||
ImageBuildID: nextJob.ImageBuildID,
|
||||
Manifest: nextJob.Manifest,
|
||||
|
|
@ -130,7 +130,7 @@ func (api *API) updateJobHandler(writer http.ResponseWriter, request *http.Reque
|
|||
return
|
||||
}
|
||||
|
||||
var body JobStatus
|
||||
var body updateJobRequest
|
||||
err = json.NewDecoder(request.Body).Decode(&body)
|
||||
if err != nil {
|
||||
statusResponseError(writer, http.StatusBadRequest, "invalid status: "+err.Error())
|
||||
|
|
|
|||
|
|
@ -71,18 +71,18 @@ func (c *Client) AddJob() (*Job, error) {
|
|||
return nil, fmt.Errorf("couldn't create job, got %d: %s", response.StatusCode, r)
|
||||
}
|
||||
|
||||
job := &Job{}
|
||||
err = json.NewDecoder(response.Body).Decode(job)
|
||||
var jr addJobResponse
|
||||
err = json.NewDecoder(response.Body).Decode(&jr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return job, nil
|
||||
return NewJob(jr.ID, jr.ImageBuildID, jr.Manifest, jr.Targets), nil
|
||||
}
|
||||
|
||||
func (c *Client) UpdateJob(job *Job, status common.ImageBuildState, result *common.ComposeResult) error {
|
||||
var b bytes.Buffer
|
||||
err := json.NewEncoder(&b).Encode(&JobStatus{status, result})
|
||||
err := json.NewEncoder(&b).Encode(&updateJobRequest{status, result})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,11 +24,6 @@ type Job struct {
|
|||
Targets []*target.Target `json:"targets"`
|
||||
}
|
||||
|
||||
type JobStatus struct {
|
||||
Status common.ImageBuildState `json:"status"`
|
||||
Result *common.ComposeResult `json:"result"`
|
||||
}
|
||||
|
||||
type TargetsError struct {
|
||||
Errors []error
|
||||
}
|
||||
|
|
@ -43,6 +38,10 @@ func (e *TargetsError) Error() string {
|
|||
return errString
|
||||
}
|
||||
|
||||
func NewJob(id uuid.UUID, imageBuildID int, manifest *osbuild.Manifest, targets []*target.Target) *Job {
|
||||
return &Job{id, imageBuildID, manifest, targets}
|
||||
}
|
||||
|
||||
func (job *Job) Run(uploader LocalTargetUploader) (*common.ComposeResult, error) {
|
||||
tmpStore, err := ioutil.TempDir("/var/tmp", "osbuild-store")
|
||||
if err != nil {
|
||||
|
|
|
|||
21
internal/jobqueue/json.go
Normal file
21
internal/jobqueue/json.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package jobqueue
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/osbuild/osbuild-composer/internal/common"
|
||||
"github.com/osbuild/osbuild-composer/internal/osbuild"
|
||||
"github.com/osbuild/osbuild-composer/internal/target"
|
||||
)
|
||||
|
||||
type addJobResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ImageBuildID int `json:"image_build_id"`
|
||||
Manifest *osbuild.Manifest `json:"manifest"`
|
||||
Targets []*target.Target `json:"targets"`
|
||||
}
|
||||
|
||||
type updateJobRequest struct {
|
||||
Status common.ImageBuildState `json:"status"`
|
||||
Result *common.ComposeResult `json:"result"`
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue