Skip to content

Commit

Permalink
feat: Update IntervalAction to use the common Address
Browse files Browse the repository at this point in the history
Since the common Address created, we can add it to the IntervalAction model and DTO.

Close #535

Signed-off-by: weichou <weichou1229@gmail.com>
  • Loading branch information
weichou1229 committed Mar 8, 2021
1 parent eae89da commit 81d8f1f
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 147 deletions.
23 changes: 23 additions & 0 deletions v2/dtos/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ type RESTAddress struct {
HTTPMethod string `json:"httpMethod" validate:"required,oneof='GET' 'HEAD' 'POST' 'PUT' 'DELETE' 'TRACE' 'CONNECT'"`
}

func NewRESTAddress(host string, port int, httpMethod string) Address {
return Address{
Type: v2.REST,
Host: host,
Port: port,
RESTAddress: RESTAddress{
HTTPMethod: httpMethod,
},
}
}

type MQTTPubAddress struct {
Publisher string `json:"publisher" validate:"required"`
Topic string `json:"topic" validate:"required"`
Expand All @@ -60,6 +71,18 @@ type MQTTPubAddress struct {
ConnectTimeout int `json:"connectTimeout,omitempty"`
}

func NewMQTTAddress(host string, port int, publisher string, topic string) Address {
return Address{
Type: v2.MQTT,
Host: host,
Port: port,
MQTTPubAddress: MQTTPubAddress{
Publisher: publisher,
Topic: topic,
},
}
}

func ToAddressModel(a Address) models.Address {
var address models.Address

Expand Down
76 changes: 24 additions & 52 deletions v2/dtos/intervalaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,40 @@ package dtos
import (
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/google/uuid"
)

// IntervalAction and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/support-scheduler/2.x#/IntervalAction
type IntervalAction struct {
common.Versionable `json:",inline"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Name string `json:"name" validate:"edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
IntervalName string `json:"intervalName" validate:"edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Protocol string `json:"protocol,omitempty"`
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
Path string `json:"path,omitempty"`
Parameters string `json:"parameters,omitempty"`
HTTPMethod string `json:"httpMethod,omitempty" validate:"omitempty,oneof='GET' 'HEAD' 'POST' 'PUT' 'DELETE' 'TRACE' 'CONNECT'"`
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
Publisher string `json:"publisher,omitempty"`
Target string `json:"target" validate:"edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Topic string `json:"topic,omitempty"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Name string `json:"name" validate:"edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
IntervalName string `json:"intervalName" validate:"edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Address Address `json:"address" validate:"required"`
}

func NewIntervalAction(name string, intervalName string, address Address) IntervalAction {
return IntervalAction{
Versionable: common.NewVersionable(),
Id: uuid.NewString(),
Name: name,
IntervalName: intervalName,
Address: address,
}
}

// UpdateIntervalAction and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/support-scheduler/2.x#/UpdateIntervalAction
type UpdateIntervalAction struct {
common.Versionable `json:",inline"`
Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"`
Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
IntervalName *string `json:"intervalName" validate:"edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Protocol *string `json:"protocol,omitempty"`
Host *string `json:"host,omitempty"`
Port *int `json:"port,omitempty"`
Path *string `json:"path,omitempty"`
Parameters *string `json:"parameters,omitempty"`
HTTPMethod *string `json:"httpMethod,omitempty" validate:"omitempty,oneof='GET' 'HEAD' 'POST' 'PUT' 'DELETE' 'TRACE' 'CONNECT'"`
User *string `json:"user,omitempty"`
Password *string `json:"password,omitempty"`
Publisher *string `json:"publisher,omitempty"`
Target *string `json:"target,omitempty" validate:"omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Topic *string `json:"topic,omitempty"`
Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"`
Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
IntervalName *string `json:"intervalName" validate:"edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Address *Address `json:"address,omitempty"`
}

// ToIntervalActionModel transforms the IntervalAction DTO to the IntervalAction Model
Expand All @@ -58,17 +50,7 @@ func ToIntervalActionModel(dto IntervalAction) models.IntervalAction {
model.Id = dto.Id
model.Name = dto.Name
model.IntervalName = dto.IntervalName
model.Protocol = dto.Protocol
model.Host = dto.Host
model.Port = dto.Port
model.Path = dto.Path
model.Parameters = dto.Parameters
model.HTTPMethod = dto.HTTPMethod
model.User = dto.User
model.Password = dto.Password
model.Publisher = dto.Publisher
model.Target = dto.Target
model.Topic = dto.Topic
model.Address = ToAddressModel(dto.Address)
return model
}

Expand All @@ -79,16 +61,6 @@ func FromIntervalActionModelToDTO(model models.IntervalAction) IntervalAction {
dto.Id = model.Id
dto.Name = model.Name
dto.IntervalName = model.IntervalName
dto.Protocol = model.Protocol
dto.Host = model.Host
dto.Port = model.Port
dto.Path = model.Path
dto.Parameters = model.Parameters
dto.HTTPMethod = model.HTTPMethod
dto.User = model.User
dto.Password = model.Password
dto.Publisher = model.Publisher
dto.Target = model.Target
dto.Topic = model.Topic
dto.Address = FromAddressModelToDTO(model.Address)
return dto
}
56 changes: 21 additions & 35 deletions v2/dtos/requests/intervalaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ type AddIntervalActionRequest struct {
// Validate satisfies the Validator interface
func (request AddIntervalActionRequest) Validate() error {
err := v2.Validate(request)
return err
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
err = request.Action.Address.Validate()
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
return nil
}

// UnmarshalJSON implements the Unmarshaler interface for the AddIntervalActionRequest type
Expand All @@ -43,7 +50,7 @@ func (request *AddIntervalActionRequest) UnmarshalJSON(b []byte) error {

// validate AddIntervalActionRequest DTO
if err := request.Validate(); err != nil {
return err
return errors.NewCommonEdgeXWrapper(err)
}
return nil
}
Expand All @@ -68,7 +75,16 @@ type UpdateIntervalActionRequest struct {
// Validate satisfies the Validator interface
func (request UpdateIntervalActionRequest) Validate() error {
err := v2.Validate(request)
return err
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
if request.Action.Address != nil {
err = request.Action.Address.Validate()
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
}
return nil
}

// UnmarshalJSON implements the Unmarshaler interface for the UpdateIntervalActionRequest type
Expand All @@ -95,38 +111,8 @@ func ReplaceIntervalActionModelFieldsWithDTO(action *models.IntervalAction, patc
if patch.IntervalName != nil {
action.IntervalName = *patch.IntervalName
}
if patch.Protocol != nil {
action.Protocol = *patch.Protocol
}
if patch.Host != nil {
action.Host = *patch.Host
}
if patch.Port != nil {
action.Port = *patch.Port
}
if patch.Path != nil {
action.Path = *patch.Path
}
if patch.Parameters != nil {
action.Parameters = *patch.Parameters
}
if patch.HTTPMethod != nil {
action.HTTPMethod = *patch.HTTPMethod
}
if patch.User != nil {
action.User = *patch.User
}
if patch.Password != nil {
action.Password = *patch.Password
}
if patch.Publisher != nil {
action.Publisher = *patch.Publisher
}
if patch.Target != nil {
action.Target = *patch.Target
}
if patch.Topic != nil {
action.Topic = *patch.Topic
if patch.Address != nil {
action.Address = dtos.ToAddressModel(*patch.Address)
}
}

Expand Down
Loading

0 comments on commit 81d8f1f

Please sign in to comment.