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

Create metadata db interface #298

Merged
merged 10 commits into from
Jul 2, 2018
52 changes: 0 additions & 52 deletions core/data/clients/mongo-client_test.go

This file was deleted.

12 changes: 6 additions & 6 deletions core/data/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"time"

"github.com/edgexfoundry/edgex-go/core/clients/types"
"github.com/edgexfoundry/edgex-go/core/data/clients"
"github.com/edgexfoundry/edgex-go/core/db"
"github.com/edgexfoundry/edgex-go/core/domain/models"
"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -215,7 +215,7 @@ func eventHandler(w http.ResponseWriter, r *http.Request) {
// Check value descriptor
vd, err := dbc.ValueDescriptorByName(e.Readings[reading].Name)
if err != nil {
if err == clients.ErrNotFound {
if err == db.ErrNotFound {
http.Error(w, "Value descriptor for a reading not found", http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand Down Expand Up @@ -269,7 +269,7 @@ func eventHandler(w http.ResponseWriter, r *http.Request) {
// Check if the event exists
to, err := dbc.EventById(from.ID.Hex())
if err != nil {
if err == clients.ErrNotFound {
if err == db.ErrNotFound {
http.Error(w, "Event not found", http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand Down Expand Up @@ -327,7 +327,7 @@ func getEventByIdHandler(w http.ResponseWriter, r *http.Request) {
// Get the event
e, err := dbc.EventById(id)
if err != nil {
if err == clients.ErrNotFound {
if err == db.ErrNotFound {
http.Error(w, "Event not found", http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand Down Expand Up @@ -421,7 +421,7 @@ func eventIdHandler(w http.ResponseWriter, r *http.Request) {
// Check if the event exists
e, err := dbc.EventById(id)
if err != nil {
if err == clients.ErrNotFound {
if err == db.ErrNotFound {
http.Error(w, "Event not found", http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand All @@ -448,7 +448,7 @@ func eventIdHandler(w http.ResponseWriter, r *http.Request) {
// Check if the event exists
e, err := dbc.EventById(id)
if err != nil {
if err == clients.ErrNotFound {
if err == db.ErrNotFound {
http.Error(w, "Event not found", http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand Down
4 changes: 2 additions & 2 deletions core/data/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strconv"
"testing"

"github.com/edgexfoundry/edgex-go/core/data/clients"
"github.com/edgexfoundry/edgex-go/core/db/memory"
"github.com/edgexfoundry/edgex-go/core/domain/models"
"github.com/edgexfoundry/edgex-go/support/logging-client"
"github.com/gorilla/mux"
Expand All @@ -34,7 +34,7 @@ var testRoutes *mux.Router
func TestMain(m *testing.M) {
testEvent.Device = "test device"
testEvent.Origin = 123456789
dbc, _ = clients.NewDBClient(clients.DBConfiguration{DbType: clients.MEMORY})
dbc = &memory.MemDB{}
testEvent.ID, _ = dbc.AddEvent(&testEvent)
testRoutes = LoadRestRoutes()
loggingClient = logger.NewMockClient()
Expand Down
46 changes: 38 additions & 8 deletions core/data/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ import (

"github.com/edgexfoundry/edgex-go/core/clients/metadata"
"github.com/edgexfoundry/edgex-go/core/clients/types"
"github.com/edgexfoundry/edgex-go/core/data/clients"
"github.com/edgexfoundry/edgex-go/core/data/interfaces"
"github.com/edgexfoundry/edgex-go/core/data/messaging"
"github.com/edgexfoundry/edgex-go/core/db"
"github.com/edgexfoundry/edgex-go/core/db/influx"
"github.com/edgexfoundry/edgex-go/core/db/memory"
"github.com/edgexfoundry/edgex-go/core/db/mongo"
"github.com/edgexfoundry/edgex-go/internal"
consulclient "github.com/edgexfoundry/edgex-go/support/consul-client"
"github.com/edgexfoundry/edgex-go/support/logging-client"
)

// Global variables
var dbc clients.DBClient
var dbc interfaces.DBClient
var loggingClient logger.LoggingClient
var ep *messaging.EventPublisher
var mdc metadata.DeviceClient
Expand Down Expand Up @@ -57,6 +61,28 @@ func ConnectToConsul(conf ConfigurationStruct) error {
return nil
}

// Return the dbClient interface
func newDBClient(dbType interfaces.DatabaseType, config db.Configuration) (interfaces.DBClient, error) {
switch dbType {
case interfaces.MONGO:
// Create the mongo client
return mongo.NewClient(config), nil
case interfaces.INFLUX:
// Create the influx client
ic, err := influx.NewClient(config)
if err != nil {
loggingClient.Error("Error creating the influx client: " + err.Error())
return nil, err
}
return ic, nil
case interfaces.MEMORY:
// Create the memory client
return &memory.MemDB{}, nil
default:
return nil, db.ErrUnsupportedDatabase
}
}

func Init(conf ConfigurationStruct, l logger.LoggingClient, useConsul bool) error {
loggingClient = l
configuration = conf
Expand All @@ -65,25 +91,29 @@ func Init(conf ConfigurationStruct, l logger.LoggingClient, useConsul bool) erro
var err error

// Create a database client
dbc, err = clients.NewDBClient(clients.DBConfiguration{
DbType: clients.MONGO,
dbc, err = newDBClient(interfaces.MONGO, db.Configuration{
Host: conf.MongoDBHost,
Port: conf.MongoDBPort,
Timeout: conf.MongoDBConnectTimeout,
DatabaseName: conf.MongoDatabaseName,
Username: conf.MongoDBUserName,
Password: conf.MongoDBPassword,
})
if err != nil {
return fmt.Errorf("couldn't create database client: %v", err.Error())
}

err = dbc.Connect()
if err != nil {
return fmt.Errorf("couldn't connect to database: %v", err.Error())
}

// Create metadata clients
params := types.EndpointParams{
ServiceKey:internal.CoreMetaDataServiceKey,
Path:conf.MetaDevicePath,
UseRegistry:useConsul,
Url:conf.MetaDeviceURL}
ServiceKey: internal.CoreMetaDataServiceKey,
Path: conf.MetaDevicePath,
UseRegistry: useConsul,
Url: conf.MetaDeviceURL}

mdc = metadata.NewDeviceClient(params, types.Endpoint{})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/
package clients
package interfaces

import (
"errors"

"github.com/edgexfoundry/edgex-go/core/domain/models"
"github.com/edgexfoundry/edgex-go/support/logging-client"
"gopkg.in/mgo.v2/bson"
)

Expand All @@ -31,6 +28,8 @@ const (
type DBClient interface {
CloseSession()

Connect() error

// ********************** EVENT FUNCTIONS *******************************
// Return all the events
// UnexpectedError - failed to retrieve events from the database
Expand Down Expand Up @@ -192,48 +191,3 @@ type DBClient interface {
// Delete all value descriptors
ScrubAllValueDescriptors() error
}

type DBConfiguration struct {
DbType DatabaseType
Host string
Port int
Timeout int
DatabaseName string
Username string
Password string
}

var ErrNotFound error = errors.New("Item not found")
var ErrUnsupportedDatabase error = errors.New("Unsuppored database type")
var ErrInvalidObjectId error = errors.New("Invalid object ID")
var ErrNotUnique error = errors.New("Resource already exists")
var DataClient = "dataClient"
var loggingClient = logger.NewClient(DataClient, false, "")

// Return the dbClient interface
func NewDBClient(config DBConfiguration) (DBClient, error) {
switch config.DbType {
case MONGO:
// Create the mongo client
mc, err := newMongoClient(config)
if err != nil {
loggingClient.Error("Error creating the mongo client: " + err.Error())
return nil, err
}
return mc, nil
case INFLUX:
// Create the influx client
ic, err := newInfluxClient(config)
if err != nil {
loggingClient.Error("Error creating the influx client: " + err.Error())
return nil, err
}
return ic, nil
case MEMORY:
// Create the memory client
mem := &memDB{}
return mem, nil
default:
return nil, ErrUnsupportedDatabase
}
}
16 changes: 8 additions & 8 deletions core/data/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"net/url"
"strconv"

"github.com/edgexfoundry/edgex-go/core/data/clients"
"github.com/edgexfoundry/edgex-go/core/db"
"github.com/edgexfoundry/edgex-go/core/domain/models"
"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -63,7 +63,7 @@ func readingHandler(w http.ResponseWriter, r *http.Request) {
// Check the value descriptor
vd, err := dbc.ValueDescriptorByName(reading.Name)
if err != nil {
if err == clients.ErrNotFound {
if err == db.ErrNotFound {
http.Error(w, "Value descriptor not found for reading", http.StatusConflict)
} else {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand Down Expand Up @@ -116,7 +116,7 @@ func readingHandler(w http.ResponseWriter, r *http.Request) {
// Check if the reading exists
to, err := dbc.ReadingById(from.Id.Hex())
if err != nil {
if err == clients.ErrNotFound {
if err == db.ErrNotFound {
http.Error(w, "Reading not found", http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand All @@ -141,7 +141,7 @@ func readingHandler(w http.ResponseWriter, r *http.Request) {
// Check the value descriptor
vd, err := dbc.ValueDescriptorByName(to.Name)
if err != nil {
if err == clients.ErrNotFound {
if err == db.ErrNotFound {
http.Error(w, "Value descriptor not found for reading", http.StatusConflict)
} else {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand Down Expand Up @@ -187,7 +187,7 @@ func getReadingByIdHandler(w http.ResponseWriter, r *http.Request) {
case http.MethodGet:
reading, err := dbc.ReadingById(id)
if err != nil {
if err == clients.ErrNotFound {
if err == db.ErrNotFound {
http.Error(w, "Reading not found", http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand Down Expand Up @@ -235,7 +235,7 @@ func deleteReadingByIdHandler(w http.ResponseWriter, r *http.Request) {
// Check if the reading exists
reading, err := dbc.ReadingById(id)
if err != nil {
if err == clients.ErrNotFound {
if err == db.ErrNotFound {
http.Error(w, "Reading not found", http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand Down Expand Up @@ -329,7 +329,7 @@ func readingbyValueDescriptorHandler(w http.ResponseWriter, r *http.Request) {
if configuration.ValidateCheck {
_, err = dbc.ValueDescriptorByName(name)
if err != nil {
if err == clients.ErrNotFound {
if err == db.ErrNotFound {
http.Error(w, "Value descriptor not found for reading", http.StatusConflict)
} else {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand Down Expand Up @@ -601,7 +601,7 @@ func readingByValueDescriptorAndDeviceHandler(w http.ResponseWriter, r *http.Req
if configuration.ValidateCheck {
_, err = dbc.ValueDescriptorByName(name)
if err != nil {
if err == clients.ErrNotFound {
if err == db.ErrNotFound {
http.Error(w, "Value descriptor not found for reading", http.StatusConflict)
} else {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand Down
Loading