Skip to content

Commit

Permalink
Fix source mgt
Browse files Browse the repository at this point in the history
  • Loading branch information
clarsonneur committed Jul 2, 2018
1 parent ac3c74f commit 2ac48d0
Show file tree
Hide file tree
Showing 26 changed files with 202 additions and 166 deletions.
2 changes: 1 addition & 1 deletion cli_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (a *Forj) ParseContext(c *cli.ForjCli, _ interface{}) (error, bool) {

// if deployTo was not set, use the default one
if deployTo == "" {
if v, found := a.f.Get("settings", "default", "dev-deploy"); found {
if v, found, _ := a.f.Get("settings", "default", "dev-deploy"); found {
deployTo = v.GetString()
} else {
a.w.SetError(fmt.Errorf("No development environment found in your Forjfile. " +
Expand Down
8 changes: 4 additions & 4 deletions creds/creds.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ func (d *Secure) SetFile(filePath, env string) {
}

// SetForjValue set a value in Forj section.
func (d *Secure) SetForjValue(env, key, value string) (_ bool, _ error) {
func (d *Secure) SetForjValue(env, source, key, value string) (_ bool, _ error) {
if d == nil {
return
}
if v, found := d.secrets.Envs[env]; found {
if v.SetForjValue(key, value) {
if v.SetForjValue(source, key, value) {
d.secrets.Envs[env] = v
d.updated = true
}
Expand All @@ -203,12 +203,12 @@ func (d *Secure) GetForjValue(env, key string) (_ string, _ bool) {
}

// SetObjectValue set object value
func (d *Secure) SetObjectValue(env, obj_name, instance_name, key_name string, value *goforjj.ValueStruct) (_ bool) {
func (d *Secure) SetObjectValue(env, source, obj_name, instance_name, key_name string, value *goforjj.ValueStruct) (_ bool) {
if d == nil {
return
}
if v, found := d.secrets.Envs[env]; found {
if v.setObjectValue(env, obj_name, instance_name, key_name, value) {
if v.setObjectValue(source, obj_name, instance_name, key_name, value) {
d.updated = true
d.secrets.Envs[env] = v
return true
Expand Down
21 changes: 13 additions & 8 deletions creds/creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,12 @@ func TestSetForjValue(t *testing.T) {
myPath = "myPath"
myFile = "myFile"
prod = "prod"
source = "source"
)
s.InitEnvDefaults(myPath, prod)

// ------------- call the function
updated, err := s.SetForjValue(prod, key1, value1)
updated, err := s.SetForjValue(prod, source, key1, value1)

// -------------- testing
if !updated {
Expand Down Expand Up @@ -168,23 +169,26 @@ func TestSetObjectValue(t *testing.T) {
myPath = "myPath"
myFile = "myFile"
prod = "prod"
source = "source"
)
s.InitEnvDefaults(myPath, prod)

// ------------- call the function
value := new(goforjj.ValueStruct)
value.Set(value1)
updated := s.SetObjectValue(prod, object1, instance1, key1, value)
updated := s.SetObjectValue(prod, source, object1, instance1, key1, value)

// -------------- testing
if !updated {
t.Error("Expected s.SetObjectValue to return updated = true. Got false")
} else if v, found, src := s.Get(object1, instance1, key1); !found {
} else if v, found, src, env := s.Get(object1, instance1, key1); !found {
t.Error("Expected value to be found. Got false")
} else if v1 := v.GetString(); v1 != value1 {
t.Errorf("Expected value to be '%s'. Got '%s'", v1, value1)
} else if src != prod {
t.Errorf("Expected value to be found from source '%s'. Got '%s'", prod, src)
} else if src != source {
t.Errorf("Expected value to be found from source '%s'. Got '%s'", source, src)
} else if env != prod {
t.Errorf("Expected value to be found from env '%s'. Got '%s'", prod, env)
}
}

Expand All @@ -202,11 +206,12 @@ func TestGetObjectInstance(t *testing.T) {
myPath = "myPath"
myFile = "myFile"
prod = "prod"
source = "source"
)
s.InitEnvDefaults(myPath, prod)
value := new(goforjj.ValueStruct)
value.Set(value1)
s.SetObjectValue(prod, object1, instance1, key1, value)
s.SetObjectValue(prod, source, object1, instance1, key1, value)

// ------------- call the function
result := s.GetObjectInstance(object1, instance1)
Expand All @@ -231,7 +236,7 @@ func TestGetObjectInstance(t *testing.T) {

// --------------- Change context
value.Set(value2)
s.SetObjectValue(Global, object1, instance1, key1, value)
s.SetObjectValue(Global, source, object1, instance1, key1, value)

// ------------- call the function
result = s.GetObjectInstance(object1, instance1)
Expand All @@ -249,7 +254,7 @@ func TestGetObjectInstance(t *testing.T) {

// --------------- Change context
value.Set(value1)
s.SetObjectValue(Global, object2, instance1, key1, value)
s.SetObjectValue(Global, source, object2, instance1, key1, value)

// ------------- call the function
result1 := s.GetObjectInstance(object1, instance1)
Expand Down
3 changes: 2 additions & 1 deletion creds/yaml_secure.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ func (d *yamlSecure) save() error {
return nil
}

func (d *yamlSecure) SetForjValue(key, value string) (updated bool) {
func (d *yamlSecure) SetForjValue(source, key, value string) (updated bool) {

d.sources = d.sources.Set(source, key, value)
if d.Forj == nil {
d.Forj = make(map[string]string)
}
Expand Down
3 changes: 2 additions & 1 deletion creds/yaml_secure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ func Test_YamlSecure_SetForjValue(t *testing.T) {
const (
key1 = "key1"
value1 = "value1"
source = "source"
)

// ------------- call the function
s.SetForjValue(key1, value1)
s.SetForjValue(source, key1, value1)

// -------------- testing
if s.Forj == nil {
Expand Down
2 changes: 1 addition & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (a *Forj) driver_do(d *drivers.Driver, instance_name, action string, args .
}

// Current deploy only
if deployName, found := repo_obj.Get(forjfile.FieldRepoDeployName); found && repo_obj.IsCurrentDeploy() {
if deployName, found, _ := repo_obj.Get(forjfile.FieldRepoDeployName); found && repo_obj.IsCurrentDeploy() {
deployObj, _ := a.f.GetADeployment(deployName.GetString())

deployObj.RunInContext(func() (_ error) {
Expand Down
38 changes: 20 additions & 18 deletions drivers_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,15 @@ func (a *Forj) GetForjjFlags(r *goforjj.PluginReqData, d *drivers.Driver, action
}

func (a *Forj) moveSecureAppData(ffd *forjfile.DeployForgeYaml, deploy, flag_name string, missing_required bool) error {
if v, found := ffd.GetString("settings", "", flag_name); found {
a.s.SetForjValue(deploy, flag_name, v)
if v, found, source := ffd.GetString("settings", "", flag_name); found {
a.s.SetForjValue(deploy, source, flag_name, v)
ffd.Remove("settings", "", flag_name)
gotrace.Trace("Moving secure flag data '%s' from Forjfile to creds.yaml", flag_name)
return nil
}
if v, error := a.cli.GetAppStringValue(flag_name); error == nil {
gotrace.Trace("Setting Forjfile flag '%s' from cli", flag_name)
a.s.SetForjValue(deploy, flag_name, v)
a.s.SetForjValue(deploy, "forjj", flag_name, v)
return nil
}
if missing_required {
Expand All @@ -332,27 +332,27 @@ func (a *Forj) moveSecureAppData(ffd *forjfile.DeployForgeYaml, deploy, flag_nam
func (a *Forj) copyCliData(deploy, flag_name, def_value string) {
if v, error := a.cli.GetAppStringValue(flag_name); error == nil && v != "" {
gotrace.Trace("Setting Forjfile flag '%s' from cli", flag_name)
a.s.SetForjValue(deploy, flag_name, v)
a.s.SetForjValue(deploy, "forjj", flag_name, v)
} else {
if def_value != "" {
gotrace.Trace("Setting Forjfile flag '%s' default value to '%s'", flag_name, def_value)
a.s.SetForjValue(deploy, flag_name, def_value)
a.s.SetForjValue(deploy, "forjj", flag_name, def_value)
}
}
}

func (a *Forj) moveSecureObjectData(ffd *forjfile.DeployForgeYaml, deploy, object_name, instance, flag_name string, missing_required bool) error {
if v, found := ffd.Get(object_name, instance, "secret_"+flag_name); found {
if v, found, source := ffd.Get(object_name, instance, "secret_"+flag_name); found {
// each key can have a secret_<key> value defined, stored in secret and can be refered in the Forjfile
// with {{ Current.Creds.<flag_name> }}
a.s.SetObjectValue(deploy, object_name, instance, flag_name, v)
a.s.SetObjectValue(deploy, source , object_name, instance, flag_name, v)
ffd.Remove(object_name, instance, flag_name)

gotrace.Trace("Removing and setting secure Object (%s/%s) flag data '%s' from Forjfile to creds.yaml",
object_name, instance, "secret_"+flag_name)
}
if v, found := ffd.Get(object_name, instance, flag_name); found {
a.s.SetObjectValue(deploy, object_name, instance, flag_name, v)
if v, found, source := ffd.Get(object_name, instance, flag_name); found {
a.s.SetObjectValue(deploy, source, object_name, instance, flag_name, v)
// When no template value is set in Forjfile flag value, (default case in next code line)
// forjj will consider this string '{{ .Current.Creds.<flag_name> }}' as way to extract it
// The Forjfile can define that flag value to a different template. A simple string is not
Expand All @@ -363,7 +363,7 @@ func (a *Forj) moveSecureObjectData(ffd *forjfile.DeployForgeYaml, deploy, objec
return nil
}
if v, found, _, _ := a.cli.GetStringValue(object_name, instance, flag_name); found {
a.s.SetObjectValue(deploy, object_name, instance, flag_name, new(goforjj.ValueStruct).Set(v))
a.s.SetObjectValue(deploy, "forjj", object_name, instance, flag_name, new(goforjj.ValueStruct).Set(v))
gotrace.Trace("Set %s/%s:%s value to Forjfile from cli.", object_name, instance, flag_name)
return nil
}
Expand All @@ -374,10 +374,10 @@ func (a *Forj) moveSecureObjectData(ffd *forjfile.DeployForgeYaml, deploy, objec
}

func (a *Forj) setSecureObjectData(ffd *forjfile.DeployForgeYaml, deploy, object_name, instance, flag_name string, missing_required bool) error {
if v, found := ffd.Get(object_name, instance, "secret-"+flag_name); found {
if v, found, source := ffd.Get(object_name, instance, "secret-"+flag_name); found {
// each key can have a secret_<key> value defined, stored in secret and can be referred in the Forjfile
// with {{ Current.Creds.<flag_name> }}
a.s.SetObjectValue(deploy, object_name, instance, flag_name, v)
a.s.SetObjectValue(deploy, source, object_name, instance, flag_name, v)
ffd.Remove(object_name, instance, "secret-"+flag_name)
gotrace.Trace("Moving secret Object (%s/%s) flag data '%s' from Forjfile to creds.yaml",
object_name, instance, "secret-"+flag_name)
Expand Down Expand Up @@ -530,10 +530,10 @@ func (a *Forj) IsRepoManaged(d *drivers.Driver, object_name, instance_name strin
// RepoManagedBy return the upstream instance name that is identified to have the ownership
func (a *Forj) RepoManagedBy(object_name, instance_name string) (_ string) {
// Determine if the upstream instance is set to this instance.
if v, found := a.f.GetString(object_name, instance_name, "git-remote"); found && v != "" {
if v, found, _ := a.f.GetString(object_name, instance_name, "git-remote"); found && v != "" {
return
}
if v, found := a.f.GetString(object_name, instance_name, "apps:upstream"); found {
if v, found, _ := a.f.GetString(object_name, instance_name, "apps:upstream"); found {
return v
}
return
Expand Down Expand Up @@ -600,15 +600,15 @@ func (a *Forj) GetObjectsData(r *goforjj.PluginReqData, d *drivers.Driver, actio
if flag.Options.Secure {
// From creds.yml
def_value := "{{ (index .Current.Creds \"" + key + "\").GetString }}"
if v, found := ffd.Get(object_name, instance_name, key); found {
if v, found, _ := ffd.Get(object_name, instance_name, key); found {
if s := v.GetString(); strings.HasPrefix("{{", s) {
def_value = s
}
}
value.Set(def_value)
} else {
// From Forjfile
if v, found := ffd.Get(object_name, instance_name, key); !found {
if v, found, _ := ffd.Get(object_name, instance_name, key); !found {
gotrace.Trace("%s/%s: NOT ADDED: Key '%s' has not been found in Forjfile. ", object_name, instance_name, key)
continue
} else {
Expand Down Expand Up @@ -669,7 +669,8 @@ func (a *Forj) DefineDeployRepositories(ffd *forjfile.DeployForgeYaml, warning b
return fmt.Errorf("The infra '%s' repository can't be a deployment repository. Please, fix your Forjfile accordingly", repoName)
}
if r := deploy.AttachedRepo(); r != nil {
return fmt.Errorf("You can't define multiple deployment repository. Deployment '%s' is already attached to '%s'. Fix your Forjfile", deploy.Name(), r.GetString(forjfile.FieldRepoName))
name, _ := r.GetString(forjfile.FieldRepoName)
return fmt.Errorf("You can't define multiple deployment repository. Deployment '%s' is already attached to '%s'. Fix your Forjfile", deploy.Name(), name)
}
deploy.AttachRepo(repo, a.w.Organization)
gotrace.Trace("Declared repo '%s' is attached to '%s' deployment", repoName, deploy.Name())
Expand All @@ -685,7 +686,8 @@ func (a *Forj) DefineDeployRepositories(ffd *forjfile.DeployForgeYaml, warning b
return fmt.Errorf("Deployment repository can't be your Infra Source repository '%s'. Fix your Forjfile", stdRepoName)
}
if r := deploy.AttachedRepo(); r != nil {
gotrace.Warning("Found repository '%s'. It has not been attached to '%s' because you declared '%s' as deployment repository.", stdRepoName, deploy.Name(), r.GetString(forjfile.FieldRepoName))
name, _ := r.GetString(forjfile.FieldRepoName)
gotrace.Warning("Found repository '%s'. It has not been attached to '%s' because you declared '%s' as deployment repository.", stdRepoName, deploy.Name(), name)
continue
}
deploy.AttachRepo(repo, a.w.Organization)
Expand Down
5 changes: 3 additions & 2 deletions flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (a *Forj) FlowApply() error {
ffd := a.f.InMemForjfile()
bInError := false
defaultFlowToApply := "default"
if v, found := a.f.Get("settings", "default", "flow"); found {
if v, found, _ := a.f.Get("settings", "default", "flow"); found {
defaultFlowToApply = v.GetString()
}

Expand All @@ -34,7 +34,8 @@ func (a *Forj) FlowApply() error {
}

if err := a.flows.Apply(flowToApply, repo, ffd); err != nil { // Applying Flow to Forjfile repo
gotrace.Error("Repo '%s': %s", repo.GetString("name"), err)
name , _ := repo.GetString("name")
gotrace.Error("Repo '%s': %s", name, err)
bInError = true
}
}
Expand Down
3 changes: 2 additions & 1 deletion flow/flow_define.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func (fd *FlowDefine) apply(repo *forjfile.RepoStruct, Forjfile *forjfile.Deploy
for _, flowTask := range tasks {
onWhat := "Forjfile"
if repo != nil {
onWhat = fmt.Sprintf("repository '%s'", repo.GetString("name"))
name, _ := repo.GetString("name")
onWhat = fmt.Sprintf("repository '%s'", name)
} else {

}
Expand Down
17 changes: 9 additions & 8 deletions forjfile/app.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package forjfile

import (
"forjj/sources_info"
"fmt"
"forjj/sources_info"

"github.com/forj-oss/goforjj"
)
Expand Down Expand Up @@ -94,17 +94,18 @@ func (a *AppStruct) Name() string {

// Get return the flag value.
// found is true if value exist in more or if the value is not empty
func (a *AppStruct) Get(flag string) (value *goforjj.ValueStruct, _ bool) {
func (a *AppStruct) Get(flag string) (value *goforjj.ValueStruct, found bool, source string) {
source = a.sources.Get(flag)
switch flag {
case appName:
return value.Set(a.name), (a.name != "")
value, found = value.Set(a.name), (a.name != "")
case appType:
return value.Set(a.Type), (a.Type != "")
value, found = value.Set(a.Type), (a.Type != "")
case appDriver:
return value.Set(a.Driver), (a.Driver != "")
value, found = value.Set(a.Driver), (a.Driver != "")
default:
v, f := a.more[flag]
return value.SetIfFound(v.Get(), f)
value, found = value.SetIfFound(v.Get(), f)
}
return
}
Expand Down Expand Up @@ -156,9 +157,9 @@ func (g *AppStruct) set_forge(f *ForgeYaml) {
g.forge = f
}

func (a *AppStruct) mergeFrom(source string, from *AppStruct) {
func (a *AppStruct) mergeFrom(from *AppStruct) {
for _, flag := range from.Flags() {
if v, found := from.Get(flag); found {
if v, found, source := from.Get(flag); found {
a.Set(source, flag, v.GetString(), (*ForjValue).Set)
}
}
Expand Down
2 changes: 1 addition & 1 deletion forjfile/app_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ func (r AppModel)Get(field string) (_ string) {
if r.app == nil {
return
}
v, _ := r.app.Get(field)
v, _, _ := r.app.Get(field)
return v.GetString()
}
4 changes: 2 additions & 2 deletions forjfile/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ func (a AppsStruct) Found(appName string) (*AppStruct, error) {
}
}

func (a AppsStruct) mergeFrom(source string, from AppsStruct) {
func (a AppsStruct) mergeFrom(from AppsStruct) {
for k, appFrom := range from {
if app, found := a[k]; found {
app.mergeFrom(source, appFrom)
app.mergeFrom(appFrom)
} else {

a[k] = appFrom
Expand Down
12 changes: 7 additions & 5 deletions forjfile/default_settings_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ type DefaultSettingsStruct struct {
}

// Get return the value of the default setting.
func (s *DefaultSettingsStruct) Get(key string) (value *goforjj.ValueStruct, _ bool) {
func (s *DefaultSettingsStruct) Get(key string) (value *goforjj.ValueStruct, found bool, source string) {
source = s.sources.Get(key)
switch key {
// TODO: Remove obsolete reference to "upstream-instance"
case "upstream-instance":
return value.SetIfFound(s.UpstreamInstance, (s.UpstreamInstance != ""))
value, found = value.SetIfFound(s.UpstreamInstance, (s.UpstreamInstance != ""))
case "flow":
return value.SetIfFound(s.Flow, (s.Flow != ""))
value, found = value.SetIfFound(s.Flow, (s.Flow != ""))
case "dev-deploy":
return value.SetIfFound(s.DevDeploy, (s.DevDeploy != ""))
value, found = value.SetIfFound(s.DevDeploy, (s.DevDeploy != ""))
default:
v, f := s.More[key]
return value.SetIfFound(v, f)
value, found = value.SetIfFound(v, f)
}
return
}

// Set udpate the value of the default setting key.
Expand Down
Loading

0 comments on commit 2ac48d0

Please sign in to comment.