Skip to content

Commit

Permalink
Read config from yml
Browse files Browse the repository at this point in the history
  • Loading branch information
ultragtx committed Mar 30, 2017
1 parent 6e567d8 commit 5cde83e
Show file tree
Hide file tree
Showing 27 changed files with 12,389 additions and 53 deletions.
6 changes: 5 additions & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 4 additions & 37 deletions broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"

"github.com/FoOTOo/cf-mongodb-broker/config"
"github.com/pivotal-cf/brokerapi"
"gopkg.in/mgo.v2/bson"
)
Expand All @@ -30,32 +31,11 @@ type InstanceBinder interface {
type MongoServiceBroker struct {
InstanceCreators map[string]InstanceCreator
InstanceBinders map[string]InstanceBinder
Config config.Config
}

func (mongoServiceBroker *MongoServiceBroker) Services(context context.Context) []brokerapi.Service {
// TODO: read config

planList := []brokerapi.ServicePlan{}
for _, plan := range mongoServiceBroker.plans() {
planList = append(planList, *plan)
}

services := []brokerapi.Service{
brokerapi.Service{
ID: "SOME-UUID-98769-mongodb-service", // TODO: better uuid
Name: "footoo-mongodb",
Description: "A in development mongodb service",
Bindable: true,
Tags: []string{"FoOTOo", "mongodb"},
PlanUpdatable: false,
Plans: planList,
//Requires
//Metadata
//DashboardClient
},
}

return services
return mongoServiceBroker.Config.Services()
}

func (mongoServiceBroker *MongoServiceBroker) Provision(context context.Context, instanceID string, details brokerapi.ProvisionDetails, asyncAllowed bool) (brokerapi.ProvisionedServiceSpec, error) {
Expand Down Expand Up @@ -183,20 +163,7 @@ func (mongoServiceBroker *MongoServiceBroker) Unbind(context context.Context, in
}

func (mongoServiceBroker *MongoServiceBroker) plans() map[string]*brokerapi.ServicePlan {
plans := map[string]*brokerapi.ServicePlan{}

free := true

plans["standard"] = &brokerapi.ServicePlan{
ID: "SOME-UUID-98769-standard", // TODO: better uuid
Name: "standard",
Description: "Standard mongodb plan",
Free: &free,
//Bindable:
//Metadata:
}

return plans
return mongoServiceBroker.Config.Plans()
}

//func (mongoServiceBroker *MongoServiceBroker) instanceExists(instanceID string) bool {
Expand Down
55 changes: 55 additions & 0 deletions config/assets/test_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
broker:
bindable: true
dashboard_client: null
description: MongoDB
host: https://p-mongodb-broker.cftesting.footoo.org
id: SOME-UUID-98769-f-mongodb-broker #
metadata:
displayName: MongoDB
documentationUrl: null
imageUrl: http://info.mongodb.com/rs/mongodb/images/MongoDB_Logo_Full.png
longDescription: Creating a service instance provisions a database. Binding
applications provisions unique credentials for each application to access
the database.
providerDisplayName: fcf
supportUrl: null
name: f-mongodb #
plan_updateable: false
plans:
- description: Shared MongoDB Server
free: true
id: mongo
metadata:
bullets:
- Shared MongoDB(3.4.2) replica set
costs:
- amount:
usd: 0
unit: month
name: standard
requires: null
security_user_name: broker-username
security_user_password: broker-password
tags:
- mongodb

mongod:
nodes:
bind_ip: 0.0.0.0
ips:
- 172.16.0.156
port: 27017
path:
log: /var/vcap/sys/log/mongod/mongod.log
replSet:
enabled: true
key: replicasetkey
name: replicasetname
root:
pass: rootpassword
user: rootusername
storage:
engine: wiredTiger
journal: true
path: /var/vcap/store/mongod
152 changes: 152 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package config

import (
"os"

"strings"

"github.com/cloudfoundry-incubator/candiedyaml"
"github.com/pivotal-cf/brokerapi"
)

type Config struct {
MongoConfig MongoConfig `yaml:"mongod"`
BrokerConfig BrokerConfig `yaml:"broker"`
}

type MongoConfig struct {
Nodes MongoNodes `yaml:"nodes"`
RootUser MongoRootUser `yaml:"root"`
}

type MongoRootUser struct {
Username string `yaml:"user"`
Password string `yaml:"pass"`
}

type MongoNodes struct {
IPs []string `yaml:"ips"`
Port string `yaml:"port"`
}

type BrokerConfig struct {
Host string `yaml:"host"`
BrokerUsername string `yaml:"security_user_name"`
BrokerPassword string `yaml:"security_user_password"`

ServiceID string `yaml:"id"`
ServiceName string `yaml:"name"`
ServiceDescription string `yaml:"description"`
ServiceBindable bool `yaml:"bindable"`
PlanUpdateable bool `yaml:"plan_updateable"`
Plans []Plan `yaml:"plans"`
Tags []string `yaml:"tags"`
ServiceMetadata ServiceMetadata `yaml:"metadata"`
}

type ServiceMetadata struct {
DisplayName string `yaml:"displayName"`
ImageUrl string `yaml:"imageUrl"`
LongDescription string `yaml:"longDescription"`
ProviderDisplayName string `yaml:"providerDisplayName"`
DocumentationUrl string `yaml:"documentationUrl"`
SupportUrl string `yaml:"supportUrl"`
}

type Plan struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Description string `yaml:"description"`
Free bool `yaml:"free"`
Metadata PlanMetadata `yaml:"metadata"`
}

type PlanMetadata struct {
Bullets []string `yaml:"bullets"`
Costs []PlanMetadataCost `yaml:"costs"`
}

type PlanMetadataCost struct {
}

func ParseConfig(path string) (Config, error) {
file, error := os.Open(path)
if error != nil {
return Config{}, error
}

var config Config
if err := candiedyaml.NewDecoder(file).Decode(&config); err != nil {
return Config{}, err
}

return config, nil
}

func (config *Config) MongoHosts() string {
var hosts []string
for _, host := range config.MongoConfig.Nodes.IPs {
hostWithPort := host + ":" + config.MongoConfig.Nodes.Port
hosts = append(hosts, hostWithPort)
}

return strings.Join(hosts, ",")
}

func (config *Config) MongoUsername() string {
return config.MongoConfig.RootUser.Username
}

func (config *Config) MongoPassword() string {
return config.MongoConfig.RootUser.Password
}

func (config *Config) Services() []brokerapi.Service {
planList := []brokerapi.ServicePlan{}
for _, plan := range config.Plans() {
planList = append(planList, *plan)
}

brokerConfig := config.BrokerConfig
serviceMetadata := brokerConfig.ServiceMetadata

services := []brokerapi.Service{
brokerapi.Service{
ID: brokerConfig.ServiceID,
Name: brokerConfig.ServiceName,
Description: brokerConfig.ServiceDescription,
Bindable: brokerConfig.ServiceBindable,
Tags: brokerConfig.Tags,
Plans: planList,
PlanUpdatable: brokerConfig.PlanUpdateable,
Metadata: &brokerapi.ServiceMetadata{
DisplayName: serviceMetadata.DisplayName,
ImageUrl: serviceMetadata.ImageUrl,
LongDescription: serviceMetadata.LongDescription,
ProviderDisplayName: serviceMetadata.ProviderDisplayName,
DocumentationUrl: serviceMetadata.DocumentationUrl,
SupportUrl: serviceMetadata.SupportUrl,
},
//Requires
//DashboardClient
},
}

return services
}

func (config *Config) Plans() map[string]*brokerapi.ServicePlan {
plans := map[string]*brokerapi.ServicePlan{}

for _, plan := range config.BrokerConfig.Plans {
plans[plan.Name] = &brokerapi.ServicePlan{
ID: plan.ID,
Name: plan.Name,
Description: plan.Description,
Free: &plan.Free,
// Metadata
}
}

return plans
}
34 changes: 34 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package config

import (
"fmt"
"path"
"path/filepath"
"testing"
)

func TestConfig(t *testing.T) {
configPath := "test_config.yml"
path, error := filepath.Abs(path.Join("assets", configPath))

if error != nil {
t.Fatal("Error: ", error)
}

config, error := ParseConfig(path)

if error != nil {
t.Fatal("Error: ", error)
}

fmt.Println("Raw Config ----------------")
fmt.Printf("%+v\n", config)

//j, _ := json.MarshalIndent(config.Plans(), "", " ")
fmt.Println("Raw Plans ----------------")
fmt.Printf("%+v\n", config.Plans())
//
//j, _ = json.MarshalIndent(config.Services(), "", " ")
fmt.Println("Raw Service ----------------")
fmt.Printf("%+v\n", config.Services())
}
36 changes: 26 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"net/http"
"os"

"github.com/FoOTOo/mongo-service-broker-golang-ultragtx/broker"
"github.com/FoOTOo/cf-mongodb-broker/broker"
"github.com/FoOTOo/cf-mongodb-broker/config"
"github.com/pivotal-cf/brokerapi"

"code.cloudfoundry.org/lager"
"github.com/FoOTOo/mongo-service-broker-golang-ultragtx/mongo"
"github.com/FoOTOo/cf-mongodb-broker/mongo"
)

