Skip to content

Commit

Permalink
Merge pull request #909 from jackchenjc/issue-908
Browse files Browse the repository at this point in the history
feat: Create data model, DTO, and HTTP client for the new support-cron-scheduler service
  • Loading branch information
cloudxxx8 authored Jul 29, 2024
2 parents 2029fc3 + 7ffbf86 commit f519fd3
Show file tree
Hide file tree
Showing 27 changed files with 2,879 additions and 1 deletion.
6 changes: 6 additions & 0 deletions clients/http/const_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ const (
TestCategory = "health-check"
TestLabel = "rest"
ExampleUUID = "82eb2e26-0f24-48aa-ae4c-de9dac3fb9bc"

TestScheduleJobName = "TestScheduleJobName"
TestInterval = "10m"
TestCrontab = "0 0 1 1 *"
TestTopic = "TestTopic"
TestAddress = "TestAddress"
)
105 changes: 105 additions & 0 deletions clients/http/scheduleactionrecord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// Copyright (C) 2024 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

package http

import (
"context"
"net/url"
"path"
"strconv"

"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/http/utils"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/interfaces"
"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/responses"
"github.com/edgexfoundry/go-mod-core-contracts/v3/errors"
)

type ScheduleActionRecordClient struct {
baseUrl string
authInjector interfaces.AuthenticationInjector
enableNameFieldEscape bool
}

// NewScheduleActionRecordClient creates an instance of ScheduleActionRecordClient
func NewScheduleActionRecordClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.ScheduleActionRecordClient {
return &ScheduleActionRecordClient{
baseUrl: baseUrl,
authInjector: authInjector,
enableNameFieldEscape: enableNameFieldEscape,
}
}

