Skip to content

Commit 5cebd9c

Browse files
authored
CLOUDP-378545: Add new GCP PSC command to atlas cli (#4455)
1 parent cee6ae7 commit 5cebd9c

6 files changed

Lines changed: 59 additions & 10 deletions

File tree

docs/command/atlas-privateEndpoints-gcp-create.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ Options
4545
- string
4646
- false
4747
- Output format. Valid values are json, json-path, go-template, or go-template-file. To see the full output, use the -o json option.
48+
* - --portMappingEnabled
49+
-
50+
- false
51+
- Flag that indicates whether this endpoint service uses PSC port-mapping. This is only applicable for GCP Private Endpoint Services.
4852
* - --projectId
4953
- string
5054
- false
@@ -86,4 +90,4 @@ Examples
8690
.. code-block::
8791
:copyable: false
8892

89-
atlas privateEndpoints gcp create --region CENTRAL_US
93+
atlas privateEndpoints gcp create --region CENTRAL_US --portMappingEnabled

internal/cli/privateendpoints/gcp/create.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ type PrivateEndpointCreator interface {
3737
type CreateOpts struct {
3838
cli.ProjectOpts
3939
cli.OutputOpts
40-
store PrivateEndpointCreator
41-
region string
40+
store PrivateEndpointCreator
41+
region string
42+
portMappingEnabled bool
4243
}
4344

4445
func (opts *CreateOpts) initStore(ctx context.Context) func() error {
@@ -64,13 +65,14 @@ func (opts *CreateOpts) Run() error {
6465

6566
func (opts *CreateOpts) newPrivateEndpointConnection() *atlasv2.CloudProviderEndpointServiceRequest {
6667
createRequest := &atlasv2.CloudProviderEndpointServiceRequest{
67-
Region: opts.region,
68-
ProviderName: provider,
68+
Region: opts.region,
69+
ProviderName: provider,
70+
PortMappingEnabled: &opts.portMappingEnabled,
6971
}
7072
return createRequest
7173
}
7274

73-
// atlas privateEndpoints gcp create --region region [--projectId projectId].
75+
// atlas privateEndpoints gcp create --region region [--portMappingEnabled] [--projectId projectId].
7476
func CreateBuilder() *cobra.Command {
7577
opts := &CreateOpts{}
7678
cmd := &cobra.Command{
@@ -81,7 +83,7 @@ func CreateBuilder() *cobra.Command {
8183
Annotations: map[string]string{
8284
"output": createTemplate,
8385
},
84-
Example: ` atlas privateEndpoints gcp create --region CENTRAL_US`,
86+
Example: ` atlas privateEndpoints gcp create --region CENTRAL_US --portMappingEnabled`,
8587
PreRunE: func(cmd *cobra.Command, _ []string) error {
8688
return opts.PreRunE(
8789
opts.ValidateProjectID,
@@ -94,6 +96,7 @@ func CreateBuilder() *cobra.Command {
9496
},
9597
}
9698
cmd.Flags().StringVar(&opts.region, flag.Region, "", usage.PrivateEndpointRegion)
99+
cmd.Flags().BoolVar(&opts.portMappingEnabled, flag.PortMappingEnabled, false, usage.PortMappingEnabled)
97100

98101
opts.AddProjectOptsFlags(cmd)
99102
opts.AddOutputOptFlags(cmd)

internal/cli/privateendpoints/gcp/create_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,44 @@ func TestCreate_Run(t *testing.T) {
4444

4545
test.VerifyOutputTemplate(t, createTemplate, expected)
4646
}
47+
48+
func TestCreate_Run_WithPortMappingEnabled(t *testing.T) {
49+
ctrl := gomock.NewController(t)
50+
mockStore := NewMockPrivateEndpointCreator(ctrl)
51+
52+
createOpts := &CreateOpts{
53+
store: mockStore,
54+
region: "region",
55+
portMappingEnabled: true,
56+
}
57+
58+
expected := &atlasv2.EndpointService{}
59+
mockStore.
60+
EXPECT().
61+
CreatePrivateEndpoint(createOpts.ProjectID, createOpts.newPrivateEndpointConnection()).
62+
Return(expected, nil).
63+
Times(1)
64+
65+
err := createOpts.Run()
66+
require.NoError(t, err)
67+
68+
// Verify that portMappingEnabled is set correctly in the request
69+
req := createOpts.newPrivateEndpointConnection()
70+
require.NotNil(t, req.PortMappingEnabled)
71+
require.True(t, *req.PortMappingEnabled)
72+
73+
test.VerifyOutputTemplate(t, createTemplate, expected)
74+
}
75+
76+
func TestCreate_Run_PortMappingEnabledDefaultFalse(t *testing.T) {
77+
createOpts := &CreateOpts{
78+
region: "region",
79+
}
80+
81+
// Verify that portMappingEnabled defaults to false when not specified
82+
require.False(t, createOpts.portMappingEnabled)
83+
84+
req := createOpts.newPrivateEndpointConnection()
85+
require.NotNil(t, req.PortMappingEnabled)
86+
require.False(t, *req.PortMappingEnabled)
87+
}

internal/cli/privateendpoints/gcp/describe.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ import (
2727
atlasv2 "go.mongodb.org/atlas-sdk/v20250312014/admin"
2828
)
2929

30-
var describeTemplate = `ID GROUP NAME REGION STATUS ERROR{{if .EndpointGroupNames}}{{range valueOrEmptySlice .EndpointGroupNames}}
31-
{{$.Id}} {{.}} {{$.RegionName}} {{$.Status}} {{$.ErrorMessage}}{{end}}{{else}}
32-
{{$.Id}} N/A {{$.RegionName}} {{$.Status}} {{$.ErrorMessage}}{{end}}
30+
var describeTemplate = `ID GROUP NAME REGION STATUS PORT MAPPING ENABLED ERROR
31+
{{.Id}} {{if and .EndpointGroupNames (gt (len .EndpointGroupNames) 0)}}{{range $i, $g := .EndpointGroupNames}}{{if $i}}, {{end}}{{$g}}{{end}}{{else}}N/A{{end}} {{.RegionName}} {{.Status}} {{.PortMappingEnabled}} {{.ErrorMessage}}
3332
`
3433

3534
//go:generate go tool go.uber.org/mock/mockgen -typed -destination=describe_mock_test.go -package=gcp . PrivateEndpointDescriber

internal/flag/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ const (
170170
PrivateEndpointID = "privateEndpointId" // PrivateEndpointID flag
171171
EndpointServiceID = "endpointServiceId" // EndpointServiceId flag
172172
PrivateEndpointIPAddress = "privateEndpointIpAddress" // PrivateEndpointIPAddress flag
173+
PortMappingEnabled = "portMappingEnabled" // PortMappingEnabled flag
173174
Endpoint = "endpoint" // Endpoint flag
174175
Retention = "retention" // Retention flag
175176
AtlasCIDRBlock = "atlasCidrBlock" // AtlasCIDRBlock flag

internal/usage/usage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ dbName and collection are required only for built-in roles.`
8484
DataLakeTestBucket = "Name of an Amazon S3 data bucket that Atlas Data Federation uses to validate the provided role."
8585
PrivateEndpointRegion = "Cloud provider region in which you want to create the private endpoint connection. For a complete list of supported AWS regions, see: https://dochub.mongodb.org/core/aws-atlas. For a complete list of supported Azure regions, see: https://dochub.mongodb.org/core/azure-atlas. For a complete list of supported GCP regions, see: https://dochub.mongodb.org/core/gcp-atlas."
8686
PrivateEndpointProvider = "Name of the cloud provider you want to create the private endpoint connection for."
87+
PortMappingEnabled = "Flag that indicates whether this endpoint service uses PSC port-mapping. This is only applicable for GCP Private Endpoint Services."
8788
Comment = "Optional description or comment for the entry."
8889
AccessListsDeleteAfter = "ISO-8601-formatted UTC date after which Atlas removes the entry from the access list."
8990
BDUsersDeleteAfter = "Timestamp in ISO 8601 in UTC after which Atlas deletes the user."

0 commit comments

Comments
 (0)