From 88e00c41f4120761dbf6867799317b4eda21cd58 Mon Sep 17 00:00:00 2001 From: Bianca Date: Wed, 20 Mar 2024 17:21:19 +0000 Subject: [PATCH] WIP --- internal/cli/atlas/alerts/list.go | 1 + internal/cli/atlas/customdbroles/create.go | 2 +- internal/cli/atlas/deployments/list.go | 4 -- internal/cli/atlas/deployments/logs.go | 3 -- .../atlas/deployments/options/connect_opts.go | 2 - .../deployments/options/deployment_opts.go | 22 +++++---- .../options/deployment_opts_telemetry.go | 36 +++++++++++++++ .../mocks/mock_deployment_opts_telemetry.go | 46 +++++++++++++++++++ 8 files changed, 96 insertions(+), 20 deletions(-) create mode 100644 internal/cli/atlas/deployments/options/deployment_opts_telemetry.go create mode 100644 internal/mocks/mock_deployment_opts_telemetry.go diff --git a/internal/cli/atlas/alerts/list.go b/internal/cli/atlas/alerts/list.go index fc8ec12582..8d5ab096d9 100644 --- a/internal/cli/atlas/alerts/list.go +++ b/internal/cli/atlas/alerts/list.go @@ -92,6 +92,7 @@ func ListBuilder() *cobra.Command { opts.ValidateProjectID, opts.initStore(cmd.Context()), opts.InitOutput(cmd.OutOrStdout(), listTemplate), + // opts.PreRun(), ) }, RunE: func(_ *cobra.Command, _ []string) error { diff --git a/internal/cli/atlas/customdbroles/create.go b/internal/cli/atlas/customdbroles/create.go index 20be699213..9837f7f207 100644 --- a/internal/cli/atlas/customdbroles/create.go +++ b/internal/cli/atlas/customdbroles/create.go @@ -61,7 +61,7 @@ func (opts *CreateOpts) Run() error { } func (opts *CreateOpts) newCustomDBRole() *atlasv2.UserCustomDBRole { - actions := convert.BuildAtlasActions(opts.action) + actions := joinActions(convert.BuildAtlasActions(opts.action)) inheritedRoles := convert.BuildAtlasInheritedRoles(opts.inheritedRoles) return &atlasv2.UserCustomDBRole{ RoleName: opts.roleName, diff --git a/internal/cli/atlas/deployments/list.go b/internal/cli/atlas/deployments/list.go index df75990293..b9278c4fa6 100644 --- a/internal/cli/atlas/deployments/list.go +++ b/internal/cli/atlas/deployments/list.go @@ -70,10 +70,6 @@ func (opts *ListOpts) Run(ctx context.Context) error { return nil } -func (opts *ListOpts) PostRun() error { - return opts.PostRunMessages() -} - // ListBuilder atlas deployments list. func ListBuilder() *cobra.Command { opts := &ListOpts{} diff --git a/internal/cli/atlas/deployments/logs.go b/internal/cli/atlas/deployments/logs.go index f6496940e2..bab5bea6da 100644 --- a/internal/cli/atlas/deployments/logs.go +++ b/internal/cli/atlas/deployments/logs.go @@ -30,7 +30,6 @@ import ( "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/search" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" - "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/telemetry" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage" "github.com/spf13/afero" "github.com/spf13/cobra" @@ -67,12 +66,10 @@ func (opts *DownloadOpts) Run(ctx context.Context) error { } if opts.IsLocalDeploymentType() { - telemetry.AppendOption(telemetry.WithDeploymentType(options.LocalCluster)) return opts.RunLocal(ctx) } if opts.IsAtlasDeploymentType() { - telemetry.AppendOption(telemetry.WithDeploymentType(options.LocalCluster)) if err := opts.validateAtlasFlags(); err != nil { return err } diff --git a/internal/cli/atlas/deployments/options/connect_opts.go b/internal/cli/atlas/deployments/options/connect_opts.go index 9f3ff87b7d..b56effc094 100644 --- a/internal/cli/atlas/deployments/options/connect_opts.go +++ b/internal/cli/atlas/deployments/options/connect_opts.go @@ -53,7 +53,6 @@ func (opts *ConnectOpts) Connect(ctx context.Context) error { } if opts.IsAtlasDeploymentType() { - telemetry.AppendOption(telemetry.WithDeploymentType(AtlasCluster)) if err := opts.validateAndPromptAtlasOpts(); err != nil { return err } @@ -61,7 +60,6 @@ func (opts *ConnectOpts) Connect(ctx context.Context) error { return opts.connectToAtlas() } - telemetry.AppendOption(telemetry.WithDeploymentType(LocalCluster)) return opts.connectToLocal(ctx) } diff --git a/internal/cli/atlas/deployments/options/deployment_opts.go b/internal/cli/atlas/deployments/options/deployment_opts.go index 746e856364..5b03607949 100644 --- a/internal/cli/atlas/deployments/options/deployment_opts.go +++ b/internal/cli/atlas/deployments/options/deployment_opts.go @@ -96,6 +96,7 @@ type DeploymentOpts struct { DefaultSetter cli.DefaultSetterOpts AtlasClusterListStore store.ClusterLister Config setup.ProfileReader + DeploymentTelemetry DeploymentTypeTelemetry } type Deployment struct { @@ -115,6 +116,7 @@ func (opts *DeploymentOpts) InitStore(ctx context.Context, writer io.Writer) fun return err } opts.DefaultSetter.OutWriter = writer + opts.DeploymentTelemetry = NewDeploymentTypeTelemetry(opts) return opts.DefaultSetter.InitStore(ctx) } } @@ -242,19 +244,11 @@ func (opts *DeploymentOpts) ValidateAndPromptDeploymentType() error { } func (opts *DeploymentOpts) IsAtlasDeploymentType() bool { - if strings.EqualFold(opts.DeploymentType, AtlasCluster) { - telemetry.AppendOption(telemetry.WithDeploymentType(AtlasCluster)) - return true - } - return false + return strings.EqualFold(opts.DeploymentType, AtlasCluster) } func (opts *DeploymentOpts) IsLocalDeploymentType() bool { - if strings.EqualFold(opts.DeploymentType, LocalCluster) { - telemetry.AppendOption(telemetry.WithDeploymentType(LocalCluster)) - return true - } - return false + return strings.EqualFold(opts.DeploymentType, LocalCluster) } func (opts *DeploymentOpts) NoDeploymentTypeSet() bool { @@ -264,3 +258,11 @@ func (opts *DeploymentOpts) NoDeploymentTypeSet() bool { func (opts *DeploymentOpts) IsAuthEnabled() bool { return opts.DBUsername != "" } + +func (opts *DeploymentOpts) PreRun() { + opts.DeploymentTelemetry.AppendDeploymentType() +} +func (opts *DeploymentOpts) PostRun() error { + opts.DeploymentTelemetry.AppendDeploymentType() + return opts.PostRunMessages() +} diff --git a/internal/cli/atlas/deployments/options/deployment_opts_telemetry.go b/internal/cli/atlas/deployments/options/deployment_opts_telemetry.go new file mode 100644 index 0000000000..f4781fb752 --- /dev/null +++ b/internal/cli/atlas/deployments/options/deployment_opts_telemetry.go @@ -0,0 +1,36 @@ +// 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 options + +import ( + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/telemetry" +) + +//go:generate mockgen -destination=../../../../mocks/mock_deployment_opts_telemetry.go -package=mocks github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/atlas/deployments/options DeploymentTypeTelemetry +type DeploymentTypeTelemetry interface { + AppendDeploymentType() +} + +func NewDeploymentTypeTelemetry(opts *DeploymentOpts) DeploymentTypeTelemetry { + return opts +} + +func (opts *DeploymentOpts) AppendDeploymentType() { + if opts.IsLocalDeploymentType() { + telemetry.AppendOption(telemetry.WithDeploymentType(LocalCluster)) + } else if opts.IsAtlasDeploymentType() { + telemetry.AppendOption(telemetry.WithDeploymentType(AtlasCluster)) + } +} diff --git a/internal/mocks/mock_deployment_opts_telemetry.go b/internal/mocks/mock_deployment_opts_telemetry.go new file mode 100644 index 0000000000..84e4f98813 --- /dev/null +++ b/internal/mocks/mock_deployment_opts_telemetry.go @@ -0,0 +1,46 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/atlas/deployments/options (interfaces: DeploymentTypeTelemetry) + +// Package mocks is a generated GoMock package. +package mocks + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" +) + +// MockDeploymentTypeTelemetry is a mock of DeploymentTypeTelemetry interface. +type MockDeploymentTypeTelemetry struct { + ctrl *gomock.Controller + recorder *MockDeploymentTypeTelemetryMockRecorder +} + +// MockDeploymentTypeTelemetryMockRecorder is the mock recorder for MockDeploymentTypeTelemetry. +type MockDeploymentTypeTelemetryMockRecorder struct { + mock *MockDeploymentTypeTelemetry +} + +// NewMockDeploymentTypeTelemetry creates a new mock instance. +func NewMockDeploymentTypeTelemetry(ctrl *gomock.Controller) *MockDeploymentTypeTelemetry { + mock := &MockDeploymentTypeTelemetry{ctrl: ctrl} + mock.recorder = &MockDeploymentTypeTelemetryMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockDeploymentTypeTelemetry) EXPECT() *MockDeploymentTypeTelemetryMockRecorder { + return m.recorder +} + +// AppendDeploymentType mocks base method. +func (m *MockDeploymentTypeTelemetry) AppendDeploymentType() { + m.ctrl.T.Helper() + m.ctrl.Call(m, "AppendDeploymentType") +} + +// AppendDeploymentType indicates an expected call of AppendDeploymentType. +func (mr *MockDeploymentTypeTelemetryMockRecorder) AppendDeploymentType() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AppendDeploymentType", reflect.TypeOf((*MockDeploymentTypeTelemetry)(nil).AppendDeploymentType)) +}