-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathlist.go
More file actions
108 lines (93 loc) · 3.33 KB
/
list.go
File metadata and controls
108 lines (93 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2023 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 settings
import (
"context"
"fmt"
"github.com/mongodb/atlas-cli-core/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
atlasv2 "go.mongodb.org/atlas-sdk/v20250312010/admin"
)
//go:generate go tool go.uber.org/mock/mockgen -typed -destination=list_mock_test.go -package=settings . AlertConfigurationLister
type AlertConfigurationLister interface {
AlertConfigurations(*atlasv2.ListAlertConfigsApiParams) (*atlasv2.PaginatedAlertConfig, error)
}
type ListOpts struct {
cli.ProjectOpts
cli.OutputOpts
cli.ListOpts
CompactResponse bool
store AlertConfigurationLister
}
func (opts *ListOpts) initStore(ctx context.Context) func() error {
return func() error {
var err error
opts.store, err = store.New(store.AuthenticatedPreset(config.Default()), store.WithContext(ctx))
return err
}
}
var settingsListTemplate = `ID TYPE ENABLED{{range valueOrEmptySlice .Results}}
{{.Id}} {{.EventTypeName}} {{.Enabled}}{{end}}
`
func (opts *ListOpts) Run() error {
params := &atlasv2.ListAlertConfigsApiParams{
GroupId: opts.ConfigProjectID(),
PageNum: &opts.PageNum,
}
if opts.ItemsPerPage > 0 {
params.ItemsPerPage = &opts.ItemsPerPage
}
r, err := opts.store.AlertConfigurations(params)
if err != nil {
return err
}
if opts.CompactResponse {
return opts.PrintForCompactResultsResponse(r)
}
return opts.Print(r)
}
// atlas alerts config(s) list --projectId projectId [--page N] [--limit N].
func ListBuilder() *cobra.Command {
opts := new(ListOpts)
cmd := &cobra.Command{
Use: "list",
Short: "Returns all alert configurations for your project.",
Long: fmt.Sprintf(usage.RequiredRole, "Project Read Only"),
Example: ` # Return a JSON-formatted list of all alert configurations for the project with the ID 5df90590f10fab5e33de2305:
atlas alerts settings list --projectId 5df90590f10fab5e33de2305 --output json`,
Annotations: map[string]string{},
Aliases: []string{"ls"},
Args: require.NoArgs,
PreRunE: func(cmd *cobra.Command, _ []string) error {
return opts.PreRunE(
opts.ValidateProjectID,
opts.initStore(cmd.Context()),
opts.InitOutput(cmd.OutOrStdout(), settingsListTemplate),
)
},
RunE: func(_ *cobra.Command, _ []string) error {
return opts.Run()
},
}
opts.AddListOptsFlagsWithoutOmitCount(cmd)
opts.AddProjectOptsFlags(cmd)
opts.AddOutputOptFlags(cmd)
cmd.Flags().BoolVarP(&opts.CompactResponse, flag.CompactResponse, flag.CompactResponseShort, false, usage.CompactResponse)
return cmd
}