Skip to content

Commit

Permalink
feat: Support query schedule jobs with labels
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Chen <jack@iotechsys.com>
  • Loading branch information
jackchenjc committed Aug 23, 2024
1 parent 44d6dc1 commit ab872a6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
18 changes: 11 additions & 7 deletions clients/http/schedulejob.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"net/url"
"strconv"
"strings"

"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/http/utils"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/interfaces"
Expand Down Expand Up @@ -55,9 +56,12 @@ func (client ScheduleJobClient) Update(ctx context.Context, reqs []requests.Upda
}

// AllScheduleJobs queries the schedule jobs with offset, limit
func (client ScheduleJobClient) AllScheduleJobs(ctx context.Context, offset int, limit int) (
func (client ScheduleJobClient) AllScheduleJobs(ctx context.Context, labels []string, offset, limit int) (
res responses.MultiScheduleJobsResponse, err errors.EdgeX) {
requestParams := url.Values{}
if len(labels) > 0 {
requestParams.Set(common.Labels, strings.Join(labels, common.CommaSeparator))
}
requestParams.Set(common.Offset, strconv.Itoa(offset))
requestParams.Set(common.Limit, strconv.Itoa(limit))
err = utils.GetRequest(ctx, &res, client.baseUrl, common.ApiAllScheduleJobRoute, requestParams, client.authInjector)
Expand All @@ -70,9 +74,9 @@ func (client ScheduleJobClient) AllScheduleJobs(ctx context.Context, offset int,
// ScheduleJobByName queries the schedule job by name
func (client ScheduleJobClient) ScheduleJobByName(ctx context.Context, name string) (
res responses.ScheduleJobResponse, err errors.EdgeX) {
path := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
SetPath(common.ApiScheduleJobRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
err = utils.GetRequest(ctx, &res, client.baseUrl, path, nil, client.authInjector)
err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, nil, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
Expand All @@ -82,9 +86,9 @@ func (client ScheduleJobClient) ScheduleJobByName(ctx context.Context, name stri
// DeleteScheduleJobByName deletes the schedule job by name
func (client ScheduleJobClient) DeleteScheduleJobByName(ctx context.Context, name string) (
res dtoCommon.BaseResponse, err errors.EdgeX) {
path := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
SetPath(common.ApiScheduleJobRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
err = utils.DeleteRequest(ctx, &res, client.baseUrl, path, client.authInjector)
err = utils.DeleteRequest(ctx, &res, client.baseUrl, requestPath, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
Expand All @@ -94,9 +98,9 @@ func (client ScheduleJobClient) DeleteScheduleJobByName(ctx context.Context, nam
// TriggerScheduleJobByName triggers the schedule job by name
func (client ScheduleJobClient) TriggerScheduleJobByName(ctx context.Context, name string) (
res dtoCommon.BaseResponse, err errors.EdgeX) {
path := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
SetPath(common.ApiTriggerScheduleJobRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
err = utils.PostRequestWithRawData(ctx, &res, client.baseUrl, path, nil, nil, client.authInjector)
err = utils.PostRequestWithRawData(ctx, &res, client.baseUrl, requestPath, nil, nil, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
Expand Down
14 changes: 7 additions & 7 deletions clients/http/schedulejob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ func TestScheduleJobClient_AllScheduleJobs(t *testing.T) {
ts := newTestServer(http.MethodGet, common.ApiAllScheduleJobRoute, responses.MultiScheduleJobsResponse{})
defer ts.Close()
client := NewScheduleJobClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.AllScheduleJobs(context.Background(), 0, 10)
res, err := client.AllScheduleJobs(context.Background(), []string{TestLabel}, 0, 10)
require.NoError(t, err)
require.IsType(t, responses.MultiScheduleJobsResponse{}, res)
}

func TestScheduleJobClient_ScheduleJobByName(t *testing.T) {
scheduleJobName := TestScheduleJobName
path := path.Join(common.ApiScheduleJobRoute, common.Name, scheduleJobName)
ts := newTestServer(http.MethodGet, path, responses.ScheduleJobResponse{})
requestPath := path.Join(common.ApiScheduleJobRoute, common.Name, scheduleJobName)
ts := newTestServer(http.MethodGet, requestPath, responses.ScheduleJobResponse{})
defer ts.Close()
client := NewScheduleJobClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.ScheduleJobByName(context.Background(), scheduleJobName)
Expand All @@ -110,8 +110,8 @@ func TestScheduleJobClient_ScheduleJobByName(t *testing.T) {

func TestScheduleJobClient_DeleteScheduleJobByName(t *testing.T) {
scheduleJobName := TestScheduleJobName
path := path.Join(common.ApiScheduleJobRoute, common.Name, scheduleJobName)
ts := newTestServer(http.MethodDelete, path, dtoCommon.BaseResponse{})
requestPath := path.Join(common.ApiScheduleJobRoute, common.Name, scheduleJobName)
ts := newTestServer(http.MethodDelete, requestPath, dtoCommon.BaseResponse{})
defer ts.Close()
client := NewScheduleJobClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.DeleteScheduleJobByName(context.Background(), scheduleJobName)
Expand All @@ -121,8 +121,8 @@ func TestScheduleJobClient_DeleteScheduleJobByName(t *testing.T) {

func TestScheduleJobClient_TriggerScheduleJobByName(t *testing.T) {
scheduleJobName := TestScheduleJobName
path := path.Join(common.ApiTriggerScheduleJobRoute, common.Name, scheduleJobName)
ts := newTestServer(http.MethodPost, path, dtoCommon.BaseResponse{})
requestPath := path.Join(common.ApiTriggerScheduleJobRoute, common.Name, scheduleJobName)
ts := newTestServer(http.MethodPost, requestPath, dtoCommon.BaseResponse{})
defer ts.Close()
client := NewScheduleJobClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.TriggerScheduleJobByName(context.Background(), scheduleJobName)
Expand Down
18 changes: 9 additions & 9 deletions clients/interfaces/mocks/ScheduleJobClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion clients/interfaces/schedulejob.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type ScheduleJobClient interface {
// The result can be limited in a certain range by specifying the offset and limit parameters.
// offset: The number of items to skip before starting to collect the result set. Default is 0.
// limit: The number of items to return. Specify -1 will return all remaining items after offset. The maximum will be the MaxResultCount as defined in the configuration of service. Default is 20.
AllScheduleJobs(ctx context.Context, offset int, limit int) (responses.MultiScheduleJobsResponse, errors.EdgeX)
AllScheduleJobs(ctx context.Context, labels []string, offset int, limit int) (responses.MultiScheduleJobsResponse, errors.EdgeX)
// ScheduleJobByName returns a schedule job by name.
ScheduleJobByName(ctx context.Context, name string) (responses.ScheduleJobResponse, errors.EdgeX)
// DeleteScheduleJobByName deletes a schedule job by name.
Expand Down

0 comments on commit ab872a6

Please sign in to comment.