Skip to content

CLOUDP-59267: upgrade all agents in Org #143

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 10 commits into from
Apr 30, 2020
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mongodb/go-client-mongodb-atlas v0.2.1-0.20200427191133-b5e334932f44
github.com/mongodb/go-client-mongodb-ops-manager v0.2.2-0.20200429114541-a82d7d125cfd
github.com/mongodb/go-client-mongodb-ops-manager v0.2.2
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/spf13/afero v1.2.2
github.com/spf13/cast v1.3.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQz
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mongodb/go-client-mongodb-atlas v0.2.1-0.20200427191133-b5e334932f44 h1:PUOc5BwUVOqGVQqX5vs2MPiJJ6PEZ+jA7TYtna1gOLo=
github.com/mongodb/go-client-mongodb-atlas v0.2.1-0.20200427191133-b5e334932f44/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
github.com/mongodb/go-client-mongodb-ops-manager v0.2.2-0.20200429114541-a82d7d125cfd h1:vGYgslrNXKaZIkAIomQ83e6bpaByXIhwNXJgYN+yv8A=
github.com/mongodb/go-client-mongodb-ops-manager v0.2.2-0.20200429114541-a82d7d125cfd/go.mod h1:RrXnfBx3CyKN64jXkXFEO07CnMBEYa5CdlHOQ/30a5A=
github.com/mongodb/go-client-mongodb-ops-manager v0.2.2 h1:xQoyeLjRJmOjvCkjCwbnkzIvugpm6eNozDw4Auk84to=
github.com/mongodb/go-client-mongodb-ops-manager v0.2.2/go.mod h1:RrXnfBx3CyKN64jXkXFEO07CnMBEYa5CdlHOQ/30a5A=
github.com/mwielbut/pointy v1.1.0 h1:U5/YEfoIkaGCHv0St3CgjduqXID4FNRoyZgLM1kY9vg=
github.com/mwielbut/pointy v1.1.0/go.mod h1:MvvO+uMFj9T5DMda33HlvogsFBX7pWWKAkFIn4teYwY=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/atlas_dbusers_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (opts *atlasDBUsersListOpts) initStore() error {

func (opts *atlasDBUsersListOpts) Run() error {
listOpts := opts.newListOptions()
result, err := opts.store.DatabaseUsers(opts.projectID, listOpts)
result, err := opts.store.DatabaseUsers(opts.ProjectID(), listOpts)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions internal/cli/ops_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func OpsManagerBuilder() *cobra.Command {
cmd.AddCommand(OpsManagerProcessesBuilder())
cmd.AddCommand(OpsManagerMeasurementsBuilder())
cmd.AddCommand(OpsManagerLogsBuilder())
cmd.AddCommand(OpsManagerAgentsBuilder())

return cmd
}
31 changes: 31 additions & 0 deletions internal/cli/ops_manager_agents.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 OpsManagerAgentsBuilder() *cobra.Command {
cmd := &cobra.Command{
Use: "agents",
Aliases: []string{"agent"},
Short: description.Agents,
}

cmd.AddCommand(OpsManagerAgentsUpgradeBuilder())
return cmd
}
63 changes: 63 additions & 0 deletions internal/cli/ops_manager_agents_upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 opsManagerAgentsUpgradeOpts struct {
globalOpts
store store.AgentUpgrader
}

func (opts *opsManagerAgentsUpgradeOpts) initStore() error {
var err error
opts.store, err = store.New()
return err
}

func (opts *opsManagerAgentsUpgradeOpts) Run() error {
r, err := opts.store.UpgradeAgent(opts.ProjectID())

if err != nil {
return err
}

return json.PrettyPrint(r)
}

// mongocli ops-manager agents upgrade [--projectId projectId]
func OpsManagerAgentsUpgradeBuilder() *cobra.Command {
opts := &opsManagerAgentsUpgradeOpts{}
cmd := &cobra.Command{
Use: "upgrade",
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(opts.initStore)
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
},
}

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

return cmd
}
47 changes: 47 additions & 0 deletions internal/cli/ops_manager_agents_upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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"
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
"github.com/mongodb/mongocli/internal/mocks"
)

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

defer ctrl.Finish()

expected := new(om.AutomationConfigAgent)

opts := &opsManagerAgentsUpgradeOpts{
store: mockStore,
}

mockStore.
EXPECT().
UpgradeAgent(opts.projectID).
Return(expected, nil).
Times(1)

err := opts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
2 changes: 1 addition & 1 deletion internal/cli/ops_manager_automation_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (opts *opsManagerAutomationDescribeOpts) initStore() error {
}

func (opts *opsManagerAutomationDescribeOpts) Run() error {
r, err := opts.store.GetAutomationConfig(opts.projectID)
r, err := opts.store.GetAutomationConfig(opts.ProjectID())

if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion internal/cli/ops_manager_processes_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func OpsManagerProcessDescribeBuilder() *cobra.Command {
Use: "describe [processID]",
Short: description.ListProcesses,
Aliases: []string{"d"},
Hidden: true,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(opts.initStore)
Expand Down
1 change: 0 additions & 1 deletion internal/cli/ops_manager_processes_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func OpsManagerProcessListBuilder() *cobra.Command {
Use: "list",
Short: description.ListProcesses,
Aliases: []string{"ls"},
Hidden: true,
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(opts.initStore)
Expand Down
1 change: 1 addition & 0 deletions internal/description/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const (
ListProjects = "List projects."
DownloadLogs = "Download logs from a log collection job."
OpsManager = "Ops Manager operations."
Agents = "Manage Ops Manager agents."
ListGlobalAlerts = "List global alerts."
Automation = "Manage Ops Manager automation config."
ShowAutomationStatus = "Show the current status of the automation config."
Expand Down
38 changes: 38 additions & 0 deletions internal/mocks/mock_agents.go

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

15 changes: 15 additions & 0 deletions internal/store/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ type AgentLister interface {
Agents(string, string) (*om.Agents, error)
}

type AgentUpgrader interface {
UpgradeAgent(string) (*om.AutomationConfigAgent, error)
}

// Agents encapsulate the logic to manage different cloud providers
func (s *Store) Agents(projectID, agentType string) (*om.Agents, error) {
switch s.service {
Expand All @@ -35,3 +39,14 @@ func (s *Store) Agents(projectID, agentType string) (*om.Agents, error) {
return nil, fmt.Errorf("unsupported service: %s", s.service)
}
}

// UpgradeAgent encapsulate the logic to manage different cloud providers
func (s *Store) UpgradeAgent(projectID string) (*om.AutomationConfigAgent, error) {
switch s.service {
case config.OpsManagerService, config.CloudManagerService:
result, _, err := s.client.(*om.Client).AutomationConfig.UpdateAgent(context.Background(), projectID)
return result, err
default:
return nil, fmt.Errorf("unsupported service: %s", s.service)
}
}