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

VX-175 valitate network entity before insert #96

Merged
merged 1 commit into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
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
24 changes: 12 additions & 12 deletions src/entity/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ const (
)

type PhyInterface struct {
Name string `bson:"name" json:"name"`
PCIID string `bson:"pciID" json:"pciID"`
Name string `bson:"name" json:"name" validate:"-"`
PCIID string `bson:"pciID" json:"pciID" validate:"-"`
}

type Node struct {
Name string `bson:"name" json:"name"`
PhyInterfaces []PhyInterface `bson:"physicalInterfaces" json:"physicalInterfaces"`
Name string `bson:"name" json:"name" validate:"required"`
PhyInterfaces []PhyInterface `bson:"physicalInterfaces" json:"physicalInterfaces" validate:"required,dive,required"`
}

type Network struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id"`
Type NetworkType `bson:"type" json:"type"`
IsDPDKPort bool `bson:"isDPDKPort" json:"isDPDKPort"`
Name string `bson:"name" json:"name"`
VLANTags []int32 `bson:"VLANTags" json:"VLANTags"`
BridgeName string `bson:"bridgeName" json:"bridgeName"`
Nodes []Node `bson:"nodes" json:"nodes"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty"`
ID bson.ObjectId `bson:"_id,omitempty" json:"id" validate:"-"`
Type NetworkType `bson:"type" json:"type" validate:"required"`
IsDPDKPort bool `bson:"isDPDKPort" json:"isDPDKPort" validate:"-"`
Name string `bson:"name" json:"name" validate:"required"`
VLANTags []int32 `bson:"VLANTags" json:"VLANTags" validate:"required,dive,max=4095,min=0"`
BridgeName string `bson:"bridgeName" json:"bridgeName" validate:"-"`
Nodes []Node `bson:"nodes" json:"nodes" validate:"required,dive,required"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty" validate:"-"`
}

