Skip to content
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

CLOUDP-73752: mongocli ops-manager serverUsage project(s) hosts lists|ls, CLOUDP-73755: mongocli ops-manager serverUsage org(s) hosts lists|ls #500

Merged
merged 4 commits into from
Oct 16, 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
Original file line number Diff line number Diff line change
@@ -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 an organization."
list = "List all host assignments for one organization."
)
Original file line number Diff line number Diff line change
@@ -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
}
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.
// +build unit

package hosts

import (
"testing"

"github.com/mongodb/mongocli/internal/cli"
)

func TestBuilder(t *testing.T) {
cli.CmdValidator(
t,
Builder(),
1,
[]string{},
)
}
93 changes: 93 additions & 0 deletions internal/cli/opsmanager/serverusage/organizations/hosts/list.go
Original file line number Diff line number Diff line change
@@ -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
}
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.

// +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},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -30,6 +31,7 @@ func Builder() *cobra.Command {

cmd.AddCommand(
servertype.Builder(),
hosts.Builder(),
)

return cmd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestListBuilder(t *testing.T) {
cli.CmdValidator(
t,
Builder(),
1,
2,
[]string{},
)
}
Original file line number Diff line number Diff line change
@@ -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."
)
35 changes: 35 additions & 0 deletions internal/cli/opsmanager/serverusage/projects/hosts/hosts.go
Original file line number Diff line number Diff line change
@@ -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
}
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.
// +build unit

package hosts

import (
"testing"

"github.com/mongodb/mongocli/internal/cli"
)

func TestBuilder(t *testing.T) {
cli.CmdValidator(
t,
Builder(),
1,
[]string{},
)
}
Loading