Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLOUDP-291621: Add describe command for Atlas Stream Processing PrivateLinks #3641

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions docs/command/atlas-streams-privateLinks-describe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
.. _atlas-streams-privateLinks-describe:

===================================
atlas streams privateLinks describe
===================================

.. default-domain:: mongodb

.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

Describes a PrivateLink endpoint that can be used as an Atlas Stream Processor connection.

To use this command, you must authenticate with a user account or an API key with any of the following roles: Project Owner, Project Stream Processing Owner.

Syntax
------

.. code-block::
:caption: Command Syntax

atlas streams privateLinks describe <connectionID> [options]

.. Code end marker, please don't delete this comment

Arguments
---------

.. list-table::
:header-rows: 1
:widths: 20 10 10 60

* - Name
- Type
- Required
- Description
* - connectionID
- string
- true
- ID of the PrivateLink endpoint.

Options
-------

.. list-table::
:header-rows: 1
:widths: 20 10 10 60

* - Name
- Type
- Required
- Description
* - -h, --help
-
- false
- help for describe
* - -o, --output
- string
- false
- Output format. Valid values are json, json-path, go-template, or go-template-file. To see the full output, use the -o json option.
* - --projectId
- string
- false
- Hexadecimal string that identifies the project to use. This option overrides the settings in the configuration file or environment variable.

Inherited Options
-----------------

.. list-table::
:header-rows: 1
:widths: 20 10 10 60

* - Name
- Type
- Required
- Description
* - -P, --profile
- string
- false
- Name of the profile to use from your configuration file. To learn about profiles for the Atlas CLI, see https://dochub.mongodb.org/core/atlas-cli-save-connection-settings.

Output
------

If the command succeeds, the CLI returns output similar to the following sample. Values in brackets represent your values.

.. code-block::

ID PROVIDER REGION VENDOR STATE INTERFACE_ENDPOINT_ID SERVICE_ENDPOINT_ID DNS_DOMAIN DNS_SUBDOMAIN
<Id> <Provider> <Region> <Vendor> <State> <InterfaceEndpointId> <ServiceEndpointId> <DnsDomain> <DnsSubDomain>


Examples
--------

.. code-block::
:copyable: false

# describe a PrivateLink endpoint for Atlas Stream Processing:
atlas streams privateLink describe 5e2211c17a3e5a48f5497de3

2 changes: 2 additions & 0 deletions docs/command/atlas-streams-privateLinks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ Related Commands
----------------

* :ref:`atlas-streams-privateLinks-create` - Creates a PrivateLink endpoint that can be used as an Atlas Stream Processor connection.
* :ref:`atlas-streams-privateLinks-describe` - Describes a PrivateLink endpoint that can be used as an Atlas Stream Processor connection.


.. toctree::
:titlesonly:

create </command/atlas-streams-privateLinks-create>
describe </command/atlas-streams-privateLinks-describe>

97 changes: 97 additions & 0 deletions internal/cli/streams/privatelink/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2025 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 privatelink

import (
"context"
"errors"
"fmt"

"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/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
)

var describeTemplate = `ID PROVIDER REGION VENDOR STATE INTERFACE_ENDPOINT_ID SERVICE_ENDPOINT_ID DNS_DOMAIN DNS_SUBDOMAIN
{{.Id}} {{.Provider}} {{.Region}} {{.Vendor}} {{.State}} {{.InterfaceEndpointId}} {{.ServiceEndpointId}} {{.DnsDomain}} {{.DnsSubDomain}}
`

type DescribeOpts struct {
cli.ProjectOpts
cli.OutputOpts
store store.PrivateLinkDescriber
connectionID string
}

func (opts *DescribeOpts) Run() error {
if opts.connectionID == "" {
return errors.New("connectionID is missing")
}

result, err := opts.store.DescribePrivateLinkEndpoint(opts.ConfigProjectID(), opts.connectionID)
if err != nil {
return err
}

return opts.Print(result)
}

func (opts *DescribeOpts) 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
}
}

