Skip to content

Commit

Permalink
refactor(api): variables dao (#5235)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored Jun 11, 2020
1 parent 80140d7 commit 5d198e3
Show file tree
Hide file tree
Showing 43 changed files with 317 additions and 198 deletions.
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
4 changes: 2 additions & 2 deletions engine/api/environment/environment_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 5d198e3

Please sign in to comment.