Start using the new jobspec2 API

This commit is contained in:
Mahmood Ali
2020-11-09 15:01:31 -05:00
parent 878b3f82fe
commit 5a784f43a3
3 changed files with 19 additions and 40 deletions

View File

@@ -675,14 +675,16 @@ func (s *HTTPServer) JobsParseRequest(resp http.ResponseWriter, req *http.Reques
return nil, CodedError(400, "Job spec is empty")
}
jobfile := strings.NewReader(args.JobHCL)
var jobStruct *api.Job
var err error
if args.HCLv1 {
jobStruct, err = jobspec.Parse(jobfile)
jobStruct, err = jobspec.Parse(strings.NewReader(args.JobHCL))
} else {
jobStruct, err = jobspec2.ParseWithArgs("input.hcl", jobfile, nil, false)
jobStruct, err = jobspec2.ParseWithConfig(&jobspec2.ParseConfig{
Path: "input.hcl",
Body: []byte(args.JobHCL),
AllowFS: false,
})
}
if err != nil {
return nil, CodedError(400, err.Error())

View File

@@ -388,10 +388,10 @@ type JobGetter struct {
// StructJob returns the Job struct from jobfile.
func (j *JobGetter) ApiJob(jpath string) (*api.Job, error) {
return j.ApiJobWithArgs(jpath, nil)
return j.ApiJobWithArgs(jpath, nil, nil)
}
func (j *JobGetter) ApiJobWithArgs(jpath string, vars map[string]string) (*api.Job, error) {
func (j *JobGetter) ApiJobWithArgs(jpath string, vars []string, varfiles []string) (*api.Job, error) {
var jobfile io.Reader
pathName := filepath.Base(jpath)
switch jpath {
@@ -447,7 +447,17 @@ func (j *JobGetter) ApiJobWithArgs(jpath string, vars map[string]string) (*api.J
if j.hcl1 {
jobStruct, err = jobspec.Parse(jobfile)
} else {
jobStruct, err = jobspec2.ParseWithArgs(pathName, jobfile, vars, true)
var buf bytes.Buffer
_, err = io.Copy(&buf, jobfile)
if err != nil {
return nil, fmt.Errorf("Error reading job file from %s: %v", jpath, err)
}
jobStruct, err = jobspec2.ParseWithConfig(&jobspec2.ParseConfig{
Path: pathName,
Body: buf.Bytes(),
ArgVars: vars,
AllowFS: true,
})
}
if err != nil {
return nil, fmt.Errorf("Error parsing job file from %s:\n%v", jpath, err)
@@ -523,25 +533,3 @@ func (w *uiErrorWriter) Close() error {
}
return nil
}
// parseVars decodes a slice of `<key>=<val>` or `<key>` strings into a golang map.
//
// `<key>` without corresponding value, is mapped to the `<key>` environment variable.
func parseVars(vars []string) map[string]string {
if len(vars) == 0 {
return nil
}
result := make(map[string]string, len(vars))
for _, v := range vars {
parts := strings.SplitN(v, "=", 2)
k := parts[0]
if len(parts) == 2 {
result[k] = parts[1]
} else {
result[k] = os.Getenv(k)
}
}
return result
}

View File

@@ -392,14 +392,3 @@ func TestUiErrorWriter(t *testing.T) {
expectedErr += "and thensome more\n"
require.Equal(t, expectedErr, errBuf.String())
}
func TestParseVars(t *testing.T) {
input := []string{"key1=val1", "HOME", "key2=321"}
expected := map[string]string{
"key1": "val1",
"HOME": os.Getenv("HOME"),
"key2": "321",
}
require.Equal(t, expected, parseVars(input))
}