Skip to content

Commit b616098

Browse files
authored
CLOUDP-54303: CLI: OM Owner creation (#43)
1 parent 00f3dbc commit b616098

File tree

14 files changed

+369
-5
lines changed

14 files changed

+369
-5
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ gen-mocks: ## Generate mocks
5454
mockgen -source=internal/store/project_ip_whitelist.go -destination=internal/mocks/mock_project_ip_whitelist.go -package=mocks
5555
mockgen -source=internal/store/projects.go -destination=internal/mocks/mock_projects.go -package=mocks
5656
mockgen -source=internal/store/organizations.go -destination=internal/mocks/mock_organizations.go -package=mocks
57+
mockgen -source=internal/store/owners.go -destination=internal/mocks/mock_owners.go -package=mocks
5758

5859
.PHONY: build
5960
build: ## Generate a binary in ./bin

cmd/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ func init() {
5050
rootCmd.AddCommand(cli.ConfigBuilder())
5151
// Atlas commands
5252
rootCmd.AddCommand(cli.AtlasBuilder())
53-
// C/OM commands
53+
// CM commands
5454
rootCmd.AddCommand(cli.CloudManagerBuilder())
55+
// OM commands
56+
rootCmd.AddCommand(cli.OpsManagerBuilder())
5557
// IAM commands
5658
rootCmd.AddCommand(cli.IAMBuilder())
5759

internal/cli/atlas.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
func AtlasBuilder() *cobra.Command {
2222
cmd := &cobra.Command{
2323
Use: "atlas",
24-
Short: "Command for working with Atlas.",
24+
Short: "Atlas operations.",
2525
}
2626
cmd.AddCommand(AtlasClustersBuilder())
2727
cmd.AddCommand(AtlasDBUsersBuilder())

internal/cli/cloud_manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
func CloudManagerBuilder() *cobra.Command {
2222
cmd := &cobra.Command{
2323
Use: "cloud-manager",
24-
Aliases: []string{"cm", "ops-manager", "om", "deployments"},
25-
Short: "Command for working with atlas.",
24+
Aliases: []string{"cm"},
25+
Short: "Cloud Manager operations.",
2626
}
2727

2828
cmd.AddCommand(CloudManagerClustersBuilder())

internal/cli/ops_manager.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2020 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cli
16+
17+
import (
18+
"github.com/spf13/cobra"
19+
)
20+
21+
func OpsManagerBuilder() *cobra.Command {
22+
cmd := &cobra.Command{
23+
Use: "ops-manager",
24+
Aliases: []string{"om"},
25+
Short: "Ops Manager operations.",
26+
}
27+
28+
cmd.AddCommand(CloudManagerClustersBuilder())
29+
cmd.AddCommand(OpsManagerOwnerBuilder())
30+
31+
return cmd
32+
}

internal/cli/ops_manager_owner.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2020 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cli
16+
17+
import (
18+
"github.com/spf13/cobra"
19+
)
20+
21+
func OpsManagerOwnerBuilder() *cobra.Command {
22+
cmd := &cobra.Command{
23+
Use: "owner",
24+
Short: "Owner management for Ops Manager.",
25+
}
26+
27+
cmd.AddCommand(OpsManagerOwnerCreateBuilder())
28+
return cmd
29+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright 2020 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cli
16+
17+
import (
18+
"github.com/10gen/mcli/internal/flags"
19+
"github.com/10gen/mcli/internal/json"
20+
"github.com/10gen/mcli/internal/store"
21+
"github.com/10gen/mcli/internal/usage"
22+
"github.com/AlecAivazis/survey/v2"
23+
cm "github.com/mongodb-labs/pcgc/cloudmanager"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
type opsManagerOwnerCreateOpts struct {
28+
email string
29+
password string
30+
firstName string
31+
lastName string
32+
whitelistIps string
33+
store store.OwnerCreator
34+
}
35+
36+
func (opts *opsManagerOwnerCreateOpts) init() error {
37+
s, err := store.NewUnauthenticated()
38+
39+
if err != nil {
40+
return err
41+
}
42+
43+
opts.store = s
44+
return nil
45+
}
46+
47+
func (opts *opsManagerOwnerCreateOpts) Run() error {
48+
user := opts.newOwner()
49+
result, err := opts.store.CreateOwner(user, opts.whitelistIps)
50+
51+
if err != nil {
52+
return err
53+
}
54+
55+
return json.PrettyPrint(result)
56+
}
57+
58+
func (opts *opsManagerOwnerCreateOpts) newOwner() *cm.User {
59+
user := &cm.User{
60+
Username: opts.email,
61+
Password: opts.password,
62+
FirstName: opts.firstName,
63+
LastName: opts.lastName,
64+
EmailAddress: opts.email,
65+
Links: nil,
66+
}
67+
return user
68+
}
69+
70+
func (opts *opsManagerOwnerCreateOpts) Prompt() error {
71+
if opts.password != "" {
72+
return nil
73+
}
74+
prompt := &survey.Password{
75+
Message: "Password:",
76+
}
77+
return survey.AskOne(prompt, &opts.password)
78+
}
79+
80+
// mcli ops-manager owner create --email username --password password --firstName firstName --lastName lastName --whitelistIps whitelistIp
81+
func OpsManagerOwnerCreateBuilder() *cobra.Command {
82+
opts := new(opsManagerOwnerCreateOpts)
83+
cmd := &cobra.Command{
84+
Use: "create",
85+
Short: "Create the first user for the installation.",
86+
Args: cobra.OnlyValidArgs,
87+
PreRunE: func(cmd *cobra.Command, args []string) error {
88+
if err := opts.init(); err != nil {
89+
return err
90+
}
91+
return opts.Prompt()
92+
},
93+
RunE: func(cmd *cobra.Command, args []string) error {
94+
return opts.Run()
95+
},
96+
}
97+
98+
cmd.Flags().StringVar(&opts.email, flags.Email, "", usage.Email)
99+
cmd.Flags().StringVar(&opts.password, flags.Password, "", usage.Password)
100+
cmd.Flags().StringVar(&opts.firstName, flags.FirstName, "", usage.FirstName)
101+
cmd.Flags().StringVar(&opts.lastName, flags.LastName, "", usage.LastName)
102+
cmd.Flags().StringVar(&opts.whitelistIps, flags.WhitelistIps, "", usage.WhitelistIps)
103+
104+
_ = cmd.MarkFlagRequired(flags.Username)
105+
_ = cmd.MarkFlagRequired(flags.FirstName)
106+
_ = cmd.MarkFlagRequired(flags.LastName)
107+
108+
return cmd
109+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2020 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cli
16+
17+
import (
18+
"testing"
19+
20+
"github.com/10gen/mcli/internal/fixtures"
21+
"github.com/10gen/mcli/internal/mocks"
22+
"github.com/golang/mock/gomock"
23+
)
24+
25+
func TestManagerOwnerCreate_Run(t *testing.T) {
26+
ctrl := gomock.NewController(t)
27+
mockStore := mocks.NewMockOwnerCreator(ctrl)
28+
29+
defer ctrl.Finish()
30+
31+
email := "[email protected]"
32+
firstName := "Testy"
33+
lastName := "McTestyson"
34+
expected := fixtures.OwnerResponse(email, firstName, lastName)
35+
36+
createOpts := &opsManagerOwnerCreateOpts{
37+
email: email,
38+
password: "Passw0rd!",
39+
firstName: firstName,
40+
lastName: lastName,
41+
store: mockStore,
42+
}
43+
44+
mockStore.
45+
EXPECT().
46+
CreateOwner(createOpts.newOwner(), createOpts.whitelistIps).Return(expected, nil).
47+
Times(1)
48+
49+
err := createOpts.Run()
50+
if err != nil {
51+
t.Fatalf("Run() unexpected error: %v", err)
52+
}
53+
}

internal/fixtures/owner.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2020 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package fixtures
16+
17+
import "github.com/mongodb-labs/pcgc/cloudmanager"
18+
19+
func OwnerResponse(email, firstName, lastName string) *cloudmanager.CreateUserResponse {
20+
user := &cloudmanager.User{
21+
Username: email,
22+
FirstName: firstName,
23+
LastName: lastName,
24+
EmailAddress: email,
25+
ID: "1",
26+
Links: nil,
27+
Roles: nil,
28+
}
29+
return &cloudmanager.CreateUserResponse{
30+
APIKey: "123",
31+
User: user,
32+
}
33+
}

internal/flags/flags.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const (
3131
Backup = "backup" // Backup flag
3232
Username = "username" // Username flag
3333
Password = "password" // Password flag
34+
Email = "email" // Email flag
35+
FirstName = "firstName" // FirstName flag
36+
LastName = "lastName" // LastName flag
3437
Role = "role" // Role flag
3538
Type = "type" // Type flag
3639
Comment = "comment" // Comment flag
@@ -39,4 +42,5 @@ const (
3942
File = "file" // File flag
4043
FileShort = "f" // File flag
4144
Force = "force" // Force flag
45+
WhitelistIps = "whitelistIps" // WhitelistIps flag
4246
)

0 commit comments

Comments
 (0)