Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(api): variables dao #5235

Merged
merged 7 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions engine/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,22 +671,10 @@ func (a *API) Serve(ctx context.Context) error {
return migrate.RefactorProjectKeys(ctx, a.DBConnectionFactory.GetDBMap())
}})

migrate.Add(ctx, sdk.Migration{Name: "RefactorApplicationVariables", Release: "0.44.0", Blocker: true, Automatic: true, ExecFunc: func(ctx context.Context) error {
return migrate.RefactorApplicationVariables(ctx, a.DBConnectionFactory.GetDBMap())
}})

migrate.Add(ctx, sdk.Migration{Name: "RefactorEnvironmentKeys", Release: "0.44.0", Blocker: true, Automatic: true, ExecFunc: func(ctx context.Context) error {
return migrate.RefactorEnvironmentKeys(ctx, a.DBConnectionFactory.GetDBMap())
}})

migrate.Add(ctx, sdk.Migration{Name: "RefactorProjectVariables", Release: "0.44.0", Blocker: true, Automatic: true, ExecFunc: func(ctx context.Context) error {
return migrate.RefactorProjectVariables(ctx, a.DBConnectionFactory.GetDBMap())
}})

migrate.Add(ctx, sdk.Migration{Name: "RefactorEnvironmentVariables", Release: "0.44.0", Blocker: true, Automatic: true, ExecFunc: func(ctx context.Context) error {
return migrate.RefactorEnvironmentVariables(ctx, a.DBConnectionFactory.GetDBMap())
}})

migrate.Add(ctx, sdk.Migration{Name: "CleanDuplicateNodes", Release: "0.44.0", Blocker: false, Automatic: true, ExecFunc: func(ctx context.Context) error {
return migrate.CleanDuplicateNodes(ctx, a.DBConnectionFactory.GetDBMap())
}})
Expand Down
2 changes: 1 addition & 1 deletion engine/api/application/application_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions engine/api/application/application_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
52 changes: 26 additions & 26 deletions engine/api/application/dao_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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") {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions engine/api/application/dao_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions engine/api/application/gorp_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions engine/api/application_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func Test_getApplicationExportHandler(t *testing.T) {
t.Fatal(err)
}

v1 := sdk.Variable{
v1 := sdk.ApplicationVariable{
Name: "var1",
Value: "value 1",
Type: sdk.StringVariable,
}

test.NoError(t, application.InsertVariable(db, app.ID, &v1, u))

v2 := sdk.Variable{
v2 := sdk.ApplicationVariable{
Name: "var2",
Value: "value 2",
Type: sdk.SecretVariable,
Expand Down
6 changes: 3 additions & 3 deletions engine/api/application_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions engine/api/application_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion engine/api/application_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Test_getVariableAuditInApplicationHandler(t *testing.T) {
}

// Add variable
v := sdk.Variable{
v := sdk.ApplicationVariable{
Name: "foo",
Type: "string",
Value: "bar",
Expand Down
2 changes: 1 addition & 1 deletion engine/api/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions engine/api/environment/dao_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions engine/api/environment/dao_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion engine/api/environment/environment_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading