Exemplo n.º 1
0
func (si *ServiceInstance) Bind(app bind.App) error {
	err := si.AddApp(app.GetName())
	if err != nil {
		return &errors.Http{Code: http.StatusConflict, Message: "This app is already binded to this service instance."}
	}
	var envVars []bind.EnvVar
	var setEnv = func(env map[string]string) {
		for k, v := range env {
			envVars = append(envVars, bind.EnvVar{
				Name:         k,
				Value:        v,
				Public:       false,
				InstanceName: si.Name,
			})
		}
	}
	cli := si.Service().ProductionEndpoint()
	if len(app.GetUnits()) == 0 {
		return &errors.Http{Code: http.StatusPreconditionFailed, Message: "This app does not have an IP yet."}
	}
	env, err := cli.Bind(si, app)
	if err != nil {
		return err
	}
	setEnv(env)
	err = si.update()
	if err != nil {
		cli.Unbind(si, app)
		return err
	}
	return app.SetEnvs(envVars, false)
}