• 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

45.26
/internal/cli/projects/create.go
1
// Copyright 2023 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 projects
16

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

22
        "github.com/mongodb/atlas-cli-core/config"
23
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
24
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
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/spf13/cobra"
29
        atlasv2 "go.mongodb.org/atlas-sdk/v20250312018/admin"
30
)
31

32
const atlasCreateTemplate = "Project '{{.Id}}' created.\n"
33

34
//go:generate go tool go.uber.org/mock/mockgen -typed -destination=create_mock_test.go -package=projects -source=create.go
35

36
type ProjectCreator interface {
37
        CreateProject(*atlasv2.CreateGroupApiParams) (*atlasv2.Group, error)
38
}
39

40
type CreateOpts struct {
41
        cli.OrgOpts
42
        cli.OutputOpts
43
        name                        string
44
        projectOwnerID              string
45
        regionUsageRestrictions     bool
46
        withoutDefaultAlertSettings bool
47
        tag                         map[string]string
48
        store                       ProjectCreator
49
}
50

51
func (opts *CreateOpts) initStore(ctx context.Context) func() error {
×
52
        return func() error {
×
53
                var err error
×
54
                opts.store, err = store.New(store.AuthenticatedPreset(config.Default()), store.WithContext(ctx))
×
55
                return err
×
56
        }
×
57
}
58

59
func (opts *CreateOpts) Run() error {
1✔
60
        r, err := opts.store.CreateProject(opts.newCreateProjectOptions())
1✔
61
        if err != nil {
1✔
62
                return err
×
63
        }
×
64

65
        return opts.Print(r)
1✔
66
}
67

68
func (opts *CreateOpts) newCreateProjectGroupTags() *[]atlasv2.ResourceTag {
1✔
69
        if len(opts.tag) == 0 {
1✔
70
                return nil
×
71
        }
×
72

73
        tags := make([]atlasv2.ResourceTag, 0)
1✔
74

1✔
75
        keys := make([]string, 0)
1✔
76
        for k := range opts.tag {
2✔
77
                keys = append(keys, k)
1✔
78
        }
1✔
79
        sort.Strings(keys)
1✔
80

1✔
81
        for _, key := range keys {
2✔
82
                value := opts.tag[key]
1✔
83
                if key == "" || value == "" {
1✔
84
                        continue
×
85
                }
86

87
                resourceTag := *atlasv2.NewResourceTag(key, value)
1✔
88

1✔
89
                tags = append(tags, resourceTag)
1✔
90
        }
91

92
        return &tags
1✔
93
}
94

95
func (opts *CreateOpts) newCreateProjectGroup() *atlasv2.Group {
1✔
96
        return &atlasv2.Group{
1✔
97
                Name:                      opts.name,
1✔
98
                OrgId:                     opts.ConfigOrgID(),
1✔
99
                WithDefaultAlertsSettings: opts.defaultAlertSettings(),
1✔
100
                RegionUsageRestrictions:   opts.newRegionUsageRestrictions(),
1✔
101
                Tags:                      opts.newCreateProjectGroupTags(),
1✔
102
        }
1✔
103
}
1✔
104

105
func (opts *CreateOpts) defaultAlertSettings() *bool {
1✔
106
        var defaultAlertSettings *bool
1✔
107
        if opts.withoutDefaultAlertSettings {
1✔
108
                f := false
×
109
                defaultAlertSettings = &f
×
110
        }
×
111
        return defaultAlertSettings
1✔
112
}
113

114
func (opts *CreateOpts) newRegionUsageRestrictions() *string {
1✔
115
        if opts.regionUsageRestrictions {
1✔
116
                govRegionOnly := "GOV_REGIONS_ONLY"
×
117
                return &govRegionOnly
×
118
        }
×
119
        return nil
1✔
120
}
121

122
func (opts *CreateOpts) newCreateProjectOptions() *atlasv2.CreateGroupApiParams {
1✔
123
        return &atlasv2.CreateGroupApiParams{
1✔
124
                ProjectOwnerId: &opts.projectOwnerID,
1✔
125
                Group:          opts.newCreateProjectGroup(),
1✔
126
        }
1✔
127
}
1✔
128

129
// atlas project(s) create <name> [--orgId orgId] [--ownerID ownerID] [--withoutDefaultAlertSettings].
130
func CreateBuilder() *cobra.Command {
×
131
        opts := &CreateOpts{}
×
132
        opts.Template = atlasCreateTemplate
×
133
        cmd := &cobra.Command{
×
134
                Use:   "create <projectName>",
×
135
                Short: "Create a project in your organization.",
×
136
                Long: `Projects group clusters into logical collections that support an application environment, workload, or both. Each project can have its own users, teams, security, and alert settings.
×
137

×
138
` + fmt.Sprintf(usage.RequiredRole, "Project Data Access Read/Write"),
×
139
                Args: require.ExactArgs(1),
×
140
                Annotations: map[string]string{
×
141
                        "projectNameDesc": "Label that identifies the project.",
×
142
                        "output":          atlasCreateTemplate,
×
143
                },
×
144
                Example: `  # Create a project in the organization with the ID 5e2211c17a3e5a48f5497de3 using default alert settings:
×
145
  atlas projects create my-project --orgId 5e2211c17a3e5a48f5497de3 --output json`,
×
146
                PreRunE: func(cmd *cobra.Command, _ []string) error {
×
147
                        opts.OutWriter = cmd.OutOrStdout()
×
148
                        return opts.PreRunE(
×
149
                                opts.ValidateOrgID,
×
150
                                opts.initStore(cmd.Context()),
×
151
                        )
×
152
                },
×
153
                RunE: func(_ *cobra.Command, args []string) error {
×
154
                        opts.name = args[0]
×
155
                        return opts.Run()
×
156
                },
×
157
        }
158
        opts.AddOrgOptFlags(cmd)
×
159
        cmd.Flags().StringVar(&opts.projectOwnerID, flag.OwnerID, "", usage.ProjectOwnerID)
×
160
        cmd.Flags().BoolVar(&opts.regionUsageRestrictions, flag.GovCloudRegionsOnly, false, usage.GovCloudRegionsOnly)
×
161
        cmd.Flags().BoolVar(&opts.withoutDefaultAlertSettings, flag.WithoutDefaultAlertSettings, false, usage.WithoutDefaultAlertSettings)
×
162
        cmd.Flags().StringToStringVar(&opts.tag, flag.Tag, nil, usage.ProjectTag)
×
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