// AllScheduleActionRecords query schedule action records with start, end, offset, and limit
func (client *ScheduleActionRecordClient) AllScheduleActionRecords(ctx context.Context, start, end int64, offset, limit int) (res responses.MultiScheduleActionRecordsResponse, err errors.EdgeX) {
requestParams := url.Values{}
requestParams.Set(common.Start, strconv.FormatInt(start, 10))
requestParams.Set(common.End, strconv.FormatInt(end, 10))
requestParams.Set(common.Offset, strconv.Itoa(offset))
requestParams.Set(common.Limit, strconv.Itoa(limit))
err = utils.GetRequest(ctx, &res, client.baseUrl, common.ApiAllScheduleActionRecordRoute, requestParams, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}

// LatestScheduleActionRecords query the latest schedule action records of each schedule job with offset, and limit
func (client *ScheduleActionRecordClient) LatestScheduleActionRecords(ctx context.Context, offset, limit int) (res responses.MultiScheduleActionRecordsResponse, err errors.EdgeX) {
requestParams := url.Values{}
requestParams.Set(common.Offset, strconv.Itoa(offset))
requestParams.Set(common.Limit, strconv.Itoa(limit))
err = utils.GetRequest(ctx, &res, client.baseUrl, common.ApiLatestScheduleActionRecordRoute, requestParams, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}

// ScheduleActionRecordsByStatus queries schedule action records with status, start, end, offset, and limit
func (client *ScheduleActionRecordClient) ScheduleActionRecordsByStatus(ctx context.Context, status string, start, end int64, offset, limit int) (res responses.MultiScheduleActionRecordsResponse, err errors.EdgeX) {
requestPath := path.Join(common.ApiScheduleActionRecordRoute, common.Status, status)
requestParams := url.Values{}
requestParams.Set(common.Start, strconv.FormatInt(start, 10))
requestParams.Set(common.End, strconv.FormatInt(end, 10))
requestParams.Set(common.Offset, strconv.Itoa(offset))
requestParams.Set(common.Limit, strconv.Itoa(limit))
err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}

// ScheduleActionRecordsByJobName queries schedule action records with jobName, start, end, offset, and limit
func (client *ScheduleActionRecordClient) ScheduleActionRecordsByJobName(ctx context.Context, jobName string, start, end int64, offset, limit int) (res responses.MultiScheduleActionRecordsResponse, err errors.EdgeX) {
requestPath := path.Join(common.ApiScheduleActionRecordRoute, common.Job, common.Name, jobName)
requestParams := url.Values{}
requestParams.Set(common.Start, strconv.FormatInt(start, 10))
requestParams.Set(common.End, strconv.FormatInt(end, 10))
requestParams.Set(common.Offset, strconv.Itoa(offset))
requestParams.Set(common.Limit, strconv.Itoa(limit))
err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}

// ScheduleActionRecordsByJobNameAndStatus queries schedule action records with jobName, status, start, end, offset, and limit
func (client *ScheduleActionRecordClient) ScheduleActionRecordsByJobNameAndStatus(ctx context.Context, jobName, status string, start, end int64, offset, limit int) (res responses.MultiScheduleActionRecordsResponse, err errors.EdgeX) {
requestPath := path.Join(common.ApiScheduleActionRecordRoute, common.Job, common.Name, jobName, common.Status, status)
requestParams := url.Values{}
requestParams.Set(common.Start, strconv.FormatInt(start, 10))
requestParams.Set(common.End, strconv.FormatInt(end, 10))
requestParams.Set(common.Offset, strconv.Itoa(offset))
requestParams.Set(common.Limit, strconv.Itoa(limit))
err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}
71 changes: 71 additions & 0 deletions clients/http/scheduleactionrecord_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// Copyright (C) 2024 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

package http

import (
"context"
"net/http"
"path"
"testing"

"github.com/stretchr/testify/require"

"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/responses"
"github.com/edgexfoundry/go-mod-core-contracts/v3/models"
)

func TestScheduleActionRecordClient_AllScheduleActionRecords(t *testing.T) {
ts := newTestServer(http.MethodGet, common.ApiAllScheduleActionRecordRoute, responses.MultiScheduleActionRecordsResponse{})
defer ts.Close()
client := NewScheduleActionRecordClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.AllScheduleActionRecords(context.Background(), 0, 0, 0, 10)
require.NoError(t, err)
require.IsType(t, responses.MultiScheduleActionRecordsResponse{}, res)
}

func TestScheduleActionRecordClient_LatestScheduleActionRecords(t *testing.T) {
ts := newTestServer(http.MethodGet, common.ApiLatestScheduleActionRecordRoute, responses.MultiScheduleActionRecordsResponse{})
defer ts.Close()
client := NewScheduleActionRecordClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.LatestScheduleActionRecords(context.Background(), 0, 10)
require.NoError(t, err)
require.IsType(t, responses.MultiScheduleActionRecordsResponse{}, res)
}

func TestScheduleActionRecordClient_ScheduleActionRecordsByStatus(t *testing.T) {
status := models.Succeeded
urlPath := path.Join(common.ApiScheduleActionRecordRoute, common.Status, status)
ts := newTestServer(http.MethodGet, urlPath, responses.MultiScheduleActionRecordsResponse{})
defer ts.Close()
client := NewScheduleActionRecordClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.ScheduleActionRecordsByStatus(context.Background(), status, 0, 0, 0, 10)
require.NoError(t, err)
require.IsType(t, responses.MultiScheduleActionRecordsResponse{}, res)
}

func TestScheduleActionRecordClient_ScheduleActionRecordsByJobName(t *testing.T) {
jobName := TestScheduleJobName
urlPath := path.Join(common.ApiScheduleActionRecordRoute, common.Job, common.Name, jobName)
ts := newTestServer(http.MethodGet, urlPath, responses.MultiScheduleActionRecordsResponse{})
defer ts.Close()
client := NewScheduleActionRecordClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.ScheduleActionRecordsByJobName(context.Background(), jobName, 0, 0, 0, 10)
require.NoError(t, err)
require.IsType(t, responses.MultiScheduleActionRecordsResponse{}, res)
}

func TestScheduleActionRecordClient_ScheduleActionRecordsByJobNameAndStatus(t *testing.T) {
jobName := TestScheduleJobName
status := models.Succeeded
urlPath := path.Join(common.ApiScheduleActionRecordRoute, common.Job, common.Name, jobName, common.Status, status)
ts := newTestServer(http.MethodGet, urlPath, responses.MultiScheduleActionRecordsResponse{})
defer ts.Close()
client := NewScheduleActionRecordClient(ts.URL, NewNullAuthenticationInjector(), false)
res, err := client.ScheduleActionRecordsByJobNameAndStatus(context.Background(), jobName, status, 0, 0, 0, 10)
require.NoError(t, err)
require.IsType(t, responses.MultiScheduleActionRecordsResponse{}, res)
}
104 changes: 104 additions & 0 deletions clients/http/schedulejob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// Copyright (C) 2024 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

package http

import (
"context"
"net/url"
"strconv"

"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/http/utils"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/interfaces"
"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/requests"
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/responses"
"github.com/edgexfoundry/go-mod-core-contracts/v3/errors"
)

type ScheduleJobClient struct {
baseUrl string
authInjector interfaces.AuthenticationInjector
enableNameFieldEscape bool
}

// NewScheduleJobClient creates an instance of ScheduleJobClient
func NewScheduleJobClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.ScheduleJobClient {
return &ScheduleJobClient{
baseUrl: baseUrl,
authInjector: authInjector,
enableNameFieldEscape: enableNameFieldEscape,
}
}

// Add adds new schedule jobs
func (client ScheduleJobClient) Add(ctx context.Context, reqs []requests.AddScheduleJobRequest) (
res []dtoCommon.BaseWithIdResponse, err errors.EdgeX) {
err = utils.PostRequestWithRawData(ctx, &res, client.baseUrl, common.ApiScheduleJobRoute, nil, reqs, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}

// Update updates schedule jobs
func (client ScheduleJobClient) Update(ctx context.Context, reqs []requests.UpdateScheduleJobRequest) (
res []dtoCommon.BaseResponse, err errors.EdgeX) {
err = utils.PatchRequest(ctx, &res, client.baseUrl, common.ApiScheduleJobRoute, nil, reqs, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}

// AllScheduleJobs queries the schedule jobs with offset, limit
func (client ScheduleJobClient) AllScheduleJobs(ctx context.Context, offset int, limit int) (
res responses.MultiScheduleJobsResponse, err errors.EdgeX) {
requestParams := url.Values{}
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)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}

// 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).
SetPath(common.ApiScheduleJobRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
err = utils.GetRequest(ctx, &res, client.baseUrl, path, nil, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}

// 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).
SetPath(common.ApiScheduleJobRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
err = utils.DeleteRequest(ctx, &res, client.baseUrl, path, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}

// 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).
SetPath(common.ApiTriggerScheduleJobRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath()
err = utils.PostRequestWithRawData(ctx, &res, client.baseUrl, path, nil, nil, client.authInjector)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}
Loading

0 comments on commit f519fd3

Please sign in to comment.