• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mongodb / mongodb-atlas-cli / 25848964073

14 May 2026 07:59AM UTC coverage: 22.479% (-41.3%) from 63.771%
25848964073

push

github

web-flow
build(deps): bump test-summary/action from 2.4 to 2.6 (#4576)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

8987 of 39979 relevant lines covered (22.48%)

0.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

42.11
/internal/cli/dbusers/update.go
1
// Copyright 2021 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 dbusers
16

17
import (
18
        "context"
19
        "fmt"
20

21
        "github.com/mongodb/atlas-cli-core/config"
22
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
23
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
24
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/convert"
25
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
26
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
27
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
28
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/validate"
29
        "github.com/spf13/cobra"
30
        atlasv2 "go.mongodb.org/atlas-sdk/v20250312018/admin"
31
)
32

33
const updateTemplate = "Successfully updated database user '{{.Username}}'.\n"
34

35
//go:generate go tool go.uber.org/mock/mockgen -typed -destination=update_mock_test.go -package=dbusers -source=update.go
36

37
type DatabaseUserUpdater interface {
38
        UpdateDatabaseUser(*atlasv2.UpdateDatabaseUserApiParams) (*atlasv2.CloudDatabaseUser, error)
39
}
40

41
type UpdateOpts struct {
42
        cli.OutputOpts
43
        cli.ProjectOpts
44
        username        string
45
        currentUsername string
46
        password        string
47
        authDB          string
48
        x509Type        string
49
        description     string
50
        roles           []string
51
        scopes          []string
52
        store           DatabaseUserUpdater
53
}
54

55
func (opts *UpdateOpts) initStore(ctx context.Context) func() error {
×
56
        return func() error {
×
57
                var err error
×
58
                opts.store, err = store.New(store.AuthenticatedPreset(config.Default()), store.WithContext(ctx))
×
59
                return err
×
60
        }
×
61
}
62

63
func (opts *UpdateOpts) Run() error {
1✔
64
        current := new(atlasv2.CloudDatabaseUser)
1✔
65
        opts.update(current)
1✔
66

1✔
67
        params := &atlasv2.UpdateDatabaseUserApiParams{
1✔
68
                GroupId:           current.GroupId,
1✔
69
                DatabaseName:      current.DatabaseName,
1✔
70
                Username:          opts.currentUsername,
1✔
71
                CloudDatabaseUser: current,
1✔
72
        }
1✔
73
        r, err := opts.store.UpdateDatabaseUser(params)
1✔
74

1✔
75
        if err != nil {
1✔
76
                return err
×
77
        }
×
78

79
        return opts.Print(r)
1✔
80
}
81

82
func (opts *UpdateOpts) update(out *atlasv2.CloudDatabaseUser) {
1✔
83
        out.GroupId = opts.ConfigProjectID()
1✔
84
        out.Username = opts.username
1✔
85
        if opts.username == "" {
2✔
86
                out.Username = opts.currentUsername
1✔
87
        }
1✔
88
        if opts.password != "" {
2✔
89
                out.Password = &opts.password
1✔
90
        }
1✔
91

92
        if opts.description != "" {
2✔
93
                out.Description = &opts.description
1✔
94
        }
1✔
95

96
        if len(opts.roles) > 0 {
2✔
97
                roles := convert.BuildAtlasRoles(opts.roles)
1✔
98
                out.Roles = roles
1✔
99
        }
1✔
100

101
        if len(opts.scopes) > 0 {
1✔
102
                scopes := convert.BuildAtlasScopes(opts.scopes)
×
103
                out.Scopes = &scopes
×
104
        }
×
105

106
        out.DatabaseName = opts.authDB
1✔
107
        if opts.authDB == "" {
2✔
108
                out.DatabaseName = convert.GetAuthDB(out)
1✔
109
        }
1✔
110
        if opts.x509Type != "" {
1✔
111
                out.X509Type = &opts.x509Type
×
112
        }
×
113
}
114

115
func (opts *UpdateOpts) validateAuthDB() error {
1✔
116
        if opts.authDB == "" {
1✔
117
                return nil
×
118
        }
×
119
        validAuthDBs := []string{convert.AdminDB, convert.ExternalAuthDB}
1✔
120
        return validate.FlagInSlice(opts.authDB, flag.AuthDB, validAuthDBs)
1✔
121
}
122

123
// atlas dbuser(s) update <username> [--password password] [--role roleName@dbName] [--projectId projectId] [--authDB authDB].
124
func UpdateBuilder() *cobra.Command {
×
125
        opts := &UpdateOpts{}
×
126
        cmd := &cobra.Command{
×
127
                Use:   "update <username>",
×
128
                Short: "Modify the details of a database user in your project.",
×
129
                Long:  fmt.Sprintf(usage.RequiredRole, "Project Owner"),
×
130
                Example: `  # Update roles for a database user named myUser for the project with the ID 5e2211c17a3e5a48f5497de3:
×
131
  atlas dbuser update myUser --role readWriteAnyDatabase --projectId 5e2211c17a3e5a48f5497de3
×
132

×
133
  # Update scopes for a database user named myUser for the project with the ID 5e2211c17a3e5a48f5497de3:
×
134
  atlas dbuser update myUser --scope resourceName:resourceType --projectId 5e2211c17a3e5a48f5497de3`,
×
135
                Args: require.ExactArgs(1),
×
136
                Annotations: map[string]string{
×
137
                        "usernameDesc": "Username to update in the MongoDB database.",
×
138
                        "output":       updateTemplate,
×
139
                },
×
140
                PreRunE: func(cmd *cobra.Command, _ []string) error {
×
141
                        return opts.PreRunE(
×
142
                                opts.ValidateProjectID,
×
143
                                opts.validateAuthDB,
×
144
                                opts.initStore(cmd.Context()),
×
145
                                opts.InitOutput(cmd.OutOrStdout(), updateTemplate),
×
146
                        )
×
147
                },
×
148
                RunE: func(_ *cobra.Command, args []string) error {
×
149
                        opts.currentUsername = args[0]
×
150
                        return opts.Run()
×
151
                },
×
152
        }
153

154
        cmd.Flags().StringVarP(&opts.username, flag.Username, flag.UsernameShort, "", usage.DBUsername)
×
155
        cmd.Flags().StringVarP(&opts.password, flag.Password, flag.PasswordShort, "", usage.DBUserPassword)
×
156
        cmd.Flags().StringVar(&opts.description, flag.Description, "", usage.DBUserDescription)
×
157
        cmd.Flags().StringVar(&opts.authDB, flag.AuthDB, "", usage.AtlasAuthDB)
×
158
        cmd.Flags().StringSliceVar(&opts.roles, flag.Role, []string{}, usage.Roles+usage.UpdateWarning)
×
159
        cmd.Flags().StringSliceVar(&opts.scopes, flag.Scope, []string{}, usage.Scopes+usage.UpdateWarning)
×
160
        cmd.Flags().StringVar(&opts.x509Type, flag.X509Type, none, usage.X509Type)
×
161

×
162
        opts.AddProjectOptsFlags(cmd)
×
163
        opts.AddOutputOptFlags(cmd)
×
164

×
165
        return cmd
×
166
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc