Skip to content

Commit

Permalink
Tag and Untag at API level on down, but am I unblocking the wrong thing?
Browse files Browse the repository at this point in the history
  • Loading branch information
philrenaud committed Aug 26, 2024
1 parent 17e3baf commit 233f051
Show file tree
Hide file tree
Showing 12 changed files with 714 additions and 1 deletion.
29 changes: 29 additions & 0 deletions api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
"log"
"maps"
"net/url"
"sort"
Expand Down Expand Up @@ -1643,3 +1644,31 @@ type JobStatusesRequest struct {
// IncludeChildren will include child (batch) jobs in the response.
IncludeChildren bool
}

// #region TaggedVersions

// Function TagVersion to apply a JobTaggedVersion to a Job Version (which itself is a job)
// by POSTing to the /v1/job/:job_id/versions/:version/tag endpoint.

type TagVersionRequest struct {
JobID string
Version string
Tag *JobTaggedVersion
WriteRequest
}

func (j *Jobs) TagVersion(jobID string, version string, name string, description string, q *WriteOptions) (*WriteMeta, error) {
var tagRequest = &TagVersionRequest{
JobID: jobID,
Version: version,
Tag: &JobTaggedVersion{
Name: name,
Description: description,
},
}

log.Printf("TagVersionRequest: %+v ", tagRequest)
return j.client.put("/v1/job/"+url.PathEscape(jobID)+"/versions/"+version+"/tag", tagRequest, nil, q)
}

// #endregion TaggedVersions
134 changes: 134 additions & 0 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ func (s *HTTPServer) JobSpecificRequest(resp http.ResponseWriter, req *http.Requ
case strings.HasSuffix(path, "/action"):
jobID := strings.TrimSuffix(path, "/action")
return s.jobRunAction(resp, req, jobID)
case strings.HasSuffix(path, "/tag"):
// jobID := strings.TrimSuffix(path, "/tag")
// TrimSuffix isn't right, the route is actually /job/:job_id/versions/:version/tag
// So we need to split the path and get the jobID from the path
// Log that this method has been hit
jobID := strings.Split(path, "/")[0]
s.logger.Debug("=====================================")
s.logger.Debug("path to /tag hit, path: ", path)
s.logger.Debug("method: ", req.Method)
s.logger.Debug("jobID: ", jobID)

return s.jobTagVersion(resp, req, jobID)
default:
return s.jobCRUD(resp, req, path)
}
Expand Down Expand Up @@ -401,6 +413,109 @@ func (s *HTTPServer) jobRunAction(resp http.ResponseWriter, req *http.Request, j
return s.execStream(conn, &args)
}

// jobTagVersion
func (s *HTTPServer) jobTagVersion(resp http.ResponseWriter, req *http.Request, jobID string) (interface{}, error) {
// Debug log that this method has been hit

s.logger.Debug("+++++++++++++++++++++++++++++++++++++++++++++++")
s.logger.Debug("jobTagVersion method hit")
s.logger.Debug("req.Method: ", req.Method)
s.logger.Debug("jobID: ", jobID)
// s.logger.Debug("args: ", args)
s.logger.Debug("+++++++++++++++++++++++++++++++++++++++++++++++")

// if err := decodeBody(req, &args); err != nil {
// return nil, CodedError(400, err.Error())
// }
// if args.JobID == "" {
// return nil, CodedError(400, "Job must be specified")
// }
// TODO: check for Version and Name too

// var args api.JobTagRequest
// if err := decodeBody(req, &args); err != nil {
// return nil, CodedError(400, err.Error())
// }

switch req.Method {
case http.MethodPut, http.MethodPost:
return s.jobVersionApplyTag(resp, req, jobID)
case http.MethodDelete:
return s.jobVersionUnsetTag(resp, req, jobID)
default:
return nil, CodedError(405, ErrInvalidMethod)
}

}

func (s *HTTPServer) jobVersionApplyTag(resp http.ResponseWriter, req *http.Request, jobID string) (interface{}, error) {
var args api.TagVersionRequest
// Decode req body
if err := decodeBody(req, &args); err != nil {
return nil, CodedError(400, err.Error())
}

s.logger.Debug("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
s.logger.Debug("jobVersionApplyTag method hit")
s.logger.Debug("jobID: ", jobID)
s.logger.Debug("req: ", req)
s.logger.Debug("args: ", args)
s.logger.Debug("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

// var args structs.JobTagRequest
// var args convertToStructsStruct(api.JobTagRequest) //TODO:
// var args api.TagVersionRequest
// rpcArgs := ApiJobTaggedVersionToStructs(args)
rpcArgs := APIJobTagRequestToStructs(&args)

// parseWriteRequest overrides Namespace, Region and AuthToken
// based on values from the original http request
s.parseWriteRequest(req, &rpcArgs.WriteRequest)

s.logger.Debug("OK Ive now parsed write requests, what can we know?", rpcArgs.WriteRequest.Region)

// if err := decodeBody(req, &args); err != nil {
// return nil, CodedError(400, err.Error())
// }

// if args.Name == "" {
// return nil, CodedError(400, "Name must be specified")
// }

var out structs.JobTagResponse
if err := s.agent.RPC("Job.TagVersion", &rpcArgs, &out); err != nil {
return nil, err
}
return out, nil
}

func (s *HTTPServer) jobVersionUnsetTag(resp http.ResponseWriter, req *http.Request, jobID string) (interface{}, error) {
var args api.TagVersionRequest
// Decode req body
if err := decodeBody(req, &args); err != nil {
return nil, CodedError(400, err.Error())
}

s.logger.Debug("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
s.logger.Debug("jobVersionUnsetTag method hit")
s.logger.Debug("jobID: ", jobID)
s.logger.Debug("req: ", req)
// s.logger.Debug("args: ", args)
s.logger.Debug("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

rpcArgs := APIJobTagRequestToStructs(&args)

// parseWriteRequest overrides Namespace, Region and AuthToken
// based on values from the original http request
s.parseWriteRequest(req, &rpcArgs.WriteRequest)

var out structs.JobTagResponse
if err := s.agent.RPC("Job.UntagVersion", &rpcArgs, &out); err != nil {
return nil, err
}
return out, nil
}

func (s *HTTPServer) jobSubmissionCRUD(resp http.ResponseWriter, req *http.Request, jobID string) (*structs.JobSubmission, error) {
version, err := strconv.ParseUint(req.URL.Query().Get("version"), 10, 64)
if err != nil {
Expand Down Expand Up @@ -2158,6 +2273,25 @@ func ApiJobTaggedVersionToStructs(jobTaggedVersion *api.JobTaggedVersion) *struc
}
}

func APIJobTagRequestToStructs(jobTagRequest *api.TagVersionRequest) *structs.JobTagRequest {
if jobTagRequest == nil {
return nil
}
versionNumber, err := strconv.ParseUint(jobTagRequest.Version, 10, 64)
if err != nil {
// handle the error
}
return &structs.JobTagRequest{
JobID: jobTagRequest.JobID,
Version: versionNumber,
Tag: ApiJobTaggedVersionToStructs(jobTagRequest.Tag),
// WriteRequest: structs.WriteRequest{
// Region: jobTagRequest.Region,
// Namespace: jobTagRequest.Namespace,
// },
}
}

func ApiAffinityToStructs(a1 *api.Affinity) *structs.Affinity {
return &structs.Affinity{
LTarget: a1.LTarget,
Expand Down
5 changes: 5 additions & 0 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,11 @@ func Commands(metaPtr *Meta, agentUi cli.Ui) map[string]cli.CommandFactory {
Meta: meta,
}, nil
},
"job tag": func() (cli.Command, error) {
return &JobTagCommand{
Meta: meta,
}, nil
},
"job validate": func() (cli.Command, error) {
return &JobValidateCommand{
Meta: meta,
Expand Down
Loading

0 comments on commit 233f051

Please sign in to comment.