Skip to content

Commit

Permalink
Break apart the Etcd client
Browse files Browse the repository at this point in the history
The Etcd Client is a little tricker because it requires
a config file. Later when we unify all the clients we will
use individual functions just like etcd.
  • Loading branch information
Ryan Hallisey committed Jun 23, 2017
1 parent d2ce427 commit fe5a7c1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
4 changes: 2 additions & 2 deletions pkg/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (

"github.com/openshift/ansible-service-broker/pkg/apb"
"github.com/openshift/ansible-service-broker/pkg/broker"
"github.com/openshift/ansible-service-broker/pkg/dao"
"github.com/openshift/ansible-service-broker/pkg/clients"
)

type Config struct {
Registry apb.RegistryConfig
Dao dao.Config
Dao clients.EtcdConfig
Log LogConfig
Openshift apb.ClusterConfig
ConfigFile string
Expand Down
40 changes: 40 additions & 0 deletions pkg/clients/etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package clients

import (
"fmt"
"time"

logging "github.com/op/go-logging"

"github.com/coreos/etcd/client"
)

type EtcdConfig struct {
EtcdHost string `yaml:"etcd_host"`
EtcdPort string `yaml:"etcd_port"`
}

func Etcd(config EtcdConfig, log *logging.Logger) (client.Client, error) {
// TODO: Config validation
endpoints := []string{etcdEndpoint(config.EtcdHost, config.EtcdPort)}

log.Info("== ETCD CX ==")
log.Infof("EtcdHost: %s", config.EtcdHost)
log.Infof("EtcdPort: %s", config.EtcdPort)
log.Infof("Endpoints: %v", endpoints)

etcdClient, err := client.New(client.Config{
Endpoints: endpoints,
Transport: client.DefaultTransport,
HeaderTimeoutPerRequest: time.Second,
})
if err != nil {
return nil, err
}

return etcdClient, nil
}

func etcdEndpoint(host string, port string) string {
return fmt.Sprintf("http://%s:%s", host, port)
}
41 changes: 8 additions & 33 deletions pkg/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,31 @@ import (
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/coreos/etcd/client"
"github.com/coreos/etcd/version"
logging "github.com/op/go-logging"
"github.com/openshift/ansible-service-broker/pkg/apb"
"github.com/openshift/ansible-service-broker/pkg/clients"
"github.com/pborman/uuid"
)

// Config - confg holds etcd host and port.
type Config struct {
EtcdHost string `yaml:"etcd_host"`
EtcdPort string `yaml:"etcd_port"`
}

// Dao - retreive, create, and update objects from etcd
type Dao struct {
config Config
log *logging.Logger
endpoints []string
client client.Client
kapi client.KeysAPI // Used to interact with kvp API over HTTP
config clients.EtcdConfig
log *logging.Logger
client client.Client
kapi client.KeysAPI // Used to interact with kvp API over HTTP
}

// NewDao - Create a new Dao object
func NewDao(config Config, log *logging.Logger) (*Dao, error) {
func NewDao(config clients.EtcdConfig, log *logging.Logger) (*Dao, error) {
var err error
dao := Dao{
config: config,
log: log,
}

// TODO: Config validation

dao.endpoints = []string{etcdEndpoint(config.EtcdHost, config.EtcdPort)}

log.Info("== ETCD CX ==")
log.Info(fmt.Sprintf("EtcdHost: %s", config.EtcdHost))
log.Info(fmt.Sprintf("EtcdPort: %s", config.EtcdPort))
log.Info(fmt.Sprintf("Endpoints: %v", dao.endpoints))

dao.client, err = client.New(client.Config{
Endpoints: dao.endpoints,
Transport: client.DefaultTransport,
HeaderTimeoutPerRequest: time.Second,
})
dao.client, err = clients.Etcd(config, log)
if err != nil {
return nil, err
}
Expand All @@ -62,17 +41,13 @@ func NewDao(config Config, log *logging.Logger) (*Dao, error) {
return &dao, nil
}

func etcdEndpoint(host string, port string) string {
return fmt.Sprintf("http://%s:%s", host, port)
}

// SetRaw - Allows the setting of the value json string to the key in the kvp API.
func (d *Dao) SetRaw(key string, val string) error {
_, err := d.kapi.Set(context.Background(), key, val /*opts*/, nil)
return err
}

func (d *Dao) GetEtcdVersion(config Config) (string, string, error) {
func (d *Dao) GetEtcdVersion(config clients.EtcdConfig) (string, string, error) {

// The next etcd release (1.4) will have client.GetVersion()
// We'll use this to test our etcd connection for now
Expand Down

0 comments on commit fe5a7c1

Please sign in to comment.