Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: add ParseHCLOpts helper method #12777

Merged
merged 1 commit into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/12777.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
api: Added ParseHCLOpts helper func to ease parsing HCLv1 jobspecs
```
11 changes: 10 additions & 1 deletion api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,21 @@ func (c *Client) Jobs() *Jobs {

// ParseHCL is used to convert the HCL repesentation of a Job to JSON server side.
// To parse the HCL client side see package github.com/hashicorp/nomad/jobspec
// Use ParseHCLOpts if you need to customize JobsParseRequest.
func (j *Jobs) ParseHCL(jobHCL string, canonicalize bool) (*Job, error) {
var job Job
req := &JobsParseRequest{
JobHCL: jobHCL,
Canonicalize: canonicalize,
}
return j.ParseHCLOpts(req)
}

// ParseHCLOpts is used to convert the HCL representation of a Job to JSON
// server side. To parse the HCL client side see package
// github.com/hashicorp/nomad/jobspec.
// ParseHCL is an alternative convenience API for HCLv2 users.
func (j *Jobs) ParseHCLOpts(req *JobsParseRequest) (*Job, error) {
var job Job
_, err := j.client.write("/v1/jobs/parse", req, &job, nil)
return &job, err
}
Expand Down
75 changes: 75 additions & 0 deletions api/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2361,3 +2361,78 @@ func TestJobs_ScaleStatus(t *testing.T) {
func TestJobs_Services(t *testing.T) {
// TODO(jrasell) add tests once registration process is in place.
}

// TestJobs_Parse asserts ParseHCL and ParseHCLOpts use the API to parse HCL.
func TestJobs_Parse(t *testing.T) {
testutil.Parallel(t)

jobspec := `job "example" {}`

// Assert ParseHCL returns an error if Nomad is not running to ensure
// that parsing is done server-side and not via the jobspec package.
{
c, err := NewClient(DefaultConfig())
require.NoError(t, err)

_, err = c.Jobs().ParseHCL(jobspec, false)
require.Error(t, err)
require.Contains(t, err.Error(), "Put")
}

c, s := makeClient(t, nil, nil)
defer s.Stop()

// Test ParseHCL
job1, err := c.Jobs().ParseHCL(jobspec, false)
require.NoError(t, err)
require.Equal(t, "example", *job1.Name)
require.Nil(t, job1.Namespace)

job1Canonicalized, err := c.Jobs().ParseHCL(jobspec, true)
require.NoError(t, err)
require.Equal(t, "example", *job1Canonicalized.Name)
require.Equal(t, "default", *job1Canonicalized.Namespace)
require.NotEqual(t, job1, job1Canonicalized)

// Test ParseHCLOpts
req := &JobsParseRequest{
JobHCL: jobspec,
HCLv1: false,
Canonicalize: false,
}

job2, err := c.Jobs().ParseHCLOpts(req)
require.NoError(t, err)
require.Equal(t, job1, job2)

// Test ParseHCLOpts with Canonicalize=true
req = &JobsParseRequest{
JobHCL: jobspec,
HCLv1: false,
Canonicalize: true,
}
job2Canonicalized, err := c.Jobs().ParseHCLOpts(req)
require.NoError(t, err)
require.Equal(t, job1Canonicalized, job2Canonicalized)

// Test ParseHCLOpts with HCLv1=true
req = &JobsParseRequest{
JobHCL: jobspec,
HCLv1: true,
Canonicalize: false,
}

job3, err := c.Jobs().ParseHCLOpts(req)
require.NoError(t, err)
require.Equal(t, job1, job3)

// Test ParseHCLOpts with HCLv1=true and Canonicalize=true
req = &JobsParseRequest{
JobHCL: jobspec,
HCLv1: true,
Canonicalize: true,
}
job3Canonicalized, err := c.Jobs().ParseHCLOpts(req)
require.NoError(t, err)
require.Equal(t, job1Canonicalized, job3Canonicalized)
}