const (
BrokerName = "mongodb-broker"
BrokerUsername = "mongodb-broker-user"
BrokerPassword = "mongodb-broker-password"
BrokerName = "cf-mongodb-broker"
)

func main() {
Expand All @@ -25,9 +24,17 @@ func main() {

brokerLogger.Info("Starting Mongodb broker")

// TODO: config file
// Config
brokerConfigPath := configPath()
config, err := config.ParseConfig(brokerConfigPath)
if err != nil {
brokerLogger.Fatal("Loading config file", err, lager.Data{
"broker-config-path": brokerConfigPath,
})
}

adminService, error := mongo.NewAdminService("172.16.0.156", "rootusername", "rootpassword", "admin") // TODO: change
//adminService, error := mongo.NewAdminService("172.16.0.156", "rootusername", "rootpassword", "admin")
adminService, error := mongo.NewAdminService(config.MongoHosts(), config.MongoUsername(), config.MongoPassword(), "admin")

if error != nil {
brokerLogger.Fatal("mongo-admin-service", error)
Expand All @@ -46,11 +53,12 @@ func main() {
InstanceBinders: map[string]broker.InstanceBinder{
"standard": instanceBinder,
},
Config: config,
}

brokerCredentials := brokerapi.BrokerCredentials{
Username: BrokerUsername,
Password: BrokerPassword,
Username: config.BrokerConfig.BrokerUsername,
Password: config.BrokerConfig.BrokerPassword,
}

// broker
Expand All @@ -61,5 +69,13 @@ func main() {

http.Handle("/", brokerAPI)

brokerLogger.Fatal("http-listen", http.ListenAndServe(":"+"9876", nil)) // TODO: config
brokerLogger.Fatal("http-listen", http.ListenAndServe(":"+"80", nil)) // TODO: ???Listening on host in config
}

func configPath() string {
brokerConfigYamlPath := os.Getenv("BROKER_CONFIG_PATH")
if brokerConfigYamlPath == "" {
panic("BROKER_CONFIG_PATH not set")
}
return brokerConfigYamlPath
}
Loading

0 comments on commit 5cde83e

Please sign in to comment.