Skip to content

CLOUDP-61107: Expose patching the automation config #118

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 2 commits into from
Apr 21, 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
4 changes: 3 additions & 1 deletion internal/cli/ops_manager_automation.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func OpsManagerAutomationBuilder() *cobra.Command {
}

cmd.AddCommand(OpsManagerAutomationStatusBuilder())
cmd.AddCommand(OpsManagerAutomationShowBuilder())
cmd.AddCommand(OpsManagerAutomationDescribeBuilder())
cmd.AddCommand(OpsManagerAutomationUpdateBuilder())

return cmd
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ import (
"github.com/spf13/cobra"
)

type opsManagerAutomationShowOpts struct {
type opsManagerAutomationDescribeOpts struct {
globalOpts
store store.AutomationGetter
}

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

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

if err != nil {
Expand All @@ -43,13 +43,14 @@ func (opts *opsManagerAutomationShowOpts) Run() error {
return json.PrettyPrint(r)
}

// mongocli ops-manager automation show [--projectId projectId]
func OpsManagerAutomationShowBuilder() *cobra.Command {
opts := &opsManagerAutomationShowOpts{}
// mongocli ops-manager automation describe [--projectId projectId]
func OpsManagerAutomationDescribeBuilder() *cobra.Command {
opts := &opsManagerAutomationDescribeOpts{}
cmd := &cobra.Command{
Use: "show",
Args: cobra.NoArgs,
Hidden: true,
Use: "describe",
Aliases: []string{"show", "get"},
Args: cobra.NoArgs,
Hidden: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(opts.initStore)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestOpsManagerAutomationShowOpts_Run(t *testing.T) {

expected := fixtures.AutomationConfig()

opts := &opsManagerAutomationShowOpts{
opts := &opsManagerAutomationDescribeOpts{
store: mockStore,
}

Expand Down
84 changes: 84 additions & 0 deletions internal/cli/ops_manager_automation_update.go
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.

package cli

import (
"fmt"

"github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/description"
"github.com/mongodb/mongocli/internal/file"
"github.com/mongodb/mongocli/internal/flags"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)

type opsManagerAutomationUpdateOpts struct {
globalOpts
filename string
fs afero.Fs
store store.AutomationUpdater
}

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

func (opts *opsManagerAutomationUpdateOpts) Run() error {
newConfig := new(opsmngr.AutomationConfig)
err := file.Load(opts.fs, opts.filename, newConfig)
if err != nil {
return err
}

if err = opts.store.UpdateAutomationConfig(opts.ProjectID(), newConfig); err != nil {
return err
}

fmt.Print(deploymentStatus(config.OpsManagerURL(), opts.ProjectID()))

return nil
}

// mongocli om cluster(s) update --projectId projectId --file myfile.yaml
func OpsManagerAutomationUpdateBuilder() *cobra.Command {
opts := &opsManagerAutomationUpdateOpts{
fs: afero.NewOsFs(),
}
cmd := &cobra.Command{
Use: "update",
Short: description.UpdateOMCluster,
Hidden: true,
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().StringVarP(&opts.filename, flags.File, flags.FileShort, "", "Filename to use")

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

_ = cmd.MarkFlagRequired(flags.File)

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

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

defer ctrl.Finish()

expected := &opsmngr.AutomationConfig{
Version: 1,
}
appFS := afero.NewMemMapFs()
// create test file
fileJSON := `
{
"version": 1
}`
fileName := "test.json"
_ = afero.WriteFile(appFS, fileName, []byte(fileJSON), 0600)
createOpts := &opsManagerAutomationUpdateOpts{
store: mockStore,
fs: appFS,
filename: fileName,
}

mockStore.
EXPECT().
UpdateAutomationConfig(createOpts.projectID, expected).
Return(nil).
Times(1)

err := createOpts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}