// atlas streams privateLink describe <connectionID>
// Describe a PrivateLink endpoint that can be used as an Atlas Stream Processor connection.
func DescribeBuilder() *cobra.Command {
opts := &DescribeOpts{}
cmd := &cobra.Command{
Use: "describe <connectionID>",
Short: "Describes a PrivateLink endpoint that can be used as an Atlas Stream Processor connection.",
Long: fmt.Sprintf(usage.RequiredOneOfRoles, commandRoles),
Args: require.ExactArgs(1),
Annotations: map[string]string{
"connectionIDDesc": "ID of the PrivateLink endpoint.",
"output": describeTemplate,
},
Example: `# describe a PrivateLink endpoint for Atlas Stream Processing:
atlas streams privateLink describe 5e2211c17a3e5a48f5497de3
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := opts.PreRunE(
opts.ValidateProjectID,
opts.initStore(cmd.Context()),
); err != nil {
return err
}
opts.connectionID = args[0]
return nil
},
RunE: func(_ *cobra.Command, _ []string) error {
return opts.Run()
},
}

opts.AddProjectOptsFlags(cmd)
opts.AddOutputOptFlags(cmd)

return cmd
}
86 changes: 86 additions & 0 deletions internal/cli/streams/privatelink/describe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2025 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 privatelink

import (
"bytes"
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
"github.com/stretchr/testify/require"
atlasv2 "go.mongodb.org/atlas-sdk/v20241113004/admin"
)

func TestDescribeOpts_Run(t *testing.T) {
t.Run("should error when no connectionID is provided", func(t *testing.T) {
describeOpts := &DescribeOpts{}

require.ErrorContains(t, describeOpts.Run(), "connectionID is missing")
})

t.Run("should call the store get privateLink method with the correct parameters", func(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockPrivateLinkDescriber(ctrl)

connectionID := "123456789012"
describeOpts := &DescribeOpts{
store: mockStore,
connectionID: connectionID,
}

mockStore.
EXPECT().
DescribePrivateLinkEndpoint(gomock.Eq(describeOpts.ConfigProjectID()), gomock.Eq(connectionID)).
Times(1)

require.NoError(t, describeOpts.Run())
})

t.Run("should print the result", func(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockPrivateLinkDescriber(ctrl)

buf := new(bytes.Buffer)
describeOpts := &DescribeOpts{
store: mockStore,
connectionID: "123456789012",
OutputOpts: cli.OutputOpts{
Template: describeTemplate,
OutWriter: buf,
},
}

expected := atlasv2.NewStreamsPrivateLinkConnection()
expected.SetId(describeOpts.connectionID)
expected.SetInterfaceEndpointId("vpce-123456789012345678")
expected.SetServiceEndpointId("/subscriptions/fd01adff-b37e-4693-8497-83ecf183a145/resourceGroups/test-rg/providers/Microsoft.EventHub/namespaces/test-namespace")
expected.SetDnsDomain("test-namespace.servicebus.windows.net")
expected.SetProvider("Azure")
expected.SetRegion("US_EAST_2")

mockStore.
EXPECT().
// This test does not assert the parameters passed to the store method
DescribePrivateLinkEndpoint(gomock.Any(), gomock.Any()).
Return(expected, nil).
Times(1)

require.NoError(t, describeOpts.Run())
test.VerifyOutputTemplate(t, describeTemplate, expected)
})
}
1 change: 1 addition & 0 deletions internal/cli/streams/privatelink/privatelink.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Builder() *cobra.Command {

cmd.AddCommand(
CreateBuilder(),
DescribeBuilder(),
)

return cmd
Expand Down
40 changes: 39 additions & 1 deletion internal/mocks/mock_streams.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion internal/store/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
atlasv2 "go.mongodb.org/atlas-sdk/v20241113004/admin"
)

//go:generate mockgen -destination=../mocks/mock_streams.go -package=mocks github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store StreamsLister,StreamsDescriber,StreamsCreator,StreamsDeleter,StreamsUpdater,StreamsDownloader,ConnectionCreator,ConnectionDeleter,ConnectionUpdater,StreamsConnectionDescriber,StreamsConnectionLister,PrivateLinkCreator
//go:generate mockgen -destination=../mocks/mock_streams.go -package=mocks github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store StreamsLister,StreamsDescriber,StreamsCreator,StreamsDeleter,StreamsUpdater,StreamsDownloader,ConnectionCreator,ConnectionDeleter,ConnectionUpdater,StreamsConnectionDescriber,StreamsConnectionLister,PrivateLinkCreator,PrivateLinkDescriber

type StreamsLister interface {
ProjectStreams(*atlasv2.ListStreamInstancesApiParams) (*atlasv2.PaginatedApiStreamsTenant, error)
Expand Down Expand Up @@ -71,6 +71,10 @@ type PrivateLinkCreator interface {
CreatePrivateLinkEndpoint(projectID string, connection *atlasv2.StreamsPrivateLinkConnection) (*atlasv2.StreamsPrivateLinkConnection, error)
}

type PrivateLinkDescriber interface {
DescribePrivateLinkEndpoint(projectID, connectionID string) (*atlasv2.StreamsPrivateLinkConnection, error)
}

func (s *Store) ProjectStreams(opts *atlasv2.ListStreamInstancesApiParams) (*atlasv2.PaginatedApiStreamsTenant, error) {
result, _, err := s.clientv2.StreamsApi.ListStreamInstancesWithParams(s.ctx, opts).Execute()
return result, err
Expand Down Expand Up @@ -141,3 +145,8 @@ func (s *Store) CreatePrivateLinkEndpoint(projectID string, connection *atlasv2.
result, _, err := s.clientv2.StreamsApi.CreatePrivateLinkConnection(s.ctx, projectID, connection).Execute()
return result, err
}

func (s *Store) DescribePrivateLinkEndpoint(projectID, connectionID string) (*atlasv2.StreamsPrivateLinkConnection, error) {
result, _, err := s.clientv2.StreamsApi.GetPrivateLinkConnection(s.ctx, projectID, connectionID).Execute()
return result, err
}
1 change: 1 addition & 0 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
| `streams instance log` | Y | Y |
| `streams privateLink` | | |
| `streams privateLink create` | Y | Y |
| `streams privateLink describe` | Y | Y |
| `config` | | |
| `completion` | Y | Y |
| `config delete` | Y | Y |
Expand Down
Loading