// GetCollection - get model mongo collection name.
Expand Down
18 changes: 7 additions & 11 deletions src/server/handler_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/linkernetworks/utils/timeutils"
"github.com/linkernetworks/vortex/src/entity"
"github.com/linkernetworks/vortex/src/errors"
response "github.com/linkernetworks/vortex/src/net/http"
"github.com/linkernetworks/vortex/src/net/http/query"
np "github.com/linkernetworks/vortex/src/networkprovider"
Expand All @@ -28,6 +27,11 @@ func createNetworkHandler(ctx *web.Context) {
// overwrite the bridge name
network.BridgeName = np.GenerateBridgeName(string(network.Type), network.Name)

if err := sp.Validator.Struct(network); err != nil {
response.BadRequest(req.Request, resp.ResponseWriter, err)
return
}

session := sp.Mongo.NewSession()
defer session.Close()
session.C(entity.NetworkCollectionName).EnsureIndex(
Expand All @@ -43,16 +47,8 @@ func createNetworkHandler(ctx *web.Context) {
}

if err := networkProvider.CreateNetwork(sp); err != nil {
if err != nil {
switch err.(type) {
case *errors.ErrInvalidVLAN:
response.BadRequest(req.Request, resp.ResponseWriter, err)
return
default:
response.InternalServerError(req.Request, resp.ResponseWriter, err)
return
}
}
response.InternalServerError(req.Request, resp.ResponseWriter, err)
return
}

network.ID = bson.NewObjectId()
Expand Down
80 changes: 56 additions & 24 deletions src/server/handler_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,17 @@ func (suite *NetworkTestSuite) TestCreateNetwork() {
tName := namesgenerator.GetRandomName(0)
network := entity.Network{
Type: entity.FakeNetworkType,
IsDPDKPort: true, //for fake network, true means success,
Name: tName,
IsDPDKPort: true, // for fake network, true means success
BridgeName: namesgenerator.GetRandomName(0), // for fake network, true meas parameter good
VLANTags: []int32{},
BridgeName: namesgenerator.GetRandomName(0),
Nodes: []entity.Node{
{},
entity.Node{
Name: tName,
PhyInterfaces: []entity.PhyInterface{},
},
},
CreatedAt: &time.Time{},
}

bodyBytes, err := json.MarshalIndent(network, "", " ")
Expand Down Expand Up @@ -98,20 +103,29 @@ func (suite *NetworkTestSuite) TestCreateNetworkFail() {
}{
{"CreateFail",
entity.Network{
Type: entity.FakeNetworkType,
Name: namesgenerator.GetRandomName(0),
VLANTags: []int32{},
BridgeName: namesgenerator.GetRandomName(0),
Type: entity.FakeNetworkType,
Nodes: []entity.Node{
entity.Node{},
entity.Node{
Name: namesgenerator.GetRandomName(0),
PhyInterfaces: []entity.PhyInterface{},
},
},
},
http.StatusInternalServerError},
{"NetworkTypeError",
entity.Network{
Name: namesgenerator.GetRandomName(0),
Type: "none-exist",
Type: "none-exist",
Name: namesgenerator.GetRandomName(0),
VLANTags: []int32{},
BridgeName: namesgenerator.GetRandomName(0),
Nodes: []entity.Node{
entity.Node{},
entity.Node{
Name: namesgenerator.GetRandomName(0),
PhyInterfaces: []entity.PhyInterface{},
},
},
},
http.StatusBadRequest},
Expand Down Expand Up @@ -141,10 +155,13 @@ func (suite *NetworkTestSuite) TestDeleteNetwork() {
ID: bson.NewObjectId(),
IsDPDKPort: true, //for fake network, true means success,
Name: tName,
VLANTags: []int32{},
Type: entity.FakeNetworkType,
BridgeName: namesgenerator.GetRandomName(0),
Nodes: []entity.Node{
entity.Node{
Name: "TestDeleteNetwork",
Name: namesgenerator.GetRandomName(0),
PhyInterfaces: []entity.PhyInterface{},
},
},
}
Expand Down Expand Up @@ -178,21 +195,31 @@ func (suite *NetworkTestSuite) TestDeleteNetworkFail() {
errorCode int
}{
{"DeleteNetwork", entity.Network{
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
Type: entity.FakeNetworkType,
ID: bson.NewObjectId(),
Type: entity.FakeNetworkType,
Name: namesgenerator.GetRandomName(0),
VLANTags: []int32{},
BridgeName: namesgenerator.GetRandomName(0),
Nodes: []entity.Node{
entity.Node{},
entity.Node{
Name: namesgenerator.GetRandomName(0),
PhyInterfaces: []entity.PhyInterface{},
},
},
},
http.StatusInternalServerError},
{"NetworkTypeError",
entity.Network{
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
Type: "none-exist",
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
VLANTags: []int32{},
BridgeName: namesgenerator.GetRandomName(0),
Type: "none-exist",
Nodes: []entity.Node{
entity.Node{},
entity.Node{
Name: namesgenerator.GetRandomName(0),
PhyInterfaces: []entity.PhyInterface{},
},
},
},
http.StatusBadRequest},
Expand All @@ -219,12 +246,14 @@ func (suite *NetworkTestSuite) TestGetNetwork() {
tName := namesgenerator.GetRandomName(0)
tType := entity.FakeNetworkType
network := entity.Network{
ID: bson.NewObjectId(),
Name: tName,
Type: tType,
ID: bson.NewObjectId(),
Name: tName,
VLANTags: []int32{},
Type: tType,
Nodes: []entity.Node{
entity.Node{
Name: namesgenerator.GetRandomName(0),
Name: namesgenerator.GetRandomName(0),
PhyInterfaces: []entity.PhyInterface{},
},
},
}
Expand Down Expand Up @@ -262,11 +291,14 @@ func (suite *NetworkTestSuite) TestListNetwork() {
count := 3
for i := 0; i < count; i++ {
networks = append(networks, entity.Network{
Name: namesgenerator.GetRandomName(0),
Type: entity.FakeNetworkType,
Type: entity.FakeNetworkType,
Name: namesgenerator.GetRandomName(0),
VLANTags: []int32{},
BridgeName: namesgenerator.GetRandomName(0),
Nodes: []entity.Node{
entity.Node{
Name: namesgenerator.GetRandomName(0),
Name: namesgenerator.GetRandomName(0),
PhyInterfaces: []entity.PhyInterface{},
},
},
})
Expand Down
4 changes: 4 additions & 0 deletions src/serviceprovider/serviceprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/linkernetworks/redis"
kubeCtl "github.com/linkernetworks/vortex/src/kubernetes"

"gopkg.in/go-playground/validator.v9"
"k8s.io/client-go/kubernetes"
fakeclientset "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/rest"
Expand All @@ -25,6 +26,7 @@ type Container struct {
Mongo *mongo.Service
Prometheus *prometheusprovider.Service
KubeCtl *kubeCtl.KubeCtl
Validator *validator.Validate
}

type ServiceDiscoverResponse struct {
Expand Down Expand Up @@ -63,6 +65,7 @@ func New(cf config.Config) *Container {
Mongo: mongo,
Prometheus: prometheus,
KubeCtl: kubeCtl.New(clientset, "default"),
Validator: validator.New(),
}

return sp
Expand All @@ -89,6 +92,7 @@ func NewForTesting(cf config.Config) *Container {
Mongo: mongo,
Prometheus: prometheus,
KubeCtl: kubeCtl.New(clientset, "default"),
Validator: validator.New(),
}

return sp
Expand Down
24 changes: 24 additions & 0 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@
"revision": "e9ed3c6dfb39bb1a32197cb10d527906fe4da4b6",
"revisionTime": "2018-05-03T02:20:59Z"
},
{
"checksumSHA1": "b4DmyMT9bicTRVJw1hJXHLhIH+0=",
"path": "github.com/go-playground/locales",
"revision": "f63010822830b6fe52288ee52d5a1151088ce039",
"revisionTime": "2018-03-23T16:04:04Z"
},
{
"checksumSHA1": "JgF260rC9YpWyY5WEljjimWLUXs=",
"path": "github.com/go-playground/locales/currency",
"revision": "f63010822830b6fe52288ee52d5a1151088ce039",
"revisionTime": "2018-03-23T16:04:04Z"
},
{
"checksumSHA1": "7QcjNTtW0Rc/5BPw1MPaQ6YNjso=",
"path": "github.com/go-playground/universal-translator",
"revision": "71201497bace774495daed26a3874fd339e0b538",
"revisionTime": "2017-03-27T19:17:03Z"
},
{
"checksumSHA1": "YuGQx5ilYzlkaqeyVQh8qpsGExI=",
"path": "github.com/godbus/dbus",
Expand Down Expand Up @@ -860,6 +878,12 @@
"revision": "947dcec5ba9c011838740e680966fd7087a71d0d",
"revisionTime": "2017-12-17T18:08:21Z"
},
{
"checksumSHA1": "AbuKUV0gxECkRpjb2fmgo3DZYac=",
"path": "gopkg.in/go-playground/validator.v9",
"revision": "ab2a8a99087e827c9af87ed6777ba897348fb178",
"revisionTime": "2018-07-09T15:35:10Z"
},
{
"checksumSHA1": "COfXAfInbcFT/YRsvLUQnNKHzF0=",
"path": "gopkg.in/inf.v0",
Expand Down