1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (c) 2020 Intel Corporation
7 "github.com/open-ness/EMCO/src/orchestrator/pkg/infra/config"
8 pkgerrors "github.com/pkg/errors"
11 // Db interface used to talk a concrete Database connection
14 // ContextDb is an interface for accessing the context database
15 type ContextDb interface {
16 // Returns nil if db health is good
18 // Puts Json Struct in db with key
19 Put(key string, value interface{}) error
21 Delete(key string) error
22 // Delete all keys in heirarchy
23 DeleteAll(key string) error
24 // Gets Json Struct from db
25 Get(key string, value interface{}) error
26 // Returns all keys with a prefix
27 GetAllKeys(path string) ([]string, error)
30 // createContextDBClient creates the DB client
31 func createContextDBClient(dbType string) error {
36 Endpoint: config.GetConfiguration().EtcdIP,
37 CertFile: config.GetConfiguration().EtcdCert,
38 KeyFile: config.GetConfiguration().EtcdKey,
39 CAFile: config.GetConfiguration().EtcdCAFile,
41 Db, err = NewEtcdClient(nil, c)
43 pkgerrors.Wrap(err, "Etcd Client Initialization failed with error")
46 return pkgerrors.New(dbType + "DB not supported")
51 // InitializeContextDatabase sets up the connection to the
52 // configured database to allow the application to talk to it.
53 func InitializeContextDatabase() error {
54 // Only support Etcd for now
55 err := createContextDBClient("etcd")
57 return pkgerrors.Cause(err)
59 err = Db.HealthCheck()
61 return pkgerrors.Cause(err)