Skip to content

Commit

Permalink
Added method to BindUnit.
Browse files Browse the repository at this point in the history
Related to #263.
  • Loading branch information
andrewsmedina committed Jan 8, 2013
1 parent af24348 commit 65b869f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/bind/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type App interface {
type Binder interface {
// BindApp makes the bind between the binder and an app.
BindApp(App) error
// BindUnit makes the bind between the binder and an unit.
BindUnit(Unit) (map[string]string, error)
// UnbindApp makes the unbind between the binder and an app.
UnbindApp(App) error
}
27 changes: 27 additions & 0 deletions service/bind/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ func createTestApp(name, framework string, teams []string, units []app.Unit) (ap
return a, err
}

func (s *S) TestBindUnit(c *C) {
called := false
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
w.Write([]byte(`{"DATABASE_USER":"root","DATABASE_PASSWORD":"s3cr3t"}`))
}))
defer ts.Close()
srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}}
err := srvc.Create()
c.Assert(err, IsNil)
defer db.Session.Services().Remove(bson.M{"_id": "mysql"})
instance := service.ServiceInstance{Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}}
instance.Create()
defer db.Session.ServiceInstances().Remove(bson.M{"_id": "my-mysql"})
a, err := createTestApp("painkiller", "", []string{s.team.Name}, []app.Unit{{Ip: "10.10.10.10"}})
c.Assert(err, IsNil)
defer db.Session.Apps().Remove(bson.M{"name": a.Name})
envs, err := instance.BindUnit(a.GetUnits()[0])
c.Assert(err, IsNil)
c.Assert(called, Equals, true)
expectedEnvs := map[string]string{
"DATABASE_USER": "root",
"DATABASE_PASSWORD": "s3cr3t",
}
c.Assert(envs, DeepEquals, expectedEnvs)
}

func (s *S) TestBindAddsAppToTheServiceInstance(c *C) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"DATABASE_USER":"root","DATABASE_PASSWORD":"s3cr3t"}`))
Expand Down
8 changes: 7 additions & 1 deletion service/service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (si *ServiceInstance) BindApp(app bind.App) error {
}
var envs map[string]string
for _, unit := range app.GetUnits() {
envs, err = cli.Bind(si, unit)
envs, err = si.BindUnit(unit)
if err != nil {
return err
}
Expand All @@ -104,6 +104,12 @@ func (si *ServiceInstance) BindApp(app bind.App) error {
return app.SetEnvs(envVars, false)
}

// BindUnit makes the bind between the binder and an unit.
func (si *ServiceInstance) BindUnit(unit bind.Unit) (map[string]string, error) {
cli := si.Service().ProductionEndpoint()
return cli.Bind(si, unit)
}

// UnbindApp makes the unbind between the service instance and an app.
func (si *ServiceInstance) UnbindApp(app bind.App) error {
err := si.RemoveApp(app.GetName())
Expand Down

0 comments on commit 65b869f

Please sign in to comment.