From d8b778612f9412238b90261121c01229df942067 Mon Sep 17 00:00:00 2001 From: Colm Quinn Date: Wed, 15 Sep 2021 11:07:13 +0100 Subject: [PATCH 1/2] Adding validation job status command --- .../mongocli-atlas-liveMigrations-create.txt | 5 ++ ...atlas-liveMigrations-validation-create.txt | 5 ++ ...las-liveMigrations-validation-describe.txt | 70 +++++++++++++++++ ...ngocli-atlas-liveMigrations-validation.txt | 2 + .../options/live_migrations_opts.go | 2 + .../livemigrations/validation/describe.go | 78 +++++++++++++++++++ .../validation/describe_test.go | 62 +++++++++++++++ .../livemigrations/validation/validation.go | 1 + .../validation/validation_test.go | 2 +- internal/flag/flags.go | 1 + .../mocks/mock_live_migration_validations.go | 40 +++++++++- internal/store/live_migration_validations.go | 17 +++- internal/usage/usage.go | 1 + 13 files changed, 283 insertions(+), 3 deletions(-) create mode 100644 docs/command/mongocli-atlas-liveMigrations-validation-describe.txt create mode 100644 internal/cli/atlas/livemigrations/validation/describe.go create mode 100644 internal/cli/atlas/livemigrations/validation/describe_test.go diff --git a/docs/command/mongocli-atlas-liveMigrations-create.txt b/docs/command/mongocli-atlas-liveMigrations-create.txt index 3fe637d03f..9c3ec72da6 100644 --- a/docs/command/mongocli-atlas-liveMigrations-create.txt +++ b/docs/command/mongocli-atlas-liveMigrations-create.txt @@ -54,6 +54,11 @@ Options - string - false - Organization ID to use. Overrides configuration file or environment variable settings. + * - -o, --output + - string + - false + - Output format. + Valid values: json|json-path|go-template|go-template-file * - --sourceCACertificatePath - string - false diff --git a/docs/command/mongocli-atlas-liveMigrations-validation-create.txt b/docs/command/mongocli-atlas-liveMigrations-validation-create.txt index a976a239c6..3bda369311 100644 --- a/docs/command/mongocli-atlas-liveMigrations-validation-create.txt +++ b/docs/command/mongocli-atlas-liveMigrations-validation-create.txt @@ -54,6 +54,11 @@ Options - string - false - Organization ID to use. Overrides configuration file or environment variable settings. + * - -o, --output + - string + - false + - Output format. + Valid values: json|json-path|go-template|go-template-file * - --sourceCACertificatePath - string - false diff --git a/docs/command/mongocli-atlas-liveMigrations-validation-describe.txt b/docs/command/mongocli-atlas-liveMigrations-validation-describe.txt new file mode 100644 index 0000000000..6b546f95b8 --- /dev/null +++ b/docs/command/mongocli-atlas-liveMigrations-validation-describe.txt @@ -0,0 +1,70 @@ +.. _mongocli-atlas-liveMigrations-validation-describe: + +================================================= +mongocli atlas liveMigrations validation describe +================================================= + +.. default-domain:: mongodb + +.. contents:: On this page + :local: + :backlinks: none + :depth: 1 + :class: singlecol + +Return one validation job. + +Your API Key must have the Organization Owner role to successfully run this command. + +Syntax +------ + +.. code-block:: + + mongocli atlas liveMigrations validation describe [options] + +Options +------- + +.. list-table:: + :header-rows: 1 + :widths: 20 10 10 60 + + * - Name + - Type + - Required + - Description + * - -h, --help + - + - false + - help for describe + * - -o, --output + - string + - false + - Output format. + Valid values: json|json-path|go-template|go-template-file + * - --projectId + - string + - false + - Project ID to use. Overrides configuration file or environment variable settings. + * - --validationId + - string + - false + - Unique 24-hexadecimal digit string that identifies the validation job. + +Inherited Options +----------------- + +.. list-table:: + :header-rows: 1 + :widths: 20 10 10 60 + + * - Name + - Type + - Required + - Description + * - -P, --profile + - string + - false + - Profile to use from your configuration file. + diff --git a/docs/command/mongocli-atlas-liveMigrations-validation.txt b/docs/command/mongocli-atlas-liveMigrations-validation.txt index e80f74b276..12bd439f73 100644 --- a/docs/command/mongocli-atlas-liveMigrations-validation.txt +++ b/docs/command/mongocli-atlas-liveMigrations-validation.txt @@ -50,10 +50,12 @@ Related Commands ---------------- * :ref:`mongocli-atlas-liveMigrations-validation-create` - Create one new validation request. +* :ref:`mongocli-atlas-liveMigrations-validation-describe` - Return one validation job. .. toctree:: :titlesonly: create + describe diff --git a/internal/cli/atlas/livemigrations/options/live_migrations_opts.go b/internal/cli/atlas/livemigrations/options/live_migrations_opts.go index 3fcc3ff1c1..565e46d3e9 100644 --- a/internal/cli/atlas/livemigrations/options/live_migrations_opts.go +++ b/internal/cli/atlas/livemigrations/options/live_migrations_opts.go @@ -28,6 +28,7 @@ import ( ) type LiveMigrationsOpts struct { + cli.OutputOpts cli.GlobalOpts DestinationClusterName string DestinationDropEnabled bool @@ -138,6 +139,7 @@ func (opts *LiveMigrationsOpts) GenerateFlags(cmd *cobra.Command) { cmd.Flags().StringVar(&opts.DestinationClusterName, flag.ClusterName, "", usage.LiveMigrationDestinationClusterName) cmd.Flags().StringSliceVar(&opts.MigrationHosts, flag.LiveMigrationHost, []string{}, usage.LiveMigrationHostEntries) cmd.Flags().BoolVar(&opts.DestinationDropEnabled, flag.LiveMigrationDropCollections, false, usage.LiveMigrationDropCollections) + cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut) _ = cmd.MarkFlagRequired(flag.LiveMigrationSourceClusterName) _ = cmd.MarkFlagRequired(flag.LiveMigrationSourceProjectID) diff --git a/internal/cli/atlas/livemigrations/validation/describe.go b/internal/cli/atlas/livemigrations/validation/describe.go new file mode 100644 index 0000000000..81aee7d40b --- /dev/null +++ b/internal/cli/atlas/livemigrations/validation/describe.go @@ -0,0 +1,78 @@ +// Copyright 2021 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 validation + +import ( + "github.com/mongodb/mongocli/internal/cli" + "github.com/mongodb/mongocli/internal/config" + "github.com/mongodb/mongocli/internal/flag" + "github.com/mongodb/mongocli/internal/store" + "github.com/mongodb/mongocli/internal/usage" + "github.com/spf13/cobra" +) + +type DescribeOpts struct { + cli.GlobalOpts + cli.OutputOpts + validationID string + store store.LiveMigrationValidationsDescriber +} + +func (opts *DescribeOpts) initStore() error { + var err error + opts.store, err = store.New(store.AuthenticatedPreset(config.Default())) + return err +} + +var describeTemplate = `ID PROJECT ID SOURCE PROJECT ID STATUS +{{.ID}} {{.GroupID}} {{.SourceGroupID}} {{.Status}}` + +func (opts *DescribeOpts) Run() error { + r, err := opts.store.GetValidationStatus(opts.ConfigProjectID(), opts.validationID) + + if err != nil { + return err + } + + return opts.Print(r) +} + +// mongocli atlas liveMigrations|lm validation(s) describe|get [--projectId projectId]. + +func DescribeBuilder() *cobra.Command { + opts := new(DescribeOpts) + cmd := &cobra.Command{ + Use: "describe", + Aliases: []string{"get"}, + Short: "Return one validation job.", + Long: "Your API Key must have the Organization Owner role to successfully run this command.", + PreRunE: func(cmd *cobra.Command, args []string) error { + return opts.PreRunE( + opts.ValidateProjectID, + opts.initStore, + opts.InitOutput(cmd.OutOrStdout(), describeTemplate), + ) + }, + RunE: func(cmd *cobra.Command, args []string) error { + return opts.Run() + }, + } + + cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID) + cmd.Flags().StringVar(&opts.validationID, flag.LiveMigrationValidationID, "", usage.LiveMigrationValidationID) + cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut) + + return cmd +} diff --git a/internal/cli/atlas/livemigrations/validation/describe_test.go b/internal/cli/atlas/livemigrations/validation/describe_test.go new file mode 100644 index 0000000000..18ed801795 --- /dev/null +++ b/internal/cli/atlas/livemigrations/validation/describe_test.go @@ -0,0 +1,62 @@ +// Copyright 2021 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. + +//go:build unit + +package validation + +import ( + "testing" + + "github.com/golang/mock/gomock" + "github.com/mongodb/mongocli/internal/cli" + "github.com/mongodb/mongocli/internal/flag" + "github.com/mongodb/mongocli/internal/mocks" + "github.com/mongodb/mongocli/internal/test" + "go.mongodb.org/atlas/mongodbatlas" +) + +func TestValidationDescribeOpts_Run(t *testing.T) { + ctrl := gomock.NewController(t) + mockStore := mocks.NewMockLiveMigrationValidationsDescriber(ctrl) + defer ctrl.Finish() + + expected := mongodbatlas.Validation{} + + describeOpts := &DescribeOpts{ + GlobalOpts: cli.GlobalOpts{ProjectID: "2"}, + validationID: "3", + store: mockStore, + } + + mockStore. + EXPECT().GetValidationStatus(describeOpts.ProjectID, describeOpts.validationID).Return(&expected, nil). + Times(1) + + if err := describeOpts.Run(); err != nil { + t.Fatalf("Run() unexpected error: %v", err) + } +} + +func TestDescribeBuilder(t *testing.T) { + test.CmdValidator( + t, + DescribeBuilder(), + 0, + []string{ + flag.ProjectID, + flag.LiveMigrationValidationID, + }, + ) +} diff --git a/internal/cli/atlas/livemigrations/validation/validation.go b/internal/cli/atlas/livemigrations/validation/validation.go index f90cd3b3b0..9f431e5ac4 100644 --- a/internal/cli/atlas/livemigrations/validation/validation.go +++ b/internal/cli/atlas/livemigrations/validation/validation.go @@ -26,6 +26,7 @@ func Builder() *cobra.Command { cmd.AddCommand( CreateBuilder(), + DescribeBuilder(), ) return cmd diff --git a/internal/cli/atlas/livemigrations/validation/validation_test.go b/internal/cli/atlas/livemigrations/validation/validation_test.go index edd229c068..5cafcec338 100644 --- a/internal/cli/atlas/livemigrations/validation/validation_test.go +++ b/internal/cli/atlas/livemigrations/validation/validation_test.go @@ -27,7 +27,7 @@ func TestBuilder(t *testing.T) { test.CmdValidator( t, Builder(), - 1, + 2, []string{}, ) } diff --git a/internal/flag/flags.go b/internal/flag/flags.go index d60c5a9620..7a8c5c02e1 100644 --- a/internal/flag/flags.go +++ b/internal/flag/flags.go @@ -261,6 +261,7 @@ const ( LiveMigrationSourceUsername = "sourceUsername" // LiveMigrationSourceUsername flag LiveMigrationSourcePassword = "sourcePassword" // LiveMigrationSourcePassword flag LiveMigrationDropCollections = "drop" // LiveMigrationDropCollections flag + LiveMigrationValidationID = "validationId" // LiveMigrationDropCollections flag OwnerID = "ownerId" // OwnerID Flag LinkToken = "linkToken" // LinkToken flag ) diff --git a/internal/mocks/mock_live_migration_validations.go b/internal/mocks/mock_live_migration_validations.go index ac0e975be1..07c08cb400 100644 --- a/internal/mocks/mock_live_migration_validations.go +++ b/internal/mocks/mock_live_migration_validations.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: github.com/mongodb/mongocli/internal/store (interfaces: LiveMigrationValidationsCreator) +// Source: github.com/mongodb/mongocli/internal/store (interfaces: LiveMigrationValidationsCreator,LiveMigrationValidationsDescriber) // Package mocks is a generated GoMock package. package mocks @@ -48,3 +48,41 @@ func (mr *MockLiveMigrationValidationsCreatorMockRecorder) CreateValidation(arg0 mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateValidation", reflect.TypeOf((*MockLiveMigrationValidationsCreator)(nil).CreateValidation), arg0, arg1) } + +// MockLiveMigrationValidationsDescriber is a mock of LiveMigrationValidationsDescriber interface. +type MockLiveMigrationValidationsDescriber struct { + ctrl *gomock.Controller + recorder *MockLiveMigrationValidationsDescriberMockRecorder +} + +// MockLiveMigrationValidationsDescriberMockRecorder is the mock recorder for MockLiveMigrationValidationsDescriber. +type MockLiveMigrationValidationsDescriberMockRecorder struct { + mock *MockLiveMigrationValidationsDescriber +} + +// NewMockLiveMigrationValidationsDescriber creates a new mock instance. +func NewMockLiveMigrationValidationsDescriber(ctrl *gomock.Controller) *MockLiveMigrationValidationsDescriber { + mock := &MockLiveMigrationValidationsDescriber{ctrl: ctrl} + mock.recorder = &MockLiveMigrationValidationsDescriberMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockLiveMigrationValidationsDescriber) EXPECT() *MockLiveMigrationValidationsDescriberMockRecorder { + return m.recorder +} + +// GetValidationStatus mocks base method. +func (m *MockLiveMigrationValidationsDescriber) GetValidationStatus(arg0, arg1 string) (*mongodbatlas.Validation, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetValidationStatus", arg0, arg1) + ret0, _ := ret[0].(*mongodbatlas.Validation) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetValidationStatus indicates an expected call of GetValidationStatus. +func (mr *MockLiveMigrationValidationsDescriberMockRecorder) GetValidationStatus(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetValidationStatus", reflect.TypeOf((*MockLiveMigrationValidationsDescriber)(nil).GetValidationStatus), arg0, arg1) +} diff --git a/internal/store/live_migration_validations.go b/internal/store/live_migration_validations.go index 953ae1a2b6..640b953f96 100644 --- a/internal/store/live_migration_validations.go +++ b/internal/store/live_migration_validations.go @@ -22,12 +22,16 @@ import ( atlas "go.mongodb.org/atlas/mongodbatlas" ) -//go:generate mockgen -destination=../mocks/mock_live_migration_validations.go -package=mocks github.com/mongodb/mongocli/internal/store LiveMigrationValidationsCreator +//go:generate mockgen -destination=../mocks/mock_live_migration_validations.go -package=mocks github.com/mongodb/mongocli/internal/store LiveMigrationValidationsCreator,LiveMigrationValidationsDescriber type LiveMigrationValidationsCreator interface { CreateValidation(string, *atlas.LiveMigration) (*atlas.Validation, error) } +type LiveMigrationValidationsDescriber interface { + GetValidationStatus(string, string) (*atlas.Validation, error) +} + // CreateValidation encapsulate the logic to manage different cloud providers. func (s *Store) CreateValidation(groupID string, liveMigration *atlas.LiveMigration) (*atlas.Validation, error) { switch s.service { @@ -38,3 +42,14 @@ func (s *Store) CreateValidation(groupID string, liveMigration *atlas.LiveMigrat return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service) } } + +// GetValidationStatus encapsulate the logic to manage different cloud providers. +func (s *Store) GetValidationStatus(groupID, liveMigrationID string) (*atlas.Validation, error) { + switch s.service { + case config.CloudService: + result, _, err := s.client.(*atlas.Client).LiveMigration.GetValidationStatus(context.Background(), groupID, liveMigrationID) + return result, err + default: + return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service) + } +} diff --git a/internal/usage/usage.go b/internal/usage/usage.go index e603bbf039..f243055d18 100644 --- a/internal/usage/usage.go +++ b/internal/usage/usage.go @@ -329,4 +329,5 @@ Valid values: 6|8|12|24.` LiveMigrationSourceUsername = "Human-readable label that identifies the SCRAM-SHA user that connects to the source Cloud Manager or Ops Manager cluster. Omit this value if --sourceManagedAuthentication is set." LiveMigrationSourcePassword = "Password that authenticates the username to the source Cloud Manager or Ops Manager cluster. Omit this value if --sourceManagedAuthentication is passed." LiveMigrationDropCollections = "Flag that indicates whether this process should drop existing collections from the destination (Atlas) cluster given in --destinationClusterName before starting the migration of data from the source cluster." + LiveMigrationValidationID = "Unique 24-hexadecimal digit string that identifies the validation job." ) From 2152b579e45b0a5fd6f6c018baa1088f280d1569 Mon Sep 17 00:00:00 2001 From: Colm Quinn Date: Thu, 16 Sep 2021 10:47:53 +0100 Subject: [PATCH 2/2] Removing output opts from migration create commands --- internal/cli/atlas/livemigrations/create.go | 2 -- internal/cli/atlas/livemigrations/validation/create.go | 2 -- 2 files changed, 4 deletions(-) diff --git a/internal/cli/atlas/livemigrations/create.go b/internal/cli/atlas/livemigrations/create.go index 97d3363704..9f1c653ca5 100644 --- a/internal/cli/atlas/livemigrations/create.go +++ b/internal/cli/atlas/livemigrations/create.go @@ -15,7 +15,6 @@ package livemigrations import ( - "github.com/mongodb/mongocli/internal/cli" "github.com/mongodb/mongocli/internal/cli/atlas/livemigrations/options" "github.com/mongodb/mongocli/internal/config" "github.com/mongodb/mongocli/internal/store" @@ -23,7 +22,6 @@ import ( ) type CreateOpts struct { - cli.OutputOpts options.LiveMigrationsOpts store store.LiveMigrationCreator } diff --git a/internal/cli/atlas/livemigrations/validation/create.go b/internal/cli/atlas/livemigrations/validation/create.go index 64c763e637..b3a46e77b7 100644 --- a/internal/cli/atlas/livemigrations/validation/create.go +++ b/internal/cli/atlas/livemigrations/validation/create.go @@ -15,7 +15,6 @@ package validation import ( - "github.com/mongodb/mongocli/internal/cli" "github.com/mongodb/mongocli/internal/cli/atlas/livemigrations/options" "github.com/mongodb/mongocli/internal/config" "github.com/mongodb/mongocli/internal/store" @@ -23,7 +22,6 @@ import ( ) type CreateOpts struct { - cli.OutputOpts options.LiveMigrationsOpts store store.LiveMigrationValidationsCreator }