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 setFreeMonitoring stub #759

Merged
merged 6 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
62 changes: 62 additions & 0 deletions integration/commands_freemonitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)

func TestCommandsFreeMonitoringGetFreeMonitoringStatus(t *testing.T) {
Expand Down Expand Up @@ -53,3 +54,64 @@ func TestCommandsFreeMonitoringGetFreeMonitoringStatus(t *testing.T) {
assert.Equal(t, m[k], item)
}
}

func TestCommandsFreeMonitoringSetFreeMonitoring(t *testing.T) {
t.Parallel()
ctx, collection := setupWithOpts(t, &setupOpts{
databaseName: "admin",
})

for name, tc := range map[string]struct {
command bson.D
err *mongo.CommandError
}{
"Enable": {
command: bson.D{{"setFreeMonitoring", 1}, {"action", "enable"}},
err: &mongo.CommandError{
Code: 50840,
Name: "Location50840",
Message: `Free Monitoring has been disabled via the command-line and/or config file`,
},
},
"Disable": {
command: bson.D{{"setFreeMonitoring", 1}, {"action", "disable"}},
err: &mongo.CommandError{
Code: 50840,
Name: "Location50840",
Message: `Free Monitoring has been disabled via the command-line and/or config file`,
},
},
"Other": {
command: bson.D{{"setFreeMonitoring", 1}, {"action", "foobar"}},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: `Enumeration value 'foobar' for field 'setFreeMonitoring.action' is not a valid value.`,
},
},
"Empty": {
command: bson.D{{"setFreeMonitoring", 1}, {"action", ""}},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: `Enumeration value '' for field 'setFreeMonitoring.action' is not a valid value.`,
},
},
} {
name, tc := name, tc

t.Run(name, func(t *testing.T) {
t.Parallel()

var actual bson.D
err := collection.Database().RunCommand(ctx, tc.command).Decode(&actual)

if tc.err != nil {
AssertEqualError(t, *tc.err, err)
return
}

require.NoError(t, err)
})
}
}
4 changes: 4 additions & 0 deletions internal/handlers/common/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ const (
// while projection document already marked as inclusion.
ErrProjectionExIn = ErrorCode(31254) // Location31254

// ErrFreeMonitoringDisabled indicates that free monitoring is disabled
// by command-line or config file.
ErrFreeMonitoringDisabled = ErrorCode(50840) // Location50840

// ErrRegexOptions indicates regex options error.
ErrRegexOptions = ErrorCode(51075) // Location51075

Expand Down
8 changes: 5 additions & 3 deletions internal/handlers/common/errorcode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions internal/handlers/common/msg_listcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ var Commands = map[string]command{
Help: "Returns an overview of the databases state.",
Handler: (handlers.Interface).MsgServerStatus,
},
"setFreeMonitoring": {
Help: "Toggles free monitoring.",
Handler: (handlers.Interface).MsgSetFreeMonitoring,
},
"update": {
Help: "Updates documents that are matched by the query.",
Handler: (handlers.Interface).MsgUpdate,
Expand Down
54 changes: 54 additions & 0 deletions internal/handlers/common/msg_setfreemonitoring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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"
"fmt"

"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgSetFreeMonitoring is a common implementation of the setFreeMonitoring command.
func MsgSetFreeMonitoring(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
document, err := msg.Document()
if err != nil {
return nil, lazyerrors.Error(err)
}

command := document.Command()
var action string
if action, err = GetRequiredParam[string](document, "action"); err != nil {
return nil, err
}

switch action {
case "enable", "disable":
return nil, NewErrorMsg(
ErrFreeMonitoringDisabled,
"Free Monitoring has been disabled via the command-line and/or config file",
)
default:
return nil, NewErrorMsg(
ErrBadValue,
fmt.Sprintf(
"Enumeration value '%s' for field '%s' is not a valid value.",
action,
command+".action",
),
)
}
}
27 changes: 27 additions & 0 deletions internal/handlers/dummy/msg_setfreemonitoring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 dummy

import (
"context"

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

// MsgSetFreeMonitoring implements HandlerInterface.
func (h *Handler) MsgSetFreeMonitoring(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return common.MsgSetFreeMonitoring(ctx, msg)
}
3 changes: 3 additions & 0 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ type Interface interface {
// MsgServerStatus returns an overview of the databases state.
MsgServerStatus(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error)

// MsgSetFreeMonitoring toggles free monitoring.
MsgSetFreeMonitoring(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error)

// MsgUpdate updates documents that are matched by the query.
MsgUpdate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error)

Expand Down
27 changes: 27 additions & 0 deletions internal/handlers/pg/msg_setfreemonitoring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 pg

import (
"context"

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

// MsgSetFreeMonitoring implements HandlerInterface.
func (h *Handler) MsgSetFreeMonitoring(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return common.MsgSetFreeMonitoring(ctx, msg)
}
27 changes: 27 additions & 0 deletions internal/handlers/tigris/msg_setfreemonitoring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 tigris

import (
"context"

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

// MsgSetFreeMonitoring implements HandlerInterface.
func (h *Handler) MsgSetFreeMonitoring(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return common.MsgSetFreeMonitoring(ctx, msg)
}