diff --git a/internal/cli/atlas/accesslists/create.go b/internal/cli/atlas/accesslists/create.go index 1adb1063ec..ae2823d9dd 100644 --- a/internal/cli/atlas/accesslists/create.go +++ b/internal/cli/atlas/accesslists/create.go @@ -22,10 +22,10 @@ import ( "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/convert" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" - "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/time" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage" "github.com/spf13/cobra" atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin" @@ -79,7 +79,7 @@ func (opts *CreateOpts) newProjectIPAccessList() ([]*atlasv2.NetworkPermissionEn Comment: &opts.comment, } if opts.deleteAfter != "" { - deleteAfterDate, err := time.ParseTimestamp(opts.deleteAfter) + deleteAfterDate, err := convert.ParseTimestamp(opts.deleteAfter) if err != nil { return nil, err } diff --git a/internal/cli/atlas/alerts/acknowledge.go b/internal/cli/atlas/alerts/acknowledge.go index 29520f531d..9ef642f32e 100644 --- a/internal/cli/atlas/alerts/acknowledge.go +++ b/internal/cli/atlas/alerts/acknowledge.go @@ -22,9 +22,9 @@ import ( "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/convert" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" - store "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store/atlas" - customTime "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/time" + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage" "github.com/spf13/cobra" "go.mongodb.org/atlas-sdk/v20231115007/admin" @@ -76,12 +76,12 @@ func (opts *AcknowledgeOpts) newAcknowledgeRequest() (*admin.AlertViewForNdsGrou const years = 100 opts.until = time.Now().AddDate(years, 1, 1).Format(time.RFC3339) } - time, err := customTime.ParseTimestamp(opts.until) + until, err := convert.ParseTimestamp(opts.until) if err != nil { return nil, err } return &admin.AlertViewForNdsGroup{ - AcknowledgedUntil: &time, + AcknowledgedUntil: &until, AcknowledgementComment: &opts.comment, }, nil } diff --git a/internal/cli/atlas/alerts/acknowledge_test.go b/internal/cli/atlas/alerts/acknowledge_test.go index 6768423db1..8c156e9f38 100644 --- a/internal/cli/atlas/alerts/acknowledge_test.go +++ b/internal/cli/atlas/alerts/acknowledge_test.go @@ -22,7 +22,7 @@ import ( "github.com/golang/mock/gomock" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" - mocks "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks/atlas" + "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/v20231115007/admin" diff --git a/internal/cli/atlas/alerts/describe.go b/internal/cli/atlas/alerts/describe.go index dc32bcadbd..80621c1415 100644 --- a/internal/cli/atlas/alerts/describe.go +++ b/internal/cli/atlas/alerts/describe.go @@ -22,7 +22,7 @@ import ( "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/flag" - store "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store/atlas" + "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/v20231115007/admin" diff --git a/internal/cli/atlas/alerts/describe_test.go b/internal/cli/atlas/alerts/describe_test.go index 5bf98be40d..162a023888 100644 --- a/internal/cli/atlas/alerts/describe_test.go +++ b/internal/cli/atlas/alerts/describe_test.go @@ -24,7 +24,7 @@ import ( "github.com/golang/mock/gomock" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" - mocks "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks/atlas" + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test" "github.com/stretchr/testify/assert" diff --git a/internal/cli/atlas/alerts/list.go b/internal/cli/atlas/alerts/list.go index a39ec16c54..ef8405444e 100644 --- a/internal/cli/atlas/alerts/list.go +++ b/internal/cli/atlas/alerts/list.go @@ -22,8 +22,7 @@ import ( "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/flag" - "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" - store "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store/atlas" + "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/v20231115007/admin" @@ -54,7 +53,9 @@ func (opts *ListOpts) Run() error { GroupId: opts.ConfigProjectID(), ItemsPerPage: &opts.ItemsPerPage, PageNum: &opts.PageNum, - Status: pointer.GetStringPointerIfNotEmpty(opts.status), + } + if opts.status != "" { + params.Status = &opts.status } r, err := opts.store.Alerts(params) diff --git a/internal/cli/atlas/alerts/list_test.go b/internal/cli/atlas/alerts/list_test.go index 68e255ade3..d35ebb8743 100644 --- a/internal/cli/atlas/alerts/list_test.go +++ b/internal/cli/atlas/alerts/list_test.go @@ -22,7 +22,7 @@ import ( "github.com/golang/mock/gomock" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli" - mocks "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks/atlas" + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test" atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin" diff --git a/internal/cli/atlas/alerts/unacknowledge.go b/internal/cli/atlas/alerts/unacknowledge.go index 3628b5358b..c8e4e03512 100644 --- a/internal/cli/atlas/alerts/unacknowledge.go +++ b/internal/cli/atlas/alerts/unacknowledge.go @@ -22,7 +22,7 @@ import ( "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/flag" - store "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store/atlas" + "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/v20231115007/admin" diff --git a/internal/cli/atlas/alerts/unacknowledge_test.go b/internal/cli/atlas/alerts/unacknowledge_test.go index 99aeca7856..de0e36dfec 100644 --- a/internal/cli/atlas/alerts/unacknowledge_test.go +++ b/internal/cli/atlas/alerts/unacknowledge_test.go @@ -20,7 +20,7 @@ import ( "testing" "github.com/golang/mock/gomock" - mocks "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks/atlas" + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks" atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin" ) diff --git a/internal/cli/atlas/datalakepipelines/availablesnapshots/list.go b/internal/cli/atlas/datalakepipelines/availablesnapshots/list.go index 9391de8047..7546969e05 100644 --- a/internal/cli/atlas/datalakepipelines/availablesnapshots/list.go +++ b/internal/cli/atlas/datalakepipelines/availablesnapshots/list.go @@ -25,9 +25,9 @@ import ( "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/convert" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" store "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store/atlas" - customTime "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/time" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage" "github.com/spf13/cobra" ) @@ -57,14 +57,14 @@ func (opts *ListOpts) initStore(ctx context.Context) func() error { func convertTime(value string) *time.Time { var result *time.Time - if completedAfter, err := customTime.ParseTimestamp(value); err == nil { + if completedAfter, err := convert.ParseTimestamp(value); err == nil { result = &completedAfter } return result } func (opts *ListOpts) validate() error { - if _, err := customTime.ParseTimestamp(opts.completedAfter); opts.completedAfter != "" && err != nil { + if _, err := convert.ParseTimestamp(opts.completedAfter); opts.completedAfter != "" && err != nil { return fmt.Errorf("%w: expected format 'YYYY-MM-DD' got '%s'", ErrCompletedAfterInvalidFormat, opts.completedAfter) } return nil diff --git a/internal/cli/atlas/dbusers/create.go b/internal/cli/atlas/dbusers/create.go index aa84ed8dba..f6cc8317b8 100644 --- a/internal/cli/atlas/dbusers/create.go +++ b/internal/cli/atlas/dbusers/create.go @@ -24,7 +24,6 @@ import ( "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/convert" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" - "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" "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" @@ -106,18 +105,29 @@ func (opts *CreateOpts) newDatabaseUser() *atlasv2.CloudDatabaseUser { authDB = convert.ExternalAuthDB } - return &atlasv2.CloudDatabaseUser{ - Roles: pointer.Get(convert.BuildAtlasRoles(opts.roles)), - Scopes: pointer.Get(convert.BuildAtlasScopes(opts.scopes)), + roles := convert.BuildAtlasRoles(opts.roles) + scopes := convert.BuildAtlasScopes(opts.scopes) + u := &atlasv2.CloudDatabaseUser{ + Roles: &roles, + Scopes: &scopes, GroupId: opts.ConfigProjectID(), Username: opts.username, - Password: pointer.GetStringPointerIfNotEmpty(opts.password), - X509Type: pointer.GetStringPointerIfNotEmpty(opts.x509Type), - AwsIAMType: pointer.GetStringPointerIfNotEmpty(opts.awsIamType), - LdapAuthType: pointer.GetStringPointerIfNotEmpty(opts.ldapType), DeleteAfterDate: convert.ParseDeleteAfter(opts.deleteAfter), DatabaseName: authDB, } + if opts.password != "" { + u.Password = &opts.password + } + if opts.x509Type != "" { + u.X509Type = &opts.x509Type + } + if opts.awsIamType != "" { + u.AwsIAMType = &opts.awsIamType + } + if opts.ldapType != "" { + u.LdapAuthType = &opts.ldapType + } + return u } func (opts *CreateOpts) Prompt() error { diff --git a/internal/cli/atlas/dbusers/update.go b/internal/cli/atlas/dbusers/update.go index 3e7eaa9a77..cf6b57e0fe 100644 --- a/internal/cli/atlas/dbusers/update.go +++ b/internal/cli/atlas/dbusers/update.go @@ -23,7 +23,6 @@ import ( "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/convert" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" - "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/validate" @@ -80,15 +79,20 @@ func (opts *UpdateOpts) update(out *admin.CloudDatabaseUser) { out.Username = opts.currentUsername } if opts.password != "" { - out.Password = pointer.GetStringPointerIfNotEmpty(opts.password) + out.Password = &opts.password } - out.Scopes = pointer.Get(convert.BuildAtlasScopes(opts.scopes)) - out.Roles = pointer.Get(convert.BuildAtlasRoles(opts.roles)) + + roles := convert.BuildAtlasRoles(opts.roles) + out.Roles = &roles + scopes := convert.BuildAtlasScopes(opts.scopes) + out.Scopes = &scopes out.DatabaseName = opts.authDB if opts.authDB == "" { out.DatabaseName = convert.GetAuthDB(out) } - out.X509Type = pointer.GetStringPointerIfNotEmpty(opts.x509Type) + if opts.x509Type != "" { + out.X509Type = &opts.x509Type + } } func (opts *UpdateOpts) validateAuthDB() error { diff --git a/internal/cli/atlas/deployments/pause_test.go b/internal/cli/atlas/deployments/pause_test.go index 4106fcadaf..b3b3dc93fe 100644 --- a/internal/cli/atlas/deployments/pause_test.go +++ b/internal/cli/atlas/deployments/pause_test.go @@ -109,7 +109,7 @@ func TestPause_RunAtlas(t *testing.T) { PauseCluster(projectID, deploymentName). Return( &admin.AdvancedClusterDescription{ - Name: pointer.GetStringPointerIfNotEmpty(deploymentName), + Name: pointer.Get(deploymentName), }, nil). Times(1) diff --git a/internal/cli/atlas/deployments/search/indexes/describe_test.go b/internal/cli/atlas/deployments/search/indexes/describe_test.go index fcec6978f3..c4a4a411b4 100644 --- a/internal/cli/atlas/deployments/search/indexes/describe_test.go +++ b/internal/cli/atlas/deployments/search/indexes/describe_test.go @@ -41,10 +41,7 @@ func TestDescribe_RunLocal(t *testing.T) { ctx := context.Background() const ( - expectedIndexName = "idx1" expectedLocalDeployment = "localDeployment1" - expectedDB = "db1" - expectedCollection = "col1" expectedStatus = "STEADY" expectedType = "search" ) @@ -108,11 +105,11 @@ func TestDescribe_RunLocal(t *testing.T) { expected := &atlasv2.ClusterSearchIndex{ Name: "name", - IndexID: pointer.GetStringPointerIfNotEmpty("test"), + IndexID: pointer.Get("test"), CollectionName: "coll", Database: "db", - Status: pointer.GetStringPointerIfNotEmpty(expectedStatus), - Type: pointer.GetStringPointerIfNotEmpty(expectedType), + Status: pointer.Get(expectedStatus), + Type: pointer.Get(expectedType), } mockMongodbClient. @@ -170,13 +167,13 @@ func TestDescribe_RunAtlas(t *testing.T) { Name: "name", Database: "db", CollectionName: "coll", - IndexID: pointer.GetStringPointerIfNotEmpty("test"), + IndexID: pointer.Get("test"), }, nil). Times(1) expected := &atlasv2.ClusterSearchIndex{ Name: "name", - IndexID: pointer.GetStringPointerIfNotEmpty("test"), + IndexID: pointer.Get("test"), CollectionName: "coll", Database: "db", } diff --git a/internal/cli/atlas/deployments/search/indexes/list_test.go b/internal/cli/atlas/deployments/search/indexes/list_test.go index 6a464f9c7c..b02374c9a3 100644 --- a/internal/cli/atlas/deployments/search/indexes/list_test.go +++ b/internal/cli/atlas/deployments/search/indexes/list_test.go @@ -120,11 +120,11 @@ func TestList_RunLocal(t *testing.T) { expected := []*atlasv2.ClusterSearchIndex{ { Name: expectedName, - IndexID: pointer.GetStringPointerIfNotEmpty(expectedID), + IndexID: pointer.Get(expectedID), CollectionName: expectedCollection, Database: expectedDB, - Status: pointer.GetStringPointerIfNotEmpty(expectedStatus), - Type: pointer.GetStringPointerIfNotEmpty(expectedType), + Status: pointer.Get(expectedStatus), + Type: pointer.Get(expectedType), }, } @@ -189,7 +189,7 @@ func TestList_RunAtlas(t *testing.T) { Name: expectedName, Database: expectedDB, CollectionName: expectedCollection, - IndexID: pointer.GetStringPointerIfNotEmpty(expectedID), + IndexID: pointer.Get(expectedID), }, }, nil). Times(1) @@ -197,7 +197,7 @@ func TestList_RunAtlas(t *testing.T) { expected := []*atlasv2.ClusterSearchIndex{ { Name: expectedName, - IndexID: pointer.GetStringPointerIfNotEmpty(expectedID), + IndexID: pointer.Get(expectedID), CollectionName: expectedCollection, Database: expectedDB, }, diff --git a/internal/cli/atlas/deployments/start_test.go b/internal/cli/atlas/deployments/start_test.go index fdae8a8b61..47546a69bb 100644 --- a/internal/cli/atlas/deployments/start_test.go +++ b/internal/cli/atlas/deployments/start_test.go @@ -177,7 +177,7 @@ func TestStart_RunAtlas(t *testing.T) { StartCluster(projectID, deploymentName). Return( &admin.AdvancedClusterDescription{ - Name: pointer.GetStringPointerIfNotEmpty(deploymentName), + Name: pointer.Get(deploymentName), }, nil). Times(1) diff --git a/internal/cli/atlas/events/list.go b/internal/cli/atlas/events/list.go index 9ae6af6998..fb7c6a097c 100644 --- a/internal/cli/atlas/events/list.go +++ b/internal/cli/atlas/events/list.go @@ -21,6 +21,7 @@ import ( "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/convert" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" @@ -82,8 +83,12 @@ func (opts *ListOpts) NewOrgListOptions() admin.ListOrganizationEventsApiParams p := admin.ListOrganizationEventsApiParams{ OrgId: opts.orgID, EventType: eventType, - MaxDate: pointer.StringToTimePointer(opts.MaxDate), - MinDate: pointer.StringToTimePointer(opts.MinDate), + } + if maxDate, err := convert.ParseTimestamp(opts.MaxDate); err == nil { + p.MaxDate = pointer.Get(maxDate) + } + if minDate, err := convert.ParseTimestamp(opts.MinDate); err == nil { + p.MinDate = pointer.Get(minDate) } if opts.ItemsPerPage > 0 { p.ItemsPerPage = &opts.ItemsPerPage @@ -91,6 +96,9 @@ func (opts *ListOpts) NewOrgListOptions() admin.ListOrganizationEventsApiParams if opts.PageNum > 0 { p.PageNum = &opts.PageNum } + if opts.OmitCount { + p.IncludeCount = pointer.Get(false) + } return p } @@ -102,8 +110,12 @@ func (opts *ListOpts) NewProjectListOptions() admin.ListProjectEventsApiParams { p := admin.ListProjectEventsApiParams{ GroupId: opts.projectID, EventType: eventType, - MaxDate: pointer.StringToTimePointer(opts.MaxDate), - MinDate: pointer.StringToTimePointer(opts.MinDate), + } + if maxDate, err := convert.ParseTimestamp(opts.MaxDate); err == nil { + p.MaxDate = pointer.Get(maxDate) + } + if minDate, err := convert.ParseTimestamp(opts.MinDate); err == nil { + p.MinDate = pointer.Get(minDate) } if opts.ItemsPerPage > 0 { p.ItemsPerPage = &opts.ItemsPerPage @@ -111,7 +123,6 @@ func (opts *ListOpts) NewProjectListOptions() admin.ListProjectEventsApiParams { if opts.PageNum > 0 { p.PageNum = &opts.PageNum } - if opts.OmitCount { p.IncludeCount = pointer.Get(false) } diff --git a/internal/cli/atlas/events/orgs_list.go b/internal/cli/atlas/events/orgs_list.go index e11f9af6fd..515924c39f 100644 --- a/internal/cli/atlas/events/orgs_list.go +++ b/internal/cli/atlas/events/orgs_list.go @@ -20,6 +20,7 @@ import ( "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/convert" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" @@ -56,15 +57,13 @@ func (opts *orgListOpts) Run() error { } func (opts *orgListOpts) NewOrgListOptions() admin.ListOrganizationEventsApiParams { - var eventType *[]string + var eventType []string if len(opts.EventType) > 0 { - eventType = &opts.EventType + eventType = opts.EventType } p := admin.ListOrganizationEventsApiParams{ OrgId: opts.ConfigOrgID(), - EventType: eventType, - MaxDate: pointer.StringToTimePointer(opts.MaxDate), - MinDate: pointer.StringToTimePointer(opts.MinDate), + EventType: &eventType, } if opts.ItemsPerPage > 0 { p.ItemsPerPage = &opts.ItemsPerPage @@ -75,6 +74,12 @@ func (opts *orgListOpts) NewOrgListOptions() admin.ListOrganizationEventsApiPara if opts.OmitCount { p.IncludeCount = pointer.Get(false) } + if maxDate, err := convert.ParseTimestamp(opts.MaxDate); err == nil { + p.MaxDate = pointer.Get(maxDate) + } + if minDate, err := convert.ParseTimestamp(opts.MinDate); err == nil { + p.MinDate = pointer.Get(minDate) + } return p } diff --git a/internal/cli/atlas/events/projects_list.go b/internal/cli/atlas/events/projects_list.go index 0bdb416650..a01b719085 100644 --- a/internal/cli/atlas/events/projects_list.go +++ b/internal/cli/atlas/events/projects_list.go @@ -20,6 +20,7 @@ import ( "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/convert" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" @@ -67,17 +68,19 @@ func (opts *projectListOpts) NewProjectListOptions() admin.ListProjectEventsApiP p := admin.ListProjectEventsApiParams{ GroupId: opts.ConfigProjectID(), EventType: eventType, - MaxDate: pointer.StringToTimePointer(opts.MaxDate), - MinDate: pointer.StringToTimePointer(opts.MinDate), } - + if maxDate, err := convert.ParseTimestamp(opts.MaxDate); err == nil { + p.MaxDate = pointer.Get(maxDate) + } + if minDate, err := convert.ParseTimestamp(opts.MinDate); err == nil { + p.MinDate = pointer.Get(minDate) + } if opts.ItemsPerPage > 0 { p.ItemsPerPage = &opts.ItemsPerPage } if opts.PageNum > 0 { p.PageNum = &opts.PageNum } - if opts.OmitCount { p.IncludeCount = pointer.Get(false) } diff --git a/internal/cli/atlas/networking/peering/create/gcp.go b/internal/cli/atlas/networking/peering/create/gcp.go index 1f102d6223..036d6c0f3c 100644 --- a/internal/cli/atlas/networking/peering/create/gcp.go +++ b/internal/cli/atlas/networking/peering/create/gcp.go @@ -22,7 +22,6 @@ import ( "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/flag" - "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage" "github.com/spf13/cobra" @@ -80,11 +79,14 @@ func (opts *GCPOpts) containerExists() (*atlasv2.CloudProviderContainer, error) } func (opts *GCPOpts) newContainer() *atlasv2.CloudProviderContainer { - return &atlasv2.CloudProviderContainer{ + c := &atlasv2.CloudProviderContainer{ AtlasCidrBlock: &opts.atlasCIDRBlock, - Regions: pointer.GetArrayPointerIfNotEmpty(opts.regions), - ProviderName: pointer.Get("GCP"), } + c.SetProviderName("GCP") + if len(opts.regions) > 0 { + c.Regions = &opts.regions + } + return c } func (opts *GCPOpts) newPeer(containerID string) *atlasv2.BaseNetworkPeeringConnectionSettings { diff --git a/internal/cli/atlas/setup/dbuser_setup.go b/internal/cli/atlas/setup/dbuser_setup.go index 7db3122767..8efdee9ba8 100644 --- a/internal/cli/atlas/setup/dbuser_setup.go +++ b/internal/cli/atlas/setup/dbuser_setup.go @@ -96,7 +96,7 @@ func (opts *Opts) newDatabaseUser() *atlasv2.CloudDatabaseUser { return &atlasv2.CloudDatabaseUser{ Roles: pointer.Get(convert.BuildAtlasRoles([]string{atlasAdmin})), GroupId: opts.ConfigProjectID(), - Password: pointer.GetStringPointerIfNotEmpty(opts.DBUserPassword), + Password: &opts.DBUserPassword, X509Type: &none, AwsIAMType: &none, LdapAuthType: &none, diff --git a/internal/cli/metrics_opt.go b/internal/cli/metrics_opt.go index 44945e8813..d86d11f8fd 100644 --- a/internal/cli/metrics_opt.go +++ b/internal/cli/metrics_opt.go @@ -19,10 +19,10 @@ import ( "strconv" "strings" + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/convert" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin" - atlas "go.mongodb.org/atlas/mongodbatlas" ) type MetricsOpts struct { @@ -35,54 +35,74 @@ type MetricsOpts struct { } func (opts *MetricsOpts) NewProcessMeasurementsAPIParams(groupID string, processID string) *atlasv2.GetHostMeasurementsApiParams { - return &atlasv2.GetHostMeasurementsApiParams{ - GroupId: groupID, - ProcessId: processID, - Granularity: pointer.GetStringPointerIfNotEmpty(opts.Granularity), - M: pointer.GetArrayPointerIfNotEmpty(opts.MeasurementType), - Period: pointer.GetStringPointerIfNotEmpty(opts.Period), - Start: pointer.StringToTimePointer(opts.Start), - End: pointer.StringToTimePointer(opts.End), + p := &atlasv2.GetHostMeasurementsApiParams{ + GroupId: groupID, + ProcessId: processID, } + if opts.Granularity != "" { + p.Granularity = &opts.Granularity + } + if len(opts.MeasurementType) > 0 { + p.M = &opts.MeasurementType + } + if opts.Period != "" { + p.Period = &opts.Period + } + if start, err := convert.ParseTimestamp(opts.Start); err == nil { + p.Start = pointer.Get(start) + } + if end, err := convert.ParseTimestamp(opts.End); err == nil { + p.End = pointer.Get(end) + } + return p } func (opts *MetricsOpts) NewDiskMeasurementsAPIParams(groupID string, processID string, partitionName string) *atlasv2.GetDiskMeasurementsApiParams { - return &atlasv2.GetDiskMeasurementsApiParams{ + p := &atlasv2.GetDiskMeasurementsApiParams{ GroupId: groupID, ProcessId: processID, PartitionName: partitionName, - Granularity: pointer.GetStringPointerIfNotEmpty(opts.Granularity), - M: pointer.GetArrayPointerIfNotEmpty(opts.MeasurementType), - Period: pointer.GetStringPointerIfNotEmpty(opts.Period), - Start: pointer.StringToTimePointer(opts.Start), - End: pointer.StringToTimePointer(opts.End), } + if opts.Granularity != "" { + p.Granularity = &opts.Granularity + } + if len(opts.MeasurementType) > 0 { + p.M = &opts.MeasurementType + } + if opts.Period != "" { + p.Period = &opts.Period + } + if start, err := convert.ParseTimestamp(opts.Start); err == nil { + p.Start = pointer.Get(start) + } + if end, err := convert.ParseTimestamp(opts.End); err == nil { + p.End = pointer.Get(end) + } + return p } func (opts *MetricsOpts) NewDatabaseMeasurementsAPIParams(groupID string, processID string, dbName string) *atlasv2.GetDatabaseMeasurementsApiParams { - return &atlasv2.GetDatabaseMeasurementsApiParams{ + p := &atlasv2.GetDatabaseMeasurementsApiParams{ GroupId: groupID, ProcessId: processID, DatabaseName: dbName, - Granularity: pointer.GetStringPointerIfNotEmpty(opts.Granularity), - M: pointer.GetArrayPointerIfNotEmpty(opts.MeasurementType), - Period: pointer.GetStringPointerIfNotEmpty(opts.Period), - Start: pointer.StringToTimePointer(opts.Start), - End: pointer.StringToTimePointer(opts.End), } -} - -func (opts *MetricsOpts) NewProcessMetricsListOptions() *atlas.ProcessMeasurementListOptions { - o := &atlas.ProcessMeasurementListOptions{ - ListOptions: opts.NewListOptions(), - } - o.Granularity = opts.Granularity - o.Period = opts.Period - o.Start = opts.Start - o.End = opts.End - o.M = opts.MeasurementType - - return o + if opts.Granularity != "" { + p.Granularity = &opts.Granularity + } + if len(opts.MeasurementType) > 0 { + p.M = &opts.MeasurementType + } + if opts.Period != "" { + p.Period = &opts.Period + } + if start, err := convert.ParseTimestamp(opts.Start); err == nil { + p.Start = pointer.Get(start) + } + if end, err := convert.ParseTimestamp(opts.End); err == nil { + p.End = pointer.Get(end) + } + return p } // ValidatePeriodStartEnd validates period, start and end flags. diff --git a/internal/config/profile.go b/internal/config/profile.go index 3426abc6ec..23a8239d21 100644 --- a/internal/config/profile.go +++ b/internal/config/profile.go @@ -141,7 +141,12 @@ func GlobalProperties() []string { } func IsTrue(s string) bool { - return slices.Contains([]string{"t", "T", "true", "True", "TRUE", "y", "Y", "yes", "Yes", "YES", "1"}, s) + switch s { + case "t", "T", "true", "True", "TRUE", "y", "Y", "yes", "Yes", "YES", "1": + return true + default: + return false + } } func Default() *Profile { diff --git a/internal/convert/database_user.go b/internal/convert/database_user.go index 02546db986..55fe066d51 100644 --- a/internal/convert/database_user.go +++ b/internal/convert/database_user.go @@ -18,8 +18,6 @@ import ( "strings" "time" - "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" - customTime "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/time" atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin" ) @@ -52,16 +50,15 @@ func BuildAtlasRoles(r []string) []atlasv2.DatabaseUserRole { } func buildCollectionName(dbCollection []string) *string { - var collectionName string if len(dbCollection) > 1 { - collectionName = strings.Join(dbCollection[1:], ".") + c := strings.Join(dbCollection[1:], ".") + return &c } - return pointer.GetStringPointerIfNotEmpty(collectionName) + return nil } func ParseDeleteAfter(deleteAfter string) *time.Time { - deleteAfterDate, err := customTime.ParseTimestamp(deleteAfter) - + deleteAfterDate, err := ParseTimestamp(deleteAfter) if err == nil { return &deleteAfterDate } @@ -102,8 +99,8 @@ func BuildAtlasScopes(r []string) []atlasv2.UserScope { // SCRAM-SHA should use admin. func GetAuthDB(user *atlasv2.CloudDatabaseUser) string { // base documentation https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/database_user - _, isX509 := adminX509Type[pointer.GetOrDefault(user.X509Type, "")] - _, isIAM := awsIAMType[pointer.GetOrDefault(user.AwsIAMType, "")] + _, isX509 := adminX509Type[user.GetX509Type()] + _, isIAM := awsIAMType[user.GetAwsIAMType()] // just USER is external isLDAP := user.LdapAuthType != nil && *user.LdapAuthType == userLdapAuthType diff --git a/internal/convert/database_user_test.go b/internal/convert/database_user_test.go index 5670d37b92..54911f5443 100644 --- a/internal/convert/database_user_test.go +++ b/internal/convert/database_user_test.go @@ -26,12 +26,14 @@ import ( func TestBuildAtlasRoles(t *testing.T) { type test struct { + name string input []string want []atlasv2.DatabaseUserRole } tests := []test{ { + name: "one with no db", input: []string{"admin"}, want: []atlasv2.DatabaseUserRole{ { @@ -41,6 +43,7 @@ func TestBuildAtlasRoles(t *testing.T) { }, }, { + name: "one with db", input: []string{"admin@test"}, want: []atlasv2.DatabaseUserRole{ { @@ -50,6 +53,7 @@ func TestBuildAtlasRoles(t *testing.T) { }, }, { + name: "one with db one without db", input: []string{"admin@test", "something"}, want: []atlasv2.DatabaseUserRole{ { @@ -63,6 +67,7 @@ func TestBuildAtlasRoles(t *testing.T) { }, }, { + name: "one with db and collection", input: []string{"admin@db.collection"}, want: []atlasv2.DatabaseUserRole{ { @@ -73,6 +78,7 @@ func TestBuildAtlasRoles(t *testing.T) { }, }, { + name: "one with db and collection with multiple dots", input: []string{"admin@db.collection.name"}, want: []atlasv2.DatabaseUserRole{ { @@ -87,7 +93,7 @@ func TestBuildAtlasRoles(t *testing.T) { for _, tc := range tests { input := tc.input want := tc.want - t.Run("", func(t *testing.T) { + t.Run(tc.name, func(t *testing.T) { t.Parallel() got := BuildAtlasRoles(input) if err := deep.Equal(want, got); err != nil { diff --git a/internal/time/time.go b/internal/convert/time.go similarity index 95% rename from internal/time/time.go rename to internal/convert/time.go index e6a7d46735..9f0a5a2c80 100644 --- a/internal/time/time.go +++ b/internal/convert/time.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package time +package convert import "time" @@ -28,9 +28,8 @@ func ParseTimestamp(timestamp string) (time.Time, error) { for _, layout := range layouts { parsedTime, err = time.Parse(layout, timestamp) if err == nil { - break + return parsedTime, err } } - return parsedTime, err } diff --git a/internal/time/time_test.go b/internal/convert/time_test.go similarity index 99% rename from internal/time/time_test.go rename to internal/convert/time_test.go index c926f768b2..a862cdd471 100644 --- a/internal/time/time_test.go +++ b/internal/convert/time_test.go @@ -14,7 +14,7 @@ //go:build unit -package time +package convert import ( "testing" diff --git a/internal/mocks/atlas/mock_alerts.go b/internal/mocks/mock_alerts.go similarity index 96% rename from internal/mocks/atlas/mock_alerts.go rename to internal/mocks/mock_alerts.go index e849128190..c3a8748954 100644 --- a/internal/mocks/atlas/mock_alerts.go +++ b/internal/mocks/mock_alerts.go @@ -1,8 +1,8 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store/atlas (interfaces: AlertDescriber,AlertLister,AlertAcknowledger) +// Source: github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store (interfaces: AlertDescriber,AlertLister,AlertAcknowledger) -// Package atlas is a generated GoMock package. -package atlas +// Package mocks is a generated GoMock package. +package mocks import ( reflect "reflect" diff --git a/internal/pointer/pointer.go b/internal/pointer/pointer.go index 07bac69611..78599d8e98 100644 --- a/internal/pointer/pointer.go +++ b/internal/pointer/pointer.go @@ -15,9 +15,6 @@ package pointer import ( - "time" - - customTime "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/time" "golang.org/x/exp/constraints" ) @@ -40,28 +37,6 @@ func Get[T any](val T) *T { return &val } -func GetStringPointerIfNotEmpty(input string) *string { - if input != "" { - return &input - } - return nil -} - -func GetArrayPointerIfNotEmpty[T any](input []T) *[]T { - if len(input) > 0 { - return &input - } - return nil -} - -func StringToTimePointer(value string) *time.Time { - var result *time.Time - if completedAfter, err := customTime.ParseTimestamp(value); err == nil { - result = &completedAfter - } - return result -} - func GetNonZeroValue[T constraints.Integer](val T) *T { if val > T(0) { return Get(val) diff --git a/internal/store/atlas/alerts.go b/internal/store/alerts.go similarity index 89% rename from internal/store/atlas/alerts.go rename to internal/store/alerts.go index 4f38258664..39f14aa88b 100644 --- a/internal/store/atlas/alerts.go +++ b/internal/store/alerts.go @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -package atlas +package store import ( "go.mongodb.org/atlas-sdk/v20231115007/admin" ) -//go:generate mockgen -destination=../../mocks/atlas/mock_alerts.go -package=atlas github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store/atlas AlertDescriber,AlertLister,AlertAcknowledger +//go:generate mockgen -destination=../mocks/mock_alerts.go -package=mocks github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store AlertDescriber,AlertLister,AlertAcknowledger type AlertDescriber interface { Alert(*admin.GetAlertApiParams) (*admin.AlertViewForNdsGroup, error)