diff --git a/internal/cli/ops_manager_automation.go b/internal/cli/ops_manager_automation.go index 10e3d18780..ec3596b83d 100644 --- a/internal/cli/ops_manager_automation.go +++ b/internal/cli/ops_manager_automation.go @@ -26,6 +26,8 @@ func OpsManagerAutomationBuilder() *cobra.Command { } cmd.AddCommand(OpsManagerAutomationStatusBuilder()) - cmd.AddCommand(OpsManagerAutomationShowBuilder()) + cmd.AddCommand(OpsManagerAutomationDescribeBuilder()) + cmd.AddCommand(OpsManagerAutomationUpdateBuilder()) + return cmd } diff --git a/internal/cli/ops_manager_automation_show.go b/internal/cli/ops_manager_automation_describe.go similarity index 75% rename from internal/cli/ops_manager_automation_show.go rename to internal/cli/ops_manager_automation_describe.go index f089431c3b..3547055d6b 100644 --- a/internal/cli/ops_manager_automation_show.go +++ b/internal/cli/ops_manager_automation_describe.go @@ -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 { @@ -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) }, diff --git a/internal/cli/ops_manager_automation_show_test.go b/internal/cli/ops_manager_automation_describe_test.go similarity index 96% rename from internal/cli/ops_manager_automation_show_test.go rename to internal/cli/ops_manager_automation_describe_test.go index 678d919532..b3b7889ce3 100644 --- a/internal/cli/ops_manager_automation_show_test.go +++ b/internal/cli/ops_manager_automation_describe_test.go @@ -30,7 +30,7 @@ func TestOpsManagerAutomationShowOpts_Run(t *testing.T) { expected := fixtures.AutomationConfig() - opts := &opsManagerAutomationShowOpts{ + opts := &opsManagerAutomationDescribeOpts{ store: mockStore, } diff --git a/internal/cli/ops_manager_automation_update.go b/internal/cli/ops_manager_automation_update.go new file mode 100644 index 0000000000..ccf1b7db35 --- /dev/null +++ b/internal/cli/ops_manager_automation_update.go @@ -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 +} diff --git a/internal/cli/ops_manager_automation_update_test.go b/internal/cli/ops_manager_automation_update_test.go new file mode 100644 index 0000000000..fda46eda81 --- /dev/null +++ b/internal/cli/ops_manager_automation_update_test.go @@ -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) + } +}