|
| 1 | +// Copyright 2025 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 privatelink |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli" |
| 22 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require" |
| 23 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config" |
| 24 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" |
| 25 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage" |
| 26 | + "github.com/spf13/cobra" |
| 27 | +) |
| 28 | + |
| 29 | +var listTemplate = `ID PROVIDER REGION VENDOR STATE INTERFACE_ENDPOINT_ID SERVICE_ENDPOINT_ID DNS_DOMAIN DNS_SUBDOMAIN{{range valueOrEmptySlice .Results}} |
| 30 | +{{.Id}} {{.Provider}} {{.Region}} {{.Vendor}} {{.State}} {{.InterfaceEndpointId}} {{.ServiceEndpointId}} {{.DnsDomain}} {{.DnsSubDomain}}{{end}} |
| 31 | +` |
| 32 | + |
| 33 | +type ListOpts struct { |
| 34 | + cli.ProjectOpts |
| 35 | + cli.OutputOpts |
| 36 | + store store.PrivateLinkLister |
| 37 | +} |
| 38 | + |
| 39 | +func (opts *ListOpts) Run() error { |
| 40 | + result, err := opts.store.ListPrivateLinkEndpoints(opts.ConfigProjectID()) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + return opts.Print(result) |
| 46 | +} |
| 47 | + |
| 48 | +func (opts *ListOpts) initStore(ctx context.Context) func() error { |
| 49 | + return func() error { |
| 50 | + var err error |
| 51 | + opts.store, err = store.New(store.AuthenticatedPreset(config.Default()), store.WithContext(ctx)) |
| 52 | + return err |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// atlas streams privateLink list |
| 57 | +// List the PrivateLink endpoints in the project that can be used as Atlas Stream Processor connections. |
| 58 | +func ListBuilder() *cobra.Command { |
| 59 | + opts := &ListOpts{} |
| 60 | + cmd := &cobra.Command{ |
| 61 | + Use: "list", |
| 62 | + Short: "Lists the PrivateLink endpoints in the project that can be used as Atlas Stream Processor connections.", |
| 63 | + Long: fmt.Sprintf(usage.RequiredOneOfRoles, commandRoles), |
| 64 | + Args: require.NoArgs, |
| 65 | + Annotations: map[string]string{ |
| 66 | + "output": listTemplate, |
| 67 | + }, |
| 68 | + Example: `# list PrivateLink endpoints for Atlas Stream Processing: |
| 69 | + atlas streams privateLink list |
| 70 | +`, |
| 71 | + PreRunE: func(cmd *cobra.Command, _ []string) error { |
| 72 | + return opts.PreRunE( |
| 73 | + opts.ValidateProjectID, |
| 74 | + opts.initStore(cmd.Context()), |
| 75 | + opts.InitOutput(cmd.OutOrStdout(), createTemplate), |
| 76 | + ) |
| 77 | + }, |
| 78 | + RunE: func(_ *cobra.Command, _ []string) error { |
| 79 | + return opts.Run() |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + opts.AddProjectOptsFlags(cmd) |
| 84 | + opts.AddOutputOptFlags(cmd) |
| 85 | + |
| 86 | + return cmd |
| 87 | +} |
0 commit comments