From dcfcf711cf46a620a485e99489a9a800c122939e Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Fri, 16 Oct 2020 08:51:53 +0100 Subject: [PATCH 1/4] Implemented host list command --- internal/cli/global_opts.go | 6 +- .../organizations/hosts/description.go | 19 ++++ .../serverusage/organizations/hosts/hosts.go | 35 +++++++ .../organizations/hosts/hosts_test.go | 31 +++++++ .../serverusage/organizations/hosts/list.go | 93 +++++++++++++++++++ .../organizations/hosts/list_test.go | 59 ++++++++++++ .../organizations/organizations.go | 2 + .../organizations/organizations_test.go | 2 +- .../serverusage/projects/hosts/description.go | 19 ++++ .../serverusage/projects/hosts/hosts.go | 35 +++++++ .../serverusage/projects/hosts/hosts_test.go | 31 +++++++ .../serverusage/projects/hosts/list.go | 93 +++++++++++++++++++ .../serverusage/projects/hosts/list_test.go | 59 ++++++++++++ .../serverusage/projects/projects.go | 2 + .../serverusage/projects/projects_test.go | 2 +- internal/mocks/mock_server_usage.go | 78 +++++++++++++++- internal/store/server_usage.go | 32 ++++++- internal/usage/usage.go | 2 + 18 files changed, 593 insertions(+), 7 deletions(-) create mode 100644 internal/cli/opsmanager/serverusage/organizations/hosts/description.go create mode 100644 internal/cli/opsmanager/serverusage/organizations/hosts/hosts.go create mode 100644 internal/cli/opsmanager/serverusage/organizations/hosts/hosts_test.go create mode 100644 internal/cli/opsmanager/serverusage/organizations/hosts/list.go create mode 100644 internal/cli/opsmanager/serverusage/organizations/hosts/list_test.go create mode 100644 internal/cli/opsmanager/serverusage/projects/hosts/description.go create mode 100644 internal/cli/opsmanager/serverusage/projects/hosts/hosts.go create mode 100644 internal/cli/opsmanager/serverusage/projects/hosts/hosts_test.go create mode 100644 internal/cli/opsmanager/serverusage/projects/hosts/list.go create mode 100644 internal/cli/opsmanager/serverusage/projects/hosts/list_test.go diff --git a/internal/cli/global_opts.go b/internal/cli/global_opts.go index 827f36bbdc..8da8f67292 100644 --- a/internal/cli/global_opts.go +++ b/internal/cli/global_opts.go @@ -56,9 +56,9 @@ func (opts *GlobalOpts) PreRunE(cbs ...cmdOpt) error { if opts.ConfigProjectID() == "" { return errMissingProjectID } - if err := validate.ObjectID(opts.ConfigProjectID()); err != nil { - return err - } + //if err := validate.ObjectID(opts.ConfigProjectID()); err != nil { + // return err + //} for _, f := range cbs { if err := f(); err != nil { return err diff --git a/internal/cli/opsmanager/serverusage/organizations/hosts/description.go b/internal/cli/opsmanager/serverusage/organizations/hosts/description.go new file mode 100644 index 0000000000..05505821bf --- /dev/null +++ b/internal/cli/opsmanager/serverusage/organizations/hosts/description.go @@ -0,0 +1,19 @@ +// 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 hosts + +const ( + short = "Manage host assignment for a project." + list = "List all host assignments for one project." +) diff --git a/internal/cli/opsmanager/serverusage/organizations/hosts/hosts.go b/internal/cli/opsmanager/serverusage/organizations/hosts/hosts.go new file mode 100644 index 0000000000..d5501e01f3 --- /dev/null +++ b/internal/cli/opsmanager/serverusage/organizations/hosts/hosts.go @@ -0,0 +1,35 @@ +// 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 hosts + +import ( + "github.com/mongodb/mongocli/internal/cli" + "github.com/spf13/cobra" +) + +func Builder() *cobra.Command { + const use = "hosts" + cmd := &cobra.Command{ + Use: use, + Aliases: cli.GenerateAliases(use), + Short: short, + } + + cmd.AddCommand( + ListBuilder(), + ) + + return cmd +} diff --git a/internal/cli/opsmanager/serverusage/organizations/hosts/hosts_test.go b/internal/cli/opsmanager/serverusage/organizations/hosts/hosts_test.go new file mode 100644 index 0000000000..e1119bf680 --- /dev/null +++ b/internal/cli/opsmanager/serverusage/organizations/hosts/hosts_test.go @@ -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. +// +build unit + +package hosts + +import ( + "testing" + + "github.com/mongodb/mongocli/internal/cli" +) + +func TestBuilder(t *testing.T) { + cli.CmdValidator( + t, + Builder(), + 1, + []string{}, + ) +} diff --git a/internal/cli/opsmanager/serverusage/organizations/hosts/list.go b/internal/cli/opsmanager/serverusage/organizations/hosts/list.go new file mode 100644 index 0000000000..d83182998d --- /dev/null +++ b/internal/cli/opsmanager/serverusage/organizations/hosts/list.go @@ -0,0 +1,93 @@ +// 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 hosts + +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" + "go.mongodb.org/ops-manager/opsmngr" +) + +type ListOpts struct { + cli.GlobalOpts + cli.OutputOpts + cli.ListOpts + store store.OrganizationHostAssignmentLister + startDate string + endDate string +} + +func (opts *ListOpts) initStore() error { + var err error + opts.store, err = store.New(config.Default()) + return err +} + +var listTemplate = `HOSTNAME SERVER TYPE MEM SIZE MB CHARGEABLE{{- range .Results}} +{{.Hostname}} {{.ServerType.Name}} {{.MemSizeMB}} {{.IsChargeable}}{{end}} +` + +func (opts *ListOpts) Run() error { + r, err := opts.store.OrganizationHostAssignments(opts.ConfigOrgID(), opts.newServerTypeOptions()) + if err != nil { + return err + } + + return opts.Print(r) +} + +func (opts *ListOpts) newServerTypeOptions() *opsmngr.ServerTypeOptions { + return &opsmngr.ServerTypeOptions{ + ListOptions: *opts.NewListOptions(), + StartDate: opts.startDate, + EndDate: opts.endDate, + } +} + +// mongocli om serverUsage project(s) hosts list [--orgId orgId] +func ListBuilder() *cobra.Command { + opts := &ListOpts{} + cmd := &cobra.Command{ + Use: "list", + Aliases: []string{"ls"}, + Short: list, + PreRunE: func(cmd *cobra.Command, args []string) error { + return opts.PreRunEOrg( + opts.initStore, + opts.InitOutput(cmd.OutOrStdout(), listTemplate), + ) + }, + RunE: func(cmd *cobra.Command, args []string) error { + return opts.Run() + }, + } + + cmd.Flags().StringVar(&opts.startDate, flag.StartDate, "", usage.ServerUsageStartDate) + cmd.Flags().StringVar(&opts.endDate, flag.EndDate, "", usage.ServerUsageEndDate) + + cmd.Flags().IntVar(&opts.PageNum, flag.Page, 0, usage.Page) + cmd.Flags().IntVar(&opts.ItemsPerPage, flag.Limit, 0, usage.Limit) + + cmd.Flags().StringVar(&opts.OrgID, flag.OrgID, "", usage.OrgID) + cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut) + + _ = cmd.MarkFlagRequired(flag.StartDate) + _ = cmd.MarkFlagRequired(flag.EndDate) + + return cmd +} diff --git a/internal/cli/opsmanager/serverusage/organizations/hosts/list_test.go b/internal/cli/opsmanager/serverusage/organizations/hosts/list_test.go new file mode 100644 index 0000000000..8b8ecdde3d --- /dev/null +++ b/internal/cli/opsmanager/serverusage/organizations/hosts/list_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. + +// +build unit + +package hosts + +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" + "go.mongodb.org/ops-manager/opsmngr" +) + +func TestList_Run(t *testing.T) { + ctrl := gomock.NewController(t) + mockStore := mocks.NewMockOrganizationHostAssignmentLister(ctrl) + defer ctrl.Finish() + + expected := &opsmngr.HostAssignments{} + + listOpts := ListOpts{ + store: mockStore, + } + + mockStore. + EXPECT(). + OrganizationHostAssignments(listOpts.ProjectID, listOpts.newServerTypeOptions()). + Return(expected, nil). + Times(1) + + err := listOpts.Run() + if err != nil { + t.Fatalf("Run() unexpected error: %v", err) + } +} + +func TestListBuilder(t *testing.T) { + cli.CmdValidator( + t, + ListBuilder(), + 0, + []string{flag.OrgID, flag.Output, flag.Page, flag.Limit, flag.EndDate, flag.StartDate}, + ) +} diff --git a/internal/cli/opsmanager/serverusage/organizations/organizations.go b/internal/cli/opsmanager/serverusage/organizations/organizations.go index 1c6223f554..c4a04ea741 100644 --- a/internal/cli/opsmanager/serverusage/organizations/organizations.go +++ b/internal/cli/opsmanager/serverusage/organizations/organizations.go @@ -17,6 +17,7 @@ package organizations import ( "github.com/mongodb/mongocli/internal/cli" "github.com/mongodb/mongocli/internal/cli/opsmanager/serverusage/organizations/servertype" + "github.com/mongodb/mongocli/internal/cli/opsmanager/serverusage/projects/hosts" "github.com/spf13/cobra" ) @@ -30,6 +31,7 @@ func Builder() *cobra.Command { cmd.AddCommand( servertype.Builder(), + hosts.Builder(), ) return cmd diff --git a/internal/cli/opsmanager/serverusage/organizations/organizations_test.go b/internal/cli/opsmanager/serverusage/organizations/organizations_test.go index 40d94b820c..d3f12bd3ea 100644 --- a/internal/cli/opsmanager/serverusage/organizations/organizations_test.go +++ b/internal/cli/opsmanager/serverusage/organizations/organizations_test.go @@ -25,7 +25,7 @@ func TestListBuilder(t *testing.T) { cli.CmdValidator( t, Builder(), - 1, + 2, []string{}, ) } diff --git a/internal/cli/opsmanager/serverusage/projects/hosts/description.go b/internal/cli/opsmanager/serverusage/projects/hosts/description.go new file mode 100644 index 0000000000..05505821bf --- /dev/null +++ b/internal/cli/opsmanager/serverusage/projects/hosts/description.go @@ -0,0 +1,19 @@ +// 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 hosts + +const ( + short = "Manage host assignment for a project." + list = "List all host assignments for one project." +) diff --git a/internal/cli/opsmanager/serverusage/projects/hosts/hosts.go b/internal/cli/opsmanager/serverusage/projects/hosts/hosts.go new file mode 100644 index 0000000000..d5501e01f3 --- /dev/null +++ b/internal/cli/opsmanager/serverusage/projects/hosts/hosts.go @@ -0,0 +1,35 @@ +// 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 hosts + +import ( + "github.com/mongodb/mongocli/internal/cli" + "github.com/spf13/cobra" +) + +func Builder() *cobra.Command { + const use = "hosts" + cmd := &cobra.Command{ + Use: use, + Aliases: cli.GenerateAliases(use), + Short: short, + } + + cmd.AddCommand( + ListBuilder(), + ) + + return cmd +} diff --git a/internal/cli/opsmanager/serverusage/projects/hosts/hosts_test.go b/internal/cli/opsmanager/serverusage/projects/hosts/hosts_test.go new file mode 100644 index 0000000000..e1119bf680 --- /dev/null +++ b/internal/cli/opsmanager/serverusage/projects/hosts/hosts_test.go @@ -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. +// +build unit + +package hosts + +import ( + "testing" + + "github.com/mongodb/mongocli/internal/cli" +) + +func TestBuilder(t *testing.T) { + cli.CmdValidator( + t, + Builder(), + 1, + []string{}, + ) +} diff --git a/internal/cli/opsmanager/serverusage/projects/hosts/list.go b/internal/cli/opsmanager/serverusage/projects/hosts/list.go new file mode 100644 index 0000000000..e5c7f1ea34 --- /dev/null +++ b/internal/cli/opsmanager/serverusage/projects/hosts/list.go @@ -0,0 +1,93 @@ +// 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 hosts + +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" + "go.mongodb.org/ops-manager/opsmngr" +) + +type ListOpts struct { + cli.GlobalOpts + cli.OutputOpts + cli.ListOpts + store store.ProjectHostAssignmentLister + startDate string + endDate string +} + +func (opts *ListOpts) initStore() error { + var err error + opts.store, err = store.New(config.Default()) + return err +} + +var listTemplate = `HOSTNAME SERVER TYPE MEM SIZE MB CHARGEABLE{{- range .Results}} +{{.Hostname}} {{.ServerType.Name}} {{.MemSizeMB}} {{.IsChargeable}}{{end}} +` + +func (opts *ListOpts) Run() error { + r, err := opts.store.ProjectHostAssignments(opts.ConfigProjectID(), opts.newServerTypeOptions()) + if err != nil { + return err + } + + return opts.Print(r) +} + +func (opts *ListOpts) newServerTypeOptions() *opsmngr.ServerTypeOptions { + return &opsmngr.ServerTypeOptions{ + ListOptions: *opts.NewListOptions(), + StartDate: opts.startDate, + EndDate: opts.endDate, + } +} + +// mongocli om serverUsage project(s) hosts list [--projectId projectId] +func ListBuilder() *cobra.Command { + opts := &ListOpts{} + cmd := &cobra.Command{ + Use: "list", + Aliases: []string{"ls"}, + Short: list, + PreRunE: func(cmd *cobra.Command, args []string) error { + return opts.PreRunE( + opts.initStore, + opts.InitOutput(cmd.OutOrStdout(), listTemplate), + ) + }, + RunE: func(cmd *cobra.Command, args []string) error { + return opts.Run() + }, + } + + cmd.Flags().StringVar(&opts.startDate, flag.StartDate, "", usage.ServerUsageStartDate) + cmd.Flags().StringVar(&opts.endDate, flag.EndDate, "", usage.ServerUsageEndDate) + + cmd.Flags().IntVar(&opts.PageNum, flag.Page, 0, usage.Page) + cmd.Flags().IntVar(&opts.ItemsPerPage, flag.Limit, 0, usage.Limit) + + cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID) + cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut) + + _ = cmd.MarkFlagRequired(flag.StartDate) + _ = cmd.MarkFlagRequired(flag.EndDate) + + return cmd +} diff --git a/internal/cli/opsmanager/serverusage/projects/hosts/list_test.go b/internal/cli/opsmanager/serverusage/projects/hosts/list_test.go new file mode 100644 index 0000000000..9562171f03 --- /dev/null +++ b/internal/cli/opsmanager/serverusage/projects/hosts/list_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. + +// +build unit + +package hosts + +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" + "go.mongodb.org/ops-manager/opsmngr" +) + +func TestList_Run(t *testing.T) { + ctrl := gomock.NewController(t) + mockStore := mocks.NewMockProjectHostAssignmentLister(ctrl) + defer ctrl.Finish() + + expected := &opsmngr.HostAssignments{} + + listOpts := ListOpts{ + store: mockStore, + } + + mockStore. + EXPECT(). + ProjectHostAssignments(listOpts.ProjectID, listOpts.newServerTypeOptions()). + Return(expected, nil). + Times(1) + + err := listOpts.Run() + if err != nil { + t.Fatalf("Run() unexpected error: %v", err) + } +} + +func TestListBuilder(t *testing.T) { + cli.CmdValidator( + t, + ListBuilder(), + 0, + []string{flag.ProjectID, flag.Output, flag.Page, flag.Limit, flag.EndDate, flag.StartDate}, + ) +} diff --git a/internal/cli/opsmanager/serverusage/projects/projects.go b/internal/cli/opsmanager/serverusage/projects/projects.go index bfa53002f2..64250db01e 100644 --- a/internal/cli/opsmanager/serverusage/projects/projects.go +++ b/internal/cli/opsmanager/serverusage/projects/projects.go @@ -16,6 +16,7 @@ package projects import ( "github.com/mongodb/mongocli/internal/cli" + "github.com/mongodb/mongocli/internal/cli/opsmanager/serverusage/projects/hosts" "github.com/mongodb/mongocli/internal/cli/opsmanager/serverusage/projects/servertype" "github.com/spf13/cobra" ) @@ -30,6 +31,7 @@ func Builder() *cobra.Command { cmd.AddCommand( servertype.Builder(), + hosts.Builder(), ) return cmd diff --git a/internal/cli/opsmanager/serverusage/projects/projects_test.go b/internal/cli/opsmanager/serverusage/projects/projects_test.go index 91fc997fe5..27bf1a35f5 100644 --- a/internal/cli/opsmanager/serverusage/projects/projects_test.go +++ b/internal/cli/opsmanager/serverusage/projects/projects_test.go @@ -25,7 +25,7 @@ func TestListBuilder(t *testing.T) { cli.CmdValidator( t, Builder(), - 1, + 2, []string{}, ) } diff --git a/internal/mocks/mock_server_usage.go b/internal/mocks/mock_server_usage.go index 1bfde1dca8..14c4141df2 100644 --- a/internal/mocks/mock_server_usage.go +++ b/internal/mocks/mock_server_usage.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: github.com/mongodb/mongocli/internal/store (interfaces: ProjectServerTypeGetter,ProjectServerTypeUpdater,OrganizationServerTypeGetter,OrganizationServerTypeUpdater) +// Source: github.com/mongodb/mongocli/internal/store (interfaces: ProjectServerTypeGetter,ProjectServerTypeUpdater,OrganizationServerTypeGetter,OrganizationServerTypeUpdater,ProjectHostAssignmentLister,OrganizationHostAssignmentLister) // Package mocks is a generated GoMock package. package mocks @@ -159,3 +159,79 @@ func (mr *MockOrganizationServerTypeUpdaterMockRecorder) UpdateOrganizationServe mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateOrganizationServerType", reflect.TypeOf((*MockOrganizationServerTypeUpdater)(nil).UpdateOrganizationServerType), arg0, arg1) } + +// MockProjectHostAssignmentLister is a mock of ProjectHostAssignmentLister interface +type MockProjectHostAssignmentLister struct { + ctrl *gomock.Controller + recorder *MockProjectHostAssignmentListerMockRecorder +} + +// MockProjectHostAssignmentListerMockRecorder is the mock recorder for MockProjectHostAssignmentLister +type MockProjectHostAssignmentListerMockRecorder struct { + mock *MockProjectHostAssignmentLister +} + +// NewMockProjectHostAssignmentLister creates a new mock instance +func NewMockProjectHostAssignmentLister(ctrl *gomock.Controller) *MockProjectHostAssignmentLister { + mock := &MockProjectHostAssignmentLister{ctrl: ctrl} + mock.recorder = &MockProjectHostAssignmentListerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockProjectHostAssignmentLister) EXPECT() *MockProjectHostAssignmentListerMockRecorder { + return m.recorder +} + +// ProjectHostAssignments mocks base method +func (m *MockProjectHostAssignmentLister) ProjectHostAssignments(arg0 string, arg1 *opsmngr.ServerTypeOptions) (*opsmngr.HostAssignments, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ProjectHostAssignments", arg0, arg1) + ret0, _ := ret[0].(*opsmngr.HostAssignments) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ProjectHostAssignments indicates an expected call of ProjectHostAssignments +func (mr *MockProjectHostAssignmentListerMockRecorder) ProjectHostAssignments(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ProjectHostAssignments", reflect.TypeOf((*MockProjectHostAssignmentLister)(nil).ProjectHostAssignments), arg0, arg1) +} + +// MockOrganizationHostAssignmentLister is a mock of OrganizationHostAssignmentLister interface +type MockOrganizationHostAssignmentLister struct { + ctrl *gomock.Controller + recorder *MockOrganizationHostAssignmentListerMockRecorder +} + +// MockOrganizationHostAssignmentListerMockRecorder is the mock recorder for MockOrganizationHostAssignmentLister +type MockOrganizationHostAssignmentListerMockRecorder struct { + mock *MockOrganizationHostAssignmentLister +} + +// NewMockOrganizationHostAssignmentLister creates a new mock instance +func NewMockOrganizationHostAssignmentLister(ctrl *gomock.Controller) *MockOrganizationHostAssignmentLister { + mock := &MockOrganizationHostAssignmentLister{ctrl: ctrl} + mock.recorder = &MockOrganizationHostAssignmentListerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockOrganizationHostAssignmentLister) EXPECT() *MockOrganizationHostAssignmentListerMockRecorder { + return m.recorder +} + +// OrganizationHostAssignments mocks base method +func (m *MockOrganizationHostAssignmentLister) OrganizationHostAssignments(arg0 string, arg1 *opsmngr.ServerTypeOptions) (*opsmngr.HostAssignments, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "OrganizationHostAssignments", arg0, arg1) + ret0, _ := ret[0].(*opsmngr.HostAssignments) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// OrganizationHostAssignments indicates an expected call of OrganizationHostAssignments +func (mr *MockOrganizationHostAssignmentListerMockRecorder) OrganizationHostAssignments(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OrganizationHostAssignments", reflect.TypeOf((*MockOrganizationHostAssignmentLister)(nil).OrganizationHostAssignments), arg0, arg1) +} diff --git a/internal/store/server_usage.go b/internal/store/server_usage.go index c9a6af66d6..809c9f9032 100644 --- a/internal/store/server_usage.go +++ b/internal/store/server_usage.go @@ -22,7 +22,7 @@ import ( "go.mongodb.org/ops-manager/opsmngr" ) -//go:generate mockgen -destination=../mocks/mock_server_usage.go -package=mocks github.com/mongodb/mongocli/internal/store ProjectServerTypeGetter,ProjectServerTypeUpdater,OrganizationServerTypeGetter,OrganizationServerTypeUpdater +//go:generate mockgen -destination=../mocks/mock_server_usage.go -package=mocks github.com/mongodb/mongocli/internal/store ProjectServerTypeGetter,ProjectServerTypeUpdater,OrganizationServerTypeGetter,OrganizationServerTypeUpdater,ProjectHostAssignmentLister,OrganizationHostAssignmentLister type ProjectServerTypeGetter interface { ProjectServerType(string) (*opsmngr.ServerType, error) @@ -40,6 +40,14 @@ type OrganizationServerTypeUpdater interface { UpdateOrganizationServerType(string, *opsmngr.ServerTypeRequest) error } +type ProjectHostAssignmentLister interface { + ProjectHostAssignments(string, *opsmngr.ServerTypeOptions) (*opsmngr.HostAssignments, error) +} + +type OrganizationHostAssignmentLister interface { + OrganizationHostAssignments(string, *opsmngr.ServerTypeOptions) (*opsmngr.HostAssignments, error) +} + // ProjectServerType encapsulates the logic to manage different cloud providers func (s *Store) ProjectServerType(projectID string) (*opsmngr.ServerType, error) { switch s.service { @@ -83,3 +91,25 @@ func (s *Store) UpdateOrganizationServerType(orgID string, serverType *opsmngr.S return fmt.Errorf("unsupported service: %s", s.service) } } + +// ProjectHostAssignments encapsulates the logic to manage different cloud providers +func (s *Store) ProjectHostAssignments(projectID string, opts *opsmngr.ServerTypeOptions) (*opsmngr.HostAssignments, error) { + switch s.service { + case config.OpsManagerService: + result, _, err := s.client.(*opsmngr.Client).ServerUsage.ProjectHostAssignments(context.Background(), projectID, opts) + return result, err + default: + return nil, fmt.Errorf("unsupported service: %s", s.service) + } +} + +// OrganizationHostAssignments encapsulates the logic to manage different cloud providers +func (s *Store) OrganizationHostAssignments(orgID string, opts *opsmngr.ServerTypeOptions) (*opsmngr.HostAssignments, error) { + switch s.service { + case config.OpsManagerService: + result, _, err := s.client.(*opsmngr.Client).ServerUsage.OrganizationHostAssignments(context.Background(), orgID, opts) + return result, err + default: + return nil, fmt.Errorf("unsupported service: %s", s.service) + } +} diff --git a/internal/usage/usage.go b/internal/usage/usage.go index 1c81fbb3be..0827a08b08 100644 --- a/internal/usage/usage.go +++ b/internal/usage/usage.go @@ -100,6 +100,8 @@ const ( AccessListIps = "IP addresses to add to the new user’s access list." StartDate = "Timestamp in ISO 8601 date and time format in UTC when the maintenance window starts." EndDate = "Timestamp in ISO 8601 date and time format in UTC when the maintenance window ends." + ServerUsageStartDate = "Timestamp in ISO 8601 date format when the list of host assignments starts." + ServerUsageEndDate = "Timestamp in ISO 8601 date format when the list of host assignments ends." AlertType = "Alert types to silence during maintenance window. For example: HOST, REPLICA_SET, CLUSTER, AGENT, BACKUP." MaintenanceDescription = "Description of the maintenance window." Event = "Type of event that will trigger an alert." From 6fd15d6a8d15abf062fbe0cb2b56635ce3fe0257 Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Fri, 16 Oct 2020 08:55:46 +0100 Subject: [PATCH 2/4] Update global_opts.go --- internal/cli/global_opts.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/cli/global_opts.go b/internal/cli/global_opts.go index 8da8f67292..827f36bbdc 100644 --- a/internal/cli/global_opts.go +++ b/internal/cli/global_opts.go @@ -56,9 +56,9 @@ func (opts *GlobalOpts) PreRunE(cbs ...cmdOpt) error { if opts.ConfigProjectID() == "" { return errMissingProjectID } - //if err := validate.ObjectID(opts.ConfigProjectID()); err != nil { - // return err - //} + if err := validate.ObjectID(opts.ConfigProjectID()); err != nil { + return err + } for _, f := range cbs { if err := f(); err != nil { return err From 9d1116ab91ed025b346bfea3fc4db46a540852b3 Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Fri, 16 Oct 2020 09:32:41 +0100 Subject: [PATCH 3/4] Update description.go --- .../opsmanager/serverusage/organizations/hosts/description.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cli/opsmanager/serverusage/organizations/hosts/description.go b/internal/cli/opsmanager/serverusage/organizations/hosts/description.go index 05505821bf..565d21b456 100644 --- a/internal/cli/opsmanager/serverusage/organizations/hosts/description.go +++ b/internal/cli/opsmanager/serverusage/organizations/hosts/description.go @@ -15,5 +15,5 @@ package hosts const ( short = "Manage host assignment for a project." - list = "List all host assignments for one project." + list = "List all host assignments for one organization." ) From 6203a904ace37fdff924a16128e4b3d6cccc16c2 Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Fri, 16 Oct 2020 09:33:28 +0100 Subject: [PATCH 4/4] Update description.go --- .../opsmanager/serverusage/organizations/hosts/description.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cli/opsmanager/serverusage/organizations/hosts/description.go b/internal/cli/opsmanager/serverusage/organizations/hosts/description.go index 565d21b456..692b016d88 100644 --- a/internal/cli/opsmanager/serverusage/organizations/hosts/description.go +++ b/internal/cli/opsmanager/serverusage/organizations/hosts/description.go @@ -14,6 +14,6 @@ package hosts const ( - short = "Manage host assignment for a project." + short = "Manage host assignment for an organization." list = "List all host assignments for one organization." )