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

feat: Validate Interval and IntervalAction before loading from config #3646

Merged
merged 2 commits into from
Aug 3, 2021
Merged
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
1 change: 1 addition & 0 deletions cmd/support-scheduler/res/configuration.toml
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@ Type = 'consul'
Target = 'core-data'
Path = '/api/v2/event/age/604800000000000' # Remove events older than 7 days
Interval = 'midnight'
AdminState = 'UNLOCKED'

[SecretStore]
Type = 'vault'
8 changes: 7 additions & 1 deletion internal/support/scheduler/application/interval.go
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ import (

bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
"github.com/edgexfoundry/go-mod-core-contracts/v2/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/requests"
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
@@ -154,12 +155,17 @@ func LoadIntervalToSchedulerManager(dic *di.Container) errors.EdgeX {
// Load intervals from config to DB
configuration := container.ConfigurationFrom(dic.Get)
for i := range configuration.Intervals {
interval := models.Interval{
dto := dtos.Interval{
Name: configuration.Intervals[i].Name,
Start: configuration.Intervals[i].Start,
End: configuration.Intervals[i].End,
Interval: configuration.Intervals[i].Interval,
}
validateErr := common.Validate(dto)
if validateErr != nil {
return errors.NewCommonEdgeX(errors.KindContractInvalid, fmt.Sprintf("validate pre-defined Interval %s from configuration failed", dto.Name), validateErr)
}
interval := dtos.ToIntervalModel(dto)
_, err := dbClient.IntervalByName(interval.Name)
if errors.Kind(err) == errors.KindEntityDoesNotExist {
_, err = dbClient.AddInterval(interval)
30 changes: 21 additions & 9 deletions internal/support/scheduler/application/intervalaction.go
Original file line number Diff line number Diff line change
@@ -152,20 +152,32 @@ func LoadIntervalActionToSchedulerManager(dic *di.Container) errors.EdgeX {
// Load intervalActions from config to DB
configuration := container.ConfigurationFrom(dic.Get)
for i := range configuration.IntervalActions {
action := models.IntervalAction{
dto := dtos.IntervalAction{
Name: configuration.IntervalActions[i].Name,
IntervalName: configuration.IntervalActions[i].Interval,
Address: models.RESTAddress{
BaseAddress: models.BaseAddress{
Type: common.REST,
Host: configuration.IntervalActions[i].Host,
Port: configuration.IntervalActions[i].Port,
Address: dtos.Address{
Type: common.REST,
Host: configuration.IntervalActions[i].Host,
Port: configuration.IntervalActions[i].Port,
RESTAddress: dtos.RESTAddress{
Path: configuration.IntervalActions[i].Path,
HTTPMethod: configuration.IntervalActions[i].Method,
},
Path: configuration.IntervalActions[i].Path,
HTTPMethod: configuration.IntervalActions[i].Method,
},
Content: configuration.IntervalActions[i].Content,
ContentType: configuration.IntervalActions[i].ContentType,
AdminState: configuration.IntervalActions[i].AdminState,
}
_, err := dbClient.IntervalActionByName(action.Name)
validateErr := common.Validate(dto)
if validateErr != nil {
return errors.NewCommonEdgeX(errors.KindContractInvalid, fmt.Sprintf("validate pre-defined IntervalAction %s from configuration failed", dto.Name), validateErr)
}
_, err := dbClient.IntervalByName(dto.IntervalName)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
action := dtos.ToIntervalActionModel(dto)
_, err = dbClient.IntervalActionByName(action.Name)
if errors.Kind(err) == errors.KindEntityDoesNotExist {
_, err = dbClient.AddIntervalAction(action)
if err != nil {
6 changes: 6 additions & 0 deletions internal/support/scheduler/config/config.go
Original file line number Diff line number Diff line change
@@ -76,6 +76,12 @@ type IntervalActionInfo struct {
Path string
// Associated Schedule for the Event
Interval string
// Content is the actual content to be sent as the body of the notification
Content string
// ContentType indicates the MIME type/Content-type of the notification's content.
ContentType string
// Administrative state (LOCKED/UNLOCKED)
AdminState string
}

// URI constructs a URI from the protocol, host and port and returns that as a string.
4 changes: 2 additions & 2 deletions internal/support/scheduler/init.go
Original file line number Diff line number Diff line change
@@ -59,13 +59,13 @@ func (b *Bootstrap) BootstrapHandler(ctx context.Context, wg *sync.WaitGroup, _

err := application.LoadIntervalToSchedulerManager(dic)
if err != nil {
lc.Errorf("Failed to load interval to scheduler %v", err)
lc.Errorf("Failed to load interval to scheduler, %v", err)
return false
}

err = application.LoadIntervalActionToSchedulerManager(dic)
if err != nil {
lc.Errorf("Failed to load interval to scheduler %v", err)
lc.Errorf("Failed to load intervalAction to scheduler, %v", err)
return false
}