-
Notifications
You must be signed in to change notification settings - Fork 414
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
feat(api,cli): add endpoint to copy variable from v1 to v2 #6931
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,13 @@ package main | |
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/ovh/cds/cli" | ||
"github.com/ovh/cds/sdk" | ||
"github.com/ovh/cds/sdk/cdsclient" | ||
) | ||
|
||
var projectVariableSetItemCmd = cli.Command{ | ||
|
@@ -23,6 +25,7 @@ func projectVariableSetItem() *cobra.Command { | |
cli.NewCommand(projectVariableSetItemCreateCmd, projectVariableSetItemCreateFunc, nil, withAllCommandModifiers()...), | ||
cli.NewCommand(projectVariableSetItemUpdateCmd, projectVariableSetItemUpdateFunc, nil, withAllCommandModifiers()...), | ||
cli.NewGetCommand(projectVariableSetItemShowCmd, projectVariableSetItemShowFunc, nil, withAllCommandModifiers()...), | ||
cli.NewCommand(projectVariableSetItemFromProjectCmd, projectVariableSetItemFromProjectFunc, nil, withAllCommandModifiers()...), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not a flag on create command ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for this one, it's not the same cmd arguments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
}) | ||
} | ||
|
||
|
@@ -79,6 +82,49 @@ func projectVariableSetItemDeleteFunc(v cli.Values) error { | |
return client.ProjectVariableSetItemDelete(context.Background(), v.GetString(_ProjectKey), v.GetString("variableset-name"), v.GetString("item-name")) | ||
} | ||
|
||
var projectVariableSetItemFromProjectCmd = cli.Command{ | ||
Name: "from-project", | ||
Aliases: []string{"fp"}, | ||
Short: "Copy a project variable to the given variable set", | ||
Example: "cdsctl X project variableset item from-project PROJECT_KEY VARIABLE_NAME MY-VARIABLESET-NAME --rename NEW_NAME", | ||
Ctx: []cli.Arg{ | ||
{Name: _ProjectKey}, | ||
}, | ||
Args: []cli.Arg{ | ||
{Name: "variable-name"}, | ||
{Name: "variableset-name"}, | ||
}, | ||
Flags: []cli.Flag{ | ||
{ | ||
Name: "rename", | ||
Type: cli.FlagString, | ||
Usage: "New name for the variable", | ||
}, | ||
{ | ||
Name: "force", | ||
Type: cli.FlagBool, | ||
Usage: "Create the variable set if it not exists", | ||
}, | ||
}, | ||
} | ||
|
||
func projectVariableSetItemFromProjectFunc(v cli.Values) error { | ||
varName := v.GetString("variable-name") | ||
vsName := v.GetString("variableset-name") | ||
rename := v.GetString("rename") | ||
force := strconv.FormatBool(v.GetBool("force")) | ||
|
||
copyRequest := sdk.CopyProjectVariableToVariableSet{ | ||
VariableName: varName, | ||
VariableSetName: vsName, | ||
NewName: rename, | ||
} | ||
if copyRequest.NewName == "" { | ||
copyRequest.NewName = varName | ||
} | ||
return client.ProjectVariableSetItemFromProjectVariable(context.Background(), v.GetString(_ProjectKey), copyRequest, cdsclient.WithQueryParameter("force", force)) | ||
} | ||
|
||
var projectVariableSetItemCreateCmd = cli.Command{ | ||
Name: "add", | ||
Aliases: []string{"create"}, | ||
|
@@ -97,7 +143,7 @@ var projectVariableSetItemCreateCmd = cli.Command{ | |
{ | ||
Name: "force", | ||
Type: cli.FlagBool, | ||
Usage: "create the variable set if it not exists", | ||
Usage: "create the variable set if it does not exists", | ||
}, | ||
}, | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
|
||
"github.com/ovh/cds/engine/api/application" | ||
"github.com/ovh/cds/engine/api/project" | ||
"github.com/ovh/cds/engine/service" | ||
"github.com/ovh/cds/sdk" | ||
) | ||
|
||
func (api *API) postMigrateApplicationVariableToVariableSetHandler() ([]service.RbacChecker, service.Handler) { | ||
return service.RBAC(api.projectManage), | ||
func(ctx context.Context, w http.ResponseWriter, req *http.Request) error { | ||
vars := mux.Vars(req) | ||
pKey := vars["projectKey"] | ||
|
||
var copyRequest sdk.CopyApplicationVariableToVariableSet | ||
if err := service.UnmarshalBody(req, ©Request); err != nil { | ||
return err | ||
} | ||
|
||
app, err := application.LoadByName(ctx, api.mustDB(), pKey, copyRequest.ApplicationName, application.LoadOptions.WithVariablesWithClearPassword) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tx, err := api.mustDB().Begin() | ||
if err != nil { | ||
return sdk.WithStack(err) | ||
} | ||
defer tx.Rollback() //nolint | ||
|
||
vs, err := project.LoadVariableSetByName(ctx, api.mustDB(), pKey, copyRequest.VariableSetName) | ||
if err != nil && !sdk.ErrorIs(err, sdk.ErrNotFound) { | ||
return err | ||
} | ||
if sdk.ErrorIs(err, sdk.ErrNotFound) { | ||
vs = &sdk.ProjectVariableSet{ | ||
Name: copyRequest.VariableSetName, | ||
ProjectKey: pKey, | ||
} | ||
if err := project.InsertVariableSet(ctx, tx, vs); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for _, v := range app.Variables { | ||
itemType := sdk.ProjectVariableTypeString | ||
if v.Type == sdk.SecretVariable { | ||
itemType = sdk.ProjectVariableTypeSecret | ||
} | ||
it := &sdk.ProjectVariableSetItem{ | ||
ProjectVariableSetID: vs.ID, | ||
Name: v.Name, | ||
Type: itemType, | ||
Value: v.Value, | ||
} | ||
switch v.Type { | ||
case sdk.SecretVariable: | ||
if err := project.InsertVariableSetItemSecret(ctx, tx, it); err != nil { | ||
return err | ||
} | ||
default: | ||
if err := project.InsertVariableSetItemText(ctx, tx, it); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
if err := tx.Commit(); err != nil { | ||
return sdk.WithStack(err) | ||
} | ||
return service.WriteJSON(w, nil, http.StatusOK) | ||
} | ||
} | ||
|
||
func (api *API) postMigrateProjectVariableHandler() ([]service.RbacChecker, service.Handler) { | ||
return service.RBAC(api.projectManage), | ||
func(ctx context.Context, w http.ResponseWriter, req *http.Request) error { | ||
vars := mux.Vars(req) | ||
pKey := vars["projectKey"] | ||
force := service.FormBool(req, "force") | ||
|
||
var copyRequest sdk.CopyProjectVariableToVariableSet | ||
if err := service.UnmarshalBody(req, ©Request); err != nil { | ||
return err | ||
} | ||
if copyRequest.NewName == "" { | ||
copyRequest.NewName = copyRequest.VariableName | ||
} | ||
|
||
proj, err := project.Load(ctx, api.mustDB(), pKey, project.LoadOptions.WithVariablesWithClearPassword) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tx, err := api.mustDB().Begin() | ||
if err != nil { | ||
return sdk.WithStack(err) | ||
} | ||
defer tx.Rollback() //nolint | ||
|
||
vs, err := project.LoadVariableSetByName(ctx, api.mustDB(), pKey, copyRequest.VariableSetName) | ||
if err != nil && !sdk.ErrorIs(err, sdk.ErrNotFound) { | ||
return err | ||
} | ||
if sdk.ErrorIs(err, sdk.ErrNotFound) { | ||
if force { | ||
vs = &sdk.ProjectVariableSet{ | ||
Name: copyRequest.VariableSetName, | ||
ProjectKey: pKey, | ||
} | ||
if err := project.InsertVariableSet(ctx, tx, vs); err != nil { | ||
return err | ||
} | ||
} else { | ||
return sdk.NewErrorFrom(sdk.ErrNotFound, "Variable set %s doesn't exist", copyRequest.VariableSetName) | ||
} | ||
} | ||
|
||
for _, v := range proj.Variables { | ||
if v.Name == copyRequest.VariableName { | ||
itemType := sdk.ProjectVariableTypeString | ||
if v.Type == sdk.SecretVariable { | ||
itemType = sdk.ProjectVariableTypeSecret | ||
} | ||
it := &sdk.ProjectVariableSetItem{ | ||
ProjectVariableSetID: vs.ID, | ||
Name: copyRequest.NewName, | ||
Type: itemType, | ||
Value: v.Value, | ||
} | ||
switch v.Type { | ||
case sdk.SecretVariable: | ||
if err := project.InsertVariableSetItemSecret(ctx, tx, it); err != nil { | ||
return err | ||
} | ||
default: | ||
if err := project.InsertVariableSetItemText(ctx, tx, it); err != nil { | ||
return err | ||
} | ||
} | ||
break | ||
} | ||
} | ||
|
||
if err := tx.Commit(); err != nil { | ||
return sdk.WithStack(err) | ||
} | ||
return service.WriteJSON(w, nil, http.StatusOK) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not just a flag on the create command ?