Skip to content

Commit

Permalink
juju: ExtraBindings used when specified
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimiter Naydenov committed Mar 1, 2016
1 parent 3887960 commit 208f2ad
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
20 changes: 7 additions & 13 deletions juju/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ func DeployService(st ServiceDeployer, args DeployServiceParams) (*state.Service
return nil, fmt.Errorf("use of --networks is deprecated. Please use spaces")
}

effectiveBindings, err := getEffectiveBindingsForCharmMeta(args.Charm.Meta(), args.EndpointBindings)
if err != nil {
return nil, errors.Annotate(err, "cannot determine effective service endpoint bindings")
}
effectiveBindings := getEffectiveBindingsForCharmMeta(args.Charm.Meta(), args.EndpointBindings)

asa := state.AddServiceArgs{
Name: args.ServiceName,
Expand All @@ -98,28 +95,25 @@ func DeployService(st ServiceDeployer, args DeployServiceParams) (*state.Service
return st.AddService(asa)
}

func getEffectiveBindingsForCharmMeta(charmMeta *charm.Meta, givenBindings map[string]string) (map[string]string, error) {
combinedEndpoints, err := state.CombinedCharmRelations(charmMeta)
if err != nil {
return nil, errors.Trace(err)
}
func getEffectiveBindingsForCharmMeta(charmMeta *charm.Meta, givenBindings map[string]string) map[string]string {
allBindings := state.DefaultEndpointBindingsForCharm(charmMeta)
if givenBindings == nil {
givenBindings = make(map[string]string, len(combinedEndpoints))
givenBindings = make(map[string]string, len(allBindings))
}

// Get the service-level default binding for all unspecified endpoint, if
// set, otherwise use the empty default.
serviceDefaultSpace, _ := givenBindings[""]

effectiveBindings := make(map[string]string, len(combinedEndpoints))
for endpoint, _ := range combinedEndpoints {
effectiveBindings := make(map[string]string, len(allBindings))
for endpoint, _ := range allBindings {
if givenSpace, isGiven := givenBindings[endpoint]; isGiven {
effectiveBindings[endpoint] = givenSpace
} else {
effectiveBindings[endpoint] = serviceDefaultSpace
}
}
return effectiveBindings, nil
return effectiveBindings
}

// AddUnits starts n units of the given service using the specified placement
Expand Down
46 changes: 45 additions & 1 deletion juju/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,16 @@ func (s *DeployLocalSuite) TestDeployWithImplicitBindings(c *gc.C) {

func (s *DeployLocalSuite) addWordpressCharm(c *gc.C) *state.Charm {
wordpressCharmURL := charm.MustParseURL("local:quantal/wordpress")
wordpressCharm, err := testing.PutCharm(s.State, wordpressCharmURL, s.repo, false)
return s.addWordpressCharmFromURL(c, wordpressCharmURL)
}

func (s *DeployLocalSuite) addWordpressCharmWithExtraBindings(c *gc.C) *state.Charm {
wordpressCharmURL := charm.MustParseURL("local:quantal/wordpress-with-extra-bindings")
return s.addWordpressCharmFromURL(c, wordpressCharmURL)
}

func (s *DeployLocalSuite) addWordpressCharmFromURL(c *gc.C, charmURL *charm.URL) *state.Charm {
wordpressCharm, err := testing.PutCharm(s.State, charmURL, s.repo, false)
c.Assert(err, jc.ErrorIsNil)
return wordpressCharm
}
Expand Down Expand Up @@ -161,6 +170,41 @@ func (s *DeployLocalSuite) TestDeployWithSomeSpecifiedBindings(c *gc.C) {
})
}

func (s *DeployLocalSuite) TestDeployWithBoundRelationNamesAndExtraBindingsNames(c *gc.C) {
wordpressCharm := s.addWordpressCharmWithExtraBindings(c)
_, err := s.State.AddSpace("db", "", nil, false)
c.Assert(err, jc.ErrorIsNil)
_, err = s.State.AddSpace("public", "", nil, false)
c.Assert(err, jc.ErrorIsNil)
_, err = s.State.AddSpace("internal", "", nil, false)
c.Assert(err, jc.ErrorIsNil)

service, err := juju.DeployService(s.State,
juju.DeployServiceParams{
ServiceName: "bob",
Charm: wordpressCharm,
EndpointBindings: map[string]string{
"": "public",
"db": "db",
"db-client": "db",
"admin-api": "internal",
},
})
c.Assert(err, jc.ErrorIsNil)

s.assertBindings(c, service, map[string]string{
"url": "public",
"logging-dir": "public",
"monitoring-port": "public",
"db": "db",
"cache": "public",
"db-client": "db",
"admin-api": "internal",
"cluster": "public",
"foo-bar": "public", // like for relations, uses the service-default.
})
}

func (s *DeployLocalSuite) TestDeployResources(c *gc.C) {
f := &fakeDeployer{State: s.State}

Expand Down

0 comments on commit 208f2ad

Please sign in to comment.