/
request.go
66 lines (58 loc) · 2.25 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package data_messages
import (
"errors"
log "github.com/sirupsen/logrus"
types "github.com/nttdots/go-dots/dots_common/types/data"
"github.com/nttdots/go-dots/dots_server/models"
"github.com/nttdots/go-dots/dots_common/messages"
)
type PostRequest struct {
Aliases *types.Aliases `json:"ietf-dots-data-channel:aliases"`
ACLs *types.ACLs `json:"ietf-dots-data-channel:acls"`
VendorMapping *types.VendorMapping `json:"ietf-dots-mapping:vendor-mapping"`
}
func (r *PostRequest) ValidateExtract(method string, customer *models.Customer) (interface{}, error) {
if r.Aliases == nil && r.ACLs == nil && r.VendorMapping == nil {
log.Error("aliases == nil and acls == nil and vendor-mapping = nil")
return nil, errors.New("Validation failed : Both of aliases and acls and vendor-mapping are not found")
}
if r.Aliases != nil && r.ACLs != nil && r.VendorMapping != nil{
log.Error("Request must be either of alias or acl or vendor-mapping")
return nil, errors.New("Validation failed : Request must be either of aliases, acls or vendor-mapping")
}
if r.VendorMapping != nil {
t := VendorMappingRequest{ *r.VendorMapping }
return &t, nil
}
// Get blocker configuration by customerId and target_type in table blocker_configuration
blockerConfig, err := models.GetBlockerConfiguration(customer.Id, string(messages.DATACHANNEL_ACL))
if err != nil {
return nil, err
}
log.WithFields(log.Fields{
"blocker_type": blockerConfig.BlockerType,
}).Debug("Get blocker configuration")
if r.Aliases != nil {
t := AliasesRequest{ *r.Aliases }
validator := GetAliasValidator(blockerConfig.BlockerType)
if validator == nil {
return nil, errors.New("Unknown blocker type: " + blockerConfig.BlockerType)
}
bValid, errorMsg := validator.ValidateAlias(&t, customer)
if bValid == false {
return nil, errors.New(errorMsg)
}
return &t, nil
} else {
t := ACLsRequest{ *r.ACLs }
validator := GetAclValidator(blockerConfig.BlockerType)
if validator == nil {
return nil, errors.New("Unknown blocker type: " + blockerConfig.BlockerType)
}
bValid, errorMsg := validator.ValidateACL(&t, customer)
if bValid == false {
return nil, errors.New(errorMsg)
}
return &t, nil
}
}