Skip to content

CLOUDP-57855: List all available measurements for a given process, Atlas #85

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

Merged
merged 8 commits into from
Apr 3, 2020
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ gen-mocks: ## Generate mocks
mockgen -source=internal/store/alerts.go -destination=internal/mocks/mock_alerts.go -package=mocks
mockgen -source=internal/store/global_alerts.go -destination=internal/mocks/mock_global_alerts.go -package=mocks
mockgen -source=internal/store/events.go -destination=internal/mocks/mock_events.go -package=mocks
mockgen -source=internal/store/process_measurements.go -destination=internal/mocks/mock_process_measurements.go -package=mocks

.PHONY: build
build: ## Generate a binary in ./bin
Expand Down
1 change: 1 addition & 0 deletions internal/cli/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func AtlasBuilder() *cobra.Command {
cmd.AddCommand(AtlasAlertsBuilder())
cmd.AddCommand(AtlasBackupsBuilder())
cmd.AddCommand(AtlasEventsBuilder())
cmd.AddCommand(AtlasMeasurementsBuilder())

return cmd
}
31 changes: 31 additions & 0 deletions internal/cli/atlas_measurements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2020 MongoDB 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 cli

import (
"github.com/mongodb/mongocli/internal/description"
"github.com/spf13/cobra"
)

func AtlasMeasurementsBuilder() *cobra.Command {
cmd := &cobra.Command{
Use: "measurements",
Short: description.Measurements,
}

cmd.AddCommand(AtlasMeasurementsProcessBuilder())

return cmd
}
109 changes: 109 additions & 0 deletions internal/cli/atlas_measurements_process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2020 MongoDB 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 cli

import (
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
"github.com/mongodb/mongocli/internal/description"
"github.com/mongodb/mongocli/internal/flags"
"github.com/mongodb/mongocli/internal/json"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
)

type atlasMeasurementsProcessOpts struct {
*globalOpts
pageNum int
itemsPerPage int
host string
port int
granularity string
period string
start string
end string
measurementType string
store store.ProcessMeasurementLister
}

func (opts *atlasMeasurementsProcessOpts) init() error {
if opts.ProjectID() == "" {
return errMissingProjectID
}

var err error
opts.store, err = store.New()
return err
}

func (opts *atlasMeasurementsProcessOpts) Run() error {
listOpts := opts.newProcessMeasurementListOptions()
result, err := opts.store.ListProcessMeasurements(opts.ProjectID(), opts.host, opts.port, listOpts)

if err != nil {
return err
}

return json.PrettyPrint(result)
}

func (opts *atlasMeasurementsProcessOpts) newProcessMeasurementListOptions() *atlas.ProcessMeasurementListOptions {
return &atlas.ProcessMeasurementListOptions{
ListOptions: &atlas.ListOptions{
PageNum: opts.pageNum,
ItemsPerPage: opts.itemsPerPage,
},
Granularity: opts.granularity,
Period: opts.period,
Start: opts.start,
End: opts.end,
M: opts.measurementType,
}
}

// mongocli atlas measurements process(es) host:port [--granularity granularity] [--period period] [--start start] [--end end] [--type type][--projectId projectId]
func AtlasMeasurementsProcessBuilder() *cobra.Command {
opts := &atlasMeasurementsProcessOpts{
globalOpts: newGlobalOpts(),
}
cmd := &cobra.Command{
Use: "process",
Short: description.ProcessMeasurements,
Aliases: []string{"processes"},
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.init()
},
RunE: func(cmd *cobra.Command, args []string) error {
var err error
opts.host, opts.port, err = GetHostNameAndPort(args[0])
if err != nil {
return err
}

return opts.Run()
},
}

cmd.Flags().StringVar(&opts.granularity, flags.Granularity, "", usage.Granularity)
cmd.Flags().StringVar(&opts.period, flags.Period, "", usage.Period)
cmd.Flags().StringVar(&opts.start, flags.Start, "", usage.Start)
cmd.Flags().StringVar(&opts.end, flags.End, "", usage.End)
cmd.Flags().StringVar(&opts.measurementType, flags.MeasurementType, "", usage.MeasurementType)

cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)

return cmd
}
56 changes: 56 additions & 0 deletions internal/cli/atlas_measurements_process_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2020 MongoDB 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 cli

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongocli/internal/fixtures"
"github.com/mongodb/mongocli/internal/mocks"
)

func TestAtlasMeasurementsProcess_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockProcessMeasurementLister(ctrl)

defer ctrl.Finish()

expected := fixtures.ProcessMeasurements()

listOpts := &atlasMeasurementsProcessOpts{
globalOpts: newGlobalOpts(),
host: "hard-00-00.mongodb.net",
port: 27017,
granularity: "PT1M",
period: "PT1M",
store: mockStore,
}

hostName, port, err := GetHostNameAndPort("hard-00-00.mongodb.net:27017")
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}

opts := listOpts.newProcessMeasurementListOptions()
mockStore.
EXPECT().ListProcessMeasurements(listOpts.projectID, hostName, port, opts).
Return(expected, nil).
Times(1)

err = listOpts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
16 changes: 16 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cli
import (
"errors"
"fmt"
"strconv"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -251,3 +252,18 @@ func (opts *atlasAlertsConfigOpts) newMatcher() *atlas.Matcher {
Value: strings.ToUpper(opts.matcherValue),
}
}

// GetHostNameAndPort return the hostname and the port starting from the string hostname:port
func GetHostNameAndPort(hostInfo string) (string, int, error) {
host := strings.SplitN(hostInfo, ":", -1)
if len(host) != 2 {
return "", 0, fmt.Errorf("expected hostname:port, got %s", host)
}

port, err := strconv.Atoi(host[1])
if err != nil {
return "", 0, fmt.Errorf("invalid port number, got %s", host[1])
}

return host[0], port, nil
}
2 changes: 2 additions & 0 deletions internal/description/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ A user’s roles apply to all the clusters in the project.`
ListDBUsers = "List Atlas database users for a project."
ListEvents = "List events for an organization or project"
UpdateDBUser = "Update a MongoDB dbuser in Atlas."
ProcessMeasurements = "Get measurements for a given host."
Whitelist = "Manage the IP whitelist for a project."
CreateWhitelist = "Create an IP whitelist for a project."
DeleteWhitelist = "Delete a database user for a project."
Expand Down Expand Up @@ -87,4 +88,5 @@ A user’s roles apply to all the clusters in the project.`
Security = "Manage clusters security configuration."
EnableSecurity = "Enable authentication mechanisms for the project."
Events = "Manage events for your project."
Measurements = "Get measurements on the state of the MongoDB process."
)
40 changes: 40 additions & 0 deletions internal/fixtures/measurements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fixtures

import atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"

func ProcessMeasurements() *atlas.ProcessMeasurements {
return &atlas.ProcessMeasurements{
End: "2017-08-22T20:31:14Z",
Granularity: "PT1M",
GroupID: "12345678",
HostID: "shard-00-00.mongodb.net:27017",
Links: []*atlas.Link{
{
Rel: "self",
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017/measurements?granularity=PT1M&period=PT1M",
},
{
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017",
Rel: "http://mms.mongodb.com/host",
},
},
Measurements: []*atlas.Measurements{
{
DataPoints: []*atlas.DataPoints{
{
Timestamp: "2017-08-22T20:31:12Z",
Value: nil,
},
{
Timestamp: "2017-08-22T20:31:14Z",
Value: nil,
},
},
Name: "ASSERT_REGULAR",
Units: "SCALAR_PER_SECOND",
},
},
ProcessID: "shard-00-00.mongodb.net:27017",
Start: "2017-08-22T20:30:45Z",
}
}
5 changes: 5 additions & 0 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ const (
Username = "username" // Username flag
UsernameShort = "u" // UsernameShort flag
Password = "password" // Password flag
Period = "period" // Period flag
PasswordShort = "p" // PasswordShort flag
Email = "email" // Email flag
Status = "status" // Status flag
Start = "start" // Start flag
End = "end" // End flag
MeasurementType = "type" // MeasurementType flag
FirstName = "firstName" // FirstName flag
LastName = "lastName" // LastName flag
Role = "role" // Role flag
Expand Down Expand Up @@ -91,4 +95,5 @@ const (
ExpirationHours = "expirationHours" // ExpirationHours flag
MaxDate = "maxDate" // MaxDate flag
MinDate = "minDate" // MinDate flag
Granularity = "granularity" // Granularity flag
)
Loading