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-171 generate bridge name #93

Merged
merged 3 commits 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
7 changes: 7 additions & 0 deletions src/networkprovider/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/linkernetworks/vortex/src/entity"
"github.com/linkernetworks/vortex/src/serviceprovider"
"github.com/linkernetworks/vortex/src/utils"
)

type NetworkProvider interface {
Expand All @@ -30,3 +31,9 @@ func GetNetworkProvider(network *entity.Network) (NetworkProvider, error) {
return nil, fmt.Errorf("unsupported Network Type %s", network.Type)
}
}

func GenerateBridgeName(datapathType, networkName string) string {
tmp := fmt.Sprintf("%s%s", datapathType, networkName)
str := utils.SHA256String(tmp)
return fmt.Sprintf("ovs-%s-%s", datapathType, str[0:6])
}
5 changes: 5 additions & 0 deletions src/networkprovider/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ func TestGetNetworkProviderFail(t *testing.T) {
})
assert.Error(t, err)
}

func TestGenerateBridgeName(t *testing.T) {
ans := GenerateBridgeName("netdev", "my network 1")
assert.Equal(t, "ovs-netdev-de0165", ans)
}
3 changes: 3 additions & 0 deletions src/server/handler_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func createNetworkHandler(ctx *web.Context) {
return
}

// overwrite the bridge name
network.BridgeName = np.GenerateBridgeName(string(network.Type), network.Name)

session := sp.Mongo.NewSession()
defer session.Close()
session.C(entity.NetworkCollectionName).EnsureIndex(
Expand Down
14 changes: 14 additions & 0 deletions src/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package utils

import (
"crypto/sha256"
"encoding/hex"
"fmt"
)

func SHA256String(str string) string {
hash := sha256.New()
hash.Write([]byte(str))
md := hash.Sum(nil)
return fmt.Sprintf("%s", hex.EncodeToString(md))
}
12 changes: 12 additions & 0 deletions src/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package utils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSHA256String(t *testing.T) {
o := SHA256String("12345678")
assert.Equal(t, "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f", o)
}