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

Implement getParameter method for SQLite #2985

Merged
merged 16 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
71 changes: 71 additions & 0 deletions integration/commands_administration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,30 @@ func TestCommandsAdministrationGetParameter(t *testing.T) {
},
altMessage: `BSON field 'allParameters' is the wrong type 'string', expected types '[bool, long, int, decimal, double]'`,
},
"FeatureCompatibilityVersion": {
command: bson.D{
{"getParameter", bson.D{}},
{"featureCompatibilityVersion", 1},
},
expected: map[string]any{
"featureCompatibilityVersion": bson.D{{"version", "6.0"}},
"ok": float64(1),
},
},
"FeatureCompatibilityVersionShowDetails": {
command: bson.D{
{"getParameter", bson.D{{"showDetails", true}}},
{"featureCompatibilityVersion", 1},
},
expected: map[string]any{
"featureCompatibilityVersion": bson.D{
{"value", bson.D{{"version", "6.0"}}},
{"settableAtRuntime", false},
{"settableAtStartup", false},
},
"ok": float64(1),
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
Expand Down Expand Up @@ -550,6 +574,53 @@ func TestCommandsAdministrationGetParameter(t *testing.T) {
}
}

func TestGetParameterCommandAuthenticationMechanisms(t *testing.T) {
noisersup marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

s := setup.SetupWithOpts(t, &setup.SetupOpts{
DatabaseName: "admin",
})

t.Run("ShowDetails", func(t *testing.T) {
var res bson.D
err := s.Collection.Database().RunCommand(s.Ctx, bson.D{
{"getParameter", bson.D{{"showDetails", true}}},
{"authenticationMechanisms", 1},
}).Decode(&res)
require.NoError(t, err)

doc := ConvertDocument(t, res)
v, _ := doc.Get("authenticationMechanisms")
require.NotNil(t, v)

authenticationMechanisms, ok := v.(*types.Document)
require.True(t, ok)

settableAtRuntime, _ := authenticationMechanisms.Get("settableAtRuntime")
require.Equal(t, false, settableAtRuntime)

settableAtStartup, _ := authenticationMechanisms.Get("settableAtStartup")
require.Equal(t, true, settableAtStartup)
})

t.Run("Plain", func(t *testing.T) {
setup.SkipForMongoDB(t, "PLAIN authentication mechanism is not support by MongoDB")

var res bson.D
err := s.Collection.Database().RunCommand(s.Ctx, bson.D{
{"getParameter", bson.D{}},
{"authenticationMechanisms", 1},
}).Decode(&res)
require.NoError(t, err)

expected := bson.D{
{"authenticationMechanisms", bson.A{"PLAIN"}},
{"ok", float64(1)},
}
require.Equal(t, expected, res)
})
}

func TestCommandsAdministrationBuildInfo(t *testing.T) {
t.Parallel()
ctx, collection := setup.Setup(t)
Expand Down
156 changes: 156 additions & 0 deletions internal/handlers/common/getparameter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

import (
"context"
"errors"

"go.uber.org/zap"

"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
"github.com/FerretDB/FerretDB/internal/handlers/commonparams"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/iterator"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgGetParameter returns parameter details.
func MsgGetParameter(_ context.Context, msg *wire.OpMsg, l *zap.Logger) (*wire.OpMsg, error) {
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
document, err := msg.Document()
if err != nil {
return nil, lazyerrors.Error(err)
}

getParameter := must.NotFail(document.Get("getParameter"))

showDetails, allParameters, err := extractGetParameter(getParameter)
if err != nil {
return nil, lazyerrors.Error(err)
}

Ignored(document, l, "comment")

parameters := must.NotFail(types.NewDocument(
// to add a new parameter, fill template and place it in the alphabetical order position
//"<name>", must.NotFail(types.NewDocument(
// "value", <value>,
// "settableAtRuntime", <bool>,
// "settableAtStartup", <bool>,
//)),
"authenticationMechanisms", must.NotFail(types.NewDocument(
"value", must.NotFail(types.NewArray("PLAIN")),
"settableAtRuntime", false,
"settableAtStartup", true,
)),
"authSchemaVersion", must.NotFail(types.NewDocument(
"value", int32(5),
"settableAtRuntime", true,
"settableAtStartup", true,
)),
"featureCompatibilityVersion", must.NotFail(types.NewDocument(
"value", must.NotFail(types.NewDocument("version", "6.0")),
"settableAtRuntime", false,
"settableAtStartup", false,
)),
"quiet", must.NotFail(types.NewDocument(
"value", false,
"settableAtRuntime", true,
"settableAtStartup", true,
)),
// parameters are alphabetically ordered
))

resDoc, err := selectParameters(document, parameters, showDetails, allParameters)
if err != nil {
return nil, lazyerrors.Error(err)
}

if resDoc.Len() < 1 {
return nil, commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrorCode(0),
"no option found to get",
document.Command(),
)
}

resDoc.Set("ok", float64(1))

var reply wire.OpMsg
must.NoError(reply.SetSections(wire.OpMsgSection{
Documents: []*types.Document{resDoc},
}))

return &reply, nil
}

// selectParameters makes a selection of requested parameters.
func selectParameters(document, parameters *types.Document, showDetails, allParameters bool) (resDoc *types.Document, err error) {
resDoc = must.NotFail(types.NewDocument())

iter := parameters.Iterator()
defer iter.Close()

for {
k, v, err := iter.Next()
if err != nil {
if errors.Is(err, iterator.ErrIteratorDone) {
break
}

return nil, lazyerrors.Error(err)
}

if !allParameters && !document.Has(k) {
continue
}

if !showDetails {
v = must.NotFail(v.(*types.Document).Get("value"))
}

resDoc.Set(k, v)
}

return resDoc, nil
}

// extractGetParameter retrieves showDetails & allParameters options set on the getParameter value.
func extractGetParameter(getParameter any) (showDetails, allParameters bool, err error) {
if getParameter == "*" {
allParameters = true
return
}

if param, ok := getParameter.(*types.Document); ok {
if v, _ := param.Get("showDetails"); v != nil {
showDetails, err = commonparams.GetBoolOptionalParam("showDetails", v)
if err != nil {
return false, false, lazyerrors.Error(err)
}
}

if v, _ := param.Get("allParameters"); v != nil {
allParameters, err = commonparams.GetBoolOptionalParam("allParameters", v)
if err != nil {
return false, false, lazyerrors.Error(err)
}
}
}

return showDetails, allParameters, nil
}
4 changes: 2 additions & 2 deletions internal/handlers/hana/msg_getparameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package hana
import (
"context"

"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgGetParameter implements HandlerInterface.
func (h *Handler) MsgGetParameter(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return nil, notImplemented(must.NotFail(msg.Document()).Command())
return common.MsgGetParameter(ctx, msg, h.L)
}
128 changes: 1 addition & 127 deletions internal/handlers/pg/msg_getparameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,138 +16,12 @@ package pg

import (
"context"
"errors"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
"github.com/FerretDB/FerretDB/internal/handlers/commonparams"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/iterator"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgGetParameter implements HandlerInterface.
func (h *Handler) MsgGetParameter(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
document, err := msg.Document()
if err != nil {
return nil, lazyerrors.Error(err)
}

showDetails, allParameters, err := extractParam(document)
if err != nil {
return nil, lazyerrors.Error(err)
}

resDB := must.NotFail(types.NewDocument(
"authSchemaVersion", must.NotFail(types.NewDocument(
"value", int32(5),
"settableAtRuntime", true,
"settableAtStartup", true,
)),
"quiet", must.NotFail(types.NewDocument(
"value", false,
"settableAtRuntime", true,
"settableAtStartup", true,
)),
"ok", float64(1),
))

resDoc := resDB
if !showDetails || !allParameters {
resDoc, err = selectUnit(document, resDB, showDetails, allParameters)
if err != nil {
return nil, lazyerrors.Error(err)
}
}

var reply wire.OpMsg
must.NoError(reply.SetSections(wire.OpMsgSection{
Documents: []*types.Document{resDoc},
}))

common.Ignored(document, h.L, "comment")

if resDoc.Len() < 2 {
return &reply, commonerrors.NewCommandErrorMsg(commonerrors.ErrorCode(0), "no option found to get")
}

return &reply, nil
}

// selectUnit is makes a selection of requested parameters.
func selectUnit(document, resDB *types.Document, showDetails, allParameters bool) (doc *types.Document, err error) {
doc = must.NotFail(types.NewDocument())

iter := resDB.Iterator()
defer iter.Close()

for {
k, v, err := iter.Next()
if err != nil {
if errors.Is(err, iterator.ErrIteratorDone) {
break
}

return nil, err
}

if k == "getParameter" || k == "comment" || k == "$db" {
continue
}

if !allParameters && !document.Has(k) {
continue
}

if !showDetails {
if itm, ok := v.(*types.Document); ok {
val, err := itm.Get("value")
if err != nil {
continue
}
v = val
}
}

doc.Set(k, v)
}

if doc.Len() < 1 {
doc.Set("ok", float64(0))
return doc, nil
}

doc.Set("ok", float64(1))
return doc, nil
}

// extractParam is getting parameters showDetails & allParameters from the request.
func extractParam(document *types.Document) (showDetails, allParameters bool, err error) {
getPrm, err := document.Get("getParameter")
if err != nil {
return false, false, lazyerrors.Error(err)
}

if param, ok := getPrm.(*types.Document); ok {
if v, _ := param.Get("showDetails"); v != nil {
showDetails, err = commonparams.GetBoolOptionalParam("showDetails", v)
if err != nil {
return false, false, lazyerrors.Error(err)
}
}

if v, _ := param.Get("allParameters"); v != nil {
allParameters, err = commonparams.GetBoolOptionalParam("allParameters", v)
if err != nil {
return false, false, lazyerrors.Error(err)
}
}
}
if getPrm == "*" {
allParameters = true
}

return showDetails, allParameters, nil
return common.MsgGetParameter(ctx, msg, h.L)
}
Loading