Skip to content

Commit

Permalink
Merge pull request #595 from cvaroqui/main
Browse files Browse the repository at this point in the history
Make keystores "add" accept no --from nor --value
  • Loading branch information
cvaroqui authored Sep 30, 2024
2 parents 467c274 + 585db4a commit fb93cb2
Show file tree
Hide file tree
Showing 34 changed files with 940 additions and 413 deletions.
24 changes: 24 additions & 0 deletions core/objectaction/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ func (t T) DoAsync() error {
err = fmt.Errorf("%s", resp.JSON401)
case 403:
err = fmt.Errorf("%s", resp.JSON403)
case 404:
err = fmt.Errorf("%s", resp.JSON404)
case 408:
err = fmt.Errorf("%s", resp.JSON408)
case 409:
Expand All @@ -476,6 +478,8 @@ func (t T) DoAsync() error {
err = fmt.Errorf("%s", resp.JSON401)
case 403:
err = fmt.Errorf("%s", resp.JSON403)
case 404:
err = fmt.Errorf("%s", resp.JSON404)
case 408:
err = fmt.Errorf("%s", resp.JSON408)
case 409:
Expand All @@ -497,6 +501,8 @@ func (t T) DoAsync() error {
err = fmt.Errorf("%s", resp.JSON401)
case 403:
err = fmt.Errorf("%s", resp.JSON403)
case 404:
err = fmt.Errorf("%s", resp.JSON404)
case 408:
err = fmt.Errorf("%s", resp.JSON408)
case 409:
Expand All @@ -518,6 +524,8 @@ func (t T) DoAsync() error {
err = fmt.Errorf("%s", resp.JSON401)
case 403:
err = fmt.Errorf("%s", resp.JSON403)
case 404:
err = fmt.Errorf("%s", resp.JSON404)
case 408:
err = fmt.Errorf("%s", resp.JSON408)
case 409:
Expand All @@ -539,6 +547,8 @@ func (t T) DoAsync() error {
err = fmt.Errorf("%s", resp.JSON401)
case 403:
err = fmt.Errorf("%s", resp.JSON403)
case 404:
err = fmt.Errorf("%s", resp.JSON404)
case 408:
err = fmt.Errorf("%s", resp.JSON408)
case 409:
Expand Down Expand Up @@ -567,6 +577,8 @@ func (t T) DoAsync() error {
err = fmt.Errorf("%s", resp.JSON401)
case 403:
err = fmt.Errorf("%s", resp.JSON403)
case 404:
err = fmt.Errorf("%s", resp.JSON404)
case 408:
err = fmt.Errorf("%s", resp.JSON408)
case 409:
Expand All @@ -588,6 +600,8 @@ func (t T) DoAsync() error {
err = fmt.Errorf("%s", resp.JSON401)
case 403:
err = fmt.Errorf("%s", resp.JSON403)
case 404:
err = fmt.Errorf("%s", resp.JSON404)
case 408:
err = fmt.Errorf("%s", resp.JSON408)
case 409:
Expand All @@ -609,6 +623,8 @@ func (t T) DoAsync() error {
err = fmt.Errorf("%s", resp.JSON401)
case 403:
err = fmt.Errorf("%s", resp.JSON403)
case 404:
err = fmt.Errorf("%s", resp.JSON404)
case 408:
err = fmt.Errorf("%s", resp.JSON408)
case 409:
Expand All @@ -630,6 +646,8 @@ func (t T) DoAsync() error {
err = fmt.Errorf("%s", resp.JSON401)
case 403:
err = fmt.Errorf("%s", resp.JSON403)
case 404:
err = fmt.Errorf("%s", resp.JSON404)
case 408:
err = fmt.Errorf("%s", resp.JSON408)
case 409:
Expand All @@ -651,6 +669,8 @@ func (t T) DoAsync() error {
err = fmt.Errorf("%s", resp.JSON401)
case 403:
err = fmt.Errorf("%s", resp.JSON403)
case 404:
err = fmt.Errorf("%s", resp.JSON404)
case 408:
err = fmt.Errorf("%s", resp.JSON408)
case 409:
Expand All @@ -672,6 +692,8 @@ func (t T) DoAsync() error {
err = fmt.Errorf("%s", resp.JSON401)
case 403:
err = fmt.Errorf("%s", resp.JSON403)
case 404:
err = fmt.Errorf("%s", resp.JSON404)
case 408:
err = fmt.Errorf("%s", resp.JSON408)
case 409:
Expand Down Expand Up @@ -699,6 +721,8 @@ func (t T) DoAsync() error {
err = fmt.Errorf("%s", resp.JSON401)
case 403:
err = fmt.Errorf("%s", resp.JSON403)
case 404:
err = fmt.Errorf("%s", resp.JSON404)
case 408:
err = fmt.Errorf("%s", resp.JSON408)
case 409:
Expand Down
22 changes: 18 additions & 4 deletions core/om/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,37 +418,51 @@ func newCmdDaemonStop() *cobra.Command {

func newCmdKeystoreAdd(kind string) *cobra.Command {
var options commands.CmdKeystoreAdd
var from, value string
cmd := &cobra.Command{
Use: "add",
Short: "add new keys",
RunE: func(cmd *cobra.Command, args []string) error {
if cmd.Flag("from").Changed {
options.From = &from
}
if cmd.Flag("value").Changed {
options.Value = &value
}
return options.Run(selectorFlag, kind)
},
}
flags := cmd.Flags()
addFlagsGlobal(flags, &options.OptsGlobal)
addFlagsLock(flags, &options.OptsLock)
addFlagKey(flags, &options.Key)
addFlagFrom(flags, &options.From)
addFlagValue(flags, &options.Value)
addFlagFrom(flags, &from)
addFlagValue(flags, &value)
cmd.MarkFlagsMutuallyExclusive("from", "value")
return cmd
}

func newCmdKeystoreChange(kind string) *cobra.Command {
var options commands.CmdKeystoreChange
var from, value string
cmd := &cobra.Command{
Use: "change",
Short: "change existing keys value",
RunE: func(cmd *cobra.Command, args []string) error {
if cmd.Flag("from").Changed {
options.From = &from
}
if cmd.Flag("value").Changed {
options.Value = &value
}
return options.Run(selectorFlag, kind)
},
}
flags := cmd.Flags()
addFlagsGlobal(flags, &options.OptsGlobal)
addFlagKey(flags, &options.Key)
addFlagFrom(flags, &options.From)
addFlagValue(flags, &options.Value)
addFlagFrom(flags, &from)
addFlagValue(flags, &value)
cmd.MarkFlagsMutuallyExclusive("from", "value")
return cmd
}
Expand Down
13 changes: 8 additions & 5 deletions core/omcmd/keystore_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type (
OptsGlobal
OptsLock
Key string
From string
Value string
From *string
Value *string
}
)

Expand All @@ -33,10 +33,13 @@ func (t *CmdKeystoreAdd) Run(selector, kind string) error {
if err != nil {
return nil, err
}
if t.Value != "" {
return nil, store.AddKey(t.Key, []byte(t.Value))
if t.Value != nil {
return nil, store.AddKey(t.Key, []byte(*t.Value))
}
m, err := uri.ReadAllFrom(t.From)
if t.From == nil {
return nil, store.AddKey(t.Key, []byte{})
}
m, err := uri.ReadAllFrom(*t.From)
if err != nil {
return nil, err
}
Expand Down
14 changes: 9 additions & 5 deletions core/omcmd/keystore_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package omcmd

import (
"context"
"fmt"

"github.com/opensvc/om3/core/naming"
"github.com/opensvc/om3/core/object"
Expand All @@ -14,8 +15,8 @@ type (
CmdKeystoreChange struct {
OptsGlobal
Key string
From string
Value string
From *string
Value *string
}
)

Expand All @@ -32,10 +33,13 @@ func (t *CmdKeystoreChange) Run(selector, kind string) error {
if err != nil {
return nil, err
}
if t.Value != "" {
return nil, store.ChangeKey(t.Key, []byte(t.Value))
if t.Value != nil {
return nil, store.ChangeKey(t.Key, []byte(*t.Value))
}
m, err := uri.ReadAllFrom(t.From)
if t.From == nil {
return nil, fmt.Errorf("value or value source mut be specified for a change action")
}
m, err := uri.ReadAllFrom(*t.From)
if err != nil {
return nil, err
}
Expand Down
5 changes: 1 addition & 4 deletions core/omcmd/lib_remote_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ func createTempRemoteConfig(p naming.Path, c *client.T) (string, error) {
buff []byte
f *os.File
)
if c, err = remoteClient(p, c); err != nil {
return "", err
}
if buff, err = fetchConfig(p, c); err != nil {
return "", err
}
Expand Down Expand Up @@ -76,7 +73,7 @@ func putConfig(p naming.Path, fName string, c *client.T) (err error) {
return err
}
switch resp.StatusCode {
case http.StatusOK:
case http.StatusNoContent:
return nil
default:
return fmt.Errorf("put object %s file from %s: %s", p, c.URL(), resp.Status)
Expand Down
7 changes: 5 additions & 2 deletions core/omcmd/object_edit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ type (
)

func (t *CmdObjectEditConfig) do(selector string, c *client.T) error {
sel := objectselector.New(selector)
wc := clientcontext.IsSet()
sel := objectselector.New(
selector,
objectselector.WithClient(c),
)
paths, err := sel.MustExpand()
if err != nil {
return err
}
wc := clientcontext.IsSet()
for _, p := range paths {
obj, err := object.NewConfigurer(p)
if err != nil {
Expand Down
58 changes: 15 additions & 43 deletions core/omcmd/object_print_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package omcmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strings"

"github.com/opensvc/om3/core/client"
"github.com/opensvc/om3/core/clientcontext"
"github.com/opensvc/om3/core/cluster"
"github.com/opensvc/om3/core/naming"
"github.com/opensvc/om3/core/object"
"github.com/opensvc/om3/core/objectselector"
Expand Down Expand Up @@ -80,52 +78,26 @@ func (t *CmdObjectPrintConfig) extractLocal(p naming.Path) (rawconfig.T, error)
}

func (t *CmdObjectPrintConfig) extractFromDaemon(p naming.Path, c *client.T) (rawconfig.T, error) {
var nodenames []string
var errs error
resp, err := c.GetObjectWithResponse(context.Background(), p.Namespace, p.Kind, p.Name)
if err != nil {
return rawconfig.T{}, err
}
switch {
case resp.JSON200 != nil:
if len(resp.JSON200.Data.Scope) == 0 {
return rawconfig.T{}, nil
} else {
nodenames = resp.JSON200.Data.Scope
}
default:
return rawconfig.T{}, fmt.Errorf("unexpected GetObject response: %s", resp.Status())
}
params := api.GetObjectConfigParams{
Evaluate: &t.Eval,
Impersonate: &t.Impersonate,
}
for _, nodename := range nodenames {
secret := cluster.ConfigData.Get().Secret()
scopeClient, err := client.New(client.WithURL(nodename), client.WithPassword(secret))
if err != nil {
return rawconfig.T{}, err
}
resp, err := scopeClient.GetObjectConfigWithResponse(context.Background(), p.Namespace, p.Kind, p.Name, &params)
if err != nil {
errs = errors.Join(errs, err)
continue
} else if resp.StatusCode() != http.StatusOK {
errs = errors.Join(errs, fmt.Errorf("get object config: %s", resp.Status()))
continue
}
data := rawconfig.T{}
if b, err := json.Marshal(resp.JSON200.Data); err != nil {
errs = errors.Join(errs, err)
continue
} else if err := json.Unmarshal(b, &data); err != nil {
errs = errors.Join(errs, err)
continue
} else {
return data, nil
}
data := rawconfig.T{}
resp, err := c.GetObjectConfigWithResponse(context.Background(), p.Namespace, p.Kind, p.Name, &params)

if err != nil {
return data, err
} else if resp.StatusCode() != http.StatusOK {
return data, fmt.Errorf("get object config: %s", resp.Status())
}
return rawconfig.T{}, errs

if b, err := json.Marshal(resp.JSON200.Data); err != nil {
return data, err
} else if err := json.Unmarshal(b, &data); err != nil {
return data, err
}

return data, nil
}

func (t *CmdObjectPrintConfig) Run(selector, kind string) error {
Expand Down
Loading

0 comments on commit fb93cb2

Please sign in to comment.