// NewBroker is a constructor for a Broker webapp struct
func NewBroker(config *config.Config) (*Broker, error) {
	bkr := &Broker{
		config:  config.Broker,
		catalog: config.Catalog,
		backups: config.Backups,
		cells:   config.Scheduler.Cells,
	}

	bkr.logger = bkr.setupLogger()
	bkr.callbacks = NewCallbacks(config.Callbacks, bkr.logger)
	var err error
	bkr.state, err = state.NewStateEtcd(config.Etcd, bkr.logger)
	if err != nil {
		bkr.logger.Error("new-broker.new-state.error", err)
		return nil, err
	}

	bkr.patroni, err = patroni.NewPatroni(config.Etcd, bkr.logger)
	if err != nil {
		bkr.logger.Error("new-broker.new-patroni.error", err)
		return nil, err
	}

	bkr.scheduler, err = scheduler.NewScheduler(config.Scheduler, bkr.patroni, bkr.logger)
	if err != nil {
		bkr.logger.Error("new-broker.new-scheduler.error", err)
		return nil, err
	}

	bkr.router, err = routing.NewRouter(config.Etcd, bkr.logger)
	if err != nil {
		bkr.logger.Error("new-broker.new-router.error", err)
		return nil, err
	}

	// Optionally, provisioned services can asynchronously look up service name
	// to aide disaster recovery/undo-delete/recreate-from-backup by users
	// that only recall the service instance name they provided Cloud Foundry
	bkr.cf, err = NewCloudFoundryFromConfig(config.CloudFoundry, bkr.logger)
	if err != nil {
		bkr.logger.Error("new-broker.new-cf.error", err)
	} else {
		bkr.logger.Info("new-broker.new-cf.success", lager.Data{"api-url": config.CloudFoundry.ApiAddress})
	}

	return bkr, nil
}
// NewBroker is a constructor for a Broker webapp struct
func NewBroker(config *config.Config) (*Broker, error) {
	bkr := &Broker{
		config:     config.Broker,
		catalog:    config.Catalog,
		etcdConfig: config.Etcd,
		cells:      config.Scheduler.Cells,
	}

	bkr.logger = bkr.setupLogger()
	bkr.callbacks = NewCallbacks(config.Callbacks, bkr.logger)
	var err error
	bkr.scheduler, err = scheduler.NewScheduler(config.Scheduler, bkr.logger)
	if err != nil {
		bkr.logger.Error("new-broker.new-scheduler.error", err)
		return nil, err
	}
	bkr.state, err = state.NewStateEtcd(config.Etcd, bkr.logger)
	if err != nil {
		bkr.logger.Error("new-broker.new-state.error", err)
		return nil, err
	}

	bkr.patroni, err = patroni.NewPatroni(config.Etcd, bkr.logger)
	if err != nil {
		bkr.logger.Error("new-broker.new-patroni.error", err)
		return nil, err
	}

	bkr.router, err = routing.NewRouter(config.Etcd, bkr.logger)
	if err != nil {
		bkr.logger.Error("new-broker.new-router.error", err)
		return nil, err
	}

	return bkr, nil
}