From 5d198e3f214d8dcdae61cb08b059531a6f1162ba Mon Sep 17 00:00:00 2001 From: Guiheux Steven Date: Thu, 11 Jun 2020 09:29:12 +0200 Subject: [PATCH] refactor(api): variables dao (#5235) --- .../api/application/application_exporter.go | 2 +- engine/api/application/application_parser.go | 3 +- engine/api/application/dao_variable.go | 52 ++++++------- engine/api/application/dao_variable_test.go | 4 +- engine/api/application/gorp_model.go | 4 +- engine/api/application_export_test.go | 4 +- engine/api/application_import_test.go | 6 +- engine/api/application_variable.go | 4 +- engine/api/application_variable_test.go | 2 +- engine/api/environment.go | 2 +- engine/api/environment/dao_variable.go | 20 ++--- engine/api/environment/dao_variable_test.go | 4 +- .../api/environment/environment_exporter.go | 2 +- .../api/environment/environment_importer.go | 4 +- .../environment/environment_importer_test.go | 8 +- engine/api/environment/environment_parser.go | 2 +- engine/api/environment/gorp_model.go | 30 ++++---- engine/api/environment_export_test.go | 4 +- engine/api/environment_import_test.go | 4 +- engine/api/environment_test.go | 4 +- engine/api/environment_variable.go | 4 +- engine/api/environment_variable_test.go | 8 +- engine/api/event/publish_application.go | 6 +- engine/api/event/publish_environment.go | 6 +- engine/api/event/publish_project.go | 6 +- engine/api/project/dao_variable.go | 20 ++--- engine/api/project/dao_variable_test.go | 4 +- engine/api/project/gorp_model.go | 30 ++++---- engine/api/project_test.go | 4 +- engine/api/project_variable.go | 7 +- engine/api/project_variable_test.go | 2 +- engine/api/workflow/execute_node_job_run.go | 8 +- engine/api/workflow/run_workflow_test.go | 2 +- engine/api/workflow_import_test.go | 4 +- sdk/application.go | 18 ++--- sdk/environment.go | 52 ++++++------- sdk/event_application.go | 8 +- sdk/event_environment.go | 8 +- sdk/event_project.go | 10 +-- sdk/exportentities/environment.go | 4 +- sdk/parameter.go | 48 +++++++++++- sdk/project.go | 18 ++--- sdk/variable.go | 73 +++++++++++++++++++ 43 files changed, 317 insertions(+), 198 deletions(-) diff --git a/engine/api/application/application_exporter.go b/engine/api/application/application_exporter.go index f5f22714b2..03377cbfde 100644 --- a/engine/api/application/application_exporter.go +++ b/engine/api/application/application_exporter.go @@ -26,7 +26,7 @@ func Export(db gorp.SqlExecutor, cache cache.Store, key string, appName string, // ExportApplication encrypt and export func ExportApplication(db gorp.SqlExecutor, app sdk.Application, encryptFunc sdk.EncryptFunc) (exportentities.Application, error) { - var appvars []sdk.Variable + var appvars []sdk.ApplicationVariable for _, v := range app.Variables { switch v.Type { case sdk.KeyVariable: diff --git a/engine/api/application/application_parser.go b/engine/api/application/application_parser.go index 31c0122903..5a825829fe 100644 --- a/engine/api/application/application_parser.go +++ b/engine/api/application/application_parser.go @@ -70,8 +70,7 @@ func ParseAndImport(ctx context.Context, db gorp.SqlExecutor, cache cache.Store, } v.Value = secret } - - vv := sdk.Variable{Name: p, Type: v.Type, Value: v.Value} + vv := sdk.ApplicationVariable{Name: p, Type: v.Type, Value: v.Value} app.Variables = append(app.Variables, vv) } diff --git a/engine/api/application/dao_variable.go b/engine/api/application/dao_variable.go index 65b13dc9a5..196746347d 100644 --- a/engine/api/application/dao_variable.go +++ b/engine/api/application/dao_variable.go @@ -28,7 +28,7 @@ func (e dbApplicationVariable) Canonical() gorpmapping.CanonicalForms { } } -func newDBApplicationVariable(v sdk.Variable, appID int64) dbApplicationVariable { +func newDBApplicationVariable(v sdk.ApplicationVariable, appID int64) dbApplicationVariable { if sdk.NeedPlaceholder(v.Type) { return dbApplicationVariable{ ID: v.ID, @@ -47,28 +47,30 @@ func newDBApplicationVariable(v sdk.Variable, appID int64) dbApplicationVariable } } -func (e dbApplicationVariable) Variable() sdk.Variable { +func (e dbApplicationVariable) Variable() sdk.ApplicationVariable { if sdk.NeedPlaceholder(e.Type) { - return sdk.Variable{ - ID: e.ID, - Name: e.Name, - Value: e.CipherValue, - Type: e.Type, + return sdk.ApplicationVariable{ + ID: e.ID, + Name: e.Name, + Value: e.CipherValue, + Type: e.Type, + ApplicationID: e.ApplicationID, } } - return sdk.Variable{ - ID: e.ID, - Name: e.Name, - Value: e.ClearValue, - Type: e.Type, + return sdk.ApplicationVariable{ + ID: e.ID, + Name: e.Name, + Value: e.ClearValue, + Type: e.Type, + ApplicationID: e.ApplicationID, } } -func loadAllVariables(db gorp.SqlExecutor, query gorpmapping.Query, opts ...gorpmapping.GetOptionFunc) ([]sdk.Variable, error) { +func loadAllVariables(db gorp.SqlExecutor, query gorpmapping.Query, opts ...gorpmapping.GetOptionFunc) ([]sdk.ApplicationVariable, error) { var ctx = context.Background() var res []dbApplicationVariable - vars := make([]sdk.Variable, 0, len(res)) + vars := make([]sdk.ApplicationVariable, 0, len(res)) if err := gorpmapping.GetAll(ctx, db, query, &res, opts...); err != nil { return nil, err @@ -88,19 +90,18 @@ func loadAllVariables(db gorp.SqlExecutor, query gorpmapping.Query, opts ...gorp return vars, nil } -// LoadAllVariables Get all variable for the given application -func LoadAllVariables(db gorp.SqlExecutor, appID int64) ([]sdk.Variable, error) { +func LoadAllVariables(db gorp.SqlExecutor, appID int64, opts ...gorpmapping.GetOptionFunc) ([]sdk.ApplicationVariable, error) { query := gorpmapping.NewQuery(` SELECT * FROM application_variable WHERE application_id = $1 - ORDER BY var_name + ORDER BY application_id, var_name `).Args(appID) - return loadAllVariables(db, query) + return loadAllVariables(db, query, opts...) } // LoadAllVariablesWithDecrytion Get all variable for the given application, it also decrypt all the secure content -func LoadAllVariablesWithDecrytion(db gorp.SqlExecutor, appID int64) ([]sdk.Variable, error) { +func LoadAllVariablesWithDecrytion(db gorp.SqlExecutor, appID int64) ([]sdk.ApplicationVariable, error) { query := gorpmapping.NewQuery(` SELECT * FROM application_variable @@ -110,7 +111,7 @@ func LoadAllVariablesWithDecrytion(db gorp.SqlExecutor, appID int64) ([]sdk.Vari return loadAllVariables(db, query, gorpmapping.GetOptions.WithDecryption) } -func loadVariable(db gorp.SqlExecutor, q gorpmapping.Query, opts ...gorpmapping.GetOptionFunc) (*sdk.Variable, error) { +func loadVariable(db gorp.SqlExecutor, q gorpmapping.Query, opts ...gorpmapping.GetOptionFunc) (*sdk.ApplicationVariable, error) { var v dbApplicationVariable found, err := gorpmapping.Get(context.Background(), db, q, &v, opts...) if err != nil { @@ -133,14 +134,14 @@ func loadVariable(db gorp.SqlExecutor, q gorpmapping.Query, opts ...gorpmapping. } // LoadVariable retrieve a specific variable -func LoadVariable(db gorp.SqlExecutor, appID int64, varName string) (*sdk.Variable, error) { +func LoadVariable(db gorp.SqlExecutor, appID int64, varName string) (*sdk.ApplicationVariable, error) { query := gorpmapping.NewQuery(`SELECT * FROM application_variable WHERE application_id = $1 AND var_name=$2`).Args(appID, varName) return loadVariable(db, query) } // LoadVariableWithDecryption retrieve a specific variable with decrypted content -func LoadVariableWithDecryption(db gorp.SqlExecutor, appID int64, varID int64, varName string) (*sdk.Variable, error) { +func LoadVariableWithDecryption(db gorp.SqlExecutor, appID int64, varID int64, varName string) (*sdk.ApplicationVariable, error) { query := gorpmapping.NewQuery(`SELECT * FROM application_variable WHERE application_id = $1 AND id = $2 AND var_name=$3`).Args(appID, varID, varName) return loadVariable(db, query, gorpmapping.GetOptions.WithDecryption) @@ -157,13 +158,12 @@ func DeleteAllVariables(db gorp.SqlExecutor, applicationID int64) error { } // InsertVariable Insert a new variable in the given application -func InsertVariable(db gorp.SqlExecutor, appID int64, v *sdk.Variable, u sdk.Identifiable) error { +func InsertVariable(db gorp.SqlExecutor, appID int64, v *sdk.ApplicationVariable, u sdk.Identifiable) error { //Check variable name rx := sdk.NamePatternRegex if !rx.MatchString(v.Name) { return sdk.NewErrorFrom(sdk.ErrInvalidName, "variable name should match pattern %s", sdk.NamePattern) } - dbVar := newDBApplicationVariable(*v, appID) err := gorpmapping.InsertAndSign(context.Background(), db, &dbVar) if err != nil && strings.Contains(err.Error(), "application_variable_pkey") { @@ -191,7 +191,7 @@ func InsertVariable(db gorp.SqlExecutor, appID int64, v *sdk.Variable, u sdk.Ide } // UpdateVariable Update a variable in the given application -func UpdateVariable(db gorp.SqlExecutor, appID int64, variable *sdk.Variable, variableBefore *sdk.Variable, u sdk.Identifiable) error { +func UpdateVariable(db gorp.SqlExecutor, appID int64, variable *sdk.ApplicationVariable, variableBefore *sdk.ApplicationVariable, u sdk.Identifiable) error { rx := sdk.NamePatternRegex if !rx.MatchString(variable.Name) { return sdk.NewErrorFrom(sdk.ErrInvalidName, "variable name should match pattern %s", sdk.NamePattern) @@ -227,7 +227,7 @@ func UpdateVariable(db gorp.SqlExecutor, appID int64, variable *sdk.Variable, va } // DeleteVariable Delete a variable from the given pipeline -func DeleteVariable(db gorp.SqlExecutor, appID int64, variable *sdk.Variable, u sdk.Identifiable) error { +func DeleteVariable(db gorp.SqlExecutor, appID int64, variable *sdk.ApplicationVariable, u sdk.Identifiable) error { query := `DELETE FROM application_variable WHERE application_variable.application_id = $1 AND application_variable.var_name = $2` result, err := db.Exec(query, appID, variable.Name) diff --git a/engine/api/application/dao_variable_test.go b/engine/api/application/dao_variable_test.go index 07d5a908e8..d73f5a443b 100644 --- a/engine/api/application/dao_variable_test.go +++ b/engine/api/application/dao_variable_test.go @@ -25,8 +25,8 @@ func Test_DAOVariable(t *testing.T) { require.NoError(t, application.Insert(db, *proj, &app)) - v1 := &sdk.Variable{Name: "clear", Type: sdk.TextVariable, Value: "clear_value"} - v2 := &sdk.Variable{Name: "secret", Type: sdk.SecretVariable, Value: "secret_value"} + v1 := &sdk.ApplicationVariable{Name: "clear", Type: sdk.TextVariable, Value: "clear_value"} + v2 := &sdk.ApplicationVariable{Name: "secret", Type: sdk.SecretVariable, Value: "secret_value"} require.NoError(t, application.InsertVariable(db, app.ID, v1, u)) assert.Equal(t, "clear_value", v1.Value) diff --git a/engine/api/application/gorp_model.go b/engine/api/application/gorp_model.go index 1a161827b1..7bbc96db46 100644 --- a/engine/api/application/gorp_model.go +++ b/engine/api/application/gorp_model.go @@ -32,7 +32,7 @@ func (ava *dbApplicationVariableAudit) PostGet(db gorp.SqlExecutor) error { } if before.Valid { - vBefore := &sdk.Variable{} + vBefore := &sdk.ApplicationVariable{} if err := json.Unmarshal([]byte(before.String), vBefore); err != nil { return err } @@ -44,7 +44,7 @@ func (ava *dbApplicationVariableAudit) PostGet(db gorp.SqlExecutor) error { } if after.Valid { - vAfter := &sdk.Variable{} + vAfter := &sdk.ApplicationVariable{} if err := json.Unmarshal([]byte(after.String), vAfter); err != nil { return err } diff --git a/engine/api/application_export_test.go b/engine/api/application_export_test.go index 147c700549..383330f85d 100644 --- a/engine/api/application_export_test.go +++ b/engine/api/application_export_test.go @@ -26,7 +26,7 @@ func Test_getApplicationExportHandler(t *testing.T) { t.Fatal(err) } - v1 := sdk.Variable{ + v1 := sdk.ApplicationVariable{ Name: "var1", Value: "value 1", Type: sdk.StringVariable, @@ -34,7 +34,7 @@ func Test_getApplicationExportHandler(t *testing.T) { test.NoError(t, application.InsertVariable(db, app.ID, &v1, u)) - v2 := sdk.Variable{ + v2 := sdk.ApplicationVariable{ Name: "var2", Value: "value 2", Type: sdk.SecretVariable, diff --git a/engine/api/application_import_test.go b/engine/api/application_import_test.go index 9f53ce0427..93344c32ec 100644 --- a/engine/api/application_import_test.go +++ b/engine/api/application_import_test.go @@ -117,7 +117,7 @@ func Test_postApplicationImportHandler_NewAppFromYAMLWithKeysAndSecrets(t *testi t.Fatal(err) } - test.NoError(t, application.InsertVariable(api.mustDB(), app.ID, &sdk.Variable{ + test.NoError(t, application.InsertVariable(api.mustDB(), app.ID, &sdk.ApplicationVariable{ Name: "myPassword", Type: sdk.SecretVariable, Value: "MySecretValue", @@ -225,7 +225,7 @@ func Test_postApplicationImportHandler_NewAppFromYAMLWithKeysAndSecretsAndReImpo t.Fatal(err) } - test.NoError(t, application.InsertVariable(api.mustDB(), app.ID, &sdk.Variable{ + test.NoError(t, application.InsertVariable(api.mustDB(), app.ID, &sdk.ApplicationVariable{ Name: "myPassword", Type: sdk.SecretVariable, Value: "MySecretValue", @@ -401,7 +401,7 @@ func Test_postApplicationImportHandler_NewAppFromYAMLWithKeysAndSecretsAndReImpo k2.KeyID = kssh.KeyID test.NoError(t, application.InsertKey(api.mustDB(), k2)) - test.NoError(t, application.InsertVariable(api.mustDB(), app.ID, &sdk.Variable{ + test.NoError(t, application.InsertVariable(api.mustDB(), app.ID, &sdk.ApplicationVariable{ Name: "myPassword", Type: sdk.SecretVariable, Value: "MySecretValue", diff --git a/engine/api/application_variable.go b/engine/api/application_variable.go index 91ee0df856..a40249aa56 100644 --- a/engine/api/application_variable.go +++ b/engine/api/application_variable.go @@ -136,7 +136,7 @@ func (api *API) updateVariableInApplicationHandler() service.Handler { appName := vars["applicationName"] varName := vars["name"] - var newVar sdk.Variable + var newVar sdk.ApplicationVariable if err := service.UnmarshalBody(r, &newVar); err != nil { return err } @@ -184,7 +184,7 @@ func (api *API) addVariableInApplicationHandler() service.Handler { appName := vars["applicationName"] varName := vars["name"] - var newVar sdk.Variable + var newVar sdk.ApplicationVariable if err := service.UnmarshalBody(r, &newVar); err != nil { return err } diff --git a/engine/api/application_variable_test.go b/engine/api/application_variable_test.go index 065eb36181..029ed28bdd 100644 --- a/engine/api/application_variable_test.go +++ b/engine/api/application_variable_test.go @@ -32,7 +32,7 @@ func Test_getVariableAuditInApplicationHandler(t *testing.T) { } // Add variable - v := sdk.Variable{ + v := sdk.ApplicationVariable{ Name: "foo", Type: "string", Value: "bar", diff --git a/engine/api/environment.go b/engine/api/environment.go index 6413545361..f7a8d35be4 100644 --- a/engine/api/environment.go +++ b/engine/api/environment.go @@ -278,7 +278,7 @@ func (api *API) cloneEnvironmentHandler() service.Handler { } } - variables := []sdk.Variable{} + variables := []sdk.EnvironmentVariable{} for _, v := range env.Variables { // do not clone secret variable to avoid 'secret value not specified' if v.Type != sdk.SecretVariable { diff --git a/engine/api/environment/dao_variable.go b/engine/api/environment/dao_variable.go index 9bc9bbf619..3c32221d95 100644 --- a/engine/api/environment/dao_variable.go +++ b/engine/api/environment/dao_variable.go @@ -11,10 +11,10 @@ import ( "github.com/ovh/cds/sdk/log" ) -func loadAllVariables(db gorp.SqlExecutor, query gorpmapping.Query, opts ...gorpmapping.GetOptionFunc) ([]sdk.Variable, error) { +func loadAllVariables(db gorp.SqlExecutor, query gorpmapping.Query, opts ...gorpmapping.GetOptionFunc) ([]sdk.EnvironmentVariable, error) { var ctx = context.Background() var res []dbEnvironmentVariable - vars := make([]sdk.Variable, 0, len(res)) + vars := make([]sdk.EnvironmentVariable, 0, len(res)) if err := gorpmapping.GetAll(ctx, db, query, &res, opts...); err != nil { return nil, err @@ -35,7 +35,7 @@ func loadAllVariables(db gorp.SqlExecutor, query gorpmapping.Query, opts ...gorp } // LoadAllVariables Get all variable for the given environment -func LoadAllVariables(db gorp.SqlExecutor, envID int64) ([]sdk.Variable, error) { +func LoadAllVariables(db gorp.SqlExecutor, envID int64) ([]sdk.EnvironmentVariable, error) { query := gorpmapping.NewQuery(` SELECT * FROM environment_variable @@ -46,7 +46,7 @@ func LoadAllVariables(db gorp.SqlExecutor, envID int64) ([]sdk.Variable, error) } // LoadAllVariablesWithDecrytion Get all variable for the given environment, it also decrypt all the secure content -func LoadAllVariablesWithDecrytion(db gorp.SqlExecutor, envID int64) ([]sdk.Variable, error) { +func LoadAllVariablesWithDecrytion(db gorp.SqlExecutor, envID int64) ([]sdk.EnvironmentVariable, error) { query := gorpmapping.NewQuery(` SELECT * FROM environment_variable @@ -56,7 +56,7 @@ func LoadAllVariablesWithDecrytion(db gorp.SqlExecutor, envID int64) ([]sdk.Vari return loadAllVariables(db, query, gorpmapping.GetOptions.WithDecryption) } -func loadVariable(db gorp.SqlExecutor, q gorpmapping.Query, opts ...gorpmapping.GetOptionFunc) (*sdk.Variable, error) { +func loadVariable(db gorp.SqlExecutor, q gorpmapping.Query, opts ...gorpmapping.GetOptionFunc) (*sdk.EnvironmentVariable, error) { var v dbEnvironmentVariable found, err := gorpmapping.Get(context.Background(), db, q, &v, opts...) if err != nil { @@ -79,14 +79,14 @@ func loadVariable(db gorp.SqlExecutor, q gorpmapping.Query, opts ...gorpmapping. } // LoadVariable retrieve a specific variable -func LoadVariable(db gorp.SqlExecutor, envID int64, varName string) (*sdk.Variable, error) { +func LoadVariable(db gorp.SqlExecutor, envID int64, varName string) (*sdk.EnvironmentVariable, error) { query := gorpmapping.NewQuery(`SELECT * FROM environment_variable WHERE environment_id = $1 AND name=$2`).Args(envID, varName) return loadVariable(db, query) } // LoadVariableWithDecryption retrieve a specific variable with decrypted content -func LoadVariableWithDecryption(db gorp.SqlExecutor, envID int64, varID int64, varName string) (*sdk.Variable, error) { +func LoadVariableWithDecryption(db gorp.SqlExecutor, envID int64, varID int64, varName string) (*sdk.EnvironmentVariable, error) { query := gorpmapping.NewQuery(`SELECT * FROM environment_variable WHERE environment_id = $1 AND id = $2 AND name=$3`).Args(envID, varID, varName) return loadVariable(db, query, gorpmapping.GetOptions.WithDecryption) @@ -103,7 +103,7 @@ func DeleteAllVariables(db gorp.SqlExecutor, environmentID int64) error { } // InsertVariable Insert a new variable in the given environment -func InsertVariable(db gorp.SqlExecutor, envID int64, v *sdk.Variable, u sdk.Identifiable) error { +func InsertVariable(db gorp.SqlExecutor, envID int64, v *sdk.EnvironmentVariable, u sdk.Identifiable) error { //Check variable name rx := sdk.NamePatternRegex if !rx.MatchString(v.Name) { @@ -137,7 +137,7 @@ func InsertVariable(db gorp.SqlExecutor, envID int64, v *sdk.Variable, u sdk.Ide } // UpdateVariable Update a variable in the given environment -func UpdateVariable(db gorp.SqlExecutor, envID int64, variable *sdk.Variable, variableBefore *sdk.Variable, u sdk.Identifiable) error { +func UpdateVariable(db gorp.SqlExecutor, envID int64, variable *sdk.EnvironmentVariable, variableBefore *sdk.EnvironmentVariable, u sdk.Identifiable) error { rx := sdk.NamePatternRegex if !rx.MatchString(variable.Name) { return sdk.NewErrorFrom(sdk.ErrInvalidName, "variable name should match %s", sdk.NamePattern) @@ -173,7 +173,7 @@ func UpdateVariable(db gorp.SqlExecutor, envID int64, variable *sdk.Variable, va } // DeleteVariable Delete a variable from the given pipeline -func DeleteVariable(db gorp.SqlExecutor, envID int64, variable *sdk.Variable, u sdk.Identifiable) error { +func DeleteVariable(db gorp.SqlExecutor, envID int64, variable *sdk.EnvironmentVariable, u sdk.Identifiable) error { query := `DELETE FROM environment_variable WHERE environment_variable.environment_id = $1 AND environment_variable.name = $2` result, err := db.Exec(query, envID, variable.Name) diff --git a/engine/api/environment/dao_variable_test.go b/engine/api/environment/dao_variable_test.go index 61ee0683bd..27a6eafe85 100644 --- a/engine/api/environment/dao_variable_test.go +++ b/engine/api/environment/dao_variable_test.go @@ -26,8 +26,8 @@ func Test_DAOVariable(t *testing.T) { } require.NoError(t, environment.InsertEnvironment(db, &env)) - v1 := &sdk.Variable{Name: "clear", Type: sdk.TextVariable, Value: "clear_value"} - v2 := &sdk.Variable{Name: "secret", Type: sdk.SecretVariable, Value: "secret_value"} + v1 := &sdk.EnvironmentVariable{Name: "clear", Type: sdk.TextVariable, Value: "clear_value"} + v2 := &sdk.EnvironmentVariable{Name: "secret", Type: sdk.SecretVariable, Value: "secret_value"} require.NoError(t, environment.InsertVariable(db, env.ID, v1, u)) assert.Equal(t, "clear_value", v1.Value) diff --git a/engine/api/environment/environment_exporter.go b/engine/api/environment/environment_exporter.go index d0b9ebf231..2211754ab2 100644 --- a/engine/api/environment/environment_exporter.go +++ b/engine/api/environment/environment_exporter.go @@ -37,7 +37,7 @@ func Export(ctx context.Context, db gorp.SqlExecutor, key string, envName string // ExportEnvironment encrypt and export func ExportEnvironment(db gorp.SqlExecutor, env sdk.Environment, encryptFunc sdk.EncryptFunc) (exportentities.Environment, error) { - var envvars []sdk.Variable + var envvars []sdk.EnvironmentVariable for _, v := range env.Variables { switch v.Type { case sdk.KeyVariable: diff --git a/engine/api/environment/environment_importer.go b/engine/api/environment/environment_importer.go index 065e46f8b7..4bbb2acc8b 100644 --- a/engine/api/environment/environment_importer.go +++ b/engine/api/environment/environment_importer.go @@ -66,7 +66,7 @@ func Import(db gorp.SqlExecutor, proj sdk.Project, env *sdk.Environment, msgChan //ImportInto import variables and groups on an existing environment func ImportInto(db gorp.SqlExecutor, env *sdk.Environment, into *sdk.Environment, msgChan chan<- sdk.Message, u sdk.Identifiable) error { - var updateVar = func(v *sdk.Variable) { + var updateVar = func(v *sdk.EnvironmentVariable) { log.Debug("ImportInto> Updating var %s", v.Name) varBefore, errV := LoadVariable(db, into.ID, v.Name) @@ -82,7 +82,7 @@ func ImportInto(db gorp.SqlExecutor, env *sdk.Environment, into *sdk.Environment msgChan <- sdk.NewMessage(sdk.MsgEnvironmentVariableUpdated, v.Name, into.Name) } - var insertVar = func(v *sdk.Variable) { + var insertVar = func(v *sdk.EnvironmentVariable) { log.Debug("ImportInto> Creating var %s", v.Name) if err := InsertVariable(db, into.ID, v, u); err != nil { msgChan <- sdk.NewMessage(sdk.MsgEnvironmentVariableCannotBeCreated, v.Name, into.Name, err) diff --git a/engine/api/environment/environment_importer_test.go b/engine/api/environment/environment_importer_test.go index a54b5ccd34..ec695a5550 100644 --- a/engine/api/environment/environment_importer_test.go +++ b/engine/api/environment/environment_importer_test.go @@ -34,19 +34,19 @@ func TestImportInto_Variable(t *testing.T) { test.NoError(t, environment.InsertEnvironment(db, &env)) - v0 := sdk.Variable{ + v0 := sdk.EnvironmentVariable{ Name: "v0", Type: sdk.StringVariable, Value: "value0", } - v1 := sdk.Variable{ + v1 := sdk.EnvironmentVariable{ Name: "v1", Type: sdk.StringVariable, Value: "value1", } - v2 := sdk.Variable{ + v2 := sdk.EnvironmentVariable{ Name: "v2", Type: sdk.StringVariable, Value: "value2", @@ -63,7 +63,7 @@ func TestImportInto_Variable(t *testing.T) { env2 := sdk.Environment{ Name: "testenv2", ProjectID: proj.ID, - Variables: []sdk.Variable{ + Variables: []sdk.EnvironmentVariable{ { Name: "v1", Type: sdk.TextVariable, diff --git a/engine/api/environment/environment_parser.go b/engine/api/environment/environment_parser.go index 86a4150869..c679ce2b0a 100644 --- a/engine/api/environment/environment_parser.go +++ b/engine/api/environment/environment_parser.go @@ -68,7 +68,7 @@ func ParseAndImport(db gorp.SqlExecutor, proj sdk.Project, eenv exportentities.E v.Value = secret } - vv := sdk.Variable{Name: p, Type: v.Type, Value: v.Value} + vv := sdk.EnvironmentVariable{Name: p, Type: v.Type, Value: v.Value, EnvironmentID: env.ID} env.Variables = append(env.Variables, vv) } diff --git a/engine/api/environment/gorp_model.go b/engine/api/environment/gorp_model.go index 63108994f7..f1d66750c9 100644 --- a/engine/api/environment/gorp_model.go +++ b/engine/api/environment/gorp_model.go @@ -41,7 +41,7 @@ func (e dbEnvironmentVariable) Canonical() gorpmapping.CanonicalForms { } } -func newdbEnvironmentVariable(v sdk.Variable, projID int64) dbEnvironmentVariable { +func newdbEnvironmentVariable(v sdk.EnvironmentVariable, projID int64) dbEnvironmentVariable { if sdk.NeedPlaceholder(v.Type) { return dbEnvironmentVariable{ ID: v.ID, @@ -60,21 +60,23 @@ func newdbEnvironmentVariable(v sdk.Variable, projID int64) dbEnvironmentVariabl } } -func (e dbEnvironmentVariable) Variable() sdk.Variable { +func (e dbEnvironmentVariable) Variable() sdk.EnvironmentVariable { if sdk.NeedPlaceholder(e.Type) { - return sdk.Variable{ - ID: e.ID, - Name: e.Name, - Value: e.CipherValue, - Type: e.Type, + return sdk.EnvironmentVariable{ + ID: e.ID, + Name: e.Name, + Value: e.CipherValue, + Type: e.Type, + EnvironmentID: e.EnvironmentID, } } - return sdk.Variable{ - ID: e.ID, - Name: e.Name, - Value: e.ClearValue, - Type: e.Type, + return sdk.EnvironmentVariable{ + ID: e.ID, + Name: e.Name, + Value: e.ClearValue, + Type: e.Type, + EnvironmentID: e.EnvironmentID, } } @@ -93,7 +95,7 @@ func (eva *dbEnvironmentVariableAudit) PostGet(db gorp.SqlExecutor) error { } if before.Valid { - vBefore := &sdk.Variable{} + vBefore := &sdk.EnvironmentVariable{} if err := json.Unmarshal([]byte(before.String), vBefore); err != nil { return sdk.WithStack(err) } @@ -105,7 +107,7 @@ func (eva *dbEnvironmentVariableAudit) PostGet(db gorp.SqlExecutor) error { } if after.Valid { - vAfter := &sdk.Variable{} + vAfter := &sdk.EnvironmentVariable{} if err := json.Unmarshal([]byte(after.String), vAfter); err != nil { return sdk.WithStack(err) } diff --git a/engine/api/environment_export_test.go b/engine/api/environment_export_test.go index 41022e3501..f42b09bf46 100644 --- a/engine/api/environment_export_test.go +++ b/engine/api/environment_export_test.go @@ -26,14 +26,14 @@ func Test_getEnvironmentExportHandler(t *testing.T) { } test.NoError(t, environment.InsertEnvironment(db, env)) - v1 := &sdk.Variable{ + v1 := &sdk.EnvironmentVariable{ Name: "var1", Value: "value 1", Type: sdk.StringVariable, } test.NoError(t, environment.InsertVariable(db, env.ID, v1, u)) - v2 := &sdk.Variable{ + v2 := &sdk.EnvironmentVariable{ Name: "var2", Value: "value 2", Type: sdk.SecretVariable, diff --git a/engine/api/environment_import_test.go b/engine/api/environment_import_test.go index 5d32168bb6..060edf292d 100644 --- a/engine/api/environment_import_test.go +++ b/engine/api/environment_import_test.go @@ -114,7 +114,7 @@ func Test_postEnvironmentImportHandler_NewEnvFromYAMLWithKeysAndSecrets(t *testi t.Fatal(err) } - require.NoError(t, environment.InsertVariable(db, env.ID, &sdk.Variable{ + require.NoError(t, environment.InsertVariable(db, env.ID, &sdk.EnvironmentVariable{ Name: "myPassword", Type: sdk.SecretVariable, Value: "MySecretValue", @@ -231,7 +231,7 @@ func Test_postEnvironmentImportHandler_NewEnvFromYAMLWithKeysAndSecretsAndReImpo t.Fatal(err) } - require.NoError(t, environment.InsertVariable(db, env.ID, &sdk.Variable{ + require.NoError(t, environment.InsertVariable(db, env.ID, &sdk.EnvironmentVariable{ Name: "myPassword", Type: sdk.SecretVariable, Value: "MySecretValue", diff --git a/engine/api/environment_test.go b/engine/api/environment_test.go index f15d74ddc6..e6b7aa8a65 100644 --- a/engine/api/environment_test.go +++ b/engine/api/environment_test.go @@ -268,14 +268,14 @@ func Test_cloneEnvironmentHandler(t *testing.T) { } test.NoError(t, environment.InsertEnvironment(api.mustDB(), &env)) - v := &sdk.Variable{ + v := &sdk.EnvironmentVariable{ Name: "var1", Type: sdk.StringVariable, Value: "val1", } test.NoError(t, environment.InsertVariable(api.mustDB(), env.ID, v, u)) - v2 := &sdk.Variable{ + v2 := &sdk.EnvironmentVariable{ Name: "var2", Type: sdk.SecretVariable, Value: "val2", diff --git a/engine/api/environment_variable.go b/engine/api/environment_variable.go index 6c006e5985..be879bdb92 100644 --- a/engine/api/environment_variable.go +++ b/engine/api/environment_variable.go @@ -126,7 +126,7 @@ func (api *API) updateVariableInEnvironmentHandler() service.Handler { envName := vars["environmentName"] varName := vars["name"] - var newVar sdk.Variable + var newVar sdk.EnvironmentVariable if err := service.UnmarshalBody(r, &newVar); err != nil { return sdk.WithStack(sdk.ErrWrongRequest) } @@ -178,7 +178,7 @@ func (api *API) addVariableInEnvironmentHandler() service.Handler { envName := vars["environmentName"] varName := vars["name"] - var newVar sdk.Variable + var newVar sdk.EnvironmentVariable if err := service.UnmarshalBody(r, &newVar); err != nil { return sdk.WithStack(sdk.ErrWrongRequest) } diff --git a/engine/api/environment_variable_test.go b/engine/api/environment_variable_test.go index 967068d221..374ed5faca 100644 --- a/engine/api/environment_variable_test.go +++ b/engine/api/environment_variable_test.go @@ -98,7 +98,7 @@ func TestUpdateVariableInEnvironmentHandler(t *testing.T) { } //4. add a variable - v := sdk.Variable{ + v := sdk.EnvironmentVariable{ Name: "foo", Value: "bar", Type: sdk.StringVariable, @@ -167,7 +167,7 @@ func TestDeleteVariableFromEnvironmentHandler(t *testing.T) { } //4. add a variable - v := sdk.Variable{ + v := sdk.EnvironmentVariable{ Name: "foo", Value: "bar", Type: sdk.StringVariable, @@ -225,7 +225,7 @@ func TestGetVariablesInEnvironmentHandler(t *testing.T) { } //4. add a variable - v := sdk.Variable{ + v := sdk.EnvironmentVariable{ Name: "foo", Value: "bar", Type: sdk.StringVariable, @@ -279,7 +279,7 @@ func Test_getVariableAuditInEnvironmentHandler(t *testing.T) { } // Add variable - v := sdk.Variable{ + v := sdk.EnvironmentVariable{ Name: "foo", Type: "string", Value: "bar", diff --git a/engine/api/event/publish_application.go b/engine/api/event/publish_application.go index 54417e5e2a..4d6fa4bea8 100644 --- a/engine/api/event/publish_application.go +++ b/engine/api/event/publish_application.go @@ -56,7 +56,7 @@ func PublishDeleteApplication(ctx context.Context, projKey string, app sdk.Appli } // PublishAddVariableApplication publishes an event when adding a new variable -func PublishAddVariableApplication(ctx context.Context, projKey string, app sdk.Application, v sdk.Variable, u sdk.Identifiable) { +func PublishAddVariableApplication(ctx context.Context, projKey string, app sdk.Application, v sdk.ApplicationVariable, u sdk.Identifiable) { if sdk.NeedPlaceholder(v.Type) { v.Value = sdk.PasswordPlaceholder } @@ -67,7 +67,7 @@ func PublishAddVariableApplication(ctx context.Context, projKey string, app sdk. } // PublishUpdateVariableApplication publishes an event when updating a variable -func PublishUpdateVariableApplication(ctx context.Context, projKey string, app sdk.Application, v sdk.Variable, vOld sdk.Variable, u sdk.Identifiable) { +func PublishUpdateVariableApplication(ctx context.Context, projKey string, app sdk.Application, v sdk.ApplicationVariable, vOld sdk.ApplicationVariable, u sdk.Identifiable) { e := sdk.EventApplicationVariableUpdate{ OldVariable: vOld, NewVariable: v, @@ -76,7 +76,7 @@ func PublishUpdateVariableApplication(ctx context.Context, projKey string, app s } // PublishDeleteVariableApplication publishes an event when deleting a new variable -func PublishDeleteVariableApplication(ctx context.Context, projKey string, app sdk.Application, v sdk.Variable, u sdk.Identifiable) { +func PublishDeleteVariableApplication(ctx context.Context, projKey string, app sdk.Application, v sdk.ApplicationVariable, u sdk.Identifiable) { e := sdk.EventApplicationVariableDelete{ Variable: v, } diff --git a/engine/api/event/publish_environment.go b/engine/api/event/publish_environment.go index 43655939e3..f534489fad 100644 --- a/engine/api/event/publish_environment.go +++ b/engine/api/event/publish_environment.go @@ -52,7 +52,7 @@ func PublishEnvironmentDelete(ctx context.Context, projKey string, env sdk.Envir } // PublishEnvironmentVariableAdd publishes an event when adding a new variable -func PublishEnvironmentVariableAdd(ctx context.Context, projKey string, env sdk.Environment, v sdk.Variable, u sdk.Identifiable) { +func PublishEnvironmentVariableAdd(ctx context.Context, projKey string, env sdk.Environment, v sdk.EnvironmentVariable, u sdk.Identifiable) { if sdk.NeedPlaceholder(v.Type) { v.Value = sdk.PasswordPlaceholder } @@ -63,7 +63,7 @@ func PublishEnvironmentVariableAdd(ctx context.Context, projKey string, env sdk. } // PublishEnvironmentVariableUpdate publishes an event when updating a variable -func PublishEnvironmentVariableUpdate(ctx context.Context, projKey string, env sdk.Environment, v sdk.Variable, vOld sdk.Variable, u sdk.Identifiable) { +func PublishEnvironmentVariableUpdate(ctx context.Context, projKey string, env sdk.Environment, v sdk.EnvironmentVariable, vOld sdk.EnvironmentVariable, u sdk.Identifiable) { e := sdk.EventEnvironmentVariableUpdate{ OldVariable: vOld, NewVariable: v, @@ -72,7 +72,7 @@ func PublishEnvironmentVariableUpdate(ctx context.Context, projKey string, env s } // PublishEnvironmentVariableDelete publishes an event when deleting a new variable -func PublishEnvironmentVariableDelete(ctx context.Context, projKey string, env sdk.Environment, v sdk.Variable, u sdk.Identifiable) { +func PublishEnvironmentVariableDelete(ctx context.Context, projKey string, env sdk.Environment, v sdk.EnvironmentVariable, u sdk.Identifiable) { e := sdk.EventEnvironmentVariableDelete{ Variable: v, } diff --git a/engine/api/event/publish_project.go b/engine/api/event/publish_project.go index 0abbef2af0..a0a9c1c61a 100644 --- a/engine/api/event/publish_project.go +++ b/engine/api/event/publish_project.go @@ -56,7 +56,7 @@ func PublishDeleteProject(ctx context.Context, p *sdk.Project, u sdk.Identifiabl } // PublishAddProjectVariable publishes an event for the creation of the given variable -func PublishAddProjectVariable(ctx context.Context, p *sdk.Project, v sdk.Variable, u sdk.Identifiable) { +func PublishAddProjectVariable(ctx context.Context, p *sdk.Project, v sdk.ProjectVariable, u sdk.Identifiable) { if sdk.NeedPlaceholder(v.Type) { v.Value = sdk.PasswordPlaceholder } @@ -67,7 +67,7 @@ func PublishAddProjectVariable(ctx context.Context, p *sdk.Project, v sdk.Variab } // PublishUpdateProjectVariable publishes an event for the modification of a variable -func PublishUpdateProjectVariable(ctx context.Context, p *sdk.Project, newVar sdk.Variable, oldVar sdk.Variable, u sdk.Identifiable) { +func PublishUpdateProjectVariable(ctx context.Context, p *sdk.Project, newVar sdk.ProjectVariable, oldVar sdk.ProjectVariable, u sdk.Identifiable) { if sdk.NeedPlaceholder(newVar.Type) { newVar.Value = sdk.PasswordPlaceholder } @@ -82,7 +82,7 @@ func PublishUpdateProjectVariable(ctx context.Context, p *sdk.Project, newVar sd } // PublishDeleteProjectVariable publishes an event on project variable deletion -func PublishDeleteProjectVariable(ctx context.Context, p *sdk.Project, v sdk.Variable, u sdk.Identifiable) { +func PublishDeleteProjectVariable(ctx context.Context, p *sdk.Project, v sdk.ProjectVariable, u sdk.Identifiable) { if sdk.NeedPlaceholder(v.Type) { v.Value = sdk.PasswordPlaceholder } diff --git a/engine/api/project/dao_variable.go b/engine/api/project/dao_variable.go index 78a4a2a735..c1ae3b3550 100644 --- a/engine/api/project/dao_variable.go +++ b/engine/api/project/dao_variable.go @@ -12,10 +12,10 @@ import ( "github.com/ovh/cds/sdk/log" ) -func loadAllVariables(db gorp.SqlExecutor, query gorpmapping.Query, opts ...gorpmapping.GetOptionFunc) ([]sdk.Variable, error) { +func loadAllVariables(db gorp.SqlExecutor, query gorpmapping.Query, opts ...gorpmapping.GetOptionFunc) ([]sdk.ProjectVariable, error) { var ctx = context.Background() var res []dbProjectVariable - vars := make([]sdk.Variable, 0, len(res)) + vars := make([]sdk.ProjectVariable, 0, len(res)) if err := gorpmapping.GetAll(ctx, db, query, &res, opts...); err != nil { return nil, err @@ -36,7 +36,7 @@ func loadAllVariables(db gorp.SqlExecutor, query gorpmapping.Query, opts ...gorp } // LoadAllVariables Get all variable for the given project -func LoadAllVariables(db gorp.SqlExecutor, projID int64) ([]sdk.Variable, error) { +func LoadAllVariables(db gorp.SqlExecutor, projID int64) ([]sdk.ProjectVariable, error) { query := gorpmapping.NewQuery(` SELECT * FROM project_variable @@ -47,7 +47,7 @@ func LoadAllVariables(db gorp.SqlExecutor, projID int64) ([]sdk.Variable, error) } // LoadAllVariablesWithDecrytion Get all variable for the given project, it also decrypt all the secure content -func LoadAllVariablesWithDecrytion(db gorp.SqlExecutor, projID int64) ([]sdk.Variable, error) { +func LoadAllVariablesWithDecrytion(db gorp.SqlExecutor, projID int64) ([]sdk.ProjectVariable, error) { query := gorpmapping.NewQuery(` SELECT * FROM project_variable @@ -57,7 +57,7 @@ func LoadAllVariablesWithDecrytion(db gorp.SqlExecutor, projID int64) ([]sdk.Var return loadAllVariables(db, query, gorpmapping.GetOptions.WithDecryption) } -func loadVariable(db gorp.SqlExecutor, q gorpmapping.Query, opts ...gorpmapping.GetOptionFunc) (*sdk.Variable, error) { +func loadVariable(db gorp.SqlExecutor, q gorpmapping.Query, opts ...gorpmapping.GetOptionFunc) (*sdk.ProjectVariable, error) { var v dbProjectVariable found, err := gorpmapping.Get(context.Background(), db, q, &v, opts...) if err != nil { @@ -80,14 +80,14 @@ func loadVariable(db gorp.SqlExecutor, q gorpmapping.Query, opts ...gorpmapping. } // LoadVariable retrieve a specific variable -func LoadVariable(db gorp.SqlExecutor, projID int64, varName string) (*sdk.Variable, error) { +func LoadVariable(db gorp.SqlExecutor, projID int64, varName string) (*sdk.ProjectVariable, error) { query := gorpmapping.NewQuery(`SELECT * FROM project_variable WHERE project_id = $1 AND var_name=$2`).Args(projID, varName) return loadVariable(db, query) } // LoadVariableWithDecryption retrieve a specific variable with decrypted content -func LoadVariableWithDecryption(db gorp.SqlExecutor, projID int64, varID int64, varName string) (*sdk.Variable, error) { +func LoadVariableWithDecryption(db gorp.SqlExecutor, projID int64, varID int64, varName string) (*sdk.ProjectVariable, error) { query := gorpmapping.NewQuery(`SELECT * FROM project_variable WHERE project_id = $1 AND id = $2 AND var_name=$3`).Args(projID, varID, varName) return loadVariable(db, query, gorpmapping.GetOptions.WithDecryption) @@ -104,7 +104,7 @@ func DeleteAllVariables(db gorp.SqlExecutor, projectID int64) error { } // InsertVariable Insert a new variable in the given project -func InsertVariable(db gorp.SqlExecutor, projID int64, v *sdk.Variable, u sdk.Identifiable) error { +func InsertVariable(db gorp.SqlExecutor, projID int64, v *sdk.ProjectVariable, u sdk.Identifiable) error { //Check variable name rx := sdk.NamePatternRegex if !rx.MatchString(v.Name) { @@ -138,7 +138,7 @@ func InsertVariable(db gorp.SqlExecutor, projID int64, v *sdk.Variable, u sdk.Id } // UpdateVariable Update a variable in the given project -func UpdateVariable(db gorp.SqlExecutor, projID int64, variable *sdk.Variable, variableBefore *sdk.Variable, u sdk.Identifiable) error { +func UpdateVariable(db gorp.SqlExecutor, projID int64, variable *sdk.ProjectVariable, variableBefore *sdk.ProjectVariable, u sdk.Identifiable) error { rx := sdk.NamePatternRegex if !rx.MatchString(variable.Name) { return sdk.NewError(sdk.ErrInvalidName, fmt.Errorf("Invalid variable name. It should match %s", sdk.NamePattern)) @@ -174,7 +174,7 @@ func UpdateVariable(db gorp.SqlExecutor, projID int64, variable *sdk.Variable, v } // DeleteVariable Delete a variable from the given pipeline -func DeleteVariable(db gorp.SqlExecutor, projID int64, variable *sdk.Variable, u sdk.Identifiable) error { +func DeleteVariable(db gorp.SqlExecutor, projID int64, variable *sdk.ProjectVariable, u sdk.Identifiable) error { query := `DELETE FROM project_variable WHERE project_variable.project_id = $1 AND project_variable.var_name = $2` result, err := db.Exec(query, projID, variable.Name) diff --git a/engine/api/project/dao_variable_test.go b/engine/api/project/dao_variable_test.go index 1d582d1799..a7c6f18e02 100644 --- a/engine/api/project/dao_variable_test.go +++ b/engine/api/project/dao_variable_test.go @@ -20,8 +20,8 @@ func Test_DAOVariable(t *testing.T) { u, _ := assets.InsertLambdaUser(t, db, &proj.ProjectGroups[0].Group) - v1 := &sdk.Variable{Name: "clear", Type: sdk.TextVariable, Value: "clear_value"} - v2 := &sdk.Variable{Name: "secret", Type: sdk.SecretVariable, Value: "secret_value"} + v1 := &sdk.ProjectVariable{Name: "clear", Type: sdk.TextVariable, Value: "clear_value"} + v2 := &sdk.ProjectVariable{Name: "secret", Type: sdk.SecretVariable, Value: "secret_value"} require.NoError(t, project.InsertVariable(db, proj.ID, v1, u)) assert.Equal(t, "clear_value", v1.Value) diff --git a/engine/api/project/gorp_model.go b/engine/api/project/gorp_model.go index 8e1c7b174e..5fbb3938ad 100644 --- a/engine/api/project/gorp_model.go +++ b/engine/api/project/gorp_model.go @@ -43,7 +43,7 @@ func (e dbProjectVariable) Canonical() gorpmapping.CanonicalForms { } } -func newDBProjectVariable(v sdk.Variable, projID int64) dbProjectVariable { +func newDBProjectVariable(v sdk.ProjectVariable, projID int64) dbProjectVariable { if sdk.NeedPlaceholder(v.Type) { return dbProjectVariable{ ID: v.ID, @@ -62,21 +62,23 @@ func newDBProjectVariable(v sdk.Variable, projID int64) dbProjectVariable { } } -func (e dbProjectVariable) Variable() sdk.Variable { +func (e dbProjectVariable) Variable() sdk.ProjectVariable { if sdk.NeedPlaceholder(e.Type) { - return sdk.Variable{ - ID: e.ID, - Name: e.Name, - Value: e.CipherValue, - Type: e.Type, + return sdk.ProjectVariable{ + ID: e.ID, + Name: e.Name, + Value: e.CipherValue, + Type: e.Type, + ProjectID: e.ProjectID, } } - return sdk.Variable{ - ID: e.ID, - Name: e.Name, - Value: e.ClearValue, - Type: e.Type, + return sdk.ProjectVariable{ + ID: e.ID, + Name: e.Name, + Value: e.ClearValue, + Type: e.Type, + ProjectID: e.ProjectID, } } @@ -97,7 +99,7 @@ func (pva *dbProjectVariableAudit) PostGet(db gorp.SqlExecutor) error { } if before.Valid { - vBefore := &sdk.Variable{} + vBefore := &sdk.ProjectVariable{} if err := json.Unmarshal([]byte(before.String), vBefore); err != nil { return err } @@ -109,7 +111,7 @@ func (pva *dbProjectVariableAudit) PostGet(db gorp.SqlExecutor) error { } if after.Valid { - vAfter := &sdk.Variable{} + vAfter := &sdk.ProjectVariable{} if err := json.Unmarshal([]byte(after.String), vAfter); err != nil { return err } diff --git a/engine/api/project_test.go b/engine/api/project_test.go index fcce495f4c..047c440f3f 100644 --- a/engine/api/project_test.go +++ b/engine/api/project_test.go @@ -33,7 +33,7 @@ func TestVariableInProject(t *testing.T) { project1 := assets.InsertTestProject(t, db, api.Cache, sdk.RandomString(10), sdk.RandomString(10)) // 2. Insert new variable - var1 := &sdk.Variable{ + var1 := &sdk.ProjectVariable{ Name: "var1", Value: "value1", Type: "PASSWORD", @@ -62,7 +62,7 @@ func TestVariableInProject(t *testing.T) { } // 5. Insert new var - var3 := &sdk.Variable{ + var3 := &sdk.ProjectVariable{ Name: "var2", Value: "value2", Type: "STRING", diff --git a/engine/api/project_variable.go b/engine/api/project_variable.go index 4cabdf95fa..689d5b15a1 100644 --- a/engine/api/project_variable.go +++ b/engine/api/project_variable.go @@ -22,7 +22,8 @@ func (api *API) postEncryptVariableHandler() service.Handler { return sdk.WrapError(errp, "unable to load project") } - variable := new(sdk.Variable) + variable := new(sdk.ProjectVariable) + variable.ProjectID = p.ID if err := service.UnmarshalBody(r, variable); err != nil { return sdk.WrapError(err, "unable to read body") } @@ -111,7 +112,7 @@ func (api *API) updateVariableInProjectHandler() service.Handler { key := vars[permProjectKey] varName := vars["name"] - var newVar sdk.Variable + var newVar sdk.ProjectVariable if err := service.UnmarshalBody(r, &newVar); err != nil { return err } @@ -153,7 +154,7 @@ func (api *API) addVariableInProjectHandler() service.Handler { key := vars[permProjectKey] varName := vars["name"] - var newVar sdk.Variable + var newVar sdk.ProjectVariable if err := service.UnmarshalBody(r, &newVar); err != nil { return err } diff --git a/engine/api/project_variable_test.go b/engine/api/project_variable_test.go index dc8339267a..c889c5534a 100644 --- a/engine/api/project_variable_test.go +++ b/engine/api/project_variable_test.go @@ -25,7 +25,7 @@ func Test_getVariableAuditInProjectHandler(t *testing.T) { proj := assets.InsertTestProject(t, db, api.Cache, pkey, pkey) // Add variable - v := sdk.Variable{ + v := sdk.ProjectVariable{ Name: "foo", Type: "string", Value: "bar", diff --git a/engine/api/workflow/execute_node_job_run.go b/engine/api/workflow/execute_node_job_run.go index a6e4fecd97..f6519054cb 100644 --- a/engine/api/workflow/execute_node_job_run.go +++ b/engine/api/workflow/execute_node_job_run.go @@ -411,10 +411,10 @@ func LoadNodeJobRunKeys(ctx context.Context, db gorp.SqlExecutor, proj *sdk.Proj } // LoadSecrets loads all secrets for a job run -func LoadSecrets(db gorp.SqlExecutor, store cache.Store, nodeRun *sdk.WorkflowNodeRun, w *sdk.WorkflowRun, pv []sdk.Variable) ([]sdk.Variable, error) { +func LoadSecrets(db gorp.SqlExecutor, store cache.Store, nodeRun *sdk.WorkflowNodeRun, w *sdk.WorkflowRun, projV []sdk.ProjectVariable) ([]sdk.Variable, error) { var secrets []sdk.Variable - pv = sdk.VariablesFilter(pv, sdk.SecretVariable, sdk.KeyVariable) + pv := sdk.VariablesFilter(sdk.FromProjectVariables(projV), sdk.SecretVariable, sdk.KeyVariable) pv = sdk.VariablesPrefix(pv, "cds.proj.") secrets = append(secrets, pv...) @@ -457,7 +457,7 @@ func LoadSecrets(db gorp.SqlExecutor, store cache.Store, nodeRun *sdk.WorkflowNo if err != nil { return nil, sdk.WrapError(err, "LoadSecrets> Cannot load application variables") } - av = sdk.VariablesFilter(appVariables, sdk.SecretVariable) + av = sdk.VariablesFilter(sdk.FromAplicationVariables(appVariables), sdk.SecretVariable) av = sdk.VariablesPrefix(av, "cds.app.") av = append(av, sdk.Variable{ @@ -475,7 +475,7 @@ func LoadSecrets(db gorp.SqlExecutor, store cache.Store, nodeRun *sdk.WorkflowNo if errE != nil { return nil, sdk.WrapError(errE, "LoadSecrets> Cannot load environment variables") } - ev = sdk.VariablesFilter(envv, sdk.SecretVariable, sdk.KeyVariable) + ev = sdk.VariablesFilter(sdk.FromEnvironmentVariables(envv), sdk.SecretVariable, sdk.KeyVariable) ev = sdk.VariablesPrefix(ev, "cds.env.") } secrets = append(secrets, ev...) diff --git a/engine/api/workflow/run_workflow_test.go b/engine/api/workflow/run_workflow_test.go index 5b4c4a60a8..43b9297468 100644 --- a/engine/api/workflow/run_workflow_test.go +++ b/engine/api/workflow/run_workflow_test.go @@ -317,7 +317,7 @@ func TestManualRun3(t *testing.T) { proj := assets.InsertTestProject(t, db, cache, key, key) // Add variable - v := sdk.Variable{ + v := sdk.ProjectVariable{ Name: "foo", Type: sdk.SecretVariable, Value: "bar", diff --git a/engine/api/workflow_import_test.go b/engine/api/workflow_import_test.go index bef23f5189..96f96e16fe 100644 --- a/engine/api/workflow_import_test.go +++ b/engine/api/workflow_import_test.go @@ -666,7 +666,7 @@ func Test_getWorkflowPushHandler(t *testing.T) { t.Fatal(err) } - v1 := sdk.Variable{ + v1 := sdk.ApplicationVariable{ Name: "var1", Value: "value 1", Type: sdk.StringVariable, @@ -674,7 +674,7 @@ func Test_getWorkflowPushHandler(t *testing.T) { test.NoError(t, application.InsertVariable(api.mustDB(), app.ID, &v1, u)) - v2 := sdk.Variable{ + v2 := sdk.ApplicationVariable{ Name: "var2", Value: "value 2", Type: sdk.SecretVariable, diff --git a/sdk/application.go b/sdk/application.go index 094d5a977b..182e969a44 100644 --- a/sdk/application.go +++ b/sdk/application.go @@ -20,7 +20,7 @@ type Application struct { Icon string `json:"icon" db:"icon"` ProjectID int64 `json:"-" db:"project_id"` ProjectKey string `json:"project_key" db:"-" cli:"project_key"` - Variables []Variable `json:"variables,omitempty" db:"-"` + Variables []ApplicationVariable `json:"variables,omitempty" db:"-"` Notifications []UserNotification `json:"notifications,omitempty" db:"-"` LastModified time.Time `json:"last_modified" db:"last_modified" mapstructure:"-"` VCSServer string `json:"vcs_server,omitempty" db:"vcs_server"` @@ -88,14 +88,14 @@ type RepositoryStrategy struct { // ApplicationVariableAudit represents an audit on an application variable type ApplicationVariableAudit struct { - ID int64 `json:"id" yaml:"-" db:"id"` - ApplicationID int64 `json:"application_id" yaml:"-" db:"application_id"` - VariableID int64 `json:"variable_id" yaml:"-" db:"variable_id"` - Type string `json:"type" yaml:"-" db:"type"` - VariableBefore *Variable `json:"variable_before,omitempty" yaml:"-" db:"-"` - VariableAfter Variable `json:"variable_after,omitempty" yaml:"-" db:"-"` - Versionned time.Time `json:"versionned" yaml:"-" db:"versionned"` - Author string `json:"author" yaml:"-" db:"author"` + ID int64 `json:"id" yaml:"-" db:"id"` + ApplicationID int64 `json:"application_id" yaml:"-" db:"application_id"` + VariableID int64 `json:"variable_id" yaml:"-" db:"variable_id"` + Type string `json:"type" yaml:"-" db:"type"` + VariableBefore *ApplicationVariable `json:"variable_before,omitempty" yaml:"-" db:"-"` + VariableAfter ApplicationVariable `json:"variable_after,omitempty" yaml:"-" db:"-"` + Versionned time.Time `json:"versionned" yaml:"-" db:"versionned"` + Author string `json:"author" yaml:"-" db:"author"` } // GetKey return a key by name diff --git a/sdk/environment.go b/sdk/environment.go index 86e97c1c44..ca8779ed13 100644 --- a/sdk/environment.go +++ b/sdk/environment.go @@ -7,29 +7,29 @@ import ( // Environment represent a deployment environment type Environment struct { - ID int64 `json:"id" yaml:"-"` - Name string `json:"name" yaml:"name" cli:"name,key"` - Variables []Variable `json:"variables,omitempty" yaml:"variables"` - ProjectID int64 `json:"-" yaml:"-"` - ProjectKey string `json:"project_key" yaml:"-"` - Created time.Time `json:"created"` - LastModified time.Time `json:"last_modified"` - Keys []EnvironmentKey `json:"keys"` - Usage *Usage `json:"usage,omitempty"` - FromRepository string `json:"from_repository,omitempty"` + ID int64 `json:"id" yaml:"-"` + Name string `json:"name" yaml:"name" cli:"name,key"` + Variables []EnvironmentVariable `json:"variables,omitempty" yaml:"variables"` + ProjectID int64 `json:"-" yaml:"-"` + ProjectKey string `json:"project_key" yaml:"-"` + Created time.Time `json:"created"` + LastModified time.Time `json:"last_modified"` + Keys []EnvironmentKey `json:"keys"` + Usage *Usage `json:"usage,omitempty"` + FromRepository string `json:"from_repository,omitempty"` } // UnmarshalJSON custom for last modified. func (e *Environment) UnmarshalJSON(data []byte) error { var tmp struct { - ID int64 `json:"id"` - Name string `json:"name"` - Variables []Variable `json:"variables"` - ProjectKey string `json:"project_key"` - Created time.Time `json:"created"` - Keys []EnvironmentKey `json:"keys"` - Usage *Usage `json:"usage"` - FromRepository string `json:"from_repository"` + ID int64 `json:"id"` + Name string `json:"name"` + Variables []EnvironmentVariable `json:"variables"` + ProjectKey string `json:"project_key"` + Created time.Time `json:"created"` + Keys []EnvironmentKey `json:"keys"` + Usage *Usage `json:"usage"` + FromRepository string `json:"from_repository"` } if err := json.Unmarshal(data, &tmp); err != nil { @@ -61,14 +61,14 @@ func (e *Environment) UnmarshalJSON(data []byte) error { // EnvironmentVariableAudit represents an audit on an environment variable type EnvironmentVariableAudit struct { - ID int64 `json:"id" yaml:"-" db:"id"` - EnvironmentID int64 `json:"environment_id" yaml:"-" db:"environment_id"` - VariableID int64 `json:"variable_id" yaml:"-" db:"variable_id"` - Type string `json:"type" yaml:"-" db:"type"` - VariableBefore *Variable `json:"variable_before,omitempty" yaml:"-" db:"-"` - VariableAfter Variable `json:"variable_after,omitempty" yaml:"-" db:"-"` - Versionned time.Time `json:"versionned" yaml:"-" db:"versionned"` - Author string `json:"author" yaml:"-" db:"author"` + ID int64 `json:"id" yaml:"-" db:"id"` + EnvironmentID int64 `json:"environment_id" yaml:"-" db:"environment_id"` + VariableID int64 `json:"variable_id" yaml:"-" db:"variable_id"` + Type string `json:"type" yaml:"-" db:"type"` + VariableBefore *EnvironmentVariable `json:"variable_before,omitempty" yaml:"-" db:"-"` + VariableAfter EnvironmentVariable `json:"variable_after,omitempty" yaml:"-" db:"-"` + Versionned time.Time `json:"versionned" yaml:"-" db:"versionned"` + Author string `json:"author" yaml:"-" db:"author"` } // GetKey return a key by name diff --git a/sdk/event_application.go b/sdk/event_application.go index 216536b780..bcd6dcaf28 100644 --- a/sdk/event_application.go +++ b/sdk/event_application.go @@ -21,18 +21,18 @@ type EventApplicationDelete struct { // EventApplicationVariableAdd represents the event when adding an application variable type EventApplicationVariableAdd struct { - Variable Variable `json:"variable"` + Variable ApplicationVariable `json:"variable"` } // EventApplicationVariableUpdate represents the event when updating an application variable type EventApplicationVariableUpdate struct { - OldVariable Variable `json:"old_variable"` - NewVariable Variable `json:"new_variable"` + OldVariable ApplicationVariable `json:"old_variable"` + NewVariable ApplicationVariable `json:"new_variable"` } // EventApplicationVariableDelete represents the event when deleting an application variable type EventApplicationVariableDelete struct { - Variable Variable `json:"variable"` + Variable ApplicationVariable `json:"variable"` } // EventApplicationPermissionAdd represents the event when adding an application permission diff --git a/sdk/event_environment.go b/sdk/event_environment.go index 191e796736..2e5de3563a 100644 --- a/sdk/event_environment.go +++ b/sdk/event_environment.go @@ -17,18 +17,18 @@ type EventEnvironmentDelete struct { // EventEnvironmentVariableAdd represents the event when adding an environment variable type EventEnvironmentVariableAdd struct { - Variable Variable `json:"variable"` + Variable EnvironmentVariable `json:"variable"` } // EventEnvironmentVariableUpdate represents the event when updating an environment variable type EventEnvironmentVariableUpdate struct { - OldVariable Variable `json:"old_variable"` - NewVariable Variable `json:"new_variable"` + OldVariable EnvironmentVariable `json:"old_variable"` + NewVariable EnvironmentVariable `json:"new_variable"` } // EventEnvironmentVariableDelete represents the event when deleting an environment variable type EventEnvironmentVariableDelete struct { - Variable Variable `json:"variable"` + Variable EnvironmentVariable `json:"variable"` } // EventEnvironmentPermissionAdd represents the event when adding an environment permission diff --git a/sdk/event_project.go b/sdk/event_project.go index 60c3c7ed9f..cc9372394d 100644 --- a/sdk/event_project.go +++ b/sdk/event_project.go @@ -2,7 +2,7 @@ package sdk // EventProjectAdd represents the event when adding a project type EventProjectAdd struct { - Variables []Variable `json:"variables"` + Variables []ProjectVariable `json:"variables"` Permissions []GroupPermission `json:"groups_permission"` Keys []ProjectKey `json:"keys"` Metadata Metadata `json:"metadata"` @@ -22,18 +22,18 @@ type EventProjectDelete struct { // EventProjectVariableAdd represents the event when adding a project variable type EventProjectVariableAdd struct { - Variable Variable `json:"variable"` + Variable ProjectVariable `json:"variable"` } // EventProjectVariableUpdate represents the event when updating a project variable type EventProjectVariableUpdate struct { - OldVariable Variable `json:"old_variable"` - NewVariable Variable `json:"new_variable"` + OldVariable ProjectVariable `json:"old_variable"` + NewVariable ProjectVariable `json:"new_variable"` } // EventProjectVariableDelete represents the event when deleting a project variable type EventProjectVariableDelete struct { - Variable Variable `json:"variable"` + Variable ProjectVariable `json:"variable"` } // EventProjectPermissionAdd represents the event when adding a project permission diff --git a/sdk/exportentities/environment.go b/sdk/exportentities/environment.go index c28472d491..e31f82cd0d 100644 --- a/sdk/exportentities/environment.go +++ b/sdk/exportentities/environment.go @@ -37,13 +37,13 @@ func NewEnvironment(e sdk.Environment, keys []EncryptedKey) Environment { func (e *Environment) Environment() (env *sdk.Environment) { env = new(sdk.Environment) env.Name = e.Name - env.Variables = make([]sdk.Variable, len(e.Values)) + env.Variables = make([]sdk.EnvironmentVariable, len(e.Values)) var i int for k, v := range e.Values { if v.Type == "" { v.Type = sdk.StringVariable } - env.Variables[i] = sdk.Variable{ + env.Variables[i] = sdk.EnvironmentVariable{ Name: k, Type: v.Type, Value: v.Value, diff --git a/sdk/parameter.go b/sdk/parameter.go index 4b67434538..c646406623 100644 --- a/sdk/parameter.go +++ b/sdk/parameter.go @@ -157,19 +157,19 @@ func ParametersToMap(params []Parameter) map[string]string { // ParametersFromProjectVariables returns a map from a slice of parameters func ParametersFromProjectVariables(proj Project) map[string]string { - params := VariablesToParameters("cds.proj", proj.Variables) + params := ProjectVariablesToParameters("cds.proj", proj.Variables) return ParametersToMap(params) } // ParametersFromApplicationVariables returns a map from a slice of parameters func ParametersFromApplicationVariables(app Application) map[string]string { - params := VariablesToParameters("cds.app", app.Variables) + params := ApplicationVariablesToParameters("cds.app", app.Variables) return ParametersToMap(params) } // ParametersFromEnvironmentVariables returns a map from a slice of parameters func ParametersFromEnvironmentVariables(env Environment) map[string]string { - params := VariablesToParameters("cds.env", env.Variables) + params := EnvironmentVariablesToParameters("cds.env", env.Variables) return ParametersToMap(params) } @@ -195,6 +195,48 @@ func ParametersFromIntegration(ppf IntegrationConfig) map[string]string { return ParametersToMap(params) } +func EnvironmentVariablesToParameters(prefix string, variables []EnvironmentVariable) []Parameter { + res := make([]Parameter, 0, len(variables)) + for _, t := range variables { + if NeedPlaceholder(t.Type) { + continue + } + if prefix != "" { + t.Name = prefix + "." + t.Name + } + res = append(res, Parameter{Name: t.Name, Type: t.Type, Value: t.Value}) + } + return res +} + +func ApplicationVariablesToParameters(prefix string, variables []ApplicationVariable) []Parameter { + res := make([]Parameter, 0, len(variables)) + for _, t := range variables { + if NeedPlaceholder(t.Type) { + continue + } + if prefix != "" { + t.Name = prefix + "." + t.Name + } + res = append(res, Parameter{Name: t.Name, Type: t.Type, Value: t.Value}) + } + return res +} + +func ProjectVariablesToParameters(prefix string, variables []ProjectVariable) []Parameter { + res := make([]Parameter, 0, len(variables)) + for _, t := range variables { + if NeedPlaceholder(t.Type) { + continue + } + if prefix != "" { + t.Name = prefix + "." + t.Name + } + res = append(res, Parameter{Name: t.Name, Type: t.Type, Value: t.Value}) + } + return res +} + func VariablesToParameters(prefix string, variables []Variable) []Parameter { res := make([]Parameter, 0, len(variables)) for _, t := range variables { diff --git a/sdk/project.go b/sdk/project.go index 60d9072fd5..7b1e1ea97d 100644 --- a/sdk/project.go +++ b/sdk/project.go @@ -37,7 +37,7 @@ type Project struct { Applications []Application `json:"applications,omitempty" yaml:"applications,omitempty" db:"-" cli:"-"` ApplicationNames IDNames `json:"application_names,omitempty" yaml:"application_names,omitempty" db:"-" cli:"-"` ProjectGroups []GroupPermission `json:"groups,omitempty" yaml:"permissions,omitempty" db:"-" cli:"-"` - Variables []Variable `json:"variables,omitempty" yaml:"variables,omitempty" db:"-" cli:"-"` + Variables []ProjectVariable `json:"variables,omitempty" yaml:"variables,omitempty" db:"-" cli:"-"` Environments []Environment `json:"environments,omitempty" yaml:"environments,omitempty" db:"-" cli:"-"` EnvironmentNames IDNames `json:"environment_names,omitempty" yaml:"environment_names,omitempty" db:"-" cli:"-"` Labels []Label `json:"labels,omitempty" yaml:"labels,omitempty" db:"-" cli:"-"` @@ -251,14 +251,14 @@ func (vcs ProjectVCSServer) Hash() uint64 { // ProjectVariableAudit represents an audit on a project variable type ProjectVariableAudit struct { - ID int64 `json:"id" yaml:"-" db:"id"` - ProjectID int64 `json:"project_id" yaml:"-" db:"project_id"` - VariableID int64 `json:"variable_id" yaml:"-" db:"variable_id"` - Type string `json:"type" yaml:"-" db:"type"` - VariableBefore *Variable `json:"variable_before,omitempty" yaml:"-" db:"-"` - VariableAfter Variable `json:"variable_after,omitempty" yaml:"-" db:"-"` - Versionned time.Time `json:"versionned" yaml:"-" db:"versionned"` - Author string `json:"author" yaml:"-" db:"author"` + ID int64 `json:"id" yaml:"-" db:"id"` + ProjectID int64 `json:"project_id" yaml:"-" db:"project_id"` + VariableID int64 `json:"variable_id" yaml:"-" db:"variable_id"` + Type string `json:"type" yaml:"-" db:"type"` + VariableBefore *ProjectVariable `json:"variable_before,omitempty" yaml:"-" db:"-"` + VariableAfter ProjectVariable `json:"variable_after,omitempty" yaml:"-" db:"-"` + Versionned time.Time `json:"versionned" yaml:"-" db:"versionned"` + Author string `json:"author" yaml:"-" db:"author"` } // Metadata represents metadata diff --git a/sdk/variable.go b/sdk/variable.go index f4c52e317f..68412cbed4 100644 --- a/sdk/variable.go +++ b/sdk/variable.go @@ -14,6 +14,79 @@ type Variable struct { Type string `json:"type" cli:"type"` } +func FromProjectVariables(appVars []ProjectVariable) []Variable { + vars := make([]Variable, len(appVars)) + for i, a := range appVars { + vars[i] = Variable{ + Value: a.Value, + Name: a.Name, + Type: a.Type, + ID: a.ID, + } + } + return vars +} + +func FromAplicationVariables(appVars []ApplicationVariable) []Variable { + vars := make([]Variable, len(appVars)) + for i, a := range appVars { + vars[i] = Variable{ + Value: a.Value, + Name: a.Name, + Type: a.Type, + ID: a.ID, + } + } + return vars +} + +func FromEnvironmentVariables(envVars []EnvironmentVariable) []Variable { + vars := make([]Variable, len(envVars)) + for i, a := range envVars { + vars[i] = Variable{ + Value: a.Value, + Name: a.Name, + Type: a.Type, + ID: a.ID, + } + } + return vars +} + +func (v *Variable) ToApplicationVariable(appID int64) *ApplicationVariable { + return &ApplicationVariable{ + ID: v.ID, + ApplicationID: appID, + Type: v.Type, + Name: v.Name, + Value: v.Value, + } +} + +type ProjectVariable struct { + ID int64 `json:"id,omitempty" cli:"-"` + Name string `json:"name" cli:"name,key"` + Value string `json:"value" cli:"value"` + Type string `json:"type" cli:"type"` + ProjectID int64 `json:"project_id" cli:"-"` +} + +type ApplicationVariable struct { + ID int64 `json:"id,omitempty" cli:"-"` + Name string `json:"name" cli:"name,key"` + Value string `json:"value" cli:"value"` + Type string `json:"type" cli:"type"` + ApplicationID int64 `json:"application_id" cli:"-"` +} + +type EnvironmentVariable struct { + ID int64 `json:"id,omitempty" cli:"-"` + Name string `json:"name" cli:"name,key"` + Value string `json:"value" cli:"value"` + Type string `json:"type" cli:"type"` + EnvironmentID int64 `json:"environment_id" cli:"-"` +} + func (v Variable) ToParameter(prefix string) Parameter { name := v.Name if prefix != "" {