-
Notifications
You must be signed in to change notification settings - Fork 87
CLOUDP-57631: Describe Atlas Alert #58
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2cb629e
CLOUDP-57631: Describe Atlas Alert
andreaangiolillo 8a33848
Merge branch 'master' into CLOUDP-57631
andreaangiolillo aec74bc
Merge remote-tracking branch 'origin/master' into CLOUDP-57631
andreaangiolillo 918fd99
Added e2e test
andreaangiolillo 8a5e322
Merge branch 'CLOUDP-57631' of github.com:mongodb/mongocli into CLOUD…
andreaangiolillo 7534f62
Refactoring
andreaangiolillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// 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. | ||
// +build e2e | ||
|
||
package e2e_test | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" | ||
) | ||
|
||
const ( | ||
closed = "CLOSED" | ||
replication_oplog_window_running_out = "REPLICATION_OPLOG_WINDOW_RUNNING_OUT" | ||
) | ||
|
||
func TestAtlasAlerts(t *testing.T) { | ||
cliPath, err := filepath.Abs("../bin/mongocli") | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
_, err = os.Stat(cliPath) | ||
|
||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
atlasEntity := "atlas" | ||
alertsEntity := "alerts" | ||
|
||
t.Run("Describe", func(t *testing.T) { | ||
alertID := "5e4d20ff5cc174527c22c606" | ||
|
||
cmd := exec.Command(cliPath, | ||
atlasEntity, | ||
alertsEntity, | ||
"describe", | ||
alertID, | ||
) | ||
|
||
cmd.Env = os.Environ() | ||
resp, err := cmd.CombinedOutput() | ||
|
||
if err != nil { | ||
t.Fatalf("unexpected error: %v, resp: %v", err, string(resp)) | ||
} | ||
|
||
alert := mongodbatlas.Alert{} | ||
err = json.Unmarshal(resp, &alert) | ||
|
||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
if alert.ID != alertID { | ||
t.Errorf("got=%#v\nwant=%#v\n", alert.ID, alertID) | ||
} | ||
|
||
if alert.Status != closed { | ||
t.Errorf("got=%#v\nwant=%#v\n", alert.Status, closed) | ||
} | ||
|
||
if alert.EventTypeName != replication_oplog_window_running_out { | ||
t.Errorf("got=%#v\nwant=%#v\n", alert.EventTypeName, replication_oplog_window_running_out) | ||
} | ||
|
||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// 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/flags" | ||
"github.com/mongodb/mongocli/internal/json" | ||
"github.com/mongodb/mongocli/internal/store" | ||
"github.com/mongodb/mongocli/internal/usage" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type atlasAlertsDescribeOpts struct { | ||
*globalOpts | ||
alertID string | ||
store store.AlertDescriber | ||
} | ||
|
||
func (opts *atlasAlertsDescribeOpts) init() error { | ||
if opts.ProjectID() == "" { | ||
return errMissingProjectID | ||
} | ||
var err error | ||
opts.store, err = store.New() | ||
return err | ||
} | ||
|
||
func (opts *atlasAlertsDescribeOpts) Run() error { | ||
result, err := opts.store.Alert(opts.ProjectID(), opts.alertID) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return json.PrettyPrint(result) | ||
} | ||
|
||
// mongocli atlas alerts describe alertID --projectId projectId | ||
func AtlasAlertsDescribeBuilder() *cobra.Command { | ||
opts := &atlasAlertsDescribeOpts{ | ||
globalOpts: newGlobalOpts(), | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "describe [alertID]", | ||
Short: "Describe an Atlas Alert.", | ||
Args: cobra.ExactArgs(1), | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return opts.init() | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
opts.alertID = args[0] | ||
return opts.Run() | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID) | ||
|
||
return cmd | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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 TestAtlasAlertsDescribe_Run(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockStore := mocks.NewMockAlertDescriber(ctrl) | ||
|
||
defer ctrl.Finish() | ||
|
||
expected := fixtures.Alert() | ||
|
||
describeOpts := &atlasAlertsDescribeOpts{ | ||
globalOpts: newGlobalOpts(), | ||
alertID: "533dc40ae4b00835ff81eaee", | ||
store: mockStore, | ||
} | ||
|
||
mockStore. | ||
EXPECT(). | ||
Alert(describeOpts.projectID, describeOpts.alertID). | ||
Return(expected, nil). | ||
Times(1) | ||
|
||
err := describeOpts.Run() | ||
if err != nil { | ||
t.Fatalf("Run() unexpected error: %v", err) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package fixtures | ||
|
||
import ( | ||
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" | ||
"github.com/mwielbut/pointy" | ||
) | ||
|
||
func Alert() *atlas.Alert { | ||
return &atlas.Alert{ | ||
ID: "533dc40ae4b00835ff81eaee", | ||
GroupID: "535683b3794d371327b", | ||
EventTypeName: "OUTSIDE_METRIC_THRESHOLD", | ||
Created: "2016-08-23T20:26:50Z", | ||
Updated: "2016-08-23T20:26:50Z", | ||
Enabled: pointy.Bool(true), | ||
Matchers: []atlas.Matcher{ | ||
{ | ||
FieldName: "HOSTNAME_AND_PORT", | ||
Operator: "EQUALS", | ||
Value: "mongo.example.com:27017", | ||
}, | ||
}, | ||
Notifications: []atlas.Notification{ | ||
{ | ||
TypeName: "SMS", | ||
IntervalMin: 5, | ||
DelayMin: pointy.Int(0), | ||
MobileNumber: "2343454567", | ||
}, | ||
}, | ||
MetricThreshold: &atlas.MetricThreshold{ | ||
MetricName: "ASSERT_REGULAR", | ||
Operator: "LESS_THAN", | ||
Threshold: 99.0, | ||
Units: "RAW", | ||
Mode: "AVERAGE", | ||
}, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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 store | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" | ||
"github.com/mongodb/mongocli/internal/config" | ||
) | ||
|
||
type AlertDescriber interface { | ||
Alert(string, string) (*atlas.Alert, error) | ||
} | ||
|
||
// Alert encapsulate the logic to manage different cloud providers | ||
func (s *Store) Alert(projectID, alertID string) (*atlas.Alert, error) { | ||
switch s.service { | ||
case config.CloudService: | ||
result, _, err := s.client.(*atlas.Client).Alerts.Get(context.Background(), projectID, alertID) | ||
return result, err | ||
default: | ||
return nil, fmt.Errorf("unsupported service: %s", s.service) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great on just implementing what we need, makes review way easier