update secret create

This commit is contained in:
Simone Gotti 2019-04-05 10:32:32 +02:00
parent 25eb200625
commit 42becb0505
4 changed files with 27 additions and 2 deletions

View File

@ -31,11 +31,13 @@ var cmdProjectGroupSecretCreate = &cobra.Command{
func init() { func init() {
flags := cmdProjectGroupSecretCreate.Flags() flags := cmdProjectGroupSecretCreate.Flags()
flags.StringVar(&secretCreateOpts.projectRef, "project", "", "project id or full path") flags.StringVar(&secretCreateOpts.parentRef, "projectgroup", "", "project group id or full path")
flags.StringVarP(&secretCreateOpts.name, "name", "n", "", "secret name") flags.StringVarP(&secretCreateOpts.name, "name", "n", "", "secret name")
flags.StringVar(&secretCreateOpts.data, "data", "", "json map of secret data")
cmdProjectGroupSecretCreate.MarkFlagRequired("project") cmdProjectGroupSecretCreate.MarkFlagRequired("project")
cmdProjectGroupSecretCreate.MarkFlagRequired("name") cmdProjectGroupSecretCreate.MarkFlagRequired("name")
cmdProjectGroupSecretCreate.MarkFlagRequired("data")
cmdProjectGroupSecret.AddCommand(cmdProjectGroupSecretCreate) cmdProjectGroupSecret.AddCommand(cmdProjectGroupSecretCreate)
} }

View File

@ -16,9 +16,12 @@ package cmd
import ( import (
"context" "context"
"encoding/json"
"github.com/sorintlab/agola/internal/services/gateway/api"
"github.com/sorintlab/agola/internal/services/types"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sorintlab/agola/internal/services/gateway/api"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -35,6 +38,7 @@ var cmdProjectSecretCreate = &cobra.Command{
type secretCreateOptions struct { type secretCreateOptions struct {
projectRef string projectRef string
name string name string
data string
} }
var secretCreateOpts secretCreateOptions var secretCreateOpts secretCreateOptions
@ -44,9 +48,11 @@ func init() {
flags.StringVar(&secretCreateOpts.projectRef, "project", "", "project id or full path)") flags.StringVar(&secretCreateOpts.projectRef, "project", "", "project id or full path)")
flags.StringVarP(&secretCreateOpts.name, "name", "n", "", "secret name") flags.StringVarP(&secretCreateOpts.name, "name", "n", "", "secret name")
flags.StringVar(&secretCreateOpts.data, "data", "", "json map of secret data")
cmdProjectSecretCreate.MarkFlagRequired("project") cmdProjectSecretCreate.MarkFlagRequired("project")
cmdProjectSecretCreate.MarkFlagRequired("name") cmdProjectSecretCreate.MarkFlagRequired("name")
cmdProjectSecretCreate.MarkFlagRequired("data")
cmdProjectSecret.AddCommand(cmdProjectSecretCreate) cmdProjectSecret.AddCommand(cmdProjectSecretCreate)
} }
@ -54,8 +60,14 @@ func init() {
func secretCreate(cmd *cobra.Command, ownertype string, args []string) error { func secretCreate(cmd *cobra.Command, ownertype string, args []string) error {
gwclient := api.NewClient(gatewayURL, token) gwclient := api.NewClient(gatewayURL, token)
var data map[string]string
if err := json.Unmarshal([]byte(secretCreateOpts.data), &data); err != nil {
log.Fatalf("failed to unmarshall values: %v", err)
}
req := &api.CreateSecretRequest{ req := &api.CreateSecretRequest{
Name: secretCreateOpts.name, Name: secretCreateOpts.name,
Type: types.SecretTypeInternal,
Data: data,
} }
switch ownertype { switch ownertype {

View File

@ -896,6 +896,15 @@ func (s *CommandHandler) CreateSecret(ctx context.Context, secret *types.Secret)
if secret.Name == "" { if secret.Name == "" {
return nil, util.NewErrBadRequest(errors.Errorf("secret name required")) return nil, util.NewErrBadRequest(errors.Errorf("secret name required"))
} }
if secret.Type != types.SecretTypeInternal {
return nil, util.NewErrBadRequest(errors.Errorf("wrong secret type %q", secret.Type))
}
switch secret.Type {
case types.SecretTypeInternal:
if len(secret.Data) == 0 {
return nil, util.NewErrBadRequest(errors.Errorf("empty secret data"))
}
}
if secret.Parent.Type == "" { if secret.Parent.Type == "" {
return nil, util.NewErrBadRequest(errors.Errorf("secret parent type required")) return nil, util.NewErrBadRequest(errors.Errorf("secret parent type required"))
} }

View File

@ -128,6 +128,8 @@ func (h *CreateSecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
s := &types.Secret{ s := &types.Secret{
Name: req.Name, Name: req.Name,
Type: req.Type,
Data: req.Data,
} }
switch parentType { switch